You are reading without signing in. Sign in or continue as guest to take quizzes and climb the ranks.
Hands-On 2-Qubit Variational Classifier
Encode a point with two RX gates, entangle, rotate with one trainable RY, and tune θ until the two classes separate.
- 01Build a 2-qubit classifier: angle encoding, a CNOT, one trainable RY and a readout qubit.
- 02Predict the class probability from P(class 1) = (1 − cos θ · cos x₀ · cos x₁)/2.
- 03Tune θ by hand and judge it by the error on a small dataset.
The dataset has four points (x₀, x₁), in radians. A = (0.3, 0.2) and B = (2.8, 2.9) belong to class 0; C = (0.3, 2.9) and D = (2.8, 0.2) belong to class 1. Points whose two features are both small or both large are class 0, mixed ones are class 1. No straight line separates the classes, the same pattern as the XOR function, which makes it a classic small test.
01The circuit
- rx(x₀) on q[0] and rx(x₁) on q[1]: angle encoding, one feature per qubit.
- cx q[0], q[1]: q[1] now carries information about both features together.
- ry(θ) on q[1]: the one trainable weight.
- measure q[1]: outcome 0 means class 0, outcome 1 means class 1.
| θ | A (class 0) | B (class 0) | C (class 1) | D (class 1) |
|---|---|---|---|---|
| 0 | 0.03 | 0.04 | 0.96 | 0.96 |
| π/4 | 0.17 | 0.18 | 0.83 | 0.83 |
| π/2 | 0.50 | 0.50 | 0.50 | 0.50 |
| π | 0.97 | 0.96 | 0.04 | 0.04 |
The table lists P(class 1). At θ = 0 all four points are classified correctly and the average chance of a wrong label is about 4%. At θ = π/2 the information is erased and every point is a coin flip; at θ = π every label is backwards. Training means letting an optimiser find θ = 0 (or 2π, the same rotation) from wherever it starts, by lowering the loss.
Tune θ for point C
This is point C (class 1) with θ = π/2: the histogram is 50/50. Change ry(pi/2) to ry(0) and outcome 1 rises to about 96%; change it to ry(pi) and it falls to about 4%. Then set the rx angles to point A, 0.3 and 0.2, and check that θ = 0 now reads 0 in about 97% of the shots.
OPENQASM 3.0;
include "stdgates.inc";
qubit[2] q;
bit[1] c;
rx(0.3) q[0];
rx(2.9) q[1];
cx q[0], q[1];
ry(pi/2) q[1];
c[0] = measure q[1];
2 questions · one try each counts
With θ = π/2, what is P(class 1) for any input?
Sign in to answer and climb the ranks; you will come back to this lesson.
Training starts at θ = 3.0 and the loss is high. Where should the optimiser take θ?
Sign in to answer and climb the ranks; you will come back to this lesson.