pub enum Gate {
Show 29 variants
Id,
X,
Y,
Z,
H,
S,
Sdg,
T,
Tdg,
SX,
SXdg,
Rx(f64),
Ry(f64),
Rz(f64),
P(f64),
Rzz(f64),
Cx,
Cz,
Swap,
Cu(Box<[[Complex64; 2]; 2]>),
Mcu(Box<McuData>),
Fused(Box<[[Complex64; 2]; 2]>),
BatchPhase(Box<BatchPhaseData>),
BatchRzz(Box<BatchRzzData>),
DiagonalBatch(Box<DiagonalBatchData>),
MultiFused(Box<MultiFusedData>),
Fused2q(Box<[[Complex64; 4]; 4]>),
Multi2q(Box<Multi2qData>),
QftBlock {
start: u8,
num: u8,
},
}Expand description
Quantum gate identifier.
Covers the v0 supported gate set. Most variants are data-free or carry an f64
parameter inline. The Fused variant uses Box to keep the enum at 16 bytes.
Variants§
Id
Identity.
X
Pauli-X (bit flip).
Y
Pauli-Y.
Z
Pauli-Z (phase flip).
H
Hadamard.
S
S gate (√Z).
Sdg
S† gate.
T
T gate (π/8).
Tdg
T† gate.
SX
√X gate.
SXdg
√X† gate.
Rx(f64)
Rotation about X-axis by angle (radians).
Ry(f64)
Rotation about Y-axis by angle (radians).
Rz(f64)
Rotation about Z-axis by angle (radians).
P(f64)
Phase gate [[1,0],[0,e^{iθ}]].
Rzz(f64)
ZZ rotation: diag(e^{-iθ/2}, e^{iθ/2}, e^{iθ/2}, e^{-iθ/2}). Qubit order: [q0, q1] (symmetric).
Cx
Controlled-X (CNOT). Qubit order: [control, target].
Cz
Controlled-Z. Qubit order: [q0, q1] (symmetric).
Swap
SWAP. Qubit order: [q0, q1] (symmetric).
Cu(Box<[[Complex64; 2]; 2]>)
Controlled-unitary. Applies the boxed 2×2 matrix to the target qubit
only when the control qubit is |1⟩. Qubit order: [control, target].
Boxed to keep Gate at 16 bytes.
Mcu(Box<McuData>)
Multi-controlled unitary. Applies the 2×2 matrix to the target qubit
only when all control qubits are |1⟩. Qubit order:
[ctrl_0, ctrl_1, ..., ctrl_{k-1}, target].
Boxed to keep Gate at 16 bytes.
Fused(Box<[[Complex64; 2]; 2]>)
Pre-fused single-qubit unitary (product of consecutive gates on the same target).
Boxed to keep Gate at 16 bytes for cache-friendly instruction streams.
BatchPhase(Box<BatchPhaseData>)
Batched controlled-phase: multiple cphase gates sharing a control qubit,
fused into a single pass over the statevector. Created by the cphase
fusion pass. Targets: [control]. The BatchPhaseData holds per-target
phases. Boxed to keep Gate at 16 bytes.
BatchRzz(Box<BatchRzzData>)
Batched ZZ rotations: multiple Rzz gates fused into a single pass.
Created by the batch-Rzz fusion pass. The BatchRzzData holds per-edge
angles. Boxed to keep Gate at 16 bytes.
DiagonalBatch(Box<DiagonalBatchData>)
Batched diagonal gates: a contiguous run of diagonal 1q and 2q gates
collapsed into a single state-vector sweep with a precomputed phase LUT.
Subsumes BatchPhase and BatchRzz for mixed diagonal runs. Created by the
diagonal batch fusion pass. Boxed to keep Gate at 16 bytes.
MultiFused(Box<MultiFusedData>)
Multiple single-qubit gates on distinct qubits, batched for a single
tiled pass over the statevector. Created by the multi-gate fusion pass.
Boxed to keep Gate at 16 bytes.
Fused2q(Box<[[Complex64; 4]; 4]>)
Pre-fused two-qubit unitary (4×4 matrix). Created by the 2q fusion pass
which absorbs adjacent single-qubit gates into a two-qubit gate.
Boxed to keep Gate at 16 bytes.
Multi2q(Box<Multi2qData>)
Multiple two-qubit gates batched for a single tiled pass over the
statevector. Created by the multi-2q fusion pass. Each entry stores
(q0, q1, 4×4 matrix). Boxed to keep Gate at 16 bytes.
QftBlock
Quantum Fourier Transform on start..start+num.
The CPU statevector backend has a fast whole-state FFT path. Subrange
blocks and non-native backends expand to textbook H, cphase, and swap
gates before execution.
Boxless: (u8, u8) fits within the 16-byte enum slot.
Implementations§
Source§impl Gate
impl Gate
Sourcepub fn num_qubits(&self) -> usize
pub fn num_qubits(&self) -> usize
Number of qubits this gate acts on.
Sourcepub fn matrix_2x2(&self) -> [[Complex64; 2]; 2]
pub fn matrix_2x2(&self) -> [[Complex64; 2]; 2]
Returns the 2×2 unitary matrix for single-qubit gates.
§Panics
Panics if called on a multi-qubit or batch gate (Cx, Cz, Swap,
Cu, Mcu, BatchPhase, MultiFused, Fused2q, Multi2q).
Sourcepub fn matrix_4x4(&self) -> [[Complex64; 4]; 4]
pub fn matrix_4x4(&self) -> [[Complex64; 4]; 4]
Returns the 4×4 unitary matrix for two-qubit gates.
Matrix indices follow the convention: row/col i*2+j where i indexes
targets[0] and j indexes targets[1].
§Panics
Panics on gates other than Cx, Cz, Swap, Cu, or Fused2q.
Sourcepub fn name(&self) -> &'static str
pub fn name(&self) -> &'static str
Human-readable gate name (for errors, logs, and OpenQASM round-tripping).
Sourcepub fn matrix_power(&self, k: i64) -> Gate
pub fn matrix_power(&self, k: i64) -> Gate
Compute integer power of a single-qubit gate.
Returns the gate raised to the k-th power. Negative k inverts first.
Only valid for single-qubit gates.
Sourcepub fn cu(mat: [[Complex64; 2]; 2]) -> Gate
pub fn cu(mat: [[Complex64; 2]; 2]) -> Gate
Create a single-controlled unitary gate with the given 2x2 matrix.
Sourcepub fn mcu(mat: [[Complex64; 2]; 2], num_controls: u8) -> Gate
pub fn mcu(mat: [[Complex64; 2]; 2], num_controls: u8) -> Gate
Create a multi-controlled unitary gate with num_controls control qubits.
Sourcepub fn cphase(theta: f64) -> Gate
pub fn cphase(theta: f64) -> Gate
Create a controlled-phase gate CPhase(θ) = Cu([[1,0],[0,e^{iθ}]]).
Applies phase e^{iθ} to |11⟩ and identity to all other basis states.
Sourcepub fn controlled_phase(&self) -> Option<Complex64>
pub fn controlled_phase(&self) -> Option<Complex64>
Returns the phase if this is a controlled-phase gate (Cu/Mcu with
diagonal matrix [[1,0],[0,e^{iθ}]]).
Used by backends to dispatch to optimized phase-only kernels that touch half the memory of the generic controlled-unitary kernel.
Sourcepub fn is_diagonal_1q(&self) -> bool
pub fn is_diagonal_1q(&self) -> bool
True if this is a diagonal single-qubit gate (matrix is [[a,0],[0,b]]).
Diagonal gates commute with CX on the control qubit and with CZ on either qubit. Used by the commutation-aware reordering pass.
Sourcepub fn is_self_inverse_2q(&self) -> bool
pub fn is_self_inverse_2q(&self) -> bool
True if this is a self-inverse two-qubit gate (applying it twice = identity).
Sourcepub fn preserves_sparsity(&self) -> bool
pub fn preserves_sparsity(&self) -> bool
True if this gate maps computational basis states to computational basis states (with at most a phase). Such gates preserve the number of non-zero amplitudes, making the sparse backend optimal (O(1) memory for |0…0⟩).
Includes diagonal gates (Z, S, T, Rz, P, CZ) and permutation gates (X, Y, CX, SWAP). Excludes superposition-creating gates (H, Rx, Ry, SX).
Sourcepub fn recognize_matrix(mat: &[[Complex64; 2]; 2]) -> Option<Gate>
pub fn recognize_matrix(mat: &[[Complex64; 2]; 2]) -> Option<Gate>
Try to recognize a 2x2 unitary matrix as a named gate (up to global phase).
Used by the fusion pass to emit named gate variants instead of opaque
Gate::Fused matrices, enabling downstream passes (e.g. clifford_prefix_split)
to identify Clifford gates that arose from fusion (e.g. T·T → S).
Sourcepub fn is_clifford(&self) -> bool
pub fn is_clifford(&self) -> bool
True if this gate is a Clifford gate (relevant for stabilizer backend).