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
QrEstimatorQNNis 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 aQNNCircuitcan be passed as circuit to simplify the composition of a feature map and ansatz. If aQNNCircuitis passed as circuit, the input and weight parameters do not have to be provided, because these two properties are taken from theQNNCircuit.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
QNNCircuitis passed, theinput_paramsandweight_paramsdo not have to be provided, because these twoproperties are taken from theQNNCircuit.estimator: Not used.observables: The observables for outputs of the neural network. IfNone,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.IfNone, the input data is not bound to any parameters.If aQNNCircuitis provided theinput_params value here is ignored. Instead, the value is taken from theQNNCircuitinput_parameters.weight_params: The parameters that correspond to the trainable weights.IfNone, the weights are not bound to any parameters.If aQNNCircuitis provided theweight_params value here is ignored. Instead, the value is taken from theweight_parameters associated withQNNCircuit.gradient: The estimator gradient to be used for the backward pass.IfNone, 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 isFalseby default, and must be explicitly set toTruefor a proper gradient computation when usingTorchConnector.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 toNone, as some primitives do not need transpiled circuits.