|q⟩ Bad Qubits

advanced · Programming · Capstone Projects

Project: A Quantum Classifier

Quantum machine learning models are parameterized circuits trained like neural networks: a feature is encoded into a quantum state, a trainable unitary transforms it, and a measurement reads off the prediction. This capstone builds the smallest non-trivial example — a single-qubit data-reuploading classifier — and connects training, inference, and benchmarking.

Encoding and the model

A scalar feature xx is loaded by a rotation, ϕ(x)=Ry(x)0|\phi(x)\rangle = R_y(x)|0\rangle. A trained weight ww is applied as a second rotation, and the label is read from a ZZ-measurement. Because both rotations share the YY axis,

ψ(x,w)=Ry(w)Ry(x)0=Ry(x+w)0=cos ⁣(x+w2)0+sin ⁣(x+w2)1.|\psi(x, w)\rangle = R_y(w)\,R_y(x)\,|0\rangle = R_y(x + w)\,|0\rangle = \cos\!\Big(\tfrac{x+w}{2}\Big)|0\rangle + \sin\!\Big(\tfrac{x+w}{2}\Big)|1\rangle.

The decision rule is a threshold on the measured probability:

y^(x)={0P(0)>P(1),1otherwise.\hat{y}(x) = \begin{cases} 0 & P(|0\rangle) > P(|1\rangle), \\ 1 & \text{otherwise.}\end{cases}

Training

Training minimizes a loss — e.g. the squared error between P(1)P(|1\rangle) and the true label — averaged over the dataset, by adjusting ww with a classical optimizer using parameter-shift gradients. For a threshold dataset where points with x>0x > 0 belong to class 11, the optimizer converges to w=π/2w = \pi/2, which rotates positive-xx points past the equator of the Bloch sphere toward 1|1\rangle.

Inference and benchmarking

At the converged weight, classify the test point x=π/2x = \pi/2:

Ry(π/2+π/2)0=Ry(π)0=1,R_y(\pi/2 + \pi/2)\,|0\rangle = R_y(\pi)\,|0\rangle = |1\rangle,

a confident class-11 prediction with P(1)=1P(|1\rangle) = 1. Benchmarking the model means sweeping the test set, recording y^\hat{y} for each point, and reporting accuracy — the same protocol used for any classifier, quantum or classical.

Try it

Build the classifier for the test point x=π/2x = \pi/2 at the trained weight w=π/2w = \pi/2 and measure. The grader checks the output distribution; all probability should land on outcome 11.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.