Error in memory with 4 Heralded CNOT

Hi,

I’m trying to use the heralded CNOT in a circuit, but the simulation takes too much memory. Here is a code that can reproduce the problem:

import perceval as pcvl
import numpy as np
from perceval.components import BS, PS
from perceval.components import catalog
import timeit

p = pcvl.Processor("SLOS",6)
cnot = catalog['heralded cnot'].as_processor().build()

p.add([0,1,2,3],cnot)
p.add(2,BS.H())
p.add(2,BS.Rx(2*0.00523828))
p.add(2,BS.H())
p.add([0,1,2,3],cnot)

p.add([0,1,4,5],cnot)
p.add(4,BS.H())
p.add(4,BS.Rx(2*0.00523828))
p.add(4,BS.H())
p.add([0,1,4,5],not)

start = timeit.default_timer()

p.with_input(pcvl.BasicState([1, 1, 1, 1, 1, 1]))
output_distribution=p.probs()["results"]
pcvl.pdisplay(output_distribution, max_v=10)
stop = timeit.default_timer()

print('Time: ', stop - start) 

For two herald CNOT, it takes 0.784 s to finish the simulation. For 3 CNOT, 306.68 s, and in the case of 4 CNOT, it takes too much memory. In this case, what is the problem? Is there a better way of working with CNOT in Perceval? or maybe there are some mistakes in how I’m using it?

Thanks for the help!

Hi Victor,

It makes sense that your system runs out of memory with such a simulation. Indeed, we’re talking about a 14 photons on 22 modes simulation because of ancillary modes (=> possible output states are in the order of 1e10).

Hopefully, there’s a (painful and manual) work-around that lowers the memory and computation time cost drastically. As it’s very manual, it’s also very error prone, so please double-check everything when using this work-around.

Given your 4 CNOT circuit example, you can predict the form of the entire circuit from the beginning of your script. Actually, you’re using 6 “data” modes and each CNOT gate comes with 4 ancillary modes, 2 being heralded (mode count = 6 + 4 * 4 = 22).
If you want to input 6 photons in “data” modes, we’ve got a photon count of 6 + 4 * 2 = 14.
With these information + the shape of your circuit (which you can check using pdisplay), you can use SLOS to mask the ancillary modes. Each CNOT brings “0101” to the mask.
With Perceval syntax, the masking gets to be " 0101010101010101" (6 spaces + 4* “0101”).

For the specific simulation you copy/pasted, the following code ran in 6 minutes on my computer and didn’t use much memory.

import perceval as pcvl
import numpy as np
from perceval.components import BS, PS
from perceval.components import catalog
import timeit

slos = pcvl.SLOSBackend(mask=["      0101010101010101"], n=14)  # Prepare a masked SLOS instance
p = pcvl.Processor(slos, 6)  # use the previously constructed SLOS in the Processor
cnot = catalog['heralded cnot'].as_processor().build()

p.add([0,1,2,3],cnot)
p.add(2,BS.H())
p.add(2,BS.Rx(2*0.00523828))
p.add(2,BS.H())
p.add([0,1,2,3],cnot)

p.add([0,1,4,5],cnot)
p.add(4,BS.H())
p.add(4,BS.Rx(2*0.00523828))
p.add(4,BS.H())
p.add([0,1,4,5],cnot)

start = timeit.default_timer()

p.with_input(pcvl.BasicState([1, 1 ,1 ,1 ,1 ,1]))
output_distribution=p.probs()["results"]
pcvl.pdisplay(output_distribution, max_v=10)
stop = timeit.default_timer()

print('Time: ', stop - start)

Hope this helped!

Best wishes,
Eric

Thanks for the fast response. I wasn’t thinking about the ancillary modes in the CNOT. It makes sense it takes too much memory. I ran your solution, and it works, but I don’t understand what mask is doing. Also, I cannot find information about it in the documentation. Can you share some references to understand what the feature mask is?

Thanks for the help!

Hi,

I’m afraid we don’t have much user friendly documentation on masks. It comes from the design of the SLOS back-end and is described in the following scientific publication: https://arxiv.org/pdf/2206.10549.pdf , chapter 3.2.1

Basically, we’re telling our back-end to skip some possible output states if they do not match a given mask. In our mask syntax, a space means “any number of photons”, whereas an integer value forces the photon count. For instance, with a dummy " 1" mask on a two modes circuit, we would compute output states only if they have exactly 1 photon in the second mode.

Best wishes,
Eric