QrSamplerQNN module
- class QrSamplerQNN(*, circuit, sampler, input_params, weight_params, sparse, interpret, output_shape, gradient, input_gradients, pass_manager)
A neural network implementation based on the Sampler primitive.
This class is a derivative of the Qiskit Machine Learning Package class
SamplerQNN
. Please refer to the class for more documentation.The
QrSamplerQNN
is a neural network that takes in a parametrized quantum circuit with designated parameters for input data and/or weights and translates the quasi-probabilities estimated by theSampler
primitive into predicted classes. 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 aQNNCircuit
can be passed as circuit to simplify the composition of a feature map and ansatz. If aQNNCircuit
is passed as circuit, the input and weight parameters do not have to be provided, because these two properties are taken from theQNNCircuit
.The output can be set up in different formats, and an optional post-processing step can be used to interpret the sampler’s output in a particular context (e.g. mapping the resulting bitstring to match the number of classes).
In this example the network maps the output of the quantum circuit to two classes via a custom interpret function:
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 QrSamplerQNN as SamplerQNN num_qubits = 2 def parity(x): return f"{bin(x)}".count("1") % 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 = SamplerQNN( circuit=qnn_qc, interpret=parity, output_shape=2 ) 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 = SamplerQNN( circuit=qc, input_params=feature_map.parameters, weight_params=ansatz.parameters, interpret=parity, output_shape=2 ) 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 SamplerQNN object has been constructed.
Attributes:
sampler (BaseSampler): The sampler primitive used to compute the neural network’s results. gradient (BaseSamplerGradient): A sampler gradient to be used for the backward pass.
- __init__(self, *, circuit, sampler, input_params, weight_params, sparse, interpret, output_shape, gradient, input_gradients, pass_manager)
- Args:
- circuit: The parametrized quantumcircuit that generates the samples of this network. If a
QNNCircuit
is passed,the input_params and weight_params do not have to be provided, because these twoproperties are taken from theQNNCircuit
.sampler: Not used.input_params: The parameters of the circuit corresponding to the input. If aQNNCircuit
is provided theinput_params value here is ignored. Instead, the value is taken from theQNNCircuit
input_parameters.weight_params: The parameters of the circuit corresponding to the trainable weights. If aQNNCircuit
is provided theweight_params value here is ignored. Instead, the value is taken from theQNNCircuit
weight_parameters
.sparse: Returns whether the output is sparse or not.interpret: A callable that maps the measured integer to another unsigned integer or tupleof unsigned integers. These are used as new indices for the (potentially sparse)output array. If no interpret function is passed, then an identity function will beused by this neural network.output_shape: The output shape of the custom interpretation. For SamplerV1, it is ignoredif no custom interpret method is provided where the shape is taken to be2^circuit.num_qubits
.gradient: An optional sampler gradient to be used for the backward pass. IfNone
isgiven, a default instance ofParamShiftSamplerGradient
will be used.input_gradients: Determines whether to compute gradients with respect to input data. Notethat this parameter isFalse
by default, and must be explicitly set toTrue
for a proper gradient computation when usingTorchConnector
.pass_manager: The pass manager to transpile the circuits, if necessary.Defaults toNone
, as some primitives do not need transpiled circuits.