QrEstimatorQNN module

class QrEstimatorQNN(*, circuit, estimator, observables, input_params, weight_params, gradient, input_gradients, default_precision, pass_manager)

A neural network implementation based on the Estimator primitive.

This class is a derivative of the Qiskit Machine Learning Package class EstimatorQNN. Please refer to the class for more documentation.

The QrEstimatorQNN 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 quantumrings.toolkit.qiskit.machine_learning import QrEstimatorQNN as 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.

Attributes:

estimator (BaseEstimator): The estimator primitive used to compute the neural network’s results. gradient (BaseEstimatorGradient): The estimator gradient to be used for the backward pass.

__init__(self, *, circuit, estimator, observables, input_params, weight_params, gradient, input_gradients, default_precision, pass_manager)
Args:
circuit: 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: Not used.
observables: 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: 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: 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: 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: 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: The default precision for the estimator if not specified during run.
pass_manager: The pass manager to transpile the circuits, if necessary.
Defaults to None, as some primitives do not need transpiled circuits.