Kernels#

Fidelity Quantum Kernel#

class FidelityQuantumKernel(encoder: BaseEncoder | None = None, n_qubits: int = 4, device: str = 'default.qubit', batch_size: int | None = None)[source]#

Bases: BaseEstimator

Fidelity-based quantum kernel matrix calculator.

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

\(K[i, j] = |\langle \phi(x_i) | \phi(x_j) \rangle|^2\)

The quantum state \(|\phi(x)\rangle\) is prepared by the given encoder circuit.

This implements the quantum kernel approach from:

  • Havlíček et al. (2019), “Supervised learning with quantum-enhanced feature spaces” (Nature 567, 209-212).

  • Schuld & Killoran (2019), “Quantum machine learning in feature Hilbert spaces” (PRL 122, 040504).

  • Schuld, Bocharov, Svore & Wiebe (2018), “Circuit-centric quantum classifiers” (arXiv:1804.00633).

Mathematically, the quantum kernel is defined as:

\[K(x_i, x_j) = |\langle \phi(x_i) | \phi(x_j) \rangle|^2\]

where \(|\phi(x)\rangle = U_{\text{enc}}(x)|0^{\otimes n}\rangle\).

Parameters:
  • encoder (BaseEncoder, optional) – The quantum encoding circuit. If None, defaults to AngleEncoder().

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

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

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

encoder_#

Fitted encoder.

Type:

BaseEncoder

n_qubits_#

Number of qubits used.

Type:

int

Examples

>>> from psipose import FidelityQuantumKernel, AngleEncoder
>>> kernel = FidelityQuantumKernel(encoder=AngleEncoder(n_qubits=4))
>>> K = kernel.compute_matrix(X_train)
>>> # Use with sklearn SVC:
>>> from sklearn.svm import SVC
>>> svc = SVC(kernel="precomputed")
>>> svc.fit(K, y_train)
fit(X)[source]#

Fit the kernel by determining the encoder parameters.

Parameters:

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

Returns:

self

Return type:

FidelityQuantumKernel

compute_matrix(X, Y=None)[source]#

Compute the kernel matrix.

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

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

Returns:

K – Kernel matrix.

Return type:

ndarray, shape (n_samples_X, n_samples_Y)