Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
supports adjoint
Browse files Browse the repository at this point in the history
dominikandreasseitz committed Nov 1, 2023
1 parent 1f2954d commit 19a7eaa
Showing 7 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion qadence/backend.py
Original file line number Diff line number Diff line change
@@ -93,7 +93,7 @@ class Backend(ABC):
name: BackendName
supports_ad: bool
support_bp: bool
# supports_adjoint: bool
supports_adjoint: bool
is_remote: bool
with_measurements: bool
native_endianness: Endianness
2 changes: 1 addition & 1 deletion qadence/backends/adjoint.py
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ def forward(
ctx.save_for_backward(*param_values)
return overlap(ctx.out_state, ctx.projected_state)

# TODO filter observable params so the length of param_values is the same as grads
@staticmethod
def backward(ctx: Any, grad_out: Tensor) -> tuple:
def _circuit_backward(ctx: Any, circuit: PyQCircuit = None) -> Any:
@@ -40,7 +41,6 @@ def _circuit_backward(ctx: Any, circuit: PyQCircuit = None) -> Any:
param_values = ctx.saved_tensors
values = param_dict(ctx.param_names, param_values)
grads: list = []
# ctx.needs_input_grad[3:]
for op in circuit.reverse():
if isinstance(op, ScalePyQOperation):
ctx.out_state = apply_operator(
1 change: 1 addition & 0 deletions qadence/backends/braket/backend.py
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ class Backend(BackendInterface):
# set standard interface parameters
name: BackendName = BackendName.BRAKET
supports_ad: bool = False
supports_adjoint: bool = False
support_bp: bool = False
is_remote: bool = False
with_measurements: bool = True
1 change: 1 addition & 0 deletions qadence/backends/pulser/backend.py
Original file line number Diff line number Diff line change
@@ -122,6 +122,7 @@ class Backend(BackendInterface):

name: BackendName = BackendName.PULSER
supports_ad: bool = False
supports_adjoint: bool = False
support_bp: bool = False
is_remote: bool = False
with_measurements: bool = True
1 change: 1 addition & 0 deletions qadence/backends/pyqtorch/backend.py
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ class Backend(BackendInterface):
name: BackendName = BackendName.PYQTORCH
supports_ad: bool = True
support_bp: bool = True
supports_adjoint: bool = True
is_remote: bool = False
with_measurements: bool = True
with_noise: bool = False
2 changes: 2 additions & 0 deletions qadence/extensions.py
Original file line number Diff line number Diff line change
@@ -56,6 +56,8 @@ def _gpsr_fns() -> dict:
def _validate_diff_mode(backend: Backend, diff_mode: DiffMode) -> None:
if not backend.supports_ad and diff_mode == DiffMode.AD:
raise TypeError(f"Backend {backend.name} does not support diff_mode {DiffMode.AD}.")
elif not backend.supports_adjoint and diff_mode == DiffMode.ADJOINT:
raise TypeError(f"Backend {backend.name} does not support diff_mode {DiffMode.ADJOINT}.")


def _set_backend_config(backend: Backend, diff_mode: DiffMode) -> None:
1 change: 0 additions & 1 deletion tests/backends/test_adjoint.py
Original file line number Diff line number Diff line change
@@ -76,7 +76,6 @@ def func(theta: torch.Tensor) -> torch.Tensor:
all_params = embeddings_fn(params, inputs)
return bknd.expectation(pyqtorch_circ, pyqtorch_obs, all_params)

# assert torch.autograd.gradcheck(func, theta)
exp = bknd.expectation(
pyqtorch_circ, pyqtorch_obs, embeddings_fn(params, {param_name: theta})
)

0 comments on commit 19a7eaa

Please sign in to comment.