Issue #5 new

# Handle measurement in circuits

Erik Bray
repo owner created an issue

It needs to possible to handle projective measurement in a circuit--generally this would be implemented as a special case of the Z gate, but the exception that implements some extra bookkeeping to mark a measured qubit as a "classical" bit for the remainder of the circuit.

Evaluating circuits containing measurements should be simple enough, thanks to the principle of deferred measurement. But there could be some trickiness involved if, for example, a classical bit as used as input to a gate that expects a qubit and might try to put it into a superposition or otherwise modify it. After that point it should no longer be treated as classical. This case could be difficult to determine a priori such as when generating a circuit diagram. But it's also an unusual use case I think and not something to worry about too much.

1. reporter

Durward McDonell Regarding this issue, IIRC you were especially interested in having this working for stabilizer circuits. In that case measurement already works, but as you can see here nothing is done with the results of the measurement:

https://bitbucket.org/embray/pyqc/src/98605d9a6dda5eec39e3d9f7aabd631c0dd55dd7/pyqc/chp.pyx?at=master#cl-141

(I apologize for how underdocumented parts of the code are; when I have time I would like to go through and rectify that). But anyways the apply_measurement function returns a 2-tuple: The first element returned is True if the measurement was purely deterministic, and False if it was random. The second return value is the result of the measurement (0 or 1).

The problem is just finding a good API for returning those measurement results. Since I designed this to be a sort of algebraic system the result of some operation like circuit|state is a new state object, to which it doesn't make a whole lot of sense to attach the results of some measurements, though for lack of a better idea state objects could maybe have some "measurements" property that rides on it like a sidecar, and only has meaning if this was the output state from some circuit.

Another option is to overhaul the .evaluate() method to return a state and the measurements. Since there can be multiple measurements in a circuit there has to be some way of relating each measurement to where it came from in the circuit too, if I understand correctly. I suppose it could just be time-ordered. But I don't know what's actually useful here. You tell me :)

For non-stabilizer circuits measurement still needs to be implemented; as I think you noticed there's currently a Measurement "operator" but right now it's just a Z-gate which I chose as a stand-in. Obviously this should be fixed to actually record a measurement.

Thanks! We are equally interested in stabilizer circuits and general circuits. I'll take a crack at it and see what I can get working. Meanwhile, we really appreciate any time you are able to devote to this. I know you're super busy otherwise.

2. reporter

Thanks your help--once I'm over the hump with this one class I'll be able to devote more time and attention to this.