XanaduAI / strawberryfields

Strawberry Fields is a full-stack Python library for designing, simulating, and optimizing continuous variable (CV) quantum optical circuits.
https://strawberryfields.ai
Apache License 2.0
745 stars 187 forks source link

`TDMprogram` requires `timebins % concurrent_modes == 0` #607

Closed fab1an-q closed 3 years ago

fab1an-q commented 3 years ago

Issue description

The TDMProgram will produce wrong results if the number of timebins is not an integer multiple of the number of concurrent modes. This is because the tdmprogram.unroll() function simpy multiplies the unique circuit with the number of shots, not taking into account which qumode has been measured most recently.

To illustrate the bug, let's consider this TDM program:

theta = [0] * 3
r = 3
shots = 2

prog = sf.TDMProgram(N=2)
with prog.context(theta) as (p, q):
    ops.Xgate(50) | q[1]
    ops.MeasureHomodyne(p[0]) | q[0]
eng = sf.Engine("gaussian")
res = eng.run(prog, shots=shots)
samples = res.samples

print(samples)

displace mode 1 # shot 1, timebin 0 -> the displacement is applied to mode 1 although the most recently measured mode is 0 measure mode 0 # shot 1, timebin 0 displace mode 0 # shot 1, timebin 1 measure mode 1 # shot 1, timebin 1 displace mode 1 # shot 1, timebin 2 measure mode 0 # shot 1, timebin 2


The second shot starts with displacement of qumode 1. But qumode 1 is the mode that just _has_ been displaced at the end of shot 0! This issue will always occur when the number of timebins is not an integer multiple of the number of concurrent modes.

It took me pretty long to figure this out, and I am available for a chat and/or further demonstrations. I thought I could fix it myself by applying an additional qumode shift at the end of each shot, but I didn't succeed.

* *Reproduces how often:* Always