Lesson 5 of 15
The SWAP Gate
The SWAP Gate
The SWAP gate exchanges the states of two qubits:
It leaves and unchanged.
Matrix Representation
State Vector Indexing
A 2-qubit system has 4 basis states indexed as :
| Index | State |
|---|---|
| 0 | $ |
| 1 | $ |
| 2 | $ |
| 3 | $ |
SWAP exchanges the amplitudes at indices 1 and 2 — swapping .
Decomposition into CNOTs
The SWAP gate can be decomposed into three CNOT gates:
This decomposition is useful in hardware where direct qubit-qubit connectivity is limited.
Self-Inverse
Like CNOT and Toffoli, SWAP is its own inverse:
Applications
- Qubit routing: moving quantum information between non-adjacent qubits in hardware
- Quantum sorting networks: sorting qubit registers
- iSWAP: a variant 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 (index 1) and (index 2).
Python runtime loading...
Loading...
Click "Run" to execute your code.