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

Raise warning when falling back on param-shift #224

Merged
merged 1 commit into from
Jan 18, 2024
Merged
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
9 changes: 9 additions & 0 deletions src/braket/pennylane_plugin/braket_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import collections
import numbers
import warnings
from collections.abc import Iterable, Sequence

# pylint: disable=invalid-name
Expand Down Expand Up @@ -848,6 +849,14 @@ def execute_and_gradients(self, circuits, **kwargs):
new_jac = np.tensor([])
elif len(observables) != 1 or measurements[0].return_type != Expectation:
gradient_circuits, post_processing_fn = param_shift(circuit)
warnings.warn(
"This circuit cannot be differentiated with the adjoint method. "
"Falling back to the parameter-shift method, which will execute "
f"{len(gradient_circuits)} circuits. "
"To use the adjoint gradient method, make sure the circuit's only measurement "
"is an expectation of one observable. "
"To avoid gradient calculation, mark parameters with `requires_grad=False`."
)
grad_circuit_results = [
self.execute(c, compute_gradient=False) for c in gradient_circuits
]
Expand Down