Skip to content

DifferentiableBackend

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
def __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)

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: Noise | 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
def expectation(
    self,
    circuit: ConvertedCircuit,
    observable: list[ConvertedObservable] | ConvertedObservable,
    param_values: ParamDictType = {},
    state: ArrayLike | None = None,
    measurement: Measurements | None = None,
    noise: Noise | 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
    else:
        try:
            fns = get_gpsr_fns()
            psr_fn = fns[self.diff_mode]
        except KeyError:
            raise ValueError(f"{self.diff_mode} differentiation mode is not supported")
        expectation = partial(differentiable_expectation.psr, psr_fn=psr_fn, **self.psr_args)
    return expectation()

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
def __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)

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: Noise | 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
def expectation(
    self,
    circuit: ConvertedCircuit,
    observable: list[ConvertedObservable] | ConvertedObservable,
    param_values: ParamDictType = {},
    state: ArrayLike | None = None,
    measurement: Measurements | None = None,
    noise: Noise | 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