Complete Reference
Gate Reference — 46 Gates
Every quantum gate implemented in Sansqrit, with unitary matrices, DSL syntax, parameters, code examples, and hardware backend mapping. Source: gates.rs
Single-Qubit Gates (18)
Single-qubit gates operate on one qubit at a time. Each gate is represented by a 2×2 unitary matrix U where U†U = I (identity). In Sansqrit, the gate is applied to a sparse state vector by iterating over non-zero entries and distributing amplitudes between the original state and its partner (same state with the target bit flipped).
Identity Gate (I)
The identity gate does nothing — it leaves the qubit unchanged. It is used as a placeholder in circuit descriptions, for timing alignment in hardware circuits, and as the default when no operation is needed.
Matrix: [[1, 0], [0, 1]] Parameters: none Qubits: 1
I(q[0]) -- no-op; qubit is unchanged
Pauli-X Gate (X) — Bit Flip
The Pauli-X gate is the quantum equivalent of a classical NOT gate. It flips |0⟩ to |1⟩ and |1⟩ to |0⟩. It is used to initialize qubits to |1⟩, as part of oracle constructions in Grover's algorithm, and as the target operation in controlled gates (CNOT = controlled-X). The X gate is its own inverse: X² = I. It is also called the σx Pauli matrix.
Matrix: [[0, 1], [1, 0]] Parameters: none Qubits: 1
X(q[0]) -- flip: |0⟩ → |1⟩, |1⟩ → |0⟩
-- Use case: Initialize qubit to |1⟩ state
simulate {
let q = quantum_register(1)
X(q[0]) -- now q[0] = |1⟩
print(measure_all(q, shots=100)) -- {"1": 100}
}
Pauli-Y Gate (Y)
The Pauli-Y gate applies both a bit flip and a phase flip. It maps |0⟩ to i|1⟩ and |1⟩ to -i|0⟩. Y = iXZ. It appears in VQE Hamiltonians (Y⊗Y terms), quantum error correction syndrome measurements, and as part of the universal gate decomposition. Like X and Z, it is Hermitian and unitary: Y† = Y, Y² = I.
Matrix: [[0, -i], [i, 0]] Parameters: none Qubits: 1
Y(q[0]) -- bit flip + phase flip
Pauli-Z Gate (Z) — Phase Flip
The Pauli-Z gate flips the phase of the |1⟩ state: |0⟩ → |0⟩, |1⟩ → -|1⟩. It leaves the computational basis probabilities unchanged but modifies the relative phase. Z is the fundamental phase gate and forms the basis for phase kickback in quantum algorithms. Z = S², Z = T⁴. In the Bloch sphere picture, Z is a 180° rotation about the z-axis.
Matrix: [[1, 0], [0, -1]] Parameters: none Qubits: 1
Z(q[0]) -- phase flip: |1⟩ → -|1⟩
-- Teleportation correction:
if b0 == 1 { Z(q[2]) }
Hadamard Gate (H) — Superposition
The Hadamard gate is the most important single-qubit gate in quantum computing. It creates an equal superposition: |0⟩ → (|0⟩ + |1⟩)/√2 and |1⟩ → (|0⟩ - |1⟩)/√2. The Hadamard gate is its own inverse: H² = I. It transforms between the computational basis (Z) and the diagonal basis (X), making it essential for: creating initial superpositions in search algorithms (Grover), implementing the quantum Fourier transform, creating Bell states (H + CNOT), basis changes for measurement in different bases, and the diffusion operator in amplitude amplification.
Matrix: (1/√2)[[1, 1], [1, -1]] Parameters: none Qubits: 1
H(q[0]) -- superposition: |0⟩ → (|0⟩+|1⟩)/√2
-- Creating uniform superposition over all n qubits:
for i in range(n) { H(q[i]) }
-- Bell state:
H(q[0]); CNOT(q[0], q[1])
S and S† Gates (Phase π/2)
The S gate applies a phase of π/2 (90°) to the |1⟩ state: |1⟩ → i|1⟩. S² = Z. The S† (Sdg) gate is the inverse: it applies a phase of -π/2. The S gate is a key component of the Clifford group and is used in quantum error correction, T gate synthesis, and the decomposition of arbitrary single-qubit rotations.
S Matrix: [[1, 0], [0, i]] S† Matrix: [[1, 0], [0, -i]]
S(q[0]) -- phase π/2: |1⟩ → i|1⟩
Sdg(q[0]) -- phase -π/2: |1⟩ → -i|1⟩
T and T† Gates (Phase π/4)
The T gate applies a phase of π/4 (45°) to |1⟩. T⁴ = S, T⁸ = Z. The T gate is NOT a Clifford gate — it is the key addition needed to achieve universality. The Clifford+T gate set is the standard universal gate set for fault-tolerant quantum computation. T gates are the most expensive gates on real hardware (requiring magic state distillation), so minimizing T-count is a major optimization goal.
T Matrix: [[1, 0], [0, eiπ/4]] T† Matrix: [[1, 0], [0, e-iπ/4]]
T(q[0]) -- phase π/4: |1⟩ → e^(iπ/4)|1⟩
Tdg(q[0]) -- phase -π/4: inverse of T
√X and √X† Gates
The SX (√X) gate is the square root of the X gate: SX² = X. It is a native gate on IBM quantum processors (ibmq), making it important for hardware-efficient circuit compilation. The SXdg (√X†) gate is its inverse.
SX Matrix: (1/2)[[1+i, 1-i], [1-i, 1+i]]
SX(q[0]) -- √X: IBM native gate
SXdg(q[0]) -- (√X)† = inverse
Rx Gate — X-axis Rotation
The Rx(θ) gate rotates the qubit about the X-axis of the Bloch sphere by angle θ. It is a continuous parametric gate used extensively in variational circuits (VQE, QAOA) and appears as the mixer operator in QAOA. Rx(π) = -iX (equivalent to X up to global phase). Rx(π/2) creates a Y-basis superposition.
Matrix: [[cos(θ/2), -i·sin(θ/2)], [-i·sin(θ/2), cos(θ/2)]]
Rx(q[0], PI/4) -- rotate about X by π/4
-- QAOA mixer layer:
for i in range(n) { Rx(q[i], 2.0 * beta) }
Ry Gate — Y-axis Rotation
The Ry(θ) gate rotates about the Y-axis. It is the most commonly used rotation gate in variational circuits because it generates real-valued amplitudes (no imaginary components when starting from |0⟩). This makes it ideal for: amplitude encoding of classical data, VQE hardware-efficient ansatz layers, and angle encoding in quantum machine learning. Ry(π) = -iY.
Matrix: [[cos(θ/2), -sin(θ/2)], [sin(θ/2), cos(θ/2)]]
Ry(q[0], PI/2) -- rotate about Y by π/2
-- Feature encoding:
for i in range(n) { Ry(q[i], data[i] * PI) }
Rz Gate — Z-axis Rotation
The Rz(θ) gate rotates about the Z-axis. It modifies only the phase of |1⟩ relative to |0⟩: |0⟩ → e-iθ/2|0⟩, |1⟩ → eiθ/2|1⟩. Rz is a virtual gate on many hardware platforms (IBM, Google) because it can be implemented by frame changes rather than physical pulses, making it essentially "free" in terms of circuit depth and error.
Matrix: [[e-iθ/2, 0], [0, eiθ/2]]
Rz(q[0], PI/3) -- rotate about Z by π/3
Phase / U1 Gate
The Phase gate (also called P or U1) applies a phase eiθ to |1⟩ without affecting |0⟩. Equivalent to Rz(θ) up to a global phase. Phase(π) = Z, Phase(π/2) = S, Phase(π/4) = T.
Matrix: [[1, 0], [0, eiθ]]
Phase(q[0], PI/4) -- equivalent to T gate
U1(q[0], PI/2) -- equivalent to S gate
U2 Gate — 2-parameter Single-Qubit
U2(φ, λ) = U3(π/2, φ, λ). A single-qubit gate with two parameters that creates superpositions with controlled phases. U2(0, π) = H (Hadamard). It is useful for creating arbitrary single-qubit states from |0⟩ with fewer parameters than U3.
Matrix: (1/√2)[[1, -eiλ], [eiφ, ei(φ+λ)]]
U2(q[0], 0.0, PI) -- equivalent to Hadamard
U3 Gate — General Single-Qubit
U3(θ, φ, λ) is the most general single-qubit gate. Any single-qubit unitary can be decomposed as U3 with appropriate parameters. U3(0, 0, λ) = Phase(λ), U3(π/2, 0, π) = H, U3(π, 0, π) = X. This gate is the native gate on IBM quantum hardware, so any single-qubit operation on IBM devices is ultimately compiled into a sequence of U3 and CNOT gates.
Matrix: [[cos(θ/2), -eiλsin(θ/2)], [eiφsin(θ/2), ei(φ+λ)cos(θ/2)]]
U3(q[0], PI/4, PI/2, PI/8) -- general rotation
Two-Qubit Gates (21)
Two-qubit gates create entanglement — the most distinctly quantum phenomenon. They operate on pairs of qubits and are represented by 4×4 unitary matrices. In Sansqrit, two-qubit gates are applied by iterating over non-zero entries and examining both qubits' bit values to determine the transformation.
CNOT (Controlled-NOT / CX)
The CNOT gate is the most fundamental two-qubit gate. It flips the target qubit if and only if the control qubit is |1⟩. CNOT is the basis of entanglement creation (H + CNOT = Bell state). Together with single-qubit rotations, CNOT forms a universal gate set — any quantum circuit can be decomposed into CNOT + single-qubit gates. CNOT is its own inverse: CNOT² = I.
CNOT(q[0], q[1]) -- flip q[1] if q[0]=|1⟩
-- Aliases: CX(q[0], q[1]) does the same thing
-- Bell state:
H(q[0]); CNOT(q[0], q[1])
-- GHZ state:
H(q[0]); for i in range(1, n) { CNOT(q[0], q[i]) }
CZ (Controlled-Z)
CZ applies a Z gate to the target when control=|1⟩. Equivalently, it flips the phase when both qubits are |1⟩: |11⟩ → -|11⟩. CZ is symmetric — swapping control and target produces the same result. CZ is the native two-qubit gate on Google Sycamore processors.
CZ(q[0], q[1]) -- phase flip when both |1⟩
CY (Controlled-Y)
CY(q[0], q[1]) -- apply Y to target when control=|1⟩
CH (Controlled-Hadamard)
Applies a Hadamard gate to the target qubit conditionally on the control qubit being |1⟩. Used in quantum random access memory (QRAM) circuits and certain oracle constructions.
CH(q[0], q[1]) -- Hadamard on q[1] when q[0]=|1⟩
CSX (Controlled-√X)
CSX(q[0], q[1]) -- √X on target when control=|1⟩
SWAP
Exchanges the states of two qubits. SWAP = CNOT(a,b) · CNOT(b,a) · CNOT(a,b). SWAP is used for qubit routing on hardware with limited connectivity (e.g., moving a qubit to an adjacent position for a CNOT). SWAP is its own inverse.
SWAP(q[0], q[1]) -- exchange q[0] ↔ q[1]
iSWAP
Swaps two qubits and applies a phase factor of i to the swapped states. Native on some superconducting architectures. iSWAP² = SWAP (up to phase).
iSWAP(q[0], q[1]) -- SWAP with i phase
√SWAP
The square root of SWAP: applying it twice gives a full SWAP. Creates partial entanglement and is useful for quantum walk implementations.
SqrtSWAP(q[0], q[1]) -- half swap
fSWAP (Fermionic SWAP)
SWAP with a (-1) phase when both qubits are |1⟩. Essential for simulating fermionic systems in quantum chemistry (Jordan-Wigner transformation).
fSWAP(q[0], q[1]) -- fermionic swap
DCX (Double-CNOT)
Two CNOTs in sequence: CNOT(a,b) then CNOT(b,a). Creates entanglement in both directions.
DCX(q[0], q[1]) -- CNOT(0,1) then CNOT(1,0)
CRx, CRy, CRz (Controlled Rotations)
Controlled versions of the Rx, Ry, and Rz rotation gates. The rotation is applied to the target qubit only when the control is |1⟩. These gates are fundamental building blocks of the Quantum Fourier Transform (which uses controlled phase rotations) and variational circuits with entanglement.
CRx(q[0], q[1], PI/4) -- controlled Rx
CRy(q[0], q[1], PI/2) -- controlled Ry
CRz(q[0], q[1], PI/3) -- controlled Rz
CP (Controlled-Phase)
Applies a phase eiθ when both qubits are |1⟩. This is the key gate in the Quantum Fourier Transform where the phase angles are π/2k for increasing k. CP is symmetric — swapping control and target gives the same result.
CP(q[0], q[1], PI/4) -- phase when both |1⟩
-- Used in QFT:
for j in range(i+1, n) {
CP(q[j], q[i], PI / (1 << (j-i)))
}
CU (Controlled-U3)
The most general controlled single-qubit gate. Applies U3(θ,φ,λ) to the target when control=|1⟩. Any controlled single-qubit operation can be expressed as CU.
CU(q[0], q[1], PI/4, PI/2, PI/8) -- controlled U3
RXX Gate — XX Ising Coupling
RXX(θ) = exp(-iθ/2 · X⊗X). The XX Ising interaction gate. Creates entanglement through X-X coupling. Used in: Mølmer-Sørensen gates on trapped-ion hardware (IonQ), Ising model simulation, and quantum approximate optimization. RXX(π/2) is the native entangling gate on IonQ processors.
RXX(q[0], q[1], PI/2) -- XX Ising coupling
RYY Gate — YY Ising Coupling
RYY(θ) = exp(-iθ/2 · Y⊗Y). The YY interaction, used in Heisenberg model simulations and certain variational ansätze for chemistry.
RYY(q[0], q[1], PI/2) -- YY Ising coupling
RZZ Gate — ZZ Ising Coupling
RZZ(θ) = exp(-iθ/2 · Z⊗Z). The ZZ interaction gate. This is the core gate in QAOA — each edge in the MaxCut graph becomes an RZZ gate. Also fundamental for Ising model simulation, quantum chemistry (Z-Z terms in Jordan-Wigner Hamiltonians), and trotterized time evolution.
RZZ(q[0], q[1], gamma) -- ZZ coupling (QAOA cost layer)
-- QAOA MaxCut cost unitary:
for (i, j) in edges { RZZ(q[i], q[j], gamma) }
RZX Gate — ZX Cross-Resonance
RZX(θ) = exp(-iθ/2 · Z⊗X). The cross-resonance interaction gate. Decomposed internally as H(target) · RZZ(θ) · H(target). Native on IBM cross-resonance hardware.
RZX(q[0], q[1], PI/4) -- ZX cross-resonance
ECR (Echoed Cross-Resonance)
The ECR gate is the native two-qubit gate on IBM Falcon and Hummingbird processors. It is equivalent to (IX + XY)/√2 and creates maximal entanglement in a single pulse.
ECR(q[0], q[1]) -- IBM native entangling gate
MS (Mølmer-Sørensen)
The Mølmer-Sørensen gate is the native entangling gate on IonQ trapped-ion hardware. Equivalent to RZZ(π/2). Creates a maximally entangled Bell-like state from two |0⟩ qubits.
MS(q[0], q[1]) -- IonQ native gate = RZZ(π/2)
Three-Qubit Gates (3)
Toffoli (CCX) — Double-Controlled NOT
The Toffoli gate flips the target qubit if and only if BOTH control qubits are |1⟩. It is a universal gate for classical reversible computation (any classical boolean function can be built from Toffoli gates). In quantum computing, it is used for: quantum arithmetic (adders, multipliers), oracle constructions in Grover's algorithm, quantum error correction (syndrome extraction), and multi-controlled operations. Decomposing Toffoli into single-qubit + CNOT gates requires 6 CNOTs.
Toffoli(q[0], q[1], q[2]) -- flip q[2] if q[0]=q[1]=|1⟩
-- Alias: CCX(q[0], q[1], q[2])
Fredkin (CSWAP) — Controlled-SWAP
The Fredkin gate swaps two qubits conditionally on a control qubit. Used in the SWAP test for computing state overlap, quantum routing, and reversible computing.
Fredkin(q[0], q[1], q[2]) -- swap q[1]↔q[2] if q[0]=|1⟩
-- Alias: CSWAP(q[0], q[1], q[2])
CCZ (Double-Controlled Z)
Flips the phase when all three qubits are |1⟩: |111⟩ → -|111⟩. CCZ is symmetric under all permutations of its three qubits. It is equivalent to Toffoli conjugated by Hadamard on the target: CCZ = (I⊗I⊗H) · Toffoli · (I⊗I⊗H). Used in phase oracles and quantum error correction.
CCZ(q[0], q[1], q[2]) -- phase flip when all three |1⟩
Multi-Qubit Gates (4)
Multi-qubit gates operate on an arbitrary number of qubits. In Sansqrit, they are implemented by iterating over non-zero entries and checking all control qubits simultaneously.
MCX — Multi-Controlled X
Flips the target qubit when ALL control qubits are |1⟩. Generalizes CNOT (1 control) and Toffoli (2 controls) to n controls. Used in Grover's oracle for multi-target search and quantum arithmetic circuits.
-- 4-controlled X: flip q[4] when q[0..3] are all |1⟩
MCX([q[0], q[1], q[2], q[3]], q[4])
MCZ — Multi-Controlled Z
Flips the phase when ALL qubits are |1⟩. Used in phase oracles and Grover's diffusion operator for multi-qubit search.
MCZ([q[0], q[1], q[2], q[3]])
C3X — 3-Controlled X
C3X(q[0], q[1], q[2], q[3]) -- flip q[3] when q[0..2]=|1⟩
C4X — 4-Controlled X
C4X(q[0], q[1], q[2], q[3], q[4]) -- flip q[4] when q[0..3]=|1⟩