VQC¶

class VQC(num_qubits=None, feature_map=None, ansatz=None, loss='cross_entropy', optimizer=None, warm_start=False, quantum_instance=None, initial_point=None, callback=None, *, sampler=None)[source]

A convenient Variational Quantum Classifier implementation.

The variational quantum classifier (VQC) is a variational algorithm where the measured bitstrings are interpreted as the output of a classifier.

Constructs a quantum circuit and corresponding neural network, then uses it to instantiate a neural network classifier.

Labels can be passed in various formats, they can be plain labels, a one dimensional numpy array that contains integer labels like [0, 1, 2, ...], or a numpy array with categorical string labels. One hot encoded labels are also supported. Internally, labels are transformed to one hot encoding and the classifier is always trained on one hot labels.

Multi-label classification is not supported. E.g., $$[[1, 1, 0], [0, 1, 1], [1, 0, 1]]$$.

Parameters:
• num_qubits (int | None) -- The number of qubits for the underlying QNN. If None is given, the number of qubits is derived from the feature map or ansatz. If neither of those is given, raises an exception. The number of qubits in the feature map and ansatz are adjusted to this number if required.

• feature_map (QuantumCircuit | None) -- The (parametrized) circuit to be used as a feature map for the underlying QNN. If None is given, the ZZFeatureMap is used if the number of qubits is larger than 1. For a single qubit classification problem the ZFeatureMap is used by default.

• ansatz (QuantumCircuit | None) -- The (parametrized) circuit to be used as an ansatz for the underlying QNN. If None is given then the RealAmplitudes circuit is used.

• loss (str | Loss) -- A target loss function to be used in training. Default value is cross_entropy.

• optimizer (Optimizer | None) -- An instance of an optimizer to be used in training. When None defaults to SLSQP.

• warm_start (bool) -- Use weights from previous fit to start next fit.

• quantum_instance (QuantumInstance | Backend | None) -- Deprecated: If a quantum instance is sent and sampler is None, the underlying QNN will be of type CircuitQNN, and the quantum instance will be used to compute the neural network's results. If a sampler instance is also set, it will override the quantum_instance parameter and a SamplerQNN will be used instead.

• initial_point (np.ndarray | None) -- Initial point for the optimizer to start from.

• callback (Callable[[np.ndarray, float], None] | None) -- a reference to a user's callback function that has two parameters and returns None. The callback can access intermediate data during training. On each iteration an optimizer invokes the callback and passes current weights as an array and a computed value as a float of the objective function being optimized. This allows to track how well optimization / training process is going on.

• sampler (BaseSampler | None) -- If a sampler instance is sent, the underlying QNN will be of type SamplerQNN, and the sampler primitive will be used to compute the neural network's results.

Raises:

QiskitMachineLearningError -- Needs at least one out of num_qubits, feature_map or ansatz to be given. Or the number of qubits in the feature map and/or ansatz can't be adjusted to num_qubits.

Attributes

 ansatz Returns the used ansatz. circuit Returns the underlying quantum circuit. feature_map Returns the used feature map. num_qubits Returns the number of qubits used by ansatz and feature map.

Methods