Curriculum / Real-World Quantum Python / Quantum Machine Learning Kernels
Quantum Machine Learning Kernels
Implement a quantum kernel classifier using quantum feature maps and the kernel trick.
Quantum Kernel Methods
Quantum kernel methods encode classical data into quantum states and use quantum inner products as a kernel function for machine learning classifiers. They are one of the strongest candidates for near-term quantum advantage in ML.
Classical Kernel Methods
A kernel function K(x, y) measures similarity between two data points. Support Vector Machines (SVMs) find a maximum-margin hyperplane in the kernel-induced feature space. The kernel trick allows this without ever computing the high-dimensional feature vectors explicitly.
Classical kernels: RBF (Gaussian), polynomial, sigmoid. These work well for many problems.
Why quantum? A quantum feature map encodes x into a dimensional Hilbert space: exponentially larger than any practical classical feature space. If this encoding captures structure that classical kernels miss, quantum SVMs could outperform classical ones.
Quantum Feature Maps
A quantum feature map φ(x) encodes classical data x into a quantum state: |φ(x)⟩ = U_φ(x)|0⟩
The ZZ feature map (Havlicek et al. 2019) for 2D data (x₁, x₂):
- 1.Apply H⊗H to create superposition
- 2.Apply RZ(x₁)⊗RZ(x₂) for linear features
- 3.Apply CX → RZ(x₁·x₂, qubit 1) → CX for the interaction term
- 4.(Optional) Repeat for more layers
The interaction term x₁·x₂ captures correlations between data dimensions that no classical linear kernel can represent.
Quantum Kernel Matrix
The quantum kernel between two data points is: K(x, y) = |⟨φ(x)|φ(y)⟩|²
On hardware, this is estimated by running two circuits, one to prepare |φ(x)⟩ and one with the inverse of |φ(y)⟩, and measuring P(|0⟩). Shot counts: typically 1000–8192 per kernel entry.
For a dataset of n samples: n² kernel evaluations → n² × 1000 = millions of shots for modest datasets. This is the main practical bottleneck.
From Kernel to Classifier
Once the n×n kernel matrix K is computed, feed it to a classical SVM. The SVM finds support vectors αᵢ ≥ 0 and a bias b such that the decision boundary is:
This is the opening of the lesson. The full walkthrough, the interactive circuit, and the graded challenge continue inside myqubit.
How this lesson works
A hands-on coding challenge. You write Qiskit-compatible Python in the browser editor, run it instantly via WebAssembly, watch the circuit and Bloch sphere react, and pass automatic output checks. The AI tutor Qubitus gives Socratic hints if you get stuck.
Part of: Real-World Quantum Python
Write production-quality quantum Python, circuit optimization, hybrid algorithms, cloud backends, noise modeling, and software engineering patterns.
This lesson is part of Pro
Unlock Real-World Quantum Python and all 10 tracks with Pro: $12.99/month, $79/year, or $97 lifetime. Start with the free track first if you are new.