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
754 stars 191 forks source link

Uninformative error message using TDMprogram on sf.fock #508

Closed rdprins closed 3 years ago

rdprins commented 3 years ago

Issue description

Trying to use TDMprogram on sf.fock, I get an uninformative error. Apparently TDMprogram is not implemented on the fock backend. Tt is trying to find MeasureHomodyne statements inside the program and failing to do so.

josh146 commented 3 years ago

Thanks @RobbeDePrins for reporting this bug!

Here is the problematic code from your previous post:

import strawberryfields as sf
from strawberryfields import ops
import numpy as np

num_iterations = 10
num_repeat = 10

prog = sf.TDMProgram(N=4)

with prog.context([0]*num_iterations) 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, shots=num_repeat)
print(results.all_samples)

Note that I receive the exact same error regarding Number of measurement operators must match number of spatial modes. In fact, this error message occurs after the end of the with block, even before the engine is created --- so this is a TDM specific bug.

I had a look in the source code, and it appears that the issue is due to this line here:

https://github.com/XanaduAI/strawberryfields/blob/e54205f7c8ad6937069f6ec43fb1b1632eab69e1/strawberryfields/tdm/tdmprogram.py#L83

@nquesada, is this intentional or a bug?

Regarding running the circuit on the Fock backend, from a SF point of view, it should be as simple as changing the line

https://github.com/XanaduAI/strawberryfields/blob/e54205f7c8ad6937069f6ec43fb1b1632eab69e1/strawberryfields/tdm/tdmprogram.py#L435

to

if compiler in ("fock", "gaussian"):
    return super().compile(device=device, compiler=compiler)

For a simple TDM program, I can verify this allows the TDMProgram to be executed on the Fock backend.

nquesada commented 3 years ago

Hi @josh146 , @RobbeDePrins --- Thanks for submitting this report. After conferring with one of my colleagues I realized that the error message is indeed correct. You TDMProgram actually has two spatial modes, each with two concurrent modes, thus you would need to set N=[2,2]. You also need to be careful about how you order your gates/preparations. We are drafting a second TDM tutorial and hoping to release it soon. We realize that writing time-domain program is not as easy as writing regular programs.

The other point I wanted to make is that while you can follow @josh146 and make changes locally, we currently do not support TDMprogram in the Fock backends, it is something that we might consider in the future.