Documentation
¶
Overview ¶
Package gate defines the Gate interface and provides a standard library of quantum gates.
A quantum gate is a unitary transformation on one or more qubits. Unitary means the transformation is reversible: U * U† = I. Every gate's Matrix method returns the unitary as a flat row-major []complex128 slice of length (2^n)^2, where n is the number of qubits. The simulator applies this matrix to the relevant amplitudes of the statevector to evolve the quantum state.
Fixed gates ¶
Single-qubit: I (identity), H (Hadamard/superposition), X (NOT/bit-flip), Y (bit-flip with phase), Z (phase-flip), S (sqrt-Z), Sdg (S†), T (pi/8 gate), Tdg (T†), SX (sqrt-X).
Two-qubit: CNOT (controlled-NOT, entangling), CZ (controlled-Z), CY (controlled-Y), SWAP, ISWAP (Google native), ECR (IBM native), DCX (double-CNOT), CH (controlled-H), CSX (controlled-sqrt-X), Sycamore (Google native FSim(pi/2, pi/6)).
Three-qubit: CCX (Toffoli), CSWAP (Fredkin), CCZ.
These are package-level singletons requiring zero allocation.
Parameterized gates ¶
Rotation gates (RX, RY, RZ) rotate the qubit state around the corresponding axis of the Bloch sphere by a given angle. Phase, U3, U1, U2, Rot, PhasedXZ, GlobalPhase provide various single-qubit parameterizations. Two-qubit: CP, CRX, CRY, CRZ (controlled rotations), RXX, RYY, RZZ (Ising interactions), FSim, PSwap. IonQ native: GPI, GPI2, MS.
Custom and multi-controlled gates ¶
Unitary and MustUnitary create custom gates from user-provided matrices with unitarity validation. Multi-controlled gates are built with MCX, MCZ, MCP, and Controlled.
Pseudo-gates ¶
Reset resets a qubit to |0>. Delay idles a qubit for a duration. Barrier prevents gate reordering across it during transpilation. These have no matrix representation - simulators handle them directly.
Matrix convention ¶
Gate matrices are stored as flat []complex128 slices in row-major order. For two-qubit gates the convention is: row bit 1 (MSB) = q0, bit 0 (LSB) = q1.
Package gate defines the quantum gate interface and standard gate library.
Index ¶
- Constants
- Variables
- type Applied
- type Bindable
- type ControlDiagonal2Q
- type ControlU2Q
- type ControlledGate
- type Delayable
- type Diagonal2Q
- type Gate
- func Barrier(n int) Gate
- func C3X() Gate
- func C4X() Gate
- func CP(phi float64) Gate
- func CRX(theta float64) Gate
- func CRY(theta float64) Gate
- func CRZ(theta float64) Gate
- func Controlled(inner Gate, nControls int) Gate
- func Delay(duration float64, unit string) Gate
- func FSim(theta, phi float64) Gate
- func GPI(phi float64) Gate
- func GPI2(phi float64) Gate
- func GlobalPhase(phi float64) Gate
- func MCP(phi float64, nControls int) Gate
- func MCX(nControls int) Gate
- func MCZ(nControls int) Gate
- func MS(phi0, phi1 float64) Gate
- func MustStatePrep(amplitudes []complex128) Gate
- func MustUnitary(name string, matrix []complex128) Gate
- func NOP(time float64) Gate
- func PSwap(phi float64) Gate
- func Phase(phi float64) Gate
- func PhasedXZ(xExp, zExp, axisPhaseExp float64) Gate
- func Pow(g Gate, k int) Gate
- func RX(theta float64) Gate
- func RXX(theta float64) Gate
- func RY(theta float64) Gate
- func RYY(theta float64) Gate
- func RZ(theta float64) Gate
- func RZZ(theta float64) Gate
- func Rot(phi, theta, omega float64) Gate
- func StatePrep(amplitudes []complex128) (Gate, error)
- func U1(lambda float64) Gate
- func U2(phi, lambda float64) Gate
- func U3(theta, phi, lambda float64) Gate
- func Unitary(name string, matrix []complex128) (Gate, error)
- type StatePrepable
Constants ¶
const ( UnitNs = "ns" // nanoseconds UnitUs = "us" // microseconds UnitMs = "ms" // milliseconds UnitS = "s" // seconds UnitDt = "dt" // backend-dependent time step )
Duration unit constants for use with Delay.
Variables ¶
var ( // I is the single-qubit identity gate. It leaves the qubit state unchanged // and is used as a placeholder or for padding in circuit layouts. // // [[1, 0], // [0, 1]] I = &fixed{name: "I", n: 1, matrix: []complex128{ 1, 0, 0, 1, }} // H is the Hadamard gate. It maps |0> to (|0>+|1>)/sqrt(2) and |1> to // (|0>-|1>)/sqrt(2), creating an equal superposition from a basis state. // It is its own inverse (H^2 = I) and is the starting point for most // quantum algorithms. On the Bloch sphere, H is a 180-degree rotation // around the axis halfway between X and Z. // // (1/sqrt(2)) * [[1, 1], // [1, -1]] H = &fixed{name: "H", n: 1, matrix: []complex128{ complex(s2, 0), complex(s2, 0), complex(s2, 0), complex(-s2, 0), }} // X is the Pauli-X gate (quantum NOT / bit-flip). It swaps |0> and |1>, // equivalent to a 180-degree rotation around the X axis of the Bloch sphere. // Self-inverse: X^2 = I. Together with Y and Z, the Pauli gates form a // basis for all single-qubit operations. // // [[0, 1], // [1, 0]] X = &fixed{name: "X", n: 1, matrix: []complex128{ 0, 1, 1, 0, }} // Y is the Pauli-Y gate. It flips |0> to i|1> and |1> to -i|0>, // combining a bit-flip with a phase-flip. Equivalent to a 180-degree // rotation around the Y axis of the Bloch sphere. Self-inverse: Y^2 = I. // // [[0, -i], // [i, 0]] Y = &fixed{name: "Y", n: 1, matrix: []complex128{ 0, -1i, 1i, 0, }} // Z is the Pauli-Z gate (phase-flip). It leaves |0> unchanged and maps // |1> to -|1>, equivalent to a 180-degree rotation around the Z axis of // the Bloch sphere. Self-inverse: Z^2 = I. // // [[1, 0], // [0, -1]] Z = &fixed{name: "Z", n: 1, matrix: []complex128{ 1, 0, 0, -1, }} // S is the S gate (sqrt-Z / phase gate / P(pi/2)). It applies a // quarter-turn (90 degrees) around the Z axis: |0> -> |0>, |1> -> i|1>. // Two applications give Z: S^2 = Z. Inverse: [Sdg]. // // [[1, 0], // [0, i]] S = &fixed{name: "S", n: 1, matrix: []complex128{ 1, 0, 0, 1i, }} // Sdg is the S-dagger gate (inverse of [S]). It applies a -90 degree // rotation around the Z axis: |0> -> |0>, |1> -> -i|1>. // // [[1, 0], // [0, -i]] Sdg = &fixed{name: "S†", n: 1, matrix: []complex128{ 1, 0, 0, -1i, }} // T is the T gate (pi/8 gate / fourth-root of Z). It applies a pi/4 // (45-degree) phase: |0> -> |0>, |1> -> exp(i*pi/4)|1>. The T gate is // essential for universal quantum computation - the Clifford+T gate set // can approximate any unitary to arbitrary precision (Solovay-Kitaev). // Inverse: [Tdg]. T^2 = S, T^4 = Z. // // [[1, 0], // [0, exp(i*pi/4)]] T = &fixed{name: "T", n: 1, matrix: []complex128{ 1, 0, 0, complex(s2, s2), }} // Tdg is the T-dagger gate (inverse of [T]). It applies a -pi/4 phase. // // [[1, 0], // [0, exp(-i*pi/4)]] Tdg = &fixed{name: "T†", n: 1, matrix: []complex128{ 1, 0, 0, complex(s2, -s2), }} // SX is the sqrt-X gate. It is the square root of the Pauli-X gate: // SX^2 = X. IBM quantum processors use SX as a native gate (alongside // CNOT and RZ), so many transpilation flows decompose into {SX, RZ, CNOT}. // // (1/2) * [[1+i, 1-i], // [1-i, 1+i]] SX = &fixed{name: "SX", n: 1, matrix: []complex128{ complex(0.5, 0.5), complex(0.5, -0.5), complex(0.5, -0.5), complex(0.5, 0.5), }} )
Standard single-qubit gates.
var ( // CNOT (Controlled-NOT / CX) flips the target qubit if and only if the // control qubit is |1>. It is the standard entangling gate: applying // H(q0) then CNOT(q0,q1) to |00> produces the Bell state // (|00>+|11>)/sqrt(2). Self-inverse: CNOT^2 = I. Most hardware platforms // use CNOT (or an equivalent like CZ) as their native two-qubit gate. // // q0: ──●── [[1, 0, 0, 0], // | [0, 1, 0, 0], // q1: ──X── [0, 0, 0, 1], // [0, 0, 1, 0]] CNOT = &fixed{name: "CNOT", n: 2, matrix: []complex128{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, }} // CZ (Controlled-Z) applies a Z gate to the target when the control is |1>, // equivalently flipping the phase of |11> to -|11>. Unlike CNOT, CZ is // symmetric: CZ(q0,q1) = CZ(q1,q0). Self-inverse. Native gate on many // superconducting platforms (Google, Rigetti). // // q0: ──●── [[1, 0, 0, 0], // | [0, 1, 0, 0], // q1: ──Z── [0, 0, 1, 0], // [0, 0, 0, -1]] CZ = &fixed{name: "CZ", n: 2, matrix: []complex128{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, }} // SWAP exchanges the states of two qubits: |01> <-> |10>. Equivalent to // three CNOTs: CNOT(0,1) CNOT(1,0) CNOT(0,1). Self-inverse. The // transpiler inserts SWAPs to route qubits on hardware with limited // connectivity. // // q0: ──X── [[1, 0, 0, 0], // | [0, 0, 1, 0], // q1: ──X── [0, 1, 0, 0], // [0, 0, 0, 1]] SWAP = &fixed{name: "SWAP", n: 2, matrix: []complex128{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, }} // CY (Controlled-Y) applies a Pauli-Y gate to the target when the control // is |1>. Maps |10> to i|11> and |11> to -i|10>. // // [[1, 0, 0, 0], // [0, 1, 0, 0], // [0, 0, 0, -i ], // [0, 0, i, 0]] CY = &fixed{name: "CY", n: 2, matrix: []complex128{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1i, 0, 0, 1i, 0, }} // ISWAP (imaginary SWAP) swaps |01> and |10> with a factor of i: // |01> -> i|10>, |10> -> i|01>. It is the native two-qubit gate on // Google's superconducting processors (with the Sycamore gate being // ISWAP + conditional phase). ISWAP^2 = SWAP (up to global phase). // // [[1, 0, 0, 0], // [0, 0, i, 0], // [0, i, 0, 0], // [0, 0, 0, 1]] ISWAP = &fixed{name: "iSWAP", n: 2, matrix: []complex128{ 1, 0, 0, 0, 0, 0, 1i, 0, 0, 1i, 0, 0, 0, 0, 0, 1, }} // ECR (Echoed Cross-Resonance) is IBM's native two-qubit gate on Eagle // and newer processors. It is equivalent to a CNOT up to single-qubit // rotations and is self-inverse. The cross-resonance interaction drives // one qubit at the frequency of another, creating an entangling ZX // interaction. // // (1/sqrt(2)) * [[0, 0, 1, i], // [0, 0, i, 1], // [1, -i, 0, 0], // [-i, 1, 0, 0]] ECR = &fixed{name: "ECR", n: 2, matrix: []complex128{ 0, 0, complex(s2, 0), complex(0, s2), 0, 0, complex(0, s2), complex(s2, 0), complex(s2, 0), complex(0, -s2), 0, 0, complex(0, -s2), complex(s2, 0), 0, 0, }} // DCX (Double-CNOT) applies CNOT(q0,q1) followed by CNOT(q1,q0). // It is a Clifford gate that swaps |01> -> |11> -> |10> -> |01> // (a 3-cycle on the computational basis states). // // [[1, 0, 0, 0], // [0, 0, 0, 1], // [0, 1, 0, 0], // [0, 0, 1, 0]] DCX = &fixed{name: "DCX", n: 2, matrix: []complex128{ 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, }} // CH (Controlled-Hadamard) applies a Hadamard gate to the target when // the control is |1>. Useful in algorithms that create conditional // superposition. // // [[1, 0, 0, 0 ], // [0, 1, 0, 0 ], // [0, 0, 1/sqrt(2), 1/sqrt(2) ], // [0, 0, 1/sqrt(2), -1/sqrt(2) ]] CH = &fixed{name: "CH", n: 2, matrix: []complex128{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, complex(s2, 0), complex(s2, 0), 0, 0, complex(s2, 0), complex(-s2, 0), }} // CSX (Controlled-sqrt-X) applies a sqrt-X ([SX]) gate to the target // when the control is |1>. // // [[1, 0, 0, 0 ], // [0, 1, 0, 0 ], // [0, 0, (1+i)/2, (1-i)/2 ], // [0, 0, (1-i)/2, (1+i)/2 ]] CSX = &fixed{name: "CSX", n: 2, matrix: []complex128{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, complex(0.5, 0.5), complex(0.5, -0.5), 0, 0, complex(0.5, -0.5), complex(0.5, 0.5), }} )
Standard two-qubit gates.
Convention: q0 is the control (or first operand), q1 is the target (or second operand). The 4x4 matrix is indexed as |q0,q1> where q0 is the MSB: |00>=0, |01>=1, |10>=2, |11>=3.
var ( // CCX (Toffoli gate) flips the target qubit if and only if both controls // are |1>. It is the quantum analog of the classical AND gate (up to // phase) and is universal for reversible classical computation. Decomposed // into 6 CNOTs + single-qubit gates for hardware execution. Self-inverse. // // q0: ──●── // | // q1: ──●── // | // q2: ──X── CCX = &fixed{name: "CCX", n: 3, matrix: []complex128{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, }} // CSWAP (Fredkin gate) swaps q1 and q2 when the control q0 is |1>. // It is a universal reversible gate: any classical computation can be // built from Fredkin gates alone. Self-inverse. // // q0: ──●── // | // q1: ──X── // | // q2: ──X── CSWAP = &fixed{name: "CSWAP", n: 3, matrix: []complex128{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, }} // CCZ (doubly-controlled Z) flips the phase of |111> to -|111> and leaves // all other basis states unchanged. Equivalent to CCX with Hadamards on // the target: H * CCX * H = CCZ. Self-inverse. Symmetric in all three // qubits. CCZ = &fixed{name: "CCZ", n: 3, matrix: []complex128{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -1, }} )
Standard three-qubit gates.
Convention: q0 and q1 are controls, q2 is the target. The 8x8 matrix is indexed as |q0,q1,q2> where q0 is MSB.
var ( // Sycamore is Google's native two-qubit gate, FSim(pi/2, pi/6). It // combines an iSWAP-like interaction (|01> <-> -i|10>) with a conditional // phase exp(-i*pi/6) on |11>. This gate was used in Google's 2019 // quantum computational advantage experiment on the Sycamore processor. // // [[1, 0, 0, 0 ], // [0, 0, -i, 0 ], // [0, -i, 0, 0 ], // [0, 0, 0, exp(-i*pi/6) ]] Sycamore = &fixed{name: "Sycamore", n: 2, matrix: func() []complex128 { phi := math.Pi / 6 return []complex128{ 1, 0, 0, 0, 0, 0, -1i, 0, 0, -1i, 0, 0, 0, 0, 0, cmplx.Exp(complex(0, -phi)), } }()} )
Special gates.
Functions ¶
This section is empty.
Types ¶
type Bindable ¶
type Bindable interface {
Bind(bindings map[string]float64) (Gate, error)
FreeParameters() []string
IsBound() bool
}
Bindable is optionally implemented by gates with symbolic parameters. It enables parameterized/variational circuits.
type ControlDiagonal2Q ¶ added in v1.2.0
type ControlDiagonal2Q interface {
// ControlDiagonal returns the two non-trivial diagonal elements [d10, d11].
ControlDiagonal() (d10, d11 complex128)
}
ControlDiagonal2Q is optionally implemented by 2-qubit controlled gates whose matrix is diag(1, 1, d10, d11) - identity on the |0x> subspace. Examples: CP, CRZ. The simulator only multiplies the |1x> amplitudes.
type ControlU2Q ¶ added in v1.2.0
type ControlU2Q interface {
// ControlSubmatrix returns the 2x2 unitary [u00, u01, u10, u11] applied
// when the control qubit is |1>.
ControlSubmatrix() (u00, u01, u10, u11 complex128)
}
ControlU2Q is optionally implemented by 2-qubit controlled gates whose matrix is I on the |0x> subspace and a 2x2 unitary on the |1x> subspace. Examples: CRX, CRY. The simulator applies only the 2x2 submatrix.
type ControlledGate ¶
ControlledGate is a gate wrapping an inner gate with additional control qubits.
type Delayable ¶ added in v1.2.0
type Delayable interface {
Duration() float64 // raw numeric duration value
Unit() string // time unit: "ns", "us", "ms", "s", "dt"
Seconds() float64 // duration converted to seconds (panics for "dt")
}
Delayable is optionally implemented by delay instructions to expose duration metadata beyond what Params() provides.
type Diagonal2Q ¶ added in v1.2.0
type Diagonal2Q interface {
// Diagonal returns the 4 diagonal elements [d00, d01, d10, d11].
Diagonal() (d00, d01, d10, d11 complex128)
}
Diagonal2Q is optionally implemented by 2-qubit gates whose matrix is fully diagonal (only diagonal elements are non-zero, e.g. RZZ). The simulator applies only phase factors, avoiding full 4x4 matrix multiplication.
type Gate ¶
type Gate interface {
// Name returns the canonical gate name (e.g., "H", "CNOT", "RZ").
Name() string
// Qubits returns the number of qubits this gate acts on.
Qubits() int
// Matrix returns the unitary matrix as a flat row-major slice.
// Length is (2^n)^2 where n = Qubits().
Matrix() []complex128
// Params returns gate parameters (rotation angles, etc.).
// Returns nil for non-parameterized gates.
Params() []float64
// Inverse returns the adjoint (inverse) of this gate.
Inverse() Gate
// Decompose breaks this gate into a sequence of simpler gates
// targeting the given qubit indices. Returns nil if already primitive.
Decompose(qubits []int) []Applied
}
Gate represents a quantum gate operation.
var Reset Gate = resetGate{}
Reset is a pseudo-gate that resets a qubit to |0⟩. It has no matrix representation - simulators handle it directly.
func Barrier ¶ added in v1.2.0
Barrier returns a barrier pseudo-gate spanning n qubits. Barriers have no matrix representation - they prevent gate reordering across them during transpilation. Simulators skip them.
func CP ¶
CP returns a controlled-phase gate: diag(1, 1, 1, exp(iφ)). It applies a phase exp(iφ) to the |11> state and leaves all others unchanged. Symmetric in q0,q1. At φ=π, CP equals CZ. Used in QFT (Quantum Fourier Transform) with φ=2π/2^k for increasing k.
func Controlled ¶
Controlled wraps inner with nControls control qubits. For well-known cases it returns existing singletons: Controlled(X,1)=CNOT, Controlled(X,2)=CCX, Controlled(Z,1)=CZ.
func Delay ¶ added in v1.2.0
Delay returns a delay pseudo-gate that idles a qubit for the given duration. It has no matrix representation - simulators skip it (or apply decoherence noise in noisy simulation). The unit should be one of the Unit* constants.
func FSim ¶ added in v1.2.0
FSim returns the fermionic simulation gate. It models the interaction between two fermionic modes with a hopping term (theta) and a conditional phase (phi). FSim is the native interaction on superconducting processors with tunable couplers (Google Sycamore). Special cases: FSim(pi/2, 0) = iSWAP; FSim(pi/2, pi/6) = Sycamore gate; FSim(0, phi) = CP(-phi).
[[1, 0, 0, 0], [0, cos θ, -i·sin θ, 0], [0, -i·sin θ, cos θ, 0], [0, 0, 0, exp(-iφ)]]
func GPI ¶
GPI returns an IonQ native GPI gate. On trapped-ion hardware, GPI implements a pi rotation (180 degrees) around an axis in the XY plane of the Bloch sphere at azimuthal angle phi. Together with GPI2 and MS, the GPI gate forms a universal native gate set for IonQ processors.
[[0, exp(-iφ)], [exp(iφ), 0]]
func GPI2 ¶
GPI2 returns an IonQ native GPI2 gate. It implements a pi/2 rotation (90 degrees) around an axis in the XY plane at azimuthal angle phi. GPI2 is the "half gate" counterpart to GPI and is used to build arbitrary single-qubit rotations on IonQ hardware.
(1/√2) * [[1, -i·exp(-iφ)],
[-i·exp(iφ), 1]]
func GlobalPhase ¶ added in v1.2.0
GlobalPhase returns a gate applying scalar phase e^(iφ) to a single qubit.
e^(iφ)·I = [[e^(iφ), 0], [0, e^(iφ)]]
func MS ¶
MS returns an IonQ native Mølmer-Sørensen gate. The MS gate generates entanglement between two trapped ions by driving both with lasers that couple through their shared motional mode. It is the native two-qubit gate on IonQ processors and is equivalent to RXX(pi/2) up to local rotations when phi0=phi1=0.
func MustStatePrep ¶
func MustStatePrep(amplitudes []complex128) Gate
MustStatePrep creates a state preparation gate, panicking on error.
func MustUnitary ¶
func MustUnitary(name string, matrix []complex128) Gate
MustUnitary is like Unitary but panics on error.
func NOP ¶ added in v1.2.0
NOP returns an IonQ native NOP (no-operation) timing gate. It is a 0-qubit instruction analogous to Delay; time is in microseconds. On IonQ hardware, NOP inserts an idle period used for timing synchronization between qubits.
func PSwap ¶ added in v1.2.0
PSwap returns the parameterized SWAP gate.
[[1, 0, 0, 0], [0, 0, exp(iφ), 0], [0, exp(iφ), 0, 0], [0, 0, 0, 1]]
func PhasedXZ ¶ added in v1.2.0
PhasedXZ returns the Cirq-style PhasedXZ gate: Z^z · P^a · X^x · (P^a)†. This is Cirq's canonical single-qubit representation: any single-qubit unitary can be written as PhasedXZ (up to global phase) using at most 3 half-turn parameters. Parameters are in half-turns (1.0 = 180 degrees). Z^z = diag(1, e^{iπz}), P^a = diag(1, e^{iπa}), X^x has matrix [[cos(πx/2), i·sin(πx/2)], [i·sin(πx/2), cos(πx/2)]].
func Pow ¶
Pow returns g raised to the integer power k. k=0 returns an identity gate, k=1 returns g unchanged, k>1 multiplies the matrix repeatedly, k<0 uses the inverse.
func RX ¶
RX returns an X-rotation gate: exp(-i * theta/2 * X). It rotates the qubit state by angle theta around the X axis of the Bloch sphere. At theta=pi, RX equals -i*X (a bit-flip with global phase). At theta=pi/2, it creates a superposition equivalent to H up to phase.
[[cos(θ/2), -i·sin(θ/2)], [-i·sin(θ/2), cos(θ/2)]]
func RXX ¶
RXX returns the Ising XX coupling gate: exp(-i * theta/2 * X⊗X). It models a two-qubit interaction where both qubits rotate simultaneously around the X axis. Native on trapped-ion platforms (IonQ) where the Mølmer-Sørensen interaction naturally produces XX coupling.
c = cos(θ/2), s = sin(θ/2) [[c, 0, 0, -is], [0, c, -is, 0], [0, -is, c, 0], [-is, 0, 0, c]]
func RY ¶
RY returns a Y-rotation gate: exp(-i * theta/2 * Y). It rotates the qubit state by angle theta around the Y axis of the Bloch sphere. Unlike RX and RZ, RY has only real entries, making it useful for state preparation (converting amplitudes between |0> and |1> without introducing phase).
[[cos(θ/2), -sin(θ/2)], [sin(θ/2), cos(θ/2)]]
func RYY ¶
RYY returns the Ising YY coupling gate: exp(-i * theta/2 * Y⊗Y). It models a two-qubit YY interaction, used in variational ansatze and Hamiltonian simulation for systems with YY coupling terms.
c = cos(θ/2), s = sin(θ/2) [[c, 0, 0, is], [0, c, -is, 0], [0, -is, c, 0], [is, 0, 0, c]]
func RZ ¶
RZ returns a Z-rotation gate: exp(-i * theta/2 * Z). It rotates the qubit state by angle theta around the Z axis of the Bloch sphere (pure phase rotation). RZ is diagonal and commutes with measurement in the computational basis. On IBM hardware, RZ is implemented as a virtual gate (frame change) with zero error, making it a preferred building block.
[[exp(-iθ/2), 0], [0, exp(iθ/2)]]
func RZZ ¶
RZZ returns the Ising ZZ coupling gate: exp(-i * theta/2 * Z⊗Z). It is fully diagonal, applying phases based on the parity of the two qubits. Appears naturally in Hamiltonian simulation of spin chains and in QAOA for combinatorial optimization (encoding problem clauses). Since it is diagonal, it commutes with measurements in the computational basis.
[[exp(-iθ/2), 0, 0, 0], [0, exp(iθ/2), 0, 0], [0, 0, exp(iθ/2), 0], [0, 0, 0, exp(-iθ/2)]]
func Rot ¶ added in v1.2.0
Rot returns the PennyLane-style rotation gate RZ(ω)·RY(θ)·RZ(φ). This is the ZYZ Euler decomposition: any single-qubit unitary can be expressed as three successive rotations RZ, RY, RZ (up to global phase). This is PennyLane's default parameterization for single-qubit rotations.
func StatePrep ¶
func StatePrep(amplitudes []complex128) (Gate, error)
StatePrep creates a state preparation gate from normalized amplitudes. The length of amplitudes must be a power of 2 (2^n for n qubits). The amplitudes must be normalized (sum of |a_i|^2 = 1).
func U1 ¶ added in v1.2.0
U1 returns a phase gate, equivalent to Phase. This is the Qiskit naming convention: U1(λ) = Phase(λ) = RZ(λ) up to global phase.
diag(1, exp(iλ))
func U2 ¶ added in v1.2.0
U2 returns the single-qubit gate U3(π/2, φ, λ). It always creates a superposition (theta is fixed at π/2) with tunable phases. U2(0, π) = H up to global phase.
(1/√2)·[[1, -exp(iλ)], [exp(iφ), exp(i(φ+λ))]]
func U3 ¶
U3 returns the universal single-qubit gate U(θ, φ, λ). Any single-qubit unitary can be expressed as U3 (up to global phase), making it the most general single-qubit rotation. Special cases: U3(θ,0,0) = RY(θ) up to phase; U3(0,0,λ) = Phase(λ) up to phase.
[[cos(θ/2), -exp(iλ)·sin(θ/2)], [exp(iφ)·sin(θ/2), exp(i(φ+λ))·cos(θ/2)]]
func Unitary ¶
func Unitary(name string, matrix []complex128) (Gate, error)
Unitary creates a gate from a user-provided unitary matrix. The name is used for display. The matrix must be 2x2, 4x4, or 8x8 (flat row-major []complex128 of length 4, 16, or 64). Returns an error if the matrix is not unitary (U†U ≈ I within 1e-10 tolerance).
type StatePrepable ¶
type StatePrepable interface {
Amplitudes() []complex128
}
StatePrepable is implemented by gates that prepare a specific quantum state.