Lesson 28: The Variational Quantum Classifier (VQC)¶
28.1 Learning Outcomes¶
By the end of this hour a student will be able to:
- State the variational principle and explain why it underpins all hybrid algorithms.
- Define the VQC model, its ansatz, its measurement and its loss function.
- Design an ansatz and count its parameters and depth.
- Choose a classical optimiser appropriate to shot noise.
- Compare the VQC with the QSVM and with a classical neural network.
28.2 The Variational Principle¶
Theorem. For any Hamiltonian \(H\) with ground-state energy \(E_0\) and any normalised state \(|\psi\rangle\),
with equality iff \(|\psi\rangle\) is a ground state.
Proof. Expand \(|\psi\rangle = \sum_j c_j|E_j\rangle\) in the eigenbasis of \(H\):
Consequence. Minimising \(\langle\psi(\vec\theta)|H|\psi(\vec\theta)\rangle\) over a parameterised family gives an upper bound on \(E_0\) that improves monotonically. This is the Variational Quantum Eigensolver (VQE) and the template for QAOA (L34) and the VQC.
Why it suits NISQ hardware.
| Property | Benefit |
|---|---|
| Shallow circuits | fits within coherence time |
| Classical optimiser in the loop | absorbs systematic errors |
| Variational bound | a noisy result is still an upper bound |
| No QPE, no long coherent evolution | orders of magnitude cheaper than Shor-style algorithms |
28.3 The VQC Model¶
Architecture¶
|0>^n --[ U_phi(x) ]--[ W(theta) ]--[ measure Z_0 ]--> <Z_0> --> class
\___________/ \__________/
encoding trainable
(fixed, L26) ansatz
with \(M\) typically \(Z_0\) (measure the first qubit) or \(Z^{\otimes n}\) (parity of all qubits).
From expectation value to a class label¶
For binary classification with \(\langle Z\rangle\in[-1,+1]\):
| Approach | Formula |
|---|---|
| Sign readout | \(\hat y = \mathrm{sign}\big(f_{\vec\theta}(\vec x) + b\big)\) |
| Probability readout | \(p(y=1) = \frac{1 + f_{\vec\theta}(\vec x)}{2}\) |
| Sigmoid readout | \(p(y=1) = \sigma\big(a f_{\vec\theta}(\vec x)+b\big)\) with trainable \(a,b\) |
For \(C\) classes, measure \(C\) different observables (or \(\lceil\log_2 C\rceil\) qubits' bitstrings) and apply a softmax.
Loss functions¶
Mean squared error (labels \(\pm1\)):
Binary cross-entropy (labels \(0/1\), probability readout):
Cross-entropy generally trains faster because its gradient does not vanish when predictions are confidently wrong.
28.4 Ansatz Design¶
The hardware-efficient ansatz¶
one layer, n qubits:
Ry(theta_1) Ry(theta_2) ... Ry(theta_n) <- rotations
Rz(theta_{n+1}) ... Rz(theta_{2n})
CNOT(0,1) CNOT(1,2) ... CNOT(n-2,n-1) CNOT(n-1,0) <- entangling ring
Parameter count for \(L\) layers with two rotations per qubit:
Depth: \(O(L\cdot n)\) if the CNOTs are sequential, \(O(L)\) if they are parallelised.
Pros: uses native gates and native connectivity, so it transpiles with little overhead. Cons: carries no problem structure and is highly prone to barren plateaus (L29).
Expressivity versus trainability¶
| Ansatz depth | Expressivity | Trainability |
|---|---|---|
| Too shallow | cannot represent the target function | easy to train, underfits |
| Just right | matches the problem | trainable |
| Too deep | approaches a random unitary (2-design) | barren plateau — untrainable |
The central tension of variational QML. Deeper circuits can represent more functions, but the gradient variance decays as \(O(2^{-n})\) once the ansatz approaches a unitary 2-design. More expressive does not mean better. This is the exact analogue of the kernel-concentration problem in L27, and the same \(2^{-n}\) factor is responsible.
Design guidelines¶
- Start shallow (2–4 layers) and add layers only if the model underfits.
- Prefer problem-inspired ansätze when domain structure is known (UCCSD for chemistry, QAOA for optimisation, QCNN for spatially local data).
- Use local cost functions (e.g. \(\langle Z_0\rangle\)) rather than global ones (\(\langle Z^{\otimes n}\rangle\)) — Cerezo et al. (2021) proved local costs have gradients that vanish only polynomially for shallow circuits, not exponentially.
- Initialise smartly: identity-block initialisation or layer-by-layer growth avoids starting in a plateau.
28.5 Classical Optimisers¶
| Optimiser | Circuit evaluations per step | Gradient | Noise robustness | Verdict |
|---|---|---|---|---|
| SPSA | 2 (independent of \(P\)) | stochastic approximation | excellent | best default on hardware |
| Adam + parameter shift | \(2P\) | exact analytic | good | best on simulators |
| COBYLA | ~1 | none (direct search) | fair | good for few parameters |
| Nelder–Mead | ~1 | none | poor | avoid for \(P > 10\) |
| L-BFGS-B | \(2P\) + line search | numerical | poor | fails with shot noise |
Why SPSA dominates on real hardware. It perturbs all parameters simultaneously with a random \(\pm1\) vector \(\vec\Delta\):
Two circuit evaluations give a noisy but unbiased descent direction, regardless of whether \(P\) is 10 or 1000. Compare Adam with parameter shift, which needs \(2P\) evaluations per step. For \(P=100\): 2 versus 200 circuit runs.
Never use gradient-free methods that assume a smooth deterministic objective (L-BFGS-B, Nelder–Mead) with finite shots — shot noise makes the objective non-deterministic, and these methods interpret the noise as curvature.
28.6 Solved Problems¶
Problem 1 — Counting parameters and depth¶
Design a VQC for a 4-feature binary classification problem with a 3-layer hardware-efficient ansatz. Count parameters, gates and depth.
Solution.
Qubits: \(n = 4\) (angle encoding, one feature per qubit).
Encoding layer: 4 \(R_y\) gates, depth 1, 0 trainable parameters.
Ansatz, per layer: 4 \(R_y\) + 4 \(R_z\) + 4 CNOTs (ring \(0{\to}1{\to}2{\to}3{\to}0\)).
Parameters:
Gate count:
Depth (rotations on distinct qubits run in parallel; the CNOT ring needs 2 time steps with nearest-neighbour coupling plus 1 for the wrap-around):
Feasibility. With gate error \(10^{-3}\) and 40 gates, circuit fidelity \(\approx(0.999)^{40} = 0.96\) — comfortably runnable on NISQ hardware.
Shot cost per training epoch. With 120 training samples and SPSA:
At 100 steps: \(2.5\times10^{7}\) shots ≈ 1.4 hours at 5 kHz.
Problem 2 — Forward pass by hand¶
A single-qubit VQC encodes \(x\) via \(R_y(x)\) and applies \(W(\theta) = R_y(\theta)\), measuring \(\langle Z\rangle\). Derive \(f_\theta(x)\) and find \(\theta\) that classifies \(x=0\) as \(+1\) and \(x = \pi\) as \(-1\).
Solution.
\(R_y\) rotations about the same axis compose additively:
Requirements.
- \(f_\theta(0) = \cos\theta = +1 \Rightarrow \theta = 0\)
- \(f_\theta(\pi) = \cos(\theta+\pi) = -\cos\theta = -1 \Rightarrow \cos\theta = 1 \Rightarrow \theta = 0\) ✓
What this model can and cannot do. \(f_\theta(x) = \cos(\theta+x)\) is a single Fourier mode — consistent with L26 Problem 4, since \(x\) is encoded exactly once. It can separate \(x\)-values that differ by roughly \(\pi\), but it cannot learn a function with two humps (e.g. classify \(x=0\) and \(x=\pi\) as \(+1\) but \(x=\pi/2\) as \(-1\)).
To fix it, re-upload the data: \(R_y(x)\,R_y(\theta_1)\,R_y(x)\dots\) — but note that consecutive \(R_y\)s just add, so the re-uploading must be interleaved with a different-axis rotation, e.g. \(R_y(x)R_z(\theta_1)R_y(x)\), to generate the second harmonic. This is why practical data-re-uploading circuits alternate rotation axes.
Problem 3 — Training curve and overfitting¶
A VQC with 24 parameters trains on 120 samples. Training accuracy reaches 98 % but test accuracy is 72 %. Diagnose and give three fixes.
Solution.
Diagnosis: overfitting. The 26-point gap between training and test accuracy is the signature.
Why it happens here. Effective capacity is set not only by \(P=24\) but by the feature-space dimension \(2^n = 16\) and by the number of accessible Fourier frequencies. With 120 samples and an expressive entangling ansatz, the model has enough freedom to memorise.
Three fixes.
1. Reduce ansatz depth. Drop from 3 layers to 2, giving \(P = 16\). Fewer parameters and fewer accessible frequencies ⟶ a smoother decision boundary. (Directly analogous to reducing the width of a neural network.)
2. Regularise. Add an \(L_2\) penalty to the loss:
Small \(\|\vec\theta\|\) keeps rotations near identity, biasing toward simpler functions. Typical \(\lambda\in[10^{-4},10^{-2}]\), chosen by cross-validation.
3. Use fewer data re-uploads. Each re-upload adds a Fourier frequency (L26). Reducing from 2 re-uploads to 1 halves the model's maximum frequency, forcing a smoother fit.
Also worth trying.
- More training data — the most reliable fix if available.
- Shot noise as regularisation. Reducing shots from 8192 to 1024 injects noise into gradients, which acts like dropout. This is a genuine and somewhat counter-intuitive benefit of NISQ hardware.
- Early stopping on a validation set.
Problem 4 — Optimiser comparison, quantified¶
A VQC has 40 parameters and 200 training samples, using 1024 shots per expectation value. Compare the circuit executions per optimisation step for SPSA, Adam+parameter-shift, and COBYLA. Which finishes 200 steps fastest at 5000 shots/second?
Solution.
SPSA. Two loss evaluations per step; each loss evaluation requires one circuit per training sample:
Adam + parameter shift. The parameter-shift rule (L29) needs 2 circuits per parameter per sample:
COBYLA. Approximately 1 loss evaluation per step:
Time for 200 steps at 5000 shots/s.
| Optimiser | Shots/step | Total shots | Time |
|---|---|---|---|
| SPSA | \(4.1\times10^{5}\) | \(8.2\times10^{7}\) | 4.6 hours |
| Adam + PS | \(1.64\times10^{7}\) | \(3.3\times10^{9}\) | 182 hours (7.6 days) |
| COBYLA | \(2.0\times10^{5}\) | \(4.1\times10^{7}\) | 2.3 hours |
Recommendation: SPSA. COBYLA is cheaper per step but converges poorly with 40 parameters and is badly affected by shot noise (it builds a linear model of a noisy objective). Adam with exact gradients converges in fewer steps, but each step is 40× more expensive — a net loss unless running on a fast simulator.
Practical hybrid strategy. Pre-train on a noiseless simulator with Adam + parameter shift (where circuits are cheap), then fine-tune on hardware with SPSA. This is standard practice.
Problem 5 — VQC versus QSVM versus classical: a decision framework¶
For each scenario, recommend VQC, QSVM or a classical model, with reasoning.
(a) 50 samples, 4 features, NISQ device available. (b) 10 000 samples, 20 features. (c) Data generated by a quantum experiment (measurement outcomes from a spin chain). (d) 500 samples, 8 features, need interpretable feature importances.
Solution.
(a) 50 samples, 4 features → QSVM. The kernel matrix is only \(\frac{50\times49}{2} = 1225\) entries — perfectly affordable. QSVM's optimisation is convex, so it needs no hyperparameter search over ansatz depth, no random initialisation, no barren-plateau risk. With so little data, a VQC would overfit and its non-convex training would be unreliable. (Also run a classical RBF SVM as the honest baseline.)
(b) 10 000 samples, 20 features → classical. QSVM needs \(5\times10^{7}\) kernel entries — utterly infeasible (\(O(M^2)\) wall). A VQC on 20 qubits with a deep ansatz would hit barren plateaus. And the shot cost of 10 000 forward passes per epoch is prohibitive. Gradient boosting or a neural network will win decisively and run in seconds.
(c) Quantum data → VQC (or a quantum autoencoder). This is the QQ quadrant — the one place a real advantage is expected. The data is already a quantum state, so there is no encoding bottleneck at all: feed the state directly into the ansatz. Classical methods would first need full tomography, which costs exponentially many measurements. This is the scenario where QML is genuinely the right tool.
(d) 500 samples, interpretability required → classical. Neither VQC nor QSVM offers interpretable feature attributions: the model output is \(\langle\psi|M|\psi\rangle\) after an entangling circuit, from which per-feature importance cannot be read off. Use logistic regression with coefficients, or a tree ensemble with SHAP values. (This is a serious, under-discussed limitation of QML for regulated domains such as credit scoring and medical diagnosis, where explainability is a legal requirement.)
The decision table.
| Situation | Choose |
|---|---|
| Small dataset, classical data, want reliability | QSVM |
| Medium dataset, want a trainable flexible model | VQC (shallow ansatz, local cost) |
| Large dataset | classical |
| Quantum-native data | VQC / quantum autoencoder — the real opportunity |
| Interpretability required | classical |
28.7 Practice Problems¶
- Count parameters for a 5-layer hardware-efficient ansatz on 6 qubits with \(R_y,R_z\) per qubit. [Ans: 60]
- Derive \(f_\theta(x)\) for encoding \(R_y(x)\), ansatz \(R_x(\theta)\), measuring \(\langle Z\rangle\).
- Why does SPSA need only 2 evaluations regardless of \(P\)?
- Compute the shots per Adam step for \(P=100\), \(M=50\), 2048 shots. [Ans: \(2.05\times10^{7}\)]
- Explain why a global cost \(\langle Z^{\otimes n}\rangle\) is worse than a local one \(\langle Z_0\rangle\).
- Give two reasons QSVM cannot suffer barren plateaus.
- A VQC gets 100 % training and 50 % test accuracy on a balanced binary task. What is happening?
28.8 Summary¶
- The variational principle guarantees \(\langle\psi(\vec\theta)|H|\psi(\vec\theta)\rangle \ge E_0\), making variational algorithms robust to noise and suitable for NISQ hardware.
- VQC: encode \(\vec x\), apply a trainable \(W(\vec\theta)\), measure an observable, map to a class label; train with a classical optimiser in a hybrid loop.
- Ansatz design is a trade-off: too shallow underfits, too deep hits barren plateaus. Prefer shallow, problem-inspired ansätze and local cost functions.
- SPSA is the practical optimiser on hardware (2 evaluations per step regardless of parameter count); parameter-shift + Adam is best on simulators; avoid L-BFGS/Nelder–Mead with shot noise.
- VQC vs QSVM: QSVM is convex and plateau-free but costs \(O(M^2)\); VQC scales better in \(M\) but is non-convex and can be untrainable.
- The honest use case for both is quantum-native data (QQ quadrant); on classical tabular data, classical models remain the baseline to beat and usually win.
Next lesson: L29 — Quantum neural networks, the parameter-shift rule and barren plateaus.
Practise now: unit3-qml-lab.html → VQC Trainer: choose a dataset, set ansatz depth and
optimiser, and watch the loss curve and decision boundary evolve in real time.