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:
|
diff_mode
|
A differentiable mode supported by the differentiation engine. |
**psr_args
|
Arguments that will be passed on to
TYPE:
|
Source code in qadence/engines/torch/differentiable_backend.py
3334353637383940def __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
TYPE:
|
observable
|
A converted observable as returned by
TYPE:
|
param_values
|
Already embedded parameters of the circuit. See
TYPE:
|
state
|
Initial state.
TYPE:
|
measurement
|
Optional measurement protocol. If None, use exact expectation value with a statevector simulator.
TYPE:
|
noise
|
A noise model to use.
TYPE:
|
mitigation
|
The error mitigation to use.
TYPE:
|
endianness
|
Endianness of the resulting bit strings.
TYPE:
|
Source code in qadence/engines/torch/differentiable_backend.py
4243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788def 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:
|
diff_mode
|
A differentiable mode supported by the differentiation engine. |
**psr_args
|
Arguments that will be passed on to
TYPE:
|
Source code in qadence/engines/jax/differentiable_backend.py
2930313233343536def __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
TYPE:
|
observable
|
A converted observable as returned by
TYPE:
|
param_values
|
Already embedded parameters of the circuit. See
TYPE:
|
state
|
Initial state.
TYPE:
|
measurement
|
Optional measurement protocol. If None, use exact expectation value with a statevector simulator.
TYPE:
|
noise
|
A noise model to use.
TYPE:
|
mitigation
|
The error mitigation to use.
TYPE:
|
endianness
|
Endianness of the resulting bit strings.
TYPE:
|
Source code in qadence/engines/jax/differentiable_backend.py
383940414243444546474849505152535455565758596061626364656667686970717273747576777879def 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