Linguaggi
English
Bengali
French
Hindi
Italian
Japanese
Korean
Malayalam
Russian
Spanish
Tamil
Turkish
Vietnamese



SamplerQNN

class SamplerQNN(*, circuit, sampler=None, input_params=None, weight_params=None, sparse=False, interpret=None, output_shape=None, gradient=None, input_gradients=False)[sorgente]

A neural network implementation based on the Sampler primitive.

The SamplerQNN 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 the Sampler 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).

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.neural_networks import SamplerQNN

num_qubits = 2
feature_map = ZZFeatureMap(feature_dimension=num_qubits)
ansatz = RealAmplitudes(num_qubits=num_qubits, reps=1)

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


def parity(x):
    return "{:b}".format(x).count("1") % 2


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])

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

sampler

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

Type:

BaseSampler

gradient

A sampler gradient to be used for the backward pass.

Type:

BaseSamplerGradient

Parametri:
  • sampler (BaseSampler | None) – The sampler primitive used to compute the neural network’s results. If None is given, a default instance of the reference sampler defined by Sampler will be used.

  • circuit (QuantumCircuit) – The parametrized quantum circuit that generates the samples of this network.

  • input_params (Sequence[Parameter] | None) – The parameters of the circuit corresponding to the input.

  • weight_params (Sequence[Parameter] | None) – The parameters of the circuit corresponding to the trainable weights.

  • sparse (bool) – Returns whether the output is sparse or not.

  • interpret (Callable[[int], int | tuple[int, ...]] | None) – A callable that maps the measured integer to another unsigned integer or tuple of 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 be used by this neural network.

  • output_shape (int | tuple[int, ...] | None) – The output shape of the custom interpretation. It is ignored if no custom interpret method is provided where the shape is taken to be 2^circuit.num_qubits..

  • gradient (BaseSamplerGradient | None) – An optional sampler gradient to be used for the backward pass. If None is given, a default instance of ParamShiftSamplerGradient 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.

Solleva:

QiskitMachineLearningError – Invalid parameter values.

Attributes

circuit

Returns the underlying quantum circuit.

input_params

Returns the list of input parameters.

interpret

Returns interpret function to be used by the neural network.

weight_params

Returns the list of trainable weights parameters.

Methods

set_interpret([interpret, output_shape])

Change 'interpret' and corresponding 'output_shape'.