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

Update deprecated gradient_kwargs usage #1045

Merged
merged 18 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

### Improvements

* Update Lightning integration tests following the `gradient_kwargs` deprecation
in Pennylane.
[(#1045)](https://github.com/PennyLaneAI/pennylane-lightning/pull/1045)

* Update `qml.MultiControlledX` tests following the latest updates in PennyLane.
[(#1040)](https://github.com/PennyLaneAI/pennylane-lightning/pull/1040)

Expand All @@ -31,7 +35,7 @@

This release contains contributions from (in alphabetical order):

Yushao Chen, Amintor Dusko, Joseph Lee, Shuli Shu
Yushao Chen, Amintor Dusko, Joseph Lee, Andrija Paurevic, Shuli Shu

---

Expand Down
16 changes: 10 additions & 6 deletions mpitests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
],
)
@pytest.mark.parametrize("batch_obs", [True, False])
def test_r_gradient(

Check notice on line 229 in mpitests/test_adjoint_jacobian.py

View check run for this annotation

codefactor.io / CodeFactor

mpitests/test_adjoint_jacobian.py#L229

Too many positional arguments (8/5) (too-many-positional-arguments)
self, tol, param, rotation, meas, expected_func, batch_obs, dev
): # pylint: disable=too-many-arguments
"""Test for the gradient of the rotation gate matches the known formula."""
Expand Down Expand Up @@ -546,7 +546,7 @@
h = self.tol_for_allclose(dev.c_dtype)
tol = self.tol_for_allclose(dev.c_dtype)

qnode2 = QNode(circuit, dev, diff_method="finite-diff", h=h)
qnode2 = QNode(circuit, dev, diff_method="finite-diff", gradient_kwargs={"h": h})
grad_fn = qml.grad(qnode2)
grad_F = grad_fn(*args)

Expand All @@ -556,7 +556,7 @@

@pytest.mark.parametrize("reused_p", thetas**3 / 19)
@pytest.mark.parametrize("other_p", thetas**2 / 1)
def test_fanout_multiple_params(

Check notice on line 559 in mpitests/test_adjoint_jacobian.py

View check run for this annotation

codefactor.io / CodeFactor

mpitests/test_adjoint_jacobian.py#L559

Too many positional arguments (6/5) (too-many-positional-arguments)
self, reused_p, other_p, tol, mocker, dev
): # pylint: disable=too-many-arguments
"""Tests that the correct gradient is computed for qnodes which
Expand Down Expand Up @@ -638,7 +638,7 @@
h = self.tol_for_allclose(dev.c_dtype)
tol = self.tol_for_allclose(dev.c_dtype)

cost = QNode(circuit, dev, diff_method="finite-diff", h=h)
cost = QNode(circuit, dev, diff_method="finite-diff", gradient_kwargs={"h": h})

grad_fn = qml.grad(cost)
grad_F = grad_fn(params)
Expand Down Expand Up @@ -678,7 +678,7 @@
tol = self.tol_for_allclose(dev.c_dtype)

qnode1 = QNode(f, dev, interface="tf", diff_method="adjoint")
qnode2 = QNode(f, dev, interface="tf", diff_method="finite-diff", h=h)
qnode2 = QNode(f, dev, interface="tf", diff_method="finite-diff", gradient_kwargs={"h": h})

with tf.GradientTape() as tape:
res1 = qnode1(params1, params2)
Expand Down Expand Up @@ -710,7 +710,9 @@
h = self.tol_for_allclose(dev.c_dtype)

qnode1 = QNode(f, dev, interface="torch", diff_method="adjoint")
qnode2 = QNode(f, dev, interface="torch", diff_method="finite-diff", h=h)
qnode2 = QNode(
f, dev, interface="torch", diff_method="finite-diff", gradient_kwargs={"h": h}
)

res1 = qnode1(params1, params2)
res1.backward()
Expand Down Expand Up @@ -749,7 +751,9 @@
tol = self.tol_for_allclose(dev.c_dtype)

qnode_adjoint = QNode(f, dev, interface="jax", diff_method="adjoint")
qnode_fd = QNode(f, dev, interface="jax", diff_method="finite-diff", h=h)
qnode_fd = QNode(
f, dev, interface="jax", diff_method="finite-diff", gradient_kwargs={"h": h}
)

grad_adjoint = jax.grad(qnode_adjoint)(params1, params2)
grad_fd = jax.grad(qnode_fd)(params1, params2)
Expand Down Expand Up @@ -1327,7 +1331,7 @@

circ = qml.QNode(circuit, dev, diff_method="adjoint")
circ_def = qml.QNode(circuit, dev_def, diff_method="adjoint")
circ_fd = qml.QNode(circuit, dev, diff_method="finite-diff", h=h)
circ_fd = qml.QNode(circuit, dev, diff_method="finite-diff", gradient_kwargs={"h": h})
circ_ps = qml.QNode(circuit, dev, diff_method="parameter-shift")
jacs = qml.jacobian(circ)(par, U)
jacs_def = qml.jacobian(circ_def)(par, U)
Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.41.0-dev6"
__version__ = "0.41.0-dev7"
16 changes: 10 additions & 6 deletions tests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ def circuit(x, y, z):

spy.assert_called()

qnode2 = QNode(circuit, dev, diff_method="finite-diff", h=h)
qnode2 = QNode(circuit, dev, diff_method="finite-diff", gradient_kwargs={"h": h})
grad_fn = qml.grad(qnode2)
grad_F = grad_fn(*args)

Expand Down Expand Up @@ -1026,7 +1026,7 @@ def circuit(params):
)
tol, h = get_tolerance_and_stepsize(dev, step_size=True)

cost = QNode(circuit, dev, diff_method="finite-diff", h=h)
cost = QNode(circuit, dev, diff_method="finite-diff", gradient_kwargs={"h": h})

grad_fn = qml.grad(cost)
grad_F = grad_fn(params)
Expand Down Expand Up @@ -1061,7 +1061,7 @@ def f(params1, params2):
params2 = tf.Variable(0.4, dtype=tf_r_dtype)

qnode1 = QNode(f, dev, interface="tf", diff_method="adjoint")
qnode2 = QNode(f, dev, interface="tf", diff_method="finite-diff", h=h)
qnode2 = QNode(f, dev, interface="tf", diff_method="finite-diff", gradient_kwargs={"h": h})

with tf.GradientTape() as tape:
res1 = qnode1(params1, params2)
Expand Down Expand Up @@ -1093,7 +1093,9 @@ def f(params1, params2):
tol, h = get_tolerance_and_stepsize(dev, step_size=True)

qnode1 = QNode(f, dev, interface="torch", diff_method="adjoint")
qnode2 = QNode(f, dev, interface="torch", diff_method="finite-diff", h=h)
qnode2 = QNode(
f, dev, interface="torch", diff_method="finite-diff", gradient_kwargs={"h": h}
)

res1 = qnode1(params1, params2)
res1.backward()
Expand Down Expand Up @@ -1130,7 +1132,9 @@ def f(params1, params2):
tol, h = get_tolerance_and_stepsize(dev, step_size=True)

qnode_adjoint = QNode(f, dev, interface="jax", diff_method="adjoint")
qnode_fd = QNode(f, dev, interface="jax", diff_method="finite-diff", h=h)
qnode_fd = QNode(
f, dev, interface="jax", diff_method="finite-diff", gradient_kwargs={"h": h}
)

grad_adjoint = jax.grad(qnode_adjoint)(params1, params2)
grad_fd = jax.grad(qnode_fd)(params1, params2)
Expand Down Expand Up @@ -1668,7 +1672,7 @@ def circuit(x, u_mat):

circ = qml.QNode(circuit, dev, diff_method="adjoint")
circ_def = qml.QNode(circuit, dev_def, diff_method="adjoint")
circ_fd = qml.QNode(circuit, dev, diff_method="finite-diff", h=h)
circ_fd = qml.QNode(circuit, dev, diff_method="finite-diff", gradient_kwargs={"h": h})
circ_ps = qml.QNode(circuit, dev, diff_method="parameter-shift")
jacs = qml.jacobian(circ)(par, U)
jacs_def = qml.jacobian(circ_def)(par, U)
Expand Down
Loading