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

Bind lightning provided VJPs #4914

Merged
merged 29 commits into from Jan 16, 2024
Merged

Bind lightning provided VJPs #4914

merged 29 commits into from Jan 16, 2024

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Dec 4, 2023

Open questions:

  1. Where do we test this? Do we test it in pennylane or pennylane-lightning?

  2. Is this the expected way to compute the vjps?

 device.vjp(numpy_tape.measurements, dy)(numpy_tape)
  1. Is there any extra validation or preprocessing we need to do?

  2. Can we re-use the device state between calls to vjp?

[sc-51789]

For the code:

n_wires = 20
n_layers = 5

dev = qml.device('lightning.qubit', wires=n_wires)

shape = qml.StronglyEntanglingLayers.shape(n_wires=n_wires, n_layers=n_layers)
rng = qml.numpy.random.default_rng(seed=42)
params = rng.random(shape)
params_torch = torch.tensor(params, requires_grad=True)

@qml.qnode(dev, device_vjp=True)
def circuit_dev_jp(params):
    qml.StronglyEntanglingLayers(params, wires=range(n_wires))
    return [qml.expval(qml.PauliZ(i)) for i in range(n_wires)]

@qml.qnode(dev, diff_method="adjoint", device_vjp=False)
def circuit_dev_jac(params):
    qml.StronglyEntanglingLayers(params, wires=range(n_wires))
    return [qml.expval(qml.PauliZ(i)) for i in range(n_wires)]

We get:

Screenshot 2023-12-04 at 10 54 40 AM

Copy link
Contributor

github-actions bot commented Dec 4, 2023

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Copy link

codecov bot commented Jan 12, 2024

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (c01cb4b) 99.67% compared to head (407200a) 99.66%.
Report is 18 commits behind head on master.

Files Patch % Lines
pennylane/workflow/jacobian_products.py 91.30% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4914      +/-   ##
==========================================
- Coverage   99.67%   99.66%   -0.02%     
==========================================
  Files         394      394              
  Lines       35670    35457     -213     
==========================================
- Hits        35554    35337     -217     
- Misses        116      120       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@albi3ro albi3ro requested review from a team and timmysilv January 12, 2024 17:19
Copy link
Contributor

@vincentmr vincentmr 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 to me. I just left some suggestions to fix typos. Thanks @albi3ro .

Copy link
Member

@multiphaseCFD multiphaseCFD 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 to me! Thanks @albi3ro for putting it together!

Co-authored-by: Vincent Michaud-Rioux <vincentm@nanoacademic.com>
Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

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

I won't make any strong claim about where these tests should be while we have this circular dependency thing going on... feels like anything goes. I'm happiest to not have to copy-paste or re-write tests, so this seems sensible. just a few comments, but mostly lgtm!

@albi3ro albi3ro enabled auto-merge (squash) January 16, 2024 21:55
@albi3ro albi3ro merged commit 0cbad06 into master Jan 16, 2024
34 of 35 checks passed
@albi3ro albi3ro deleted the lightning-vjps branch January 16, 2024 22:07
mudit2812 pushed a commit that referenced this pull request Jan 19, 2024
For the code:
```python
n_wires = 20
n_layers = 5

dev = qml.device('lightning.qubit', wires=n_wires)

shape = qml.StronglyEntanglingLayers.shape(n_wires=n_wires, n_layers=n_layers)
rng = qml.numpy.random.default_rng(seed=42)
params = rng.random(shape)
params_torch = torch.tensor(params, requires_grad=True)

@qml.qnode(dev, device_vjp=True)
def circuit_dev_jp(params):
    qml.StronglyEntanglingLayers(params, wires=range(n_wires))
    return [qml.expval(qml.PauliZ(i)) for i in range(n_wires)]

@qml.qnode(dev, diff_method="adjoint", device_vjp=False)
def circuit_dev_jac(params):
    qml.StronglyEntanglingLayers(params, wires=range(n_wires))
    return [qml.expval(qml.PauliZ(i)) for i in range(n_wires)]
```

We get:

![Screenshot 2023-12-04 at 10 54 40
AM](https://github.com/PennyLaneAI/pennylane/assets/6364575/f88ea573-a437-4775-a697-4f5da0de4a2d)

---------

Co-authored-by: Vincent Michaud-Rioux <vincentm@nanoacademic.com>
Co-authored-by: Matthew Silverman <matthews@xanadu.ai>
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