QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
HomeLearnCatalogRanksPlaygroundDocs
05 · Hands-On 2-Qubit Variational Classifier0%

You are reading without signing in. Sign in or continue as guest to take quizzes and climb the ranks.

Learn/QC 302Cutting and Quantum ML·Lesson 5 of 7

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.

Advanced14 min read5 sections · 2 questions+45
By the end you will be able to
  1. 01Build a 2-qubit classifier: angle encoding, a CNOT, one trainable RY and a readout qubit.
  2. 02Predict the class probability from P(class 1) = (1 − cos θ · cos x₀ · cos x₁)/2.
  3. 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.
P(class 1)=21​(1−cosθcosx0​cosx1​)
Worked out for this exact circuit. cos x₀ · cos x₁ is close to +1 for class 0 points and close to −1 for class 1 points; θ decides how much of that signal reaches the readout.
θA (class 0)B (class 0)C (class 1)D (class 1)
00.030.040.960.96
π/40.170.180.830.83
π/20.500.500.500.50
π0.970.960.040.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.

Hands-on

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
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];
Sign in to run (nothing is saved)
Open in the IDE Open in the Playground
Note

Be honest about what this model can do: with one parameter it can only sharpen, blur or flip the boundary. It works because the encoding and the CNOT already compute the useful feature cos x₀ · cos x₁. Real classifiers stack several layers with many parameters, and choosing the encoding is often the hardest part.

Knowledge check

2 questions · one try each counts

Question 1

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.

Question 2

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.

Key takeaway

A variational classifier is encoding, entangling and trainable rotations followed by a measurement. Here one RY weight turns an XOR-like feature into a class probability, and training is the search for the angle with the lowest error.

Finished reading?

Knowledge check: 2 questions waiting above. Questions are optional. Mark it complete, or press Next: moving on marks it complete too.

Up next
Lesson 6 · 14 min
Observables, Hamiltonians & Real Applications
Turn 0/1 counts into ⟨Z⟩, measure X and Y by rotating first, and add Pauli terms into the energy that chemistry and optimisation minimise.
Further reading
  • Docs: encodings and trainable circuits
More in Cutting and Quantum ML
  • Circuit cutting
  • Classical ML vs. Quantum ML: The Hybrid Loop
  • Variational circuits
Data encodingObservables, Hamiltonians & Real Applications
In this lesson0%
  1. Introduction
  2. The circuit
  3. Try it: Tune θ for point C
  4. Knowledge check
  5. Key takeaway
QubitLab
IDELearnPlaygroundDocsCommunity
Log in
QubitLab
IDELearnPlaygroundDocsCommunity
Log in