|q⟩ Bad Qubits

intermediate · Programming · Quantum Machine Learning (Intro)

Checkpoint: A Tiny Quantum Classifier

This checkpoint pulls together every idea from Module 12: angle encoding, entangling ansatz layers, trained weights, and measurement. You will assemble a tiny but complete quantum classifier and verify that it correctly predicts class 1 for a given input.

The classification task

We have a one-dimensional dataset with two classes:

The classifier is a two-qubit circuit U(x,θ)U(x, \boldsymbol{\theta}) where xx is the data feature and θ=(θ0,θ1)\boldsymbol{\theta} = (\theta_0, \theta_1) are the trained weights. Measurement of the full two-qubit register at the end produces a probability distribution; a class-1 prediction corresponds to a high probability of measuring 1|1\rangle on qubit 1 (the label qubit).

Circuit structure

The circuit has three layers:

U(x,θ0,θ1)=(Ry(θ1)1Ry(θ0)0)CNOT01Ry(x)0.U(x, \theta_0, \theta_1) = \bigl(R_y(\theta_1)_1 \otimes R_y(\theta_0)_0\bigr) \cdot \mathrm{CNOT}_{01} \cdot R_y(x)_0.

Layer 1 — data encoding. Apply Ry(x)R_y(x) to qubit 0. Starting from 00|00\rangle, the state after encoding is

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

For x=3π/4x = 3\pi/4 the amplitude on 1|1\rangle for qubit 0 is sin(3π/8)0.924\sin(3\pi/8) \approx 0.924, so the data qubit already leans strongly toward 1|1\rangle.

Layer 2 — entanglement. A CNOT with control on qubit 0 and target on qubit 1 creates correlations between the data qubit and the label qubit:

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

Layer 3 — trainable weights. Independent RyR_y rotations on both qubits let the optimizer tune which region of the entangled state maps to class 0 or class 1.

Deriving the trained weights

A classical optimizer minimizes a cost function by evaluating the circuit on training data. For this toy task, gradient descent (using the parameter-shift rule) converges to the weights θ0=π/3\theta_0^* = \pi/3 and θ1=π/6\theta_1^* = \pi/6.

We can verify analytically that these weights work for x=3π/4x = 3\pi/4. After the encoding and CNOT the state is

cos(3π/8)00+sin(3π/8)11.\cos(3\pi/8)|00\rangle + \sin(3\pi/8)|11\rangle.

Applying Ry(π/3)R_y(\pi/3) to qubit 0 and Ry(π/6)R_y(\pi/6) to qubit 1 rotates the amplitudes within each two-qubit basis state. The marginal probability of measuring qubit 1 in 1|1\rangle works out to approximately 0.810.81, well above the 50% decision threshold. For the class-0 input x=π/4x = \pi/4 the same weights produce a probability of roughly 0.190.19 on 1|1\rangle for qubit 1 — correctly predicted as class 0.

Try it

Build the complete classifier circuit for the class-1 input x=3π/4x = 3\pi/4, using the trained weights θ0=π/3\theta_0^* = \pi/3 and θ1=π/6\theta_1^* = \pi/6. Then measure. The grader checks that the output distribution matches the reference.

Run your code to see the quantum state.

After a correct run, the 11|11\rangle outcome should dominate the histogram — qubit 1 in 1|1\rangle signals the class-1 prediction. Compare what happens when you change xx to π/4\pi/4 (a class-0 input): the distribution shifts dramatically toward 00|00\rangle.

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