Boson sampling example code

Hi there,

I ran into a problem, potentially not a problem, when running the Boson sampling example code from the website (Boson Sampling — perceval v0.13 documentation).

I’m pretty much running a 1:1 copy of the code, just changed the number of photons to 4, the modes to 20, and the number of samples to 50k, so I don’t need to wait that long.
Everything runs pretty much fine, however, when I look at the results:

with gzip.open(filepath + ".txt.gz", "rt") as f:
    for l in f:
        l = l.strip()
        if l.startswith("|") and l.endswith(">"):
            try:
                st = pcvl.BasicState(l)
                count+=1
                bunching_distribution[st.photon2mode(st.n-1)]+=1
            except Exception:
                pass
print(count, "samples")
print("Bunching Distribution:", "\t".join([str(bunching_distribution[k]) for k in range(M)]))

it only gives me 20 outputs rather than the m!/(n!(m-n)!) = 4845 possible modes to distribute 4 photons in 20 modes. I believe something is not quite right, so different results are put into the same category, but I am not sure. It looks like the line creating the bunching_distribution checks every state for the number of photons <st.n>, but then the <st.photon2mode(st.n-1)> does what exactly? I assume the -1 is just for indexing in Python, but I’m lost as to what is happening there. Like I did a little toy example, it returned 2 for the state [1,0,1,1], [0,0,1,1], and [0,1,1,0], so it would sort all in the same category.

The help only says:
photon2mode(…)
photon2mode(self: exqalibur.FockState, arg0: int) → int
and I’m a physicist, not a CS guy :wink:

Any help would be appreciated :slight_smile: and my apologies if it is not quite clear what I am trying to say here.
I have some sort of idea what might be happening but would prefer for someone smarter to explain it :slight_smile:

Thanks
Markus

Also: it looks like it creates around 50 samples a second (took 15min for the 50k samples). Is there a way to speed this up? Would it work faster on the cloud simulators and which would be the fastest one?

Hello Markus,

In this example, we group states so we don’t have to print all of them. If you look at the values in bunching_distribution, the sum should be 50 000.

About the photon2mode(k) method, it’s true that the doc is missing here (we are currently working on improving the documentation). What it does is giving the mode number of the k-th photon (considering photons are sorted in ascending order and start at 0), so in this example, this gives you the last non-empty mode number, as the photon n - 1 is the last photon.

About the speed, I fear there is not much you can do as it is since this is a huge circuit. However, I’m a bit surprised it is so slow as I generate 10 000 samples a second on my laptop for 4 photons and 20 modes, and 1000 for 14 photons and 60 modes. For your information, the display of the circuit takes a lot of time so if you are counting it, it might impact the time taken.

If you have a bad computer, it will probably be better to run this on the cloud on sim:clifford. However, as this notebook is most of all a demonstration of what the Clifford backend can do, I suggest that for 4 photons and 20 modes, you first try using SLOS (or remotely using sim:slos) and asking directly N samples to the Sampler, which should be much faster for huge number of samples. For 14 photons and 60 modes, SLOS is unable to do it, that’s why we use Clifford in the notebook.

I hope this will help you,

Regards,
Aurélien

Hi Aurélien,

Thanks for your reply.
I actually found why it was so slow: it saved every sample individually in the original code. I just ran it with sample_count = sampler.sample_count(1e7),
which took 30 seconds, so around 300k samples a second on my MacBook Pro M2 (M2 Max chip). I think I can work with that :slight_smile:

Thanks again for your explanation.
Markus