Quantum Kernels#

QuantumKernel#

class QuantumKernel(feature_map=None, n_qubits=4, device='default.qubit', batch_size=None)[source]

Bases: BaseEstimator

Quantum kernel using fidelity between encoded states.

Computes the Gram matrix where each entry is the fidelity (squared inner product) between quantum-encoded data points:

K[i, j] = |⟨φ(x_i) | φ(x_j)⟩|^2

This quantum kernel approach was introduced in the landmark paper:

  • Havlíček et al. (2019), “Supervised learning with quantum-inspired kernel” (arXiv:1904.01567). The paper demonstrates how quantum circuits can implicitly define kernel functions that may provide quantum advantage for classification tasks.

The kernel computation uses the overlap circuit method: by applying the adjoint of the encoding circuit for x_j after the encoding for x_i, the probability of measuring the all-zeros state equals the squared fidelity between the two encoded quantum states.

Parameters:
  • feature_map (FeatureMap, optional) – The quantum feature map. If None, uses AngleFeatureMap() with n_qubits.

  • n_qubits (int, default=4) – Number of qubits. Only used if feature_map is None.

  • device (str, default="default.qubit") – PennyLane device.

  • batch_size (int, optional) – Number of circuits to evaluate in parallel.

feature_map_

Fitted feature map.

Type:

FeatureMap

n_qubits_

Number of qubits used.

Type:

int

References

fit(X)[source]

Fit the kernel.

Parameters:

X (array-like, shape (n_samples, n_features)) – Training data.

Returns:

self

Return type:

QuantumKernel

compute_matrix(X, Y=None)[source]

Compute the kernel matrix.

Parameters:
  • X (array-like, shape (n_samples_X, n_features)) – First set.

  • Y (array-like, shape (n_samples_Y, n_features), optional) – Second set. If None, uses X.

Returns:

K – Kernel matrix.

Return type:

ndarray, shape (n_samples_X, n_samples_Y)

QSVC#

class QSVC(*, n_qubits: int = 4, feature_map: FeatureMap | None = None, device: str = 'default.qubit', C: float = 1.0, shrinking: bool = True, probability: bool = False, tol: float = 0.001, cache_size: float = 200, class_weight=None, verbose: bool = False, max_iter: int = -1, decision_function_shape: str = 'ovr', break_ties: bool = False, random_state: int | None = None, kernel: str = 'precomputed')[source]

Bases: QuantumEstimator, ClassifierMixin

Quantum Support Vector Classifier.

A support vector machine that uses a quantum kernel to implicitly map data into a high-dimensional quantum feature space. The kernel is computed as the fidelity (squared inner product) between quantum- encoded states, following the quantum kernel method of Havlíček et al. (2019, arXiv:1904.01567).

The implementation: 1. Encodes data using a quantum feature map 2. Computes the quantum kernel matrix (Gram matrix) 3. Trains a classical SVM with precomputed kernel

This hybrid approach leverages quantum computers to generate kernel matrices that may be classically intractable to compute, while using well-established classical SVM training.

Parameters:
  • n_qubits (int, default=4) – Number of qubits for the quantum circuit.

  • feature_map (FeatureMap, optional) – Quantum feature map for data encoding. Default: AngleFeatureMap()

  • device (str, default="default.qubit") – PennyLane device for simulation.

  • C (float, default=1.0) – Regularization parameter. The strength of regularization is inversely proportional to C.

  • shrinking (bool, default=True) – Whether to use the shrinking heuristic.

  • probability (bool, default=False) – Whether to enable probability estimates.

  • tol (float, default=1e-3) – Tolerance for stopping criterion.

  • cache_size (float, default=200) – Kernel cache size in MB.

  • class_weight (dict or "balanced", optional) – Class weights for imbalanced datasets.

  • verbose (bool, default=False) – Enable verbose output.

  • max_iter (int, default=-1) – Maximum number of iterations (-1 for no limit).

  • decision_function_shape ({"ovr", "ovo"}, default="ovr") – Decision function shape (one-vs-rest or one-vs-one).

  • break_ties (bool, default=False) – Whether to break ties according to confidence.

  • random_state (int, optional) – Random seed.

classes_

Class labels.

Type:

ndarray

n_features_in_

Number of features seen during fit.

Type:

int

n_classes_

Number of classes.

Type:

int

kernel_

The fitted quantum kernel.

Type:

QuantumKernel

X_train_

Training data.

Type:

ndarray

K_train_

Training kernel matrix.

Type:

ndarray

svc_

The underlying scikit-learn SVM classifier.

Type:

SVC

References

fit(X, y, sample_weight=None)[source]

Fit the quantum SVM.

predict(X)[source]

Predict class labels.

predict_proba(X)[source]

Predict class probabilities.

decision_function(X)[source]

Compute decision function.

score(X, y)[source]

Return classification accuracy.

set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') QSVC

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

The QSVC uses a quantum kernel matrix computed via fidelity between encoded quantum states, then delegates to sklearn.svm.SVC.