|q⟩ Bad Qubits

intermediate · Programming · Quantum Machine Learning (Intro)

Training a Quantum Classifier

Previous lessons introduced parameterized quantum circuits and feature maps. Now we ask: how do we actually train such a circuit to separate classes?

The classification task

Suppose we have a dataset of labeled points, each a real number x[0,π]x \in [0, \pi]. Points with xπ/2x \leq \pi/2 belong to class 0; points with x>π/2x > \pi/2 belong to class 1. We want a quantum circuit U(x,θ)U(x, \boldsymbol{\theta}) — parameterized by both the data xx and trainable weights θ\boldsymbol{\theta} — whose measurement on a designated label qubit tells us the class.

A natural circuit structure for one data point xx on two qubits is:

U(x,θw)=Ry(θw)1CNOT01Ry(x)0.U(x, \theta_w) = R_y(\theta_w)_1 \cdot \mathrm{CNOT}_{01} \cdot R_y(x)_0.

Qubit 0 is the data qubit (encoding xx) and qubit 1 is the label qubit (read out after the circuit). The trainable parameter θw\theta_w sits on the label qubit after the entangling gate.

How training works

Starting from 00|00\rangle, the circuit produces the state

ψ(x,θw)=(Ry(θw)1CNOT01)(Ry(x)0I1)00.|\psi(x, \theta_w)\rangle = \bigl(R_y(\theta_w)_1 \cdot \mathrm{CNOT}_{01}\bigr) \bigl(R_y(x)_0 \otimes I_1\bigr)|00\rangle.

After the Ry(x)R_y(x) on qubit 0:

(cosx20+sinx21)0.\bigl(\cos\tfrac{x}{2}|0\rangle + \sin\tfrac{x}{2}|1\rangle\bigr) \otimes |0\rangle.

The CNOT flips qubit 1 only when qubit 0 is 1|1\rangle, entangling the two qubits:

cosx200+sinx211.\cos\tfrac{x}{2}|00\rangle + \sin\tfrac{x}{2}|11\rangle.

Finally, Ry(θw)R_y(\theta_w) on qubit 1 rotates the 1|1\rangle component of each term. The probability of measuring qubit 1 in 1|1\rangle — the "class 1" output — becomes a function of both xx and θw\theta_w.

Training means choosing θw\theta_w so that this probability is small for class-0 points and large for class-1 points. A classical optimizer (gradient descent, COBYLA, etc.) adjusts θw\theta_w by evaluating the circuit repeatedly and minimizing a cost function that penalizes misclassified points. Gradients can be computed exactly with the parameter-shift rule (covered in the next lessons).

The trained classifier

For our toy dataset, suppose the optimizer has already run and found the optimal weight θw=π/4\theta_w = \pi/4. At this setting the probability of measuring 1|1\rangle on the label qubit grows monotonically with xx over [0,π][0, \pi], correctly ranking class-0 inputs below the classification threshold and class-1 inputs above it.

We can verify this concretely for the boundary point x=π/2x = \pi/2:

After the encoding step: cos(π/4)00+sin(π/4)11=12(00+11)\cos(\pi/4)|00\rangle + \sin(\pi/4)|11\rangle = \tfrac{1}{\sqrt{2}}(|00\rangle + |11\rangle).

After Ry(π/4)R_y(\pi/4) on qubit 1, the amplitude on 11|11\rangle picks up a rotation by π/8\pi/8. Using the general result P(qubit 1=1)=12(1cosxcosθw)P(\text{qubit }1 = |1\rangle) = \tfrac{1}{2}\bigl(1 - \cos x\cdot\cos\theta_w\bigr), substituting x=π/2x = \pi/2 and θw=π/4\theta_w = \pi/4 gives 12(1cos(π/2)cos(π/4))=12\tfrac{1}{2}\bigl(1 - \cos(\pi/2)\cdot\cos(\pi/4)\bigr) = \tfrac{1}{2} — exactly at the 50% threshold, as expected for the class boundary: inputs with x<π/2x < \pi/2 produce P<12P < \tfrac{1}{2} (class 0) and inputs with x>π/2x > \pi/2 produce P>12P > \tfrac{1}{2} (class 1).

Try it

Implement the trained circuit: encode the data point x=π/2x = \pi/2 on qubit 0 with Ry(π/2)R_y(\pi/2), entangle the two qubits with a CNOT, then apply the trained weight Ry(π/4)R_y(\pi/4) on the label qubit. The grader checks the exact statevector produced by your circuit.

Run your code to see the quantum state.

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