strawberryfields icon indicating copy to clipboard operation
strawberryfields copied to clipboard

Add threshold detector measurements to frontend & backends

Open co9olguy opened this issue 6 years ago • 9 comments

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?

co9olguy avatar Jul 15 '19 00:07 co9olguy

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.

josh146 avatar Jul 15 '19 10:07 josh146

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.

nquesada avatar Jul 15 '19 14:07 nquesada

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

co9olguy avatar Jul 15 '19 14:07 co9olguy

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...

nquesada avatar Jul 15 '19 14:07 nquesada

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

co9olguy avatar Jul 15 '19 16:07 co9olguy

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.

nquesada avatar Jul 15 '19 17:07 nquesada

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.

MattRos90 avatar Mar 06 '20 11:03 MattRos90

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]

co9olguy avatar Mar 06 '20 19:03 co9olguy

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

ziofil avatar Mar 06 '20 21:03 ziofil