-
Notifications
You must be signed in to change notification settings - Fork 586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mid-circuit measurements and conditional operations #2211
Conversation
…gration tests; add lightning to the list of tested devices
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @antalszava @puzzleshark - I've left a final few comments/suggestions, but overall approved! 🟢 Great job everyone, it's very exciting to get this feature added 🚀
doc/introduction/measurements.rst
Outdated
for the ``0`` measurement outcome.. | ||
|
||
PennyLane implements the deferred measurement principle to transform | ||
conditional operations with the :func:`~.pennylane.transforms.defer_measurements` quantum |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This link still won't work for some reason 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the catch! I'll be trying out other ones. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that having qfunc_transform
as the decorator doesn't allow such references 🤔 See #2283.
measurement_id = str(uuid.uuid4())[:8] | ||
MeasurementProcess(MidMeasure, wires=wire, id=measurement_id) | ||
return MeasurementValue(measurement_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to make an ID, rather than, e.g., having MeasurementValue
contain a MeasurementProcess
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! We were thinking about this too.
having MeasurementValue contain a MeasurementProcess?
This seemed the most practical, yet, it is a bit confusing that the value object contains a measurement. Sam mentioned , however, that having an ID would be required when we do serialization of QNodes with mid-circuit measurements, so sticking with the original ID generation method seemed like a good approach.
def teleportation_circuit(rads): | ||
|
||
# Create Alice's secret qubit state | ||
qml.RY(rads, wires=0) | ||
|
||
# create an EPR pair with wires 1 and 2. 1 is held by Alice and 2 held by Bob | ||
qml.Hadamard(wires=1) | ||
qml.CNOT(wires=[1, 2]) | ||
|
||
# Alice sends her qubits through a CNOT gate. | ||
qml.CNOT(wires=[0, 1]) | ||
|
||
# Alice then sends the first qubit through a Hadamard gate. | ||
qml.Hadamard(wires=0) | ||
|
||
# Alice measures her qubits, obtaining one of four results, and sends this information to Bob. | ||
m_0 = qml.measure(0) | ||
m_1 = qml.measure(1) | ||
|
||
# Given Alice's measurements, Bob performs one of four operations on his half of the EPR pair and | ||
# recovers the original quantum state. | ||
qml.cond(m_1, qml.RX)(math.pi, wires=2) | ||
qml.cond(m_0, qml.RZ)(math.pi, wires=2) | ||
|
||
return qml.probs(wires=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!!
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
Co-authored-by: Tom Bromley <49409390+trbromley@users.noreply.github.com>
…eAI/pennylane into conditional-ops-alpha-sanitized
…eAI/pennylane into conditional-ops-alpha-sanitized
* implement and test __invert__ * add else_op to qml.cond * using inversion in an integration * format * copy measurement under the hood when else; else test * format * err tests * format * docstrings * isort * test_condition * docstrings * format * no need for separate logic for ops * refactor wrapper as per Josh's suggestion from code review * Update tests/transforms/test_defer_measurements.py Co-authored-by: Josh Izaac <josh146@gmail.com> * two unit tests for checking qml.cond queuing * add queue unit tests * format * invert docstring * rename arguments * module docstring * module docstring: note where integration tests are * changelog * Update pennylane/transforms/condition.py Co-authored-by: Josh Izaac <josh146@gmail.com> Co-authored-by: Josh Izaac <josh146@gmail.com>
Context:
Certain quantum circuits include measuring qubits in the middle of the circuit and conditioning operations in the circuit on the classical information obtained during measurement. Example use cases include quantum teleportation and error correction schemes.
Description of the Change:
This PR adds the user interface and a transform to allow mid-circuit measurements and conditional operations to be defined in PennyLane.
It adds two user-facing functionality:
qml.measure
top-level function to perform a mid-circuit measurement on a single qubit;qml.cond
top-level function that can be used to apply operations conditioned on measurement outcomes.Further, a transform applying the deferred measurement principle is added.
>>> qnode_conditional_op_on_zero(*pars) tensor([0.98645017, 0.01354983], requires_grad=True)
Benefits:
defer_measurements
transform in cases when a device doesn't support the former capabilities.Possible Drawbacks:
N/A
Related GitHub Issues:
N/A