QuantumCore

Hands-on lab · 1 step

SWAP from three CNOTs

Build the two-qubit SWAP gate, which exchanges the states of the two wires, using only CNOT gates and at most three of them.

0/1 step passedBest score 0/100

Step 1: Exchange two qubits

Using only CNOT gates (at most three, in either direction), build a two-qubit circuit whose unitary equals SWAP=(1000001001000001),\mathrm{SWAP}=\begin{pmatrix}1&0&0&0\\0&0&1&0\\0&1&0&0\\0&0&0&1\end{pmatrix}, that is, SWAP ∣a⟩∣b⟩=∣b⟩∣a⟩\mathrm{SWAP}\,\lvert a\rangle\lvert b\rangle=\lvert b\rangle\lvert a\rangle for all a,b∈{0,1}a,b\in\{0,1\}. Qubit 0 is the most significant bit, so the basis order is ∣00⟩,∣01⟩,∣10⟩,∣11⟩\lvert00\rangle,\lvert01\rangle,\lvert10\rangle,\lvert11\rangle. The grader computes your circuit unitary UU and the process fidelity FU=∣Tr(SWAP†U)∣/4F_U=\lvert\mathrm{Tr}(\mathrm{SWAP}^\dagger U)\rvert/4, which equals 11 exactly when U=eiγ SWAPU=e^{i\gamma}\,\mathrm{SWAP} for some real γ\gamma. Measurements are not allowed.

  • 2 qubits
  • allowed gates: CNOT
  • at most 3 gates
  • no measurements

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 answerEmpty circuitq0q1
Statevector amplitudes
BasisAmplitude α|α|Phase arg αProbability
|00⟩1
1.0000
0
100.00%
3 basis states with zero amplitude hidden