|q⟩ Bad Qubits

beginner · Programming · Controlled Gates: CNOT, CZ, SWAP

The SWAP Gate

The SWAP gate exchanges the complete quantum states of two qubits. Where CNOT conditionally flips one qubit, SWAP unconditionally trades the contents of both — whatever amplitude was on qubit aa moves to qubit bb, and vice versa.

What SWAP does

On basis states, SWAP simply permutes the two labels:

SWAP00=00,SWAP01=10,SWAP10=01,SWAP11=11.\text{SWAP}\,|00\rangle = |00\rangle, \quad \text{SWAP}\,|01\rangle = |10\rangle, \quad \text{SWAP}\,|10\rangle = |01\rangle, \quad \text{SWAP}\,|11\rangle = |11\rangle.

In the computational basis {00,01,10,11}\{|00\rangle, |01\rangle, |10\rangle, |11\rangle\} (qubit 0 most significant) this corresponds to the unitary matrix

SWAP=(1000001001000001).\text{SWAP} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}.

The off-diagonal 11s in the 01|01\rangle/10|10\rangle block are what effect the exchange.

SWAP on superpositions

Because SWAP is linear, it works just as cleanly on superpositions. If qubit 0 carries amplitude α0+β1\alpha|0\rangle + \beta|1\rangle and qubit 1 carries γ0+δ1\gamma|0\rangle + \delta|1\rangle, then after SWAP qubit 0 carries γ0+δ1\gamma|0\rangle + \delta|1\rangle and qubit 1 carries α0+β1\alpha|0\rangle + \beta|1\rangle — a full exchange of the amplitude packets, including any relative phases.

Try it

The circuit below puts qubit 0 into 1|1\rangle, leaving the two-qubit register in 10|10\rangle. Your task is to apply SWAP so the register ends in 01|01\rangle — qubit 0 returns to 0|0\rangle and qubit 1 receives the 1|1\rangle.

Run your code to see the quantum state.

After a correct solution the statevector should show amplitude 11 on the 01|01\rangle basis state and zero everywhere else.

Sign in on the full site to ask questions and join the discussion.