Skip to content
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

Merged
merged 308 commits into from Mar 4, 2022

Conversation

antalszava
Copy link
Contributor

@antalszava antalszava commented Feb 17, 2022

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:

  1. A new qml.measure top-level function to perform a mid-circuit measurement on a single qubit;
  2. A 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.

    @qml.qnode(dev)
    @qml.defer_measurements
    def qnode_conditional_op_on_zero(x, y):
        qml.RY(x, wires=1)
        qml.CNOT(wires=[0, 1])
        m_0 = qml.measure(1)
        qml.cond(m_0 == 0, qml.RY)(y, wires=0)
        return qml.probs(wires=[0])
    pars = np.array([0.643, 0.246], requires_grad=True)
>>> qnode_conditional_op_on_zero(*pars)
tensor([0.98645017, 0.01354983], requires_grad=True)

Benefits:

  • Mid-circuit measurement and conditional operations can be defined in quantum functions;
  • PennyLane applies the defer_measurements transform in cases when a device doesn't support the former capabilities.

Possible Drawbacks:
N/A

Related GitHub Issues:
N/A

…gration tests; add lightning to the list of tested devices
Copy link
Contributor

@trbromley trbromley left a 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 Show resolved Hide resolved
for the ``0`` measurement outcome..

PennyLane implements the deferred measurement principle to transform
conditional operations with the :func:`~.pennylane.transforms.defer_measurements` quantum
Copy link
Contributor

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 🤔

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

doc/introduction/measurements.rst Outdated Show resolved Hide resolved
doc/introduction/measurements.rst Outdated Show resolved Hide resolved
doc/introduction/measurements.rst Outdated Show resolved Hide resolved
Comment on lines +704 to +706
measurement_id = str(uuid.uuid4())[:8]
MeasurementProcess(MidMeasure, wires=wire, id=measurement_id)
return MeasurementValue(measurement_id)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

pennylane/transforms/condition.py Outdated Show resolved Hide resolved
pennylane/transforms/defer_measurements.py Outdated Show resolved Hide resolved
pennylane/transforms/defer_measurements.py Outdated Show resolved Hide resolved
Comment on lines +210 to +234
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!!

@antalszava antalszava mentioned this pull request Mar 4, 2022
3 tasks
antalszava and others added 18 commits March 4, 2022 09:02
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants