Skip to content
Pasqal Documentation

DifferentiableBackend

DifferentiableBackend(backend, diff_mode=DiffMode.AD, **psr_args)

Section titled “ DifferentiableBackend(backend, diff_mode=DiffMode.AD, **psr_args) ”

Bases: DifferentiableBackend

A class which wraps a QuantumBackend with the automatic differentation engine TORCH.

PARAMETER DESCRIPTION
backend

An instance of the QuantumBackend type perform execution.

TYPE: Backend

diff_mode

A differentiable mode supported by the differentiation engine.

TYPE: DiffMode DEFAULT: AD

**psr_args

Arguments that will be passed on to DifferentiableExpectation.

TYPE: int | float | None DEFAULT: {}

Source code in qadence/engines/torch/differentiable_backend.py
33
34
35
36
37
38
39
40def __init__(
self,
backend: QuantumBackend,
diff_mode: DiffMode = DiffMode.AD,
**psr_args: int | float | None,
) -> None:
super().__init__(backend=backend, engine=Engine.TORCH, diff_mode=diff_mode)
self.psr_args = psr_args

expectation(circuit, observable, param_values={}, state=None, measurement=None, noise=None, mitigation=None, endianness=Endianness.BIG)

Section titled “ expectation(circuit, observable, param_values={}, state=None, measurement=None, noise=None, mitigation=None, endianness=Endianness.BIG) ”

Compute the expectation value of the circuit with the given observable.

PARAMETER DESCRIPTION
circuit

A converted circuit as returned by backend.circuit.

TYPE: ConvertedCircuit

observable

A converted observable as returned by backend.observable.

TYPE: list[ConvertedObservable] | ConvertedObservable

param_values

Already embedded parameters of the circuit. See embedding for more info.

TYPE: ParamDictType DEFAULT: {}

state

Initial state.

TYPE: ArrayLike | None DEFAULT: None

measurement

Optional measurement protocol. If None, use exact expectation value with a statevector simulator.

TYPE: Measurements | None DEFAULT: None

noise

A noise model to use.

TYPE: NoiseHandler | None DEFAULT: None

mitigation

The error mitigation to use.

TYPE: Mitigations | None DEFAULT: None

endianness

Endianness of the resulting bit strings.

TYPE: Endianness DEFAULT: BIG

Source code in qadence/engines/torch/differentiable_backend.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88def expectation(
self,
circuit: ConvertedCircuit,
observable: list[ConvertedObservable] | ConvertedObservable,
param_values: ParamDictType = {},
state: ArrayLike | None = None,
measurement: Measurements | None = None,
noise: NoiseHandler | None = None,
mitigation: Mitigations | None = None,
endianness: Endianness = Endianness.BIG,
) -> ArrayLike:
"""Compute the expectation value of the `circuit` with the given `observable`.
Arguments:
circuit: A converted circuit as returned by `backend.circuit`.
observable: A converted observable as returned by `backend.observable`.
param_values: _**Already embedded**_ parameters of the circuit. See
[`embedding`][qadence.blocks.embedding.embedding] for more info.
state: Initial state.
measurement: Optional measurement protocol. If None, use
exact expectation value with a statevector simulator.
noise: A noise model to use.
mitigation: The error mitigation to use.
endianness: Endianness of the resulting bit strings.
"""
observable = observable if isinstance(observable, list) else [observable]
differentiable_expectation = DifferentiableExpectation(
backend=self.backend,
circuit=circuit,
observable=observable,
param_values=param_values,
state=state,
measurement=measurement,
noise=noise,
mitigation=mitigation,
endianness=endianness,
)
if self.diff_mode == DiffMode.AD:
expectation = differentiable_expectation.ad
elif self.diff_mode == DiffMode.ADJOINT:
expectation = differentiable_expectation.adjoint
elif self.diff_mode == DiffMode.GPSR:
expectation = partial(
differentiable_expectation.psr, psr_fn=general_psr, **self.psr_args
)
return expectation()

DifferentiableBackend(backend, diff_mode=DiffMode.AD, **psr_args)

Section titled “ DifferentiableBackend(backend, diff_mode=DiffMode.AD, **psr_args) ”

Bases: DifferentiableBackend

A class which wraps a QuantumBackend with the automatic differentation engine JAX.

PARAMETER DESCRIPTION
backend

An instance of the QuantumBackend type perform execution.

TYPE: Backend

diff_mode

A differentiable mode supported by the differentiation engine.

TYPE: DiffMode DEFAULT: AD

**psr_args

Arguments that will be passed on to DifferentiableExpectation.

TYPE: int | float | None DEFAULT: {}

Source code in qadence/engines/jax/differentiable_backend.py
29
30
31
32
33
34
35
36def __init__(
self,
backend: Backend,
diff_mode: DiffMode = DiffMode.AD,
**psr_args: int | float | None,
) -> None:
super().__init__(backend=backend, engine=Engine.JAX, diff_mode=diff_mode)
self.psr_args = psr_args

expectation(circuit, observable, param_values={}, state=None, measurement=None, noise=None, mitigation=None, endianness=Endianness.BIG)

Section titled “ expectation(circuit, observable, param_values={}, state=None, measurement=None, noise=None, mitigation=None, endianness=Endianness.BIG) ”

Compute the expectation value of the circuit with the given observable.

PARAMETER DESCRIPTION
circuit

A converted circuit as returned by backend.circuit.

TYPE: ConvertedCircuit

observable

A converted observable as returned by backend.observable.

TYPE: list[ConvertedObservable] | ConvertedObservable

param_values

Already embedded parameters of the circuit. See embedding for more info.

TYPE: ParamDictType DEFAULT: {}

state

Initial state.

TYPE: ArrayLike | None DEFAULT: None

measurement

Optional measurement protocol. If None, use exact expectation value with a statevector simulator.

TYPE: Measurements | None DEFAULT: None

noise

A noise model to use.

TYPE: NoiseHandler | None DEFAULT: None

mitigation

The error mitigation to use.

TYPE: Mitigations | None DEFAULT: None

endianness

Endianness of the resulting bit strings.

TYPE: Endianness DEFAULT: BIG

Source code in qadence/engines/jax/differentiable_backend.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79def expectation(
self,
circuit: ConvertedCircuit,
observable: list[ConvertedObservable] | ConvertedObservable,
param_values: ParamDictType = {},
state: ArrayLike | None = None,
measurement: Measurements | None = None,
noise: NoiseHandler | None = None,
mitigation: Mitigations | None = None,
endianness: Endianness = Endianness.BIG,
) -> ArrayLike:
"""Compute the expectation value of the `circuit` with the given `observable`.
Arguments:
circuit: A converted circuit as returned by `backend.circuit`.
observable: A converted observable as returned by `backend.observable`.
param_values: _**Already embedded**_ parameters of the circuit. See
[`embedding`][qadence.blocks.embedding.embedding] for more info.
state: Initial state.
measurement: Optional measurement protocol. If None, use
exact expectation value with a statevector simulator.
noise: A noise model to use.
mitigation: The error mitigation to use.
endianness: Endianness of the resulting bit strings.
"""
observable = observable if isinstance(observable, list) else [observable]
if self.diff_mode == DiffMode.AD:
expectation = self.backend.expectation(circuit, observable, param_values, state)
else:
expectation = DifferentiableExpectation(
backend=self.backend,
circuit=circuit,
observable=observable,
param_values=param_values,
state=state,
measurement=measurement,
noise=noise,
mitigation=mitigation,
endianness=endianness,
).psr()
return expectation