Gates ​
Gates are functions that transform qubit states. They're reversible and operate 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 |
cy | 2 | c, t <- cy | Controlled-Y |
ch | 2 | c, t <- ch | Controlled-Hadamard |
crx(theta) | 2 | (c, t) = crx(1.57, c, t) | Controlled X-rotation |
cry(theta) | 2 | (c, t) = cry(1.57, c, t) | Controlled Y-rotation |
crz(theta) | 2 | (c, t) = crz(1.57, c, t) | Controlled Z-rotation |
swap | 2 | q1, q2 <- swap | Swap qubit states |
ccnot | 3 | c1, c2, t <- ccnot | Toffoli |
ccz | 3 | (c1, c2, t) = ccz(c1, c2, t) | Controlled-controlled-Z |
Gate Inverses ​
All quantum gates (except measurement) are reversible. Many are self-inverse; others have explicit adjoint versions:
| Gate | Inverse | Notes |
|---|---|---|
hadamard | hadamard | Self-inverse |
pauli_x, pauli_y, pauli_z | Same gate | Self-inverse |
cnot, cz, cy, ch | Same gate | Self-inverse |
swap | swap | Self-inverse |
ccnot, ccz | Same gate | Self-inverse |
s_gate | s_gate_dag | S†(inverse phase) |
t_gate | t_gate_dag | T†(inverse π/8 phase) |
rx(θ), ry(θ), rz(θ) | rx(-θ), ry(-θ), rz(-θ) | Negate angle |
crx(θ), cry(θ), crz(θ) | crx(-θ), cry(-θ), crz(-θ) | Negate angle |
Manual uncomputation:
fn apply_and_undo(q: Qubit) -> Qubit with Quantum
q <- hadamard -- Apply H
q <- hadamard -- Undo H (self-inverse)
q <- t_gate -- Apply T
q <- t_gate_dag -- Undo T
q <- rx(1.57) -- Rotate by θ
q <- rx(-1.57) -- Rotate by -θ (undo)
q
end fnFor automatic uncomputation, see Compute Blocks.