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
748 stars 187 forks source link

Add threshold detector measurements to frontend & backends #134

Open co9olguy opened 5 years ago

co9olguy commented 5 years ago

At the moment this can be hacked by doing MeasureFock and converting any nonzero results to a value of 1. However, there is likely a simpler way to implement these things by leveraging sampling algorithms in the hafnian library (at least for Gaussian and Fock backends).

@nquesada @josh146 thoughts?

josh146 commented 5 years ago

I would propose MeasureThreshold - more explicit to the user, fits with the existing approach we already use for new measurements in SF.

This allows the backends to implement this differently to MeasureFock, and they can just directly call hafnian.sample.torontonian_sample_state if applicable.

nquesada commented 5 years ago

I agree with Josh --- Mostly what is needed is some frontend operation and a way to communicate this to the backend. Ideally, for the Fock backends it should be not too hard to also construct conditional states after MeasureThreshold if the partial trace and post select on vacuum operations are available.

co9olguy commented 5 years ago

Yep, only blocker I foresee here is (as usual), the TF backend, which has to implement all operations using tf ops

nquesada commented 5 years ago

Does the TF backend have operations for partial trace and postselection on vacuum? If so, conditional states after successful MeasureThreshold are just the difference of these two...

co9olguy commented 5 years ago

I was thinking more that actually implementing the sampling would be the harder part (since we can't just call hafnian lib)

nquesada commented 5 years ago

I think all the pieces are there and there is very minimal coding needed since the Fock backends already know about probabilities in the Fock basis.

MattRos90 commented 4 years ago

Hi there!!! I'm trying to run some simulations using SF with TensorFlow backend and it would be really helpful to have a MeasureThreshold function with this backend. I have also tried to switch to Fock or Gaussian (1 shot) but it does not seem to be defined. I would be especially interested in manipulating the post-measurement state.

co9olguy commented 4 years ago

Hi @MattRos90

Thanks for your feedback. To help us distill what you're looking for, I have a couple questions:

Are you looking to carry out threshold measurements specifically with the TF backend, or would any backend do? (from a development perspective, it doesn't really matter so much, adding this feature to all backends will be roughly the same amount of work).

Note that measurements, being random, are not differentiable, which means you can't train circuits containing measurements. So there may not be any advantage to using the TF backend for this.

If you are not concerned with differentiability, as a quick workaround, you could use the existing MeasureFock measurement, then perform a little post-processing. For example:

meas_fock_result # a numpy array of non-negative integers e.g., [0,2,5,0,1]
np.array(meas_fock_result >= 1, dtype=int) # [0,1,1,0,1]
ziofil commented 4 years ago

If you want to optimize a conditional state you can use a constant projector onto the outcome of the measurement that you want.