EstimatorQNN#

class EstimatorQNN(*, circuit, estimator=None, observables=None, input_params=None, weight_params=None, gradient=None, input_gradients=False)[소스]#

기반 클래스: 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 parameterized 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

매개변수:
  • estimator (BaseEstimator | None) – The estimator used to compute neural network’s results. If None, a default instance of the reference estimator, Estimator, will be used.

  • 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.

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

  • 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 QNNCircuit weight_parameters.

  • 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.

예외 발생:

QiskitMachineLearningError – Invalid parameter values.

Attributes

circuit#

The quantum circuit representing the neural network.

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.

매개변수:
  • 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) –

반환:

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.

반환 형식:

tuple[numpy.ndarray | qiskit_machine_learning.neural_networks.neural_network.SparseArray | None, numpy.ndarray | qiskit_machine_learning.neural_networks.neural_network.SparseArray | None]

forward(input_data, weights)#

Forward pass of the network.

매개변수:
  • 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.

반환:

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

반환 형식:

ndarray | SparseArray