Skip to content

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:

ContextCodeMeaning
Classicalx <- doubleRebind x to double(x)
Quantumq <- hadamardRebind 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.

kettle
q <- hadamard      -- apply Hadamard gate to q
q <- pauli_x       -- apply Pauli-X gate to q

Gates can be chained in sequence:

Single-Qubit Gates ​

Hadamard Gate ​

The most important gate - creates equal superposition:

kettle
q <- hadamard    -- |0> -> (|0> + |1>)/sqrt(2)

Pauli Gates ​

The three Pauli gates rotate around different axes:

GateEffect
pauli_xBit flip: swaps
pauli_yBit and phase flip
pauli_zPhase flip:

Rotation Gates ​

Parameterized rotations around each axis:

GateDescription
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>:

kettle
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 ​

GateQubitsSyntaxDescription
hadamard1q <- hadamardCreates superposition
pauli_x1q <- pauli_xBit flip (NOT)
pauli_y1q <- pauli_yBit and phase flip
pauli_z1q <- pauli_zPhase flip
rx(theta)1q <- rx(1.57)X-axis rotation
ry(theta)1q <- ry(1.57)Y-axis rotation
rz(theta)1q <- rz(1.57)Z-axis rotation
cnot2c, t <- cnotControlled-NOT
cz2c, t <- czControlled-Z
swap2q1, q2 <- swapSwap qubit states
ccnot3c1, c2, t <- ccnotToffoli