-
Notifications
You must be signed in to change notification settings - Fork 603
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
Jacobian Slice Bug StrawberryFields #2485
Changes from all commits
2287c5a
9ab1edf
4e2dcea
7f8651f
88776d4
e2fd7bf
ff7e39c
ee58574
b6589a8
83920aa
f5827df
5420462
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
from pennylane import numpy as np | ||
|
||
import pennylane as qml | ||
from pennylane.devices import DefaultQubit | ||
from pennylane.gradients import finite_diff, param_shift | ||
from pennylane.interfaces import execute | ||
|
||
|
@@ -1111,3 +1112,30 @@ def test_multiple_hamiltonians_trainable(self, cost_fn, execute_kwargs, tol): | |
res = np.hstack(qml.jacobian(cost_fn)(weights, coeffs1, coeffs2, dev=dev)) | ||
expected = self.cost_fn_jacobian(weights, coeffs1, coeffs2) | ||
assert np.allclose(res, expected, atol=tol, rtol=0) | ||
|
||
|
||
class TestCustomJacobian: | ||
def test_custom_jacobians(self): | ||
class CustomJacobianDevice(DefaultQubit): | ||
@classmethod | ||
def capabilities(cls): | ||
capabilities = super().capabilities() | ||
capabilities["provides_jacobian"] = True | ||
return capabilities | ||
|
||
def jacobian(self, tape): | ||
return np.array([1.0, 2.0, 3.0, 4.0]) | ||
|
||
dev = CustomJacobianDevice(wires=2) | ||
|
||
@qml.qnode(dev, diff_method="device") | ||
def circuit(v): | ||
qml.RX(v, wires=0) | ||
return qml.probs(wires=[0, 1]) | ||
|
||
d_circuit = qml.jacobian(circuit, argnum=0) | ||
|
||
params = np.array(1.0, requires_grad=True) | ||
|
||
d_out = d_circuit(params) | ||
assert np.allclose(d_out, np.array([1.0, 2.0, 3.0, 4.0])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @antalszava I am trying to understand why this bug didn't cause a problem for us. What was There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very good question! Another good question is if this change would affect your custom device jacobians 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I see: We had actually worked around this by returning |
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 seems to be broken. Probably it should be #2485 without the -sf?
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.
Yes, thank you for catching that! 🎉