Kernels#

Quantum Kernel#

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)