Error TDMprogram on sf.fock "ValueError: Number of measurement operators must match number of spatial modes"

Context
I am trying to use the sf.fock engine to do the following:
I have 4 concurrent modes. Each iteration, I apply an interferometer to all 4 modes and then I measure the first 2 modes.
So actually the last two modes of my system get feedbacked every iteration.

I get the following error: ValueError: Number of measurement operators must match number of spatial modes. What am I doing wrong?

Kind regards, Robbe

Can you give a small code example that reproduces the error?

num_iterations = 10
num_repeat = 10

prog = sf.TDMProgram(N=4)

with prog.context([0]*num_iterations, copies=num_repeat) as (p, q):
    ops.Fock(1) | q[0]
    ops.Fock(0) | q[1]

    ops.BSgate(0.1,0.2) | (q[0], q[1])
    ops.BSgate(0.3,0.4) | (q[2], q[3])
    ops.BSgate(0.5,0.6) | (q[1], q[2])

    ops.MeasureFock() | (q[0],q[1])

eng = sf.Engine("fock", backend_options={"cutoff_dim": 5})

results = eng.run(prog)
print(results.all_samples)

Hi @Robbe — Currently time-domain programs can only be run on the Gaussian backend. Admittedly the error message you are getting is completely uninformative; it is trying to find MeasureHomodyne statements inside the program and failing to do so. Would be grateful if you could start an issue in the SF repo.

Nicolas

1 Like

Hi @Nicolas_Quesada,
Thank you for you answer. I added the issue to the SF repo (https://github.com/XanaduAI/strawberryfields/issues/508). Do you know when TDMprogram would become available on other backends? Is there a possible workaround?

First I tried measuring a wire and then resetting a number of photons to that wire to do some calculations (to do the next time iteration). That gave an error saying that all operations must be in front of the detectors. I suppose this is a result of the ‘delayed measurement principle’, but doesn’t this limit time domain calculations?

If you code such a program ‘brute force’ (in a unrolled staircase manner), as explained on https://strawberryfields.ai/photonics/demos/run_time_domain.html, you can apply the ‘delayed measurement principle’, but then you’re limited to 23 wires.

If you want to re-use wires, you can’t use delayed measurement, right?

Kind regards,
Robbe

Hi @Robbe ---- We replied in the PR. Thanks!

Hi @Nicolas_Quesada,

Thank you for your answer on the repo issue. I still have a few questions:

  • I’m not quite sure why I need to use N=[2,2] instead of N=4. What’s the difference between spatial modes and concurrent modes? I don’t see how I can split my 4 modes in 2 bands of 2 modes, because each iteration my interferometer works on all 4 modes.

  • Does the shift_by function in the time domain tutorial give possible workaround that doesn’t require the local files to be changed? Or is the amount of time iterations then limited by the number of wires? (In other words: is this just the same as the ‘brute force’ implementation that I mentioned in my last question?)

  • Could TDMprogram also be used in PennyLane in some way (to enable training the parameters of the interferometer? Do you know if/when it would be added to PennyLane?

Kind regards,
Robbe

Hi @Robbe

  1. You have two spatial modes because you have two measurements at the end. The number of spatial modes is equivalent to the number of modes that are measured inside each time-domain iteration.
  2. You can in principle write a regular sf.Program and shift the modes manually, however, we have not given much thought to how things would work in the Fock backends and cannot guarantee correct behaviour.
  3. Likely not, at least at the moment. TDMPrograms have (stochastic-outcome) measurements at the end which are incompatible with differentiability.

Thank you very much for your time and the clear answers, @Nicolas_Quesada!

Kind regards and have a nice day,
Robbe

1 Like