Quantum Gates β
Quantum gates manipulate qubit states. They're the quantum equivalent of classical logic gates, but reversible and operating on probability amplitudes.
Applying Gates with <- β
Quantum gates use the same rebind operator (<-) you learned in Language Basics. This creates a unified modelβwhether you're transforming numbers or qubits, the syntax is the same:
| Context | Code | Meaning |
|---|---|---|
| Classical | x <- double | Rebind x to double(x) |
| Quantum | q <- hadamard | Rebind q to hadamard(q) |
The syntax q <- hadamard is sugar for q <- hadamard(q). The gate function takes a qubit, transforms it, and the result is rebound to the same variable.
q <- hadamard -- apply Hadamard gate to q
q <- pauli_x -- apply Pauli-X gate to qGates can be chained in sequence:
Single-Qubit Gates β
Hadamard Gate β
The most important gate - creates equal superposition:
q <- hadamard -- |0> -> (|0> + |1>)/sqrt(2)Pauli Gates β
The three Pauli gates rotate around different axes:
| Gate | Effect |
|---|---|
pauli_x | Bit flip: swaps |
pauli_y | Bit and phase flip |
pauli_z | Phase flip: |
Rotation Gates β
Parameterized rotations around each axis:
| Gate | Description |
|---|---|
rx(theta) | Rotate around X-axis by theta radians |
ry(theta) | Rotate around Y-axis by theta radians |
rz(theta) | Rotate around Z-axis by theta radians |
Multi-Qubit Gates β
CNOT (Controlled-NOT) β
The workhorse of quantum computing - flips the target qubit when the control is |1>:
control, target <- cnot -- flip target if control is |1>CZ (Controlled-Z) β
Applies a phase flip when both qubits are |1>:
SWAP β
Exchanges the states of two qubits:
Toffoli (CCNOT) β
A three-qubit gate - flips the target only when both controls are |1>:
Gate Summary β
| Gate | Qubits | Syntax | Description |
|---|---|---|---|
hadamard | 1 | q <- hadamard | Creates superposition |
pauli_x | 1 | q <- pauli_x | Bit flip (NOT) |
pauli_y | 1 | q <- pauli_y | Bit and phase flip |
pauli_z | 1 | q <- pauli_z | Phase flip |
rx(theta) | 1 | q <- rx(1.57) | X-axis rotation |
ry(theta) | 1 | q <- ry(1.57) | Y-axis rotation |
rz(theta) | 1 | q <- rz(1.57) | Z-axis rotation |
cnot | 2 | c, t <- cnot | Controlled-NOT |
cz | 2 | c, t <- cz | Controlled-Z |
swap | 2 | q1, q2 <- swap | Swap qubit states |
ccnot | 3 | c1, c2, t <- ccnot | Toffoli |