QuantumCore

Hands-on lab · 1 step

Train a one-qubit variational classifier

The circuit RY(θ)RY(x)∣0⟩R_Y(\theta)R_Y(x)\lvert0\rangle with observable ZZ is the smallest variational quantum classifier: feature map U(x)=RY(x)U(x)=R_Y(x), ansatz W(θ)=RY(θ)W(\theta)=R_Y(\theta), model output fθ(x)=⟨Z⟩=cos⁡(x+θ)f_\theta(x)=\langle Z\rangle=\cos(x+\theta) and prediction y^=sign fθ(x)\hat y=\mathrm{sign}\,f_\theta(x). Choose the single weight θ\theta so that all four training points are classified correctly.

0/1 step passedBest score 0/100

Step 1: Choose θ so that sign⟨Z⟩ reproduces every label

The circuit is fixed: RYR_Y with the angle given by the data symbol xx (parameter x, not trainable), followed by RYR_Y with the angle given by the weight θ\theta (parameter theta). Only the value of theta may change; the gates, their order and the parameter x are locked.

The grader binds xx to each of the four training points, simulates the state RY(θ)RY(x)∣0⟩R_Y(\theta)R_Y(x)\lvert0\rangle, and predicts y^=sign ⟨Z⟩\hat y=\mathrm{sign}\,\langle Z\rangle:

xx0.30.30.90.91.81.82.22.2
label yy−1-1−1-1+1+1+1+1

The check passes when the accuracy is 11, i.e. all four predictions equal their labels (an output of exactly 00 counts as wrong). In the lesson it is shown that ⟨Z⟩=cos⁡(x+θ)\langle Z\rangle=\cos(x+\theta), so you need cos⁡(x+θ)<0\cos(x+\theta)<0 at x=0.3, 0.9x=0.3,\,0.9 and cos⁡(x+θ)>0\cos(x+\theta)>0 at x=1.8, 2.2x=1.8,\,2.2. At the initial value θ=0\theta=0 the accuracy is 00.

  • 1 qubit
  • no measurements
  • structure fixed: change parameters only
  • free parameters: theta

One gate per line or separated by ;, e.g. H(0); CNOT(0,1); RY(1, pi/3); measure(1 -> 0). Qubit 0 is the top wire (most significant bit); controls come first. # starts a comment.

Measure into a classical bit with measure(q -> c); apply a gate only when a bit is set with X(2) if c1==1 (several bits, first = most significant: if c[0,1]==3).

Parameters
= 0(fixed)
= 0
Circuit diagram of your answerRY(x) on q0; RY(theta) on q0q0RY(x)RY(theta)
Statevector amplitudes
BasisAmplitude α|α|Phase arg αProbability
|0⟩1
1.0000
0
100.00%
1 basis state with zero amplitude hidden