Quantum Kernels, Noise, and Examples
Quantum kernels are useful for small-sample supervised learning, clustering, dimensionality reduction, and anomaly detection. Noise components evaluate the effects of channel noise and readout error on a model. This page also includes runnable example entry points.
Quantum kernels
import torch
from unitarylab.library.qml.kernels import OverlapKernel, kernel_matrix
x = torch.tensor([
[0.0, 0.2],
[0.3, -0.1],
[0.8, 0.4],
], dtype=torch.float64)
kernel = OverlapKernel(num_qubits=2)
gram = kernel_matrix(x, kernel=kernel)
print(gram)| Capability | Interfaces |
|---|---|
| Basic kernels | OverlapKernel, StatevectorFidelityKernel, ProjectedQuantumKernel |
| Supervised learning | QuantumKernelClassifier, QuantumKernelRegressor |
| Unsupervised learning | QuantumKernelKMeans, QuantumKernelPCA, QuantumKernelAnomalyDetector |
| Matrix utilities | kernel_matrix(), center_kernel(), closest_psd_matrix(), kernel_target_alignment() |
The training Gram matrix must be square. For prediction, the second dimension of the cross-kernel matrix must correspond to the training samples. Finite shots or numerical error may make the matrix not exactly symmetric positive semidefinite; symmetrize it and use closest_psd_matrix() when correction is needed.
Noise and readout error
from unitarylab.library.qml.noise import (
DepolarizingChannel,
NoiseModel,
ReadoutErrorModel,
mitigate_readout_probabilities,
)
noise = NoiseModel()
noise.add(DepolarizingChannel(probability=0.01), wires=(0, 1))Built-in channels include BitFlipChannel, PhaseFlipChannel, AmplitudeDampingChannel, and DepolarizingChannel. NoiseModel associates channels with circuit locations or gate conditions. ReadoutErrorModel describes measurement confusion; mitigate_readout_probabilities() corrects probabilities using a calibration model.
Probability parameters must be within valid ranges. Error mitigation may produce small negative components or amplify statistical noise; check normalization and physical bounds. Noise models do not replace calibration on a real device. Report a noise-free baseline, shots, and random seed when comparing models.
PyTorch integration example
import torch
from unitarylab.library.qml.examples.classification import build_classifier
module = build_classifier("reuploading", seed=0)
x = torch.zeros((2, 4), dtype=torch.float64)
logits = module(x)
print(logits.shape)Example subpackages provide these entry points:
| Scenario | Module |
|---|---|
| Classification | qml.examples.classification |
| Regression | qml.examples.regression |
| Generative modeling | qml.examples.generative |
| QGAN | qml.examples.qgan |
| Continuous-variable QNN | qml.examples.cvqnn |
| Anomaly detection | qml.examples.anomaly_detection |
| Small datasets | qml.examples.datasets |
Examples help validate installation and workflows; they do not define optimal hyperparameters for a particular dataset. For production training, create a separate validation set, record experiment settings, and increase data and circuit sizes gradually.
Common errors
- Feature count does not match the encoding layer: scale, select, or pad features first.
- PyTorch input and model parameter dtype/device differ: convert them consistently before training.
- Classification labels are not integer class indices, or the class count does not match the output dimension.
- Too few shots cause large fluctuations in kernel matrices and gradients.
- Selecting kernels, noise strengths, or model hyperparameters on the test set causes data leakage.