Lesson 5 of 15

The SWAP Gate

The SWAP Gate

The SWAP gate exchanges the states of two qubits:

SWAP∣01⟩=∣10⟩SWAP∣10⟩=∣01⟩\text{SWAP}|01\rangle = |10\rangle \qquad \text{SWAP}|10\rangle = |01\rangle

It leaves ∣00⟩|00\rangle and ∣11⟩|11\rangle unchanged.

Matrix Representation

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

State Vector Indexing

A 2-qubit system has 4 basis states indexed as q0⋅2+q1q_0 \cdot 2 + q_1:

IndexState
0$
1$
2$
3$

SWAP exchanges the amplitudes at indices 1 and 2 — swapping ∣01⟩↔∣10⟩|01\rangle \leftrightarrow |10\rangle.

Decomposition into CNOTs

The SWAP gate can be decomposed into three CNOT gates:

SWAP=CNOT01⋅CNOT10⋅CNOT01\text{SWAP} = \text{CNOT}_{01} \cdot \text{CNOT}_{10} \cdot \text{CNOT}_{01}

This decomposition is useful in hardware where direct qubit-qubit connectivity is limited.

Self-Inverse

Like CNOT and Toffoli, SWAP is its own inverse:

SWAP2=I\text{SWAP}^2 = I

Applications

  • Qubit routing: moving quantum information between non-adjacent qubits in hardware
  • Quantum sorting networks: sorting qubit registers
  • iSWAP: a variant iSWAP∣01⟩=i∣10⟩\text{iSWAP}|01\rangle = i|10\rangle used in superconducting processors

Implementation

def swap_gate(state):
    result = list(state)
    result[1], result[2] = state[2], state[1]
    return result

Your Task

Implement swap_gate(state) for a 4-element 2-qubit state vector that swaps the amplitudes of ∣01⟩|01\rangle (index 1) and ∣10⟩|10\rangle (index 2).

Python runtime loading...
Loading...
Click "Run" to execute your code.