The U gate is universal — any single-qubit gate can be expressed as U(theta, phi, lambda).
gate.U — Universal Parameterization
The standard way to construct any single-qubit gate. The matrix is:gate.New — Define a Matrix Directly
Constructs a gate from explicit complex128 row vectors. Use this when your gate cannot be expressed cleanly through parameterization.*matrix.Matrix. There is no size restriction imposed by gate.New itself, but the simulator’s single-qubit methods (G, C) expect a 2×2 matrix.
gate.SU — Special Unitary
Returns a gate with determinant +1 (no global phase). Useful when the global phase matters, e.g., when building controlled operations where the global phase of the sub-gate becomes a relative phase.gate.SU(theta, phi, lambda) multiplies gate.U(theta, phi, lambda) by exp(-i·(phi+lambda)/2).
gate.ABC — ABC Decomposition
Decomposes U(theta, phi, lambda) into four components: a global phasealpha and three 2×2 matrices A, B, C satisfying:
- A·B·C = I
- exp(i·alpha)·A·X·B·X·C = U(theta, phi, lambda)
qsim.G — Apply Arbitrary Gate via Simulator
Applies any 2×2*matrix.Matrix to one or more qubits. Each qubit in the list receives the gate independently.
qsim.C — Controlled Arbitrary Gate
Applies a controlled version ofg: g is applied to target when control is |1⟩.
qsim.Controlled:
qsim.Cond — Conditional Gate Application
Appliesg to the listed qubits only if condition is true. Has no effect when condition is false. Useful for classically-controlled corrections in teleportation and error correction.
Full Example: Bell State via Custom U Gates
This is the exact example from the library README — constructing H and X throughgate.U and entangling two qubits.
Why use gate.U instead of qsim.H / qsim.CNOT?
Why use gate.U instead of qsim.H / qsim.CNOT?
The built-in methods like
qsim.H and qsim.CNOT are convenience wrappers. Using gate.U + qsim.G / qsim.C is preferred when:- You need to parameterize the gate at runtime (e.g., variational quantum algorithms).
- You want to pass the same gate object to multiple simulator instances.
- You are building a gate from a decomposition (e.g.,
gate.ABC) and applying its pieces individually. - You need to apply the controlled version of a gate that has no dedicated simulator method.