From 9c785ce46961f6cc40d9f466952b061079a325e9 Mon Sep 17 00:00:00 2001 From: Christina Lee Date: Wed, 15 Jan 2025 17:38:40 -0500 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Yushao Chen (Jerry) --- doc/releases/changelog-dev.md | 2 +- pennylane/workflow/jacobian_products.py | 10 ++++++---- tests/gradients/core/test_pulse_gradient.py | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index fe3de10d1cd..55c9656dfcf 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -15,7 +15,7 @@ * The coefficients of observables now have improved differentiability. [(#6598)](https://github.com/PennyLaneAI/pennylane/pull/6598) -* An informative error is raised in a qnode with `diff_method=None` is differentiated. +* An informative error is raised when a `QNode` with `diff_method=None` is differentiated. [(#6770)](https://github.com/PennyLaneAI/pennylane/pull/6770)

Breaking changes 💔

diff --git a/pennylane/workflow/jacobian_products.py b/pennylane/workflow/jacobian_products.py index a83d3e0f0f6..f27d554b70a 100644 --- a/pennylane/workflow/jacobian_products.py +++ b/pennylane/workflow/jacobian_products.py @@ -212,20 +212,22 @@ def execute_and_compute_jacobian(self, tapes: QuantumScriptBatch) -> tuple[Resul class NoGradients(JacobianProductCalculator): """A jacobian product calculator that raises errors when a vjp or jvp is requested.""" + + error_msg = "Derivatives cannot be calculated with diff_method=None" def compute_jacobian(self, tapes: QuantumScriptBatch) -> tuple: - raise qml.QuantumFunctionError("Derivatives cannot be calculated with diff_method=None") + raise qml.QuantumFunctionError(NoGradients.error_msg) def compute_vjp(self, tapes: QuantumScriptBatch, dy: Sequence[Sequence[TensorLike]]) -> tuple: - raise qml.QuantumFunctionError("Derivatives cannot be calculated with diff_method=None") + raise qml.QuantumFunctionError(NoGradients.error_msg) def execute_and_compute_jvp( self, tapes: QuantumScriptBatch, tangents: Sequence[Sequence[TensorLike]] ) -> tuple[ResultBatch, tuple]: - raise qml.QuantumFunctionError("Derivatives cannot be calculated with diff_method=None") + raise qml.QuantumFunctionError(NoGradients.error_msg) def execute_and_compute_jacobian(self, tapes: QuantumScriptBatch) -> tuple[ResultBatch, tuple]: - raise qml.QuantumFunctionError("Derivatives cannot be calculated with diff_method=None") + raise qml.QuantumFunctionError(NoGradients.error_msg) class TransformJacobianProducts(JacobianProductCalculator): diff --git a/tests/gradients/core/test_pulse_gradient.py b/tests/gradients/core/test_pulse_gradient.py index 95fa71d706c..c684d53b0c6 100644 --- a/tests/gradients/core/test_pulse_gradient.py +++ b/tests/gradients/core/test_pulse_gradient.py @@ -1152,7 +1152,8 @@ def test_constant_commuting(self, t): assert qml.math.allclose(res, exp_grad) r = qml.execute([tape], dev, None) # Effective rotation parameters - assert qml.math.isclose(r, jnp.cos(2 * p[0]) * jnp.cos(2 * p[1])) + exp = jnp.cos(2 * p[0]) * jnp.cos(2 * p[1]) + assert qml.math.isclose(r, exp) jax.clear_caches() @pytest.mark.slow