QuantumCore

Hands-on lab · 1 step

Build the SWAP test

Build the SWAP test on three qubits and read the fidelity of two one-qubit states from the ancilla alone: with ∣ψ⟩=RY(π/3)∣0⟩\lvert\psi\rangle=R_Y(\pi/3)\lvert0\rangle and ∣ϕ⟩=∣0⟩\lvert\phi\rangle=\lvert0\rangle the overlap is F=∣⟨ψ∣ϕ⟩∣2=cos⁡2π6=34F=\lvert\langle\psi\vert\phi\rangle\rvert^2=\cos^2\frac\pi6=\frac34, so the ancilla must return 00 with probability P(0)=12(1+F)=78P(0)=\frac12(1+F)=\frac78.

0/1 step passedBest score 0/100

Step 1: Ancilla, controlled swap, ancilla

The register has three qubits, all starting in ∣0⟩\lvert0\rangle; qubit 00 is the most significant bit, so ∣c a b⟩\lvert c\,a\,b\rangle has index 4c+2a+b4c+2a+b.

  • qubit 00 is the ancilla;
  • qubit 11 holds ∣ψ⟩=RY(π/3)∣0⟩=32∣0⟩+12∣1⟩\lvert\psi\rangle=R_Y(\pi/3)\lvert0\rangle=\frac{\sqrt3}2\lvert0\rangle+\frac12\lvert1\rangle, prepared by the locked first operation RY(1,π/3)R_Y(1,\pi/3), which you must not edit or remove;
  • qubit 22 holds ∣ϕ⟩=∣0⟩\lvert\phi\rangle=\lvert0\rangle.

Append the SWAP test so that the probability of measuring the ancilla in ∣0⟩\lvert0\rangle equals

P(0)=12(1+∣⟨ψ∣ϕ⟩∣2)=12(1+cos⁡2π6)=78=0.875.P(0)=\frac12\big(1+\lvert\langle\psi\vert\phi\rangle\rvert^2\big)=\frac12\Big(1+\cos^2\frac\pi6\Big)=\frac78=0.875.

Allowed gates after the preparation: HH, CSWAP\mathrm{CSWAP} (control first, then the two swapped qubits), CNOT\mathrm{CNOT} and CCX\mathrm{CCX}; at most 55 gates; no measurements (the grader reads the exact pre-measurement state).

p-ancilla: the marginal distribution of qubit 00 must be (78,18)\big(\frac78,\frac18\big) within 10−610^{-6}.

z-ancilla: the expectation value ⟨Z⊗I⊗I⟩=P(0)−P(1)\langle Z\otimes I\otimes I\rangle=P(0)-P(1) must be 34\frac34 within 10−610^{-6}; it carries the same information as the marginal and is used for targeted feedback.

  • 3 qubits
  • allowed gates: H, CSWAP, CNOT, CCX
  • at most 5 gates
  • no measurements
  • first 1 op locked

Locked prefix (always applied first)

RY(1, pi/3)

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).

Circuit diagram of your answerRY(π/3) on q1q0q1q2RY(π/3)
Statevector amplitudes
BasisAmplitude α|α|Phase arg αProbability
|000⟩0.866
0.8660
0
75.00%
|010⟩0.5
0.5000
0
25.00%
6 basis states with zero amplitude hidden