Lesson 26: Introduction to Quantum Machine Learning and Data Encoding¶
26.1 Learning Outcomes¶
By the end of this hour a student will be able to:
- Classify QML into the four quadrants (CC, CQ, QC, QQ) and place any proposal in one.
- Describe and compare basis, amplitude, angle and higher-order encodings.
- Compute the qubit cost and circuit depth of each encoding.
- Define a feature map and explain why encoding is the feature map.
- Define a parameterised quantum circuit (PQC) and the hybrid training loop.
26.2 What Is Quantum Machine Learning?¶
The four quadrants¶
Classify by the nature of the data and the nature of the processing device:
| C data (classical) | Q data (quantum) | |
|---|---|---|
| C algorithm (classical) | CC — ordinary ML | CQ... |
| Q algorithm (quantum) | QC — quantum algorithms on classical data | QQ — quantum algorithms on quantum data |
Standard naming (data first, device second):
| Quadrant | Meaning | Example |
|---|---|---|
| CC | classical data, classical computer | ordinary machine learning; also quantum-inspired classical algorithms |
| CQ | classical data, quantum computer | QSVM, VQC, QNN — most of this unit |
| QC | quantum data, classical computer | classical shadows, tomography, learning from measurement outcomes |
| quantum data, quantum computer | learning phases of matter, quantum sensing, quantum autoencoders — most likely to give real advantage |
The central strategic point. The CQ quadrant — running ML on classical data with a quantum computer — faces a severe bottleneck: getting classical data into the machine (L24's state preparation caveat, and Tang's dequantisation). The QQ quadrant, where data is already quantum, avoids the bottleneck entirely and is where most researchers now expect genuine advantage. Nonetheless, CQ is where the pedagogically important algorithms live, so it is where we spend Unit III.
Why classical data is a problem¶
To load \(N\) classical numbers into amplitudes you must specify \(N\) amplitudes. Generic state preparation costs \(O(N)\) gates — wiping out any \(O(\log N)\) algorithmic advantage. Efficient loading requires either structure in the data or a qRAM that does not exist.
Consequence. Most modern QML work has abandoned the "exponential speed-up on big data" framing and focuses instead on:
- expressivity — quantum models may represent function classes classically hard to represent;
- generalisation — possibly better sample complexity on structured problems;
- quantum data — the QQ quadrant.
26.3 Data Encoding (Quantum Feature Maps)¶
Definition (Encoding / feature map). An encoding is a map from a classical data point \(\vec x\in\mathbb{R}^{d}\) to a quantum state:
The encoding is not preprocessing — it is the model's feature map. The kernel it induces, \(k(\vec x,\vec x') = |\langle\phi(\vec x)|\phi(\vec x')\rangle|^2\), determines what the model can learn. Choosing the encoding is the most consequential design decision in QML, more so than the choice of ansatz.
Basis encoding¶
Encode a binary string directly:
| Property | Value |
|---|---|
| Qubits | \(n = d\) (one per bit) |
| Depth | \(O(1)\) — just \(X\) gates |
| Data type | binary only |
| Use | Grover-based search, oracles, arithmetic circuits |
Simple and exact, but wasteful: \(n\) qubits store only \(n\) bits, using none of the \(2^n\)-dimensional space.
Amplitude encoding¶
Encode a normalised vector into amplitudes:
| Property | Value |
|---|---|
| Qubits | \(n = \lceil\log_2 N\rceil\) — exponentially compact |
| Depth | \(O(N)\) in general — the bottleneck |
| Data type | real or complex, must be normalised |
| Use | HHL, quantum PCA, qk-means |
The trade. 1024 features fit in 10 qubits — wonderful. But preparing the state takes \(O(1024)\) gates — which is exactly the cost of just reading the data classically. Exponential space saving, no time saving.
Note also that normalisation destroys magnitude information; only the direction of \(\vec x\) survives.
Angle encoding (the practical workhorse)¶
Encode each feature as a rotation angle:
| Property | Value |
|---|---|
| Qubits | \(n = d\) |
| Depth | \(O(1)\) — one layer of rotations, all parallel |
| Data type | continuous real |
| Use | VQC, QNN, QSVM — nearly all NISQ QML |
Why it dominates NISQ work: constant depth, hardware-native gates, continuous data, and no normalisation constraint. Scale features to \([0,\pi]\) (or \([-\pi,\pi]\)) to use the full Bloch range without wrapping.
Higher-order / ZZ feature map¶
Add entangling gates to encode feature interactions:
with, in the standard Havlíček et al. (2019) choice,
The two-body terms create entanglement and encode products of features — the quantum analogue of polynomial kernel features.
| Property | Value |
|---|---|
| Qubits | \(n = d\) |
| Depth | \(O(d^2)\) per repetition |
| Use | QSVM with the ZZ feature map (the standard Qiskit demonstration) |
Conjectured advantage. The kernel induced by the ZZ map with \(L\ge2\) is believed hard to compute classically — this is the basis for claims of quantum kernel advantage. It is a conjecture, and Huang et al. (2021) showed that many such kernels are efficiently approximable classically. Present scientifically honest.
Comparison¶
| Encoding | Qubits for \(d\) features | Depth | Data | Verdict |
|---|---|---|---|---|
| Basis | \(d\) | \(O(1)\) | binary | oracle circuits |
| Amplitude | \(\log_2 d\) | \(O(d)\) | normalised real | compact but slow to load |
| Angle | \(d\) | \(O(1)\) | real | NISQ default |
| ZZ / higher-order | \(d\) | \(O(d^2)\) | real | expressive, entangling |
| Hamiltonian | problem-dependent | varies | quantum | QQ quadrant |
Repetition trick. Encoding the same feature several times (\(R_y(x)\), then later \(R_y(2x)\), …) generates higher Fourier frequencies. Schuld, Sweke and Meyer (2021) proved that a PQC computes a truncated Fourier series in the input, with the accessible frequency spectrum set by how often and with what scaling the data is re-uploaded. Data re-uploading is how you control model expressivity.
26.4 Parameterised Quantum Circuits¶
Definition (PQC / ansatz). A circuit \(U(\vec\theta)\) whose gates depend on trainable parameters \(\vec\theta\). The quantum model is
where \(M\) is a measured observable (usually \(Z_1\) or a sum of Paulis).
Reading the formula. Encode \(\vec x\); process with a trainable unitary; measure an observable; the expectation value is the model output. It is a neural network in which the "layers" are unitaries and the "activation" comes from measurement.
The hybrid training loop¶
repeat until converged:
QUANTUM for each batch item x:
prepare U_phi(x)|0>
apply U(theta)
measure <M> over many shots
CLASSICAL compute loss L(theta)
compute gradients (parameter-shift rule, L29)
update theta <- theta - eta * grad L
The quantum device is a subroutine inside a classical optimiser — Adam, SPSA or COBYLA running on a laptop. This division of labour is what makes variational algorithms runnable on NISQ hardware: each circuit is shallow, and the classical optimiser absorbs the noise.
Common ansätze¶
| Ansatz | Structure | Notes |
|---|---|---|
| Hardware-efficient | layers of \(R_y R_z\) + a ring of CNOTs | matches device topology; prone to barren plateaus |
| Strongly entangling | rotations + all-to-all CNOTs | expressive, deep |
| Problem-inspired (UCCSD) | derived from chemistry | physically meaningful, deep |
| QAOA ansatz | alternating cost and mixer Hamiltonians | for optimisation (L34) |
| Tree / QCNN | logarithmic depth, halving qubits | avoids barren plateaus (L30) |
26.5 Solved Problems¶
Problem 1 — Amplitude encoding a data vector¶
Encode \(\vec x = (1, 2, 3, 4)\) by amplitude encoding. Give the state, the number of qubits, and state what information is lost.
Solution.
Normalise.
Check: \(0.0333+0.1333+0.3+0.5333 = 1\) ✓.
Qubits: \(n = \log_2 4 = \mathbf{2}\).
Information lost: the magnitude \(\|\vec x\| = \sqrt{30}\). The vectors \((1,2,3,4)\) and \((2,4,6,8)\) produce identical states. If magnitude matters (as in regression on unnormalised targets), store \(\|\vec x\|\) classically alongside, or append a padding component.
Measurement probabilities: \(\Pr[i] = x_i^2/30\), i.e. \((0.033, 0.133, 0.300, 0.533)\) — note these are proportional to the squares, so measurement gives you \(x_i^2\), not \(x_i\) (sign information also requires extra work).
Problem 2 — Angle encoding and the induced kernel¶
Encode \(\vec x = (0.3, 1.2)\) with \(R_y\) angle encoding. Compute the state, then compute the kernel \(k(\vec x,\vec x')\) with \(\vec x' = (0.5, 1.0)\).
Solution.
Encoding \(\vec x\).
The kernel. For a product encoding the overlap factorises:
(using \(\cos\frac{a}{2}\cos\frac{b}{2} + \sin\frac{a}{2}\sin\frac{b}{2} = \cos\frac{a-b}{2}\)).
Interpretation. The two points are close, so the kernel is near 1 — this encoding induces a cosine-product kernel, which is smooth and similar to an RBF kernel. Crucially, this kernel is trivially computable classically, so plain product angle encoding offers no quantum advantage. Advantage requires entangling feature maps (§2.4).
Problem 3 — Resource comparison across encodings¶
A dataset has 1024 features. Compute qubits and encoding depth for basis, amplitude, angle and ZZ encoding. Which are feasible on a 100-qubit NISQ device?
Solution.
| Encoding | Qubits | Depth | Feasible on 100 qubits? |
|---|---|---|---|
| Basis | 1024 | \(O(1)\) | ✗ too many qubits |
| Amplitude | \(\log_2 1024 = \mathbf{10}\) | \(O(1024)\) ≈ 1000 gates | ✓ qubits, ✗ depth (too deep for NISQ) |
| Angle | 1024 | \(O(1)\) | ✗ too many qubits |
| ZZ | 1024 | \(O(1024^2) \approx 10^{6}\) | ✗ both |
None is directly feasible.
The practical answer: reduce dimensionality first. Apply classical PCA to compress 1024 features to, say, 8 principal components, then use angle encoding on 8 qubits with depth \(O(1)\) — comfortably runnable.
This is standard practice in every published NISQ QML experiment, and it is worth stating plainly to students: the classical preprocessing usually does most of the work. It also explains why "quantum advantage on real datasets" claims must be scrutinised — if PCA reduces the data to 8 dimensions, a classical model on those 8 dimensions is the honest baseline.
Problem 4 — Data re-uploading and Fourier frequencies¶
A single-qubit circuit applies \(R_y(\theta_1)\), then \(R_z(x)\), then \(R_y(\theta_2)\), then \(R_z(x)\), then \(R_y(\theta_3)\), and measures \(\langle Z\rangle\). What frequencies in \(x\) can the model express?
Solution.
The theorem (Schuld–Sweke–Meyer). A PQC in which the data-encoding gate \(R_z(x)\) (generator \(Z\), eigenvalues \(\pm\frac12\)) is repeated \(L\) times computes
where the frequency set \(\Omega\) consists of all sums and differences of the encoding generators' eigenvalue gaps.
Here \(L = 2\). Each \(R_z(x)\) contributes a gap of \(\pm1\), so the accessible frequencies are
— a truncated Fourier series of degree 2. The trainable parameters \(\vec\theta\) control the coefficients \(a_k\) and phases \(\varphi_k\), but not which frequencies exist.
Design consequences.
- Repeating the encoding \(L\) times gives frequencies up to \(L\). To fit a function with \(K\) frequency components you need \(L \ge K\) encoding repetitions.
- Scaling the input (\(R_z(\lambda x)\)) shifts the spectrum, letting you reach higher frequencies without more layers.
- This is the cleanest statement of what a PQC can and cannot represent — a genuinely useful design principle, and the quantum analogue of choosing the width of a neural network.
Corollary: a circuit that encodes \(x\) only once can only produce \(a_0 + a_1\cos(x+\varphi)\) — it cannot fit anything with a second harmonic, no matter how many trainable layers you add afterwards.
Problem 5 — Designing an end-to-end QML pipeline¶
Design a complete QML pipeline to classify the Iris dataset (150 samples, 4 features, 3 classes) on a NISQ device. Specify every choice and give the honest classical baseline.
Solution.
1. Preprocessing (classical).
- Standardise each feature to zero mean, unit variance.
- Rescale to \([0,\pi]\) so angles use the full Bloch range without wrapping: \(x'_j = \pi\,\frac{x_j - \min_j}{\max_j - \min_j}\).
- Split 80/20 train/test, stratified.
2. Encoding. Angle encoding with \(R_y\) on 4 qubits (one per feature), repeated twice (data re-uploading) so the model can express degree-2 Fourier components.
3. Ansatz. Hardware-efficient, 3 layers:
per layer: Ry(theta_i) and Rz(theta_i') on each of the 4 qubits
then CNOTs in a ring: 0->1, 1->2, 2->3, 3->0
Parameter count: \(3 \text{ layers} \times 4 \text{ qubits} \times 2 \text{ rotations} = \mathbf{24}\) parameters.
4. Measurement and readout. Measure \(\langle Z_0\rangle\), \(\langle Z_1\rangle\), \(\langle Z_2\rangle\); apply a classical softmax over these three values to get class probabilities. (3 classes ⟶ 3 outputs.)
5. Loss. Cross-entropy.
6. Optimiser. SPSA (Simultaneous Perturbation Stochastic Approximation) rather than Adam, because SPSA needs only 2 circuit evaluations per step regardless of parameter count and is robust to shot noise. Alternatively Adam with parameter-shift gradients (L29) if shot budget allows.
7. Shots. 1024 per expectation value. Per SPSA step: \(2 \times 3 \times \text{batch size}\) circuit executions.
8. Total circuit depth. \(2\) encoding layers + \(3\) ansatz layers \(\approx 20\) gates — comfortably within NISQ coherence.
The honest baseline. Logistic regression or an SVM on the same 4 features achieves 96–98 % accuracy on Iris in milliseconds. A well-tuned VQC typically reaches 90–96 % after minutes of quantum runtime.
State this to students explicitly. The purpose of this pipeline is to learn the QML workflow, not to beat scikit-learn. Iris is a pedagogical dataset; no NISQ QML experiment to date has demonstrated a practical advantage on a classical dataset of this kind. Understanding why — the data-loading bottleneck, barren plateaus (L29), and dequantisation (L24) — is more valuable than the accuracy number.
26.6 Practice Problems¶
- Amplitude-encode \((3,4)\). Give the state and the qubit count. [Ans: \(0.6|0\rangle+0.8|1\rangle\); 1 qubit]
- Angle-encode \(\vec x=(\pi/2, \pi)\) with \(R_y\). Give the state. [Ans: \(|+\rangle\otimes|1\rangle\)]
- Compute \(k(\vec x,\vec x')\) for \(\vec x = (0,0)\), \(\vec x' = (\pi,\pi)\) with product angle encoding. [Ans: 0]
- How many qubits to amplitude-encode a \(256\times256\) image? [Ans: 16]
- A PQC encodes \(x\) three times via \(R_z(x)\). What is the frequency set? [Ans: \(\{-3,\dots,3\}\)]
- Count the parameters in a 5-layer hardware-efficient ansatz on 6 qubits with \(R_y,R_z\) per qubit. [Ans: 60]
- Explain why product angle encoding cannot give quantum advantage.
26.7 Summary¶
- QML splits into CC / CQ / QC / QQ; the CQ quadrant (classical data on quantum hardware) suffers the data-loading bottleneck, while QQ is where genuine advantage is most expected.
- Encoding is the feature map and determines the induced kernel \(k(\vec x,\vec x') = |\langle\phi(\vec x)|\phi(\vec x')\rangle|^2\).
- Basis (\(d\) qubits, \(O(1)\) depth, binary), amplitude (\(\log d\) qubits, \(O(d)\) depth), angle (\(d\) qubits, \(O(1)\) depth — the NISQ default), ZZ/higher-order (entangling, conjectured hard to simulate).
- Amplitude encoding is exponentially compact in qubits but costs \(O(N)\) gates to prepare — space saving, not time saving. It also discards magnitude.
- Product angle encoding gives a classically computable cosine kernel — entanglement in the feature map is necessary (though not sufficient) for advantage.
- A PQC computes \(f_{\vec\theta}(\vec x) = \langle 0|U_\phi^\dagger U^\dagger(\vec\theta)MU(\vec\theta)U_\phi|0\rangle\); data re-uploading \(L\) times gives Fourier frequencies up to \(L\) — the cleanest handle on expressivity.
- Training is a hybrid loop: quantum circuits evaluate, a classical optimiser updates.
Next lesson: L27 — Quantum kernel methods and QSVM.
Practise now: unit3-qml-lab.html → Encoding Explorer: enter a data point, choose an encoding,
and see the resulting state, Bloch vectors and the induced kernel matrix on a small dataset.