Chapter 9: Variational Quantum Classifiers¶
Kernels use the quantum computer as a passive measuring instrument. The other main approach puts trainable dials inside the circuit itself and turns them with a classical optimiser. These are variational quantum classifiers (VQCs), and they are what most people picture when they hear the words quantum machine learning. This chapter describes their architecture: the three stages of encode, transform and measure; the choice of the trainable circuit, the ansatz; how a measured expectation becomes a class label; and the loss functions that are minimised, together with the way finite shots make every one of them noisy. It also shows, with a short calculation, that a VQC searches a subset of the hypotheses of the corresponding kernel machine. Chapter 10 then shows how such a model is differentiated and trained.
9.1 Three stages¶
A variational classifier has three stages.
- Encode. \(U_\varphi(x)\) writes the datum into the state, exactly as for a kernel.
- Transform. \(U(\theta)\) with \(\theta\in\mathbb{R}^p\) is the learned part.
- Measure. Read out an observable, for example \(Z_1\) (the Pauli-\(Z\) of the first qubit) or the parity \(\bigotimes_jZ_j\).
The state is \(\lvert\psi(x,\theta)\rangle=U(\theta)\,U_\varphi(x)\lvert0\cdots0\rangle\) and the prediction is an expectation value estimated from shots, \(\hat y=\langle Z\rangle_{x,\theta}\).
|0...0> ──[ U_φ(x) : encode ]──[ U(θ) : trainable ]──[ measure Z ]── ⟨Z⟩_{x,θ}
This is the hybrid loop of Chapter 1: a classical optimiser updates \(\theta\) from the measured loss, while the device evaluates the model and its gradients.
A VQC searches a subset of the kernel hypotheses¶
A subtle point is often missed. Fix the encoding, and write the model output as
For a fixed encoding this is a linear functional of the feature vector \(\rho(x)\), in the same feature space as the fidelity kernel of Chapter 6. The weight vector \(O_\theta\) is not arbitrary, however: it must be a rotated copy of the fixed observable \(O\). So the variational model explores only a restricted family of the linear models that the corresponding kernel machine could use.
The ansatz restricts, it does not extend
A VQC with fixed encoding and a linear readout is a linear model in the same feature space as the corresponding quantum kernel. The ansatz restricts the accessible hypotheses rather than enlarging them, so a VQC cannot exceed the expressivity of the optimal kernel machine on the same encoding. Its advantage, if any, lies in sample and shot efficiency through implicit regularisation. Data re-uploading (Chapter 5) is the way to escape this restriction, since it changes the feature map itself.
9.2 Ansatz design¶
The standard template repeats a layer that applies two single-qubit rotations to every qubit, followed by a fixed block of entangling gates:
where \(\mathcal{E}\) is a fixed entangling block, for example a ladder of CNOTs between neighbouring qubits. The parameter count is \(2nL\): it grows linearly in both width and depth.
Hardware-efficient means choosing gates that the machine executes natively and respecting which qubits are physically connected, so the compiled depth stays small. Two standard diagnostics describe an ansatz.
- Expressibility: how uniformly the ansatz covers the state space, measured as the divergence of its fidelity distribution from the one of random (Haar) states.
- Entangling capability: the mean Meyer-Wallach entanglement of the states it generates.
Both diagnostics are due to Sim, Johnson and Aspuru-Guzik. Here is the trade-off: greater depth raises expressibility and entangling capability, but also noise accumulation and the risk of barren plateaus (Chapter 12).
Maximal expressibility is the wrong target
A maximally expressive ansatz forms an approximate unitary 2-design, and that guarantees flat cost landscapes. Better choices are symmetry-respecting (equivariant) or problem-inspired ansätze restricted to the relevant subspace. Equivariant ansätze that build in the symmetries of the data are among the most promising current directions.
9.3 Output and decision rule¶
The model output is the real number
where \(w,b\in\mathbb{R}\) are classical scale and bias parameters, trained alongside \(\theta\) at no quantum cost: they need no extra circuits. For a projective \(Z\) measurement on a single qubit, the map from expectation to probability is exact,
which maps \([-1,1]\) onto \([0,1]\) and gives scores suitable for cross-entropy training.
- Parity readout \(\langle Z_1\cdots Z_n\rangle\) uses all the qubits, but it is a global observable, which matters for trainability (Chapter 12).
- Multi-class: use one observable per class, or a softmax over several expectation values.
- Noise: hardware noise contracts \(\langle Z\rangle\) towards \(0\), and the trainable scale \(w\) absorbs much of this, which is why it is almost always included.
9.4 Loss functions and estimator noise¶
Three standard choices for what to minimise, with \(\hat y_i=f(x_i;\theta)\) the model output:
In the cross-entropy, \(\tilde y_i=(y_i+1)/2\in\{0,1\}\) and \(p_i=\tfrac12(1+\langle Z\rangle_{x_i})\). In the hinge loss, \(\lambda\) sets the regularisation strength, limiting effective capacity.
Example 9.1: three losses for the same prediction
A one-qubit classifier outputs \(\langle Z\rangle=0.6\) for a point with label \(y=+1\), and \(\langle Z\rangle=-0.5\) for another point with \(y=+1\). Compute the three losses (without the regulariser) for each.
First point (\(\hat y=0.6\), \(\tilde y=1\), \(p=\tfrac12(1+0.6)=0.8\)): squared \((0.6-1)^2=0.16\); cross-entropy \(-\ln0.8=0.2231\); hinge \(\max(0,1-0.6)=0.4\).
Second point (\(\hat y=-0.5\), \(p=\tfrac12(1-0.5)=0.25\)): squared \((-0.5-1)^2=2.25\); cross-entropy \(-\ln0.25=1.3863\); hinge \(\max(0,1+0.5)=1.5\).
All three losses increase sharply when the prediction is on the wrong side of zero. Cross-entropy grows without bound as \(p\to0\), which is why it is "sharper" than the squared loss near the extremes, and also why it amplifies estimator noise there.
Every term is an estimate. The expectation \(\langle Z\rangle\) comes from \(M\) shots, so
and the loss surface is stochastic even for fixed data. This distinguishes QML optimisation from classical deep learning: the noise is intrinsic to measurement, not to minibatching, and it does not vanish with more data. Consequences:
- Accuracy costs shots. Accuracy \(\varepsilon\) in the loss needs \(M=O(1/\varepsilon^2)\) shots per data point, per evaluation.
- Squared loss is smooth and shot-tolerant; cross-entropy is sharper near \(p\to0,1\) but amplifies estimator noise there. The standard fix is to clip the probabilities away from \(0\) and \(1\).
- Stochastic optimisers (Adam, SPSA) are used rather than exact line searches.
- Shot noise interacts badly with flat landscapes (Chapter 12).
9.5 The smallest variational classifier¶
The simplest complete model has one qubit and one parameter. The input is encoded by \(R_Y(x)\), then a trainable rotation \(R_Y(\theta)\) is applied, and \(Z\) is measured. Rotations about the same axis add, so
The decision rule \(\operatorname{sign}f\) has its boundary where \(\cos(x+\theta)=0\), that is \(x=\tfrac\pi2-\theta\) (and its \(2\pi\)-periodic repeats). The trainable parameter is a shift of the threshold. This is the model that Chapter 10 trains by gradient descent, and it is also the model whose gradients can be computed in closed form to check the parameter-shift rule.
9.6 Summary¶
Chapter Summary
- A VQC has three stages: encode \(U_\varphi(x)\), transform \(U(\theta)\), measure an observable; the prediction is \(\hat y=\langle Z\rangle_{x,\theta}\), estimated from shots.
- For a fixed encoding, \(f=\langle\rho(x),O_\theta\rangle_{HS}\) is a linear model in the quantum-kernel feature space with \(O_\theta\) restricted to rotations of \(O\): the ansatz restricts rather than extends. Re-uploading changes the feature map.
- The hardware-efficient ansatz has \(2nL\) parameters; expressibility and entangling capability grow with depth, but so do noise and plateau risk. Prefer symmetry-respecting or problem-inspired ansätze.
- The label is \(\operatorname{sign}(w\langle Z\rangle+b)\) with free classical \(w,b\); \(p(+1\mid x)=\tfrac12(1+\langle Z\rangle)\). Parity readout is global.
- Losses: squared, cross-entropy, hinge plus \(\lambda\lVert\theta\rVert^2\). Every term is estimated from \(M\) shots with \(\operatorname{Var}\hat y=(1-\langle Z\rangle^2)/M\); accuracy \(\varepsilon\) needs \(M=O(1/\varepsilon^2)\) per point and per evaluation.
- The one-qubit model \(R_Y(x)\) then \(R_Y(\theta)\) gives \(f=\cos(x+\theta)\).
9.7 Exercises¶
Exercise 9.1
How many trainable parameters does a hardware-efficient ansatz with \(R_Y\) and \(R_Z\) on each qubit have for \(n=5\) qubits and \(L=2\) layers?
Answer
\(2nL=2\cdot5\cdot2=20\).
Exercise 9.2
A model outputs \(\langle Z\rangle=0.2\) for a point with label \(y=-1\). Compute the squared, cross-entropy and hinge losses.
Answer
Squared: \((0.2-(-1))^2=1.44\). Cross-entropy: \(\tilde y=0\), \(p=\tfrac12(1+0.2)=0.6\), so \(-\ln(1-p)=-\ln0.4=0.9163\). Hinge: \(\max(0,1-(-1)(0.2))=1.2\).
Exercise 9.3
The prediction \(\hat y\) is estimated from \(M=100\) shots and the true value is \(\langle Z\rangle=0.8\). What is the standard error?
Answer
\(\sqrt{(1-0.64)/100}=\sqrt{0.0036}=0.06\).
Exercise 9.4
Noise multiplies every \(\langle Z\rangle\) by \(0.8\). Explain how a trainable scale \(w\) compensates.
Answer
The measured output is \(0.8\langle Z\rangle_{\text{ideal}}\). Choosing \(w=1/0.8=1.25\) restores the ideal value in \(w\langle Z\rangle\). Since \(w\) is a classical parameter it costs no extra circuits, which is why it is almost always included. (This compensates a uniform contraction only; it does not recover information lost by noise that depends on the state.)
Exercise 9.5
For the model \(f(x;\theta)=\cos(x+\theta)\), where is the decision boundary with \(\theta=0.5\) in the interval \(0\le x\le\pi\)?
Answer
\(\cos(x+0.5)=0\) at \(x+0.5=\pi/2\), so \(x=\pi/2-0.5\approx1.071\). For \(x<1.071\) the output is positive (class \(+1\)), for larger \(x\) (up to the next zero at \(x+0.5=3\pi/2\)) negative.
Exercise 9.6
Explain in two sentences why a variational classifier with fixed encoding and linear readout cannot be more expressive than the corresponding quantum-kernel SVM.
Answer
Both are linear models in the same feature space, \(\rho(x)\). The kernel machine optimises over all linear functionals (through the dual), whereas the VQC's functional \(O_\theta\) is restricted to rotations of one fixed observable, so its hypotheses are a subset of the kernel machine's.
Exercise 9.7
Why is cross-entropy training more sensitive to shot noise than squared-loss training?
Answer
Near \(p\to0\) or \(p\to1\) the logarithm changes very rapidly, so a small fluctuation of the estimated \(\langle Z\rangle\) produces a large fluctuation of the loss. The squared loss is smooth and bounded on \([-1,1]\). Clipping probabilities away from \(0\) and \(1\) is the standard remedy for cross-entropy.
Exercise 9.8
Name one advantage and one disadvantage of a parity readout \(\langle Z_1\cdots Z_n\rangle\) compared with \(\langle Z_1\rangle\).
Answer
Advantage: it depends on all \(n\) qubits, so the output carries information from the whole register. Disadvantage: it is a global observable, which brings barren plateaus even at constant depth (Chapter 12), whereas a local readout keeps gradients polynomially large for shallow circuits.