From 3344c77e39354df70b7ca989a65f5fe982b63479 Mon Sep 17 00:00:00 2001 From: Sam Banning Date: Thu, 2 Jun 2022 13:51:33 -0400 Subject: [PATCH] Revert device gradients fix (#2595) * reverted device level jacobians issue * progress * Update autograd.py * Update autograd.py * Update autograd.py * Update autograd.py * added to changelog * added test * added test * strawberry fields fix * removed whitespace * try code cov again * Apply suggestions from code review * Update doc/releases/changelog-dev.md * Update tests/interfaces/test_autograd.py * use analytic value for expected result * Update autograd.py * Update autograd.py Co-authored-by: antalszava --- doc/releases/changelog-dev.md | 6 ++++-- pennylane/interfaces/autograd.py | 9 ++++---- tests/interfaces/test_autograd.py | 34 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index b90b889ecc6..5751505469d 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -203,6 +203,9 @@ instead of the controlled version of the diagonal unitary. [(#2525)](https://github.com/PennyLaneAI/pennylane/pull/2525) +* Updated the gradients fix [(#2485)](https://github.com/PennyLaneAI/pennylane/pull/2485) to only apply to the `strawberryfields.gbs` device, since + the original logic was breaking some devices. [(#2595)](https://github.com/PennyLaneAI/pennylane/pull/2595) +

Deprecations

Documentation

@@ -222,5 +225,4 @@ This release contains contributions from (in alphabetical order): Amintor Dusko, Chae-Yeun Park, Christian Gogolin, Christina Lee, David Wierichs, Edward Jiang, Guillermo Alonso-Linaje, -Jay Soni, Juan Miguel Arrazola, Maria Schuld, Mikhail Andrenkov, Soran Jahangiri, Utkarsh Azad - +Jay Soni, Juan Miguel Arrazola, Maria Schuld, Mikhail Andrenkov, Samuel Banning, Soran Jahangiri, Utkarsh Azad \ No newline at end of file diff --git a/pennylane/interfaces/autograd.py b/pennylane/interfaces/autograd.py index 3e63872eaa2..e4822635e99 100644 --- a/pennylane/interfaces/autograd.py +++ b/pennylane/interfaces/autograd.py @@ -234,11 +234,10 @@ def grad_fn(dy): return_vjps = [ qml.math.to_numpy(v, max_depth=_n) if isinstance(v, ArrayBox) else v for v in vjps ] - if device.capabilities().get("provides_jacobian", False): - # in the case where the device provides the jacobian, - # the output of grad_fn must be wrapped in a tuple in - # order to match the input parameters to _execute. - return (return_vjps,) + if device.short_name == "strawberryfields.gbs": # pragma: no cover + # TODO: remove this exceptional case once the source of this issue + # https://github.com/PennyLaneAI/pennylane-sf/issues/89 is determined + return (return_vjps,) # pragma: no cover return return_vjps return grad_fn diff --git a/tests/interfaces/test_autograd.py b/tests/interfaces/test_autograd.py index c98f4711dc0..63a1bc05768 100644 --- a/tests/interfaces/test_autograd.py +++ b/tests/interfaces/test_autograd.py @@ -1141,3 +1141,37 @@ def circuit(v): d_out = d_circuit(params) assert np.allclose(d_out, np.array([1.0, 2.0, 3.0, 4.0])) + + def test_custom_jacobians_2(self): + """Test computing the gradient using the parameter-shift + rule with a device that provides a jacobian""" + + class MyQubit(DefaultQubit): + @classmethod + def capabilities(cls): + capabilities = super().capabilities().copy() + capabilities.update( + provides_jacobian=True, + ) + return capabilities + + def jacobian(self, *args, **kwargs): + raise NotImplementedError() + + dev = MyQubit(wires=2) + + @qml.qnode(dev, diff_method="parameter-shift", mode="backward") + def qnode(a, b): + qml.RY(a, wires=0) + qml.RX(b, wires=1) + qml.CNOT(wires=[0, 1]) + return [qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliY(1))] + + a = np.array(0.1, requires_grad=True) + b = np.array(0.2, requires_grad=True) + + res = qml.jacobian(qnode)(a, b) + expected = ([-np.sin(a), np.sin(a) * np.sin(b)], [0, -np.cos(a) * np.cos(b)]) + + assert np.allclose(res[0], expected[0]) + assert np.allclose(res[1], expected[1])