EstimatorQNN

class EstimatorQNN(*, circuit, estimator=None, observables=None, input_params=None, weight_params=None, gradient=None, input_gradients=False, default_precision=0.015625, pass_manager=None)[source]

Bases: NeuralNetwork

A neural network implementation based on the Estimator primitive.

The EstimatorQNN is a neural network that takes in a parametrized quantum circuit with designated parameters for input data and/or weights, an optional observable(s) and outputs their expectation value(s). Quite often, a combined quantum circuit is used. Such a circuit is built from two circuits: a feature map, it provides input parameters for the network, and an ansatz (weight parameters). In this case a QNNCircuit can be passed as circuit to simplify the composition of a feature map and ansatz. If a QNNCircuit is passed as circuit, the input and weight parameters do not have to be provided, because these two properties are taken from the QNNCircuit.

Example:

from qiskit import QuantumCircuit
from qiskit.circuit.library import ZZFeatureMap, RealAmplitudes
from qiskit_machine_learning.circuit.library import QNNCircuit

from qiskit_machine_learning.neural_networks import EstimatorQNN

num_qubits = 2

# Using the QNNCircuit:
# Create a parametrized 2 qubit circuit composed of the default ZZFeatureMap feature map
# and RealAmplitudes ansatz.
qnn_qc = QNNCircuit(num_qubits)

qnn = EstimatorQNN(
    circuit=qnn_qc
)

qnn.forward(input_data=[1, 2], weights=[1, 2, 3, 4, 5, 6, 7, 8])

# Explicitly specifying the ansatz and feature map:
feature_map = ZZFeatureMap(feature_dimension=num_qubits)
ansatz = RealAmplitudes(num_qubits=num_qubits)

qc = QuantumCircuit(num_qubits)
qc.compose(feature_map, inplace=True)
qc.compose(ansatz, inplace=True)

qnn = EstimatorQNN(
    circuit=qc,
    input_params=feature_map.parameters,
    weight_params=ansatz.parameters
)

qnn.forward(input_data=[1, 2], weights=[1, 2, 3, 4, 5, 6, 7, 8])

The following attributes can be set via the constructor but can also be read and updated once the EstimatorQNN object has been constructed.

estimator

The estimator primitive used to compute the neural network’s results.

Type:

BaseEstimator

gradient

The estimator gradient to be used for the backward pass.

Type:

BaseEstimatorGradient

Parameters:
  • circuit (QuantumCircuit) – The quantum circuit to represent the neural network. If a QNNCircuit is passed, the input_params and weight_params do not have to be provided, because these two properties are taken from the QNNCircuit.

  • estimator (BaseEstimator | BaseEstimatorV2 | None) –

    The estimator used to compute neural network’s results. If None, a default instance of the reference estimator, Estimator, will be used.

    Warning

    The assignment estimator=None defaults to using Estimator, which points to a deprecated estimator V1 (as of Qiskit 1.2). EstimatorQNN will adopt Estimator V2 as default no later than Qiskit Machine Learning 0.9.

  • observables (Sequence[BaseOperator] | BaseOperator | None) – The observables for outputs of the neural network. If None, use the default \(Z^{\otimes n}\) observable, where \(n\) is the number of qubits.

  • input_params (Sequence[Parameter] | None) – The parameters that correspond to the input data of the network. If None, the input data is not bound to any parameters. If a QNNCircuit is provided the input_params value here is ignored. Instead, the value is taken from the QNNCircuit input_parameters.

  • weight_params (Sequence[Parameter] | None) – The parameters that correspond to the trainable weights. If None, the weights are not bound to any parameters. If a QNNCircuit is provided the weight_params value here is ignored. Instead, the value is taken from the weight_parameters associated with QNNCircuit.

  • gradient (BaseEstimatorGradient | None) – The estimator gradient to be used for the backward pass. If None, a default instance of the estimator gradient, ParamShiftEstimatorGradient, will be used.

  • input_gradients (bool) – Determines whether to compute gradients with respect to input data. Note that this parameter is False by default, and must be explicitly set to True for a proper gradient computation when using TorchConnector.

  • default_precision (float) – The default precision for the estimator if not specified during run.

  • pass_manager (BasePassManager | None) – The pass manager to transpile the circuits, if necessary. Defaults to None, as some primitives do not need transpiled circuits.

Raises:

Attributes

circuit

The quantum circuit representing the neural network.

default_precision

Return the default precision

input_gradients

Returns whether gradients with respect to input data are computed by this neural network in the backward method or not. By default, such gradients are not computed.

input_params

The parameters that correspond to the input data of the network.

num_inputs

Returns the number of input features.

num_weights

Returns the number of trainable weights.

observables

Returns the underlying observables of this QNN.

output_shape

Returns the output shape.

sparse

Returns whether the output is sparse or not.

weight_params

The parameters that correspond to the trainable weights.

Methods

backward(input_data, weights)

Backward pass of the network.

Parameters:
  • input_data (float | list[float] | ndarray | None) – input data of the shape (num_inputs). In case of a single scalar input it is directly cast to and interpreted like a one-element array.

  • weights (float | list[float] | ndarray | None) – trainable weights of the shape (num_weights). In case of a single scalar weight

  • array. (it is directly cast to and interpreted like a one-element)

Returns:

The result of the neural network of the backward pass, i.e., a tuple with the gradients for input and weights of shape (output_shape, num_input) and (output_shape, num_weights), respectively.

Return type:

tuple[ndarray | SparseArray | None, ndarray | SparseArray | None]

forward(input_data, weights)

Forward pass of the network.

Parameters:
  • input_data (float | list[float] | ndarray | None) – input data of the shape (num_inputs). In case of a single scalar input it is directly cast to and interpreted like a one-element array.

  • weights (float | list[float] | ndarray | None) – trainable weights of the shape (num_weights). In case of a single scalar weight it is directly cast to and interpreted like a one-element array.

Returns:

The result of the neural network of the shape (output_shape).

Return type:

ndarray | SparseArray