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

[Return-types #3] QNode integration, enable and disable functions #2860

Merged
merged 131 commits into from
Aug 5, 2022

Conversation

rmoyard
Copy link
Contributor

@rmoyard rmoyard commented Jul 25, 2022

Context:

Third PR of the new return type project.

Description of the Change:

We add the QNode integration of the new return types.

The changes are made separately from the main pipeline. It can be activated by qml.enable_return()
, deactivated by qml.disable_return() and we can return mode by qml.active_return.

Example
Forward pass:

dev = qml.device("default.qubit", wires=2)

def circuit(x):
        qml.Hadamard(wires=[0])
        qml.CRX(x, wires=[0, 1])
        return (qml.probs(wires=[0]), qml.vn_entropy(wires=[0]), qml.probs(wires=0), qml.expval(wires=1))

qnode = qml.QNode(circuit, dev)
res = qnode(0.5)

>>> res
(tensor([0.5, 0.5], requires_grad=True), tensor(0.08014815, requires_grad=True), tensor([0.5, 0.5], requires_grad=True), tensor(0.93879128, requires_grad=True))

Backpropagation:

import jax

dev = qml.device("default.qubit", wires=2)
qml.enable_return()

@qml.qnode(dev, interface="jax")
def circuit(a):
    qml.RX(a[0], wires=0)
    qml.CNOT(wires=(0, 1))
    qml.RY(a[1], wires=1)
    qml.RZ(a[2], wires=1)
    return qml.expval(qml.PauliZ(wires=0)), qml.probs(wires=[0, 1]), qml.vn_entropy(wires=1)

x = jax.numpy.array([0.1, 0.2, 0.3])

res = jax.jacobian(circuit)(x)

>>> res

(DeviceArray([-9.9833414e-02, -7.4505806e-09, -3.9932679e-10], dtype=float32), 
DeviceArray([[-4.9419206e-02, -9.9086545e-02,  3.4938008e-09],
             [-4.9750542e-04,  9.9086538e-02,  1.2768372e-10],
             [ 4.9750548e-04,  2.4812977e-04,  4.8371929e-13],
             [ 4.9419202e-02, -2.4812980e-04,  2.6696912e-11]],            dtype=float32), 
DeviceArray([ 2.9899091e-01, -4.4703484e-08,  9.5104014e-10], dtype=float32))

Copy link
Contributor

@antalszava antalszava left a comment

Choose a reason for hiding this comment

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

@rmoyard great push with this! 💪 🔥

No concrete blocker, just curious about some things.

pennylane/devices/default_mixed.py Show resolved Hide resolved
pennylane/interfaces/execution.py Show resolved Hide resolved
tests/math/test_functions.py Outdated Show resolved Hide resolved
tests/math/test_functions.py Show resolved Hide resolved
tests/math/test_functions.py Outdated Show resolved Hide resolved
tests/test_new_return_types_qnode.py Outdated Show resolved Hide resolved
tests/test_new_return_types_qnode.py Outdated Show resolved Hide resolved
tests/test_new_return_types_qnode.py Outdated Show resolved Hide resolved
tests/test_new_return_types_qnode.py Outdated Show resolved Hide resolved
tests/test_new_return_types_qnode.py Outdated Show resolved Hide resolved
@rmoyard rmoyard requested a review from antalszava August 5, 2022 15:15
Copy link
Contributor

@antalszava antalszava left a comment

Choose a reason for hiding this comment

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

Looks good! 💪 🥇 Massive push here, great to have all these tests.

I'm wondering about the note on skipping some sampling tests.

The PR became quite big eventually, it could help to separate some conceptual parts (e.g., have backrop be tested in a separate PR) in other such cases.

.github/workflows/tests.yml Show resolved Hide resolved
pennylane/interfaces/execution.py Show resolved Hide resolved
@rmoyard rmoyard merged commit 7760660 into master Aug 5, 2022
@rmoyard rmoyard deleted the return_types_qnode branch August 5, 2022 20:25
@antalszava
Copy link
Contributor

[sc-24246]

@rmoyard rmoyard restored the return_types_qnode branch November 10, 2022 20:13
@rmoyard rmoyard deleted the return_types_qnode branch March 5, 2024 14:15
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.

4 participants