matrix package provides the Matrix type — a dense complex128 matrix used throughout q to represent quantum gates. Every gate in the gate package returns a *matrix.Matrix, and the simulator applies it to the state vector under the hood.
matrix.Matrix is the fundamental building block for all quantum gates. When you call qsim.H(q0), the simulator constructs the Hadamard matrix internally and applies it to the state vector. You only need to work with this package directly when building custom gates.Creating matrices
New
Zero
0+0i.
ZeroLike
m. Useful when building a result matrix in a loop.
Identity
size×size identity matrix.
Element access
At
i, column j (zero-indexed).
Row
i as a slice. The slice shares the underlying array with the matrix.
Set
(i, j) to z.
AddAt, SubAt, MulAt, DivAt
(i, j).
Dim
Seq2
(index, row) pairs, suitable for use with range in Go 1.23+.
Arithmetic
MatMul (method)
m × n. For two n×n matrices this is the standard matrix multiplication.
Apply (method)
n × m. The ordering is intentionally reversed so you can chain gate applications in the natural left-to-right reading order:
Mul
z × m.
Add, Sub
Package-level functions
MatMul
MatMul(A, B, C) computes A·B·C.
Apply
Apply(A, B, C) computes C·B·A — equivalent to applying A first, then B, then C.
ApplyN
m to itself n times. If n is 0, returns the identity matrix.
Tensor product
TensorProduct (method)
m and n. For an a×b matrix and a c×d matrix, the result is (a·c)×(b·d).
TensorProduct (package-level)
TensorProductN
m ⊗ m ⊗ ... ⊗ m (n times). If n is omitted, returns m unchanged.
Transformations
Transpose
m.
Conjugate
m.
Dagger
m, also written m†. For a unitary gate U, U.Dagger() is its inverse.
Inverse
m computed via Gauss-Jordan elimination. For unitary matrices, Dagger() is equivalent and faster.
Swap
i and j swapped. Used internally by Inverse.
Clone
m. Modifying the clone does not affect the original.
Properties
IsSquare
true if the matrix has equal rows and columns.
IsIdentity
true if m is a square identity matrix, within tolerance.
IsHermitian
true if m == m† (self-adjoint). Hermitian matrices have real eigenvalues, making them suitable as quantum observables.
IsUnitary
true if m·m† = I. All valid quantum gates are unitary.
Equal
true if m and n have the same dimensions and all elements are within tolerance. Default tolerances come from the epsilon package (AbsTol = 1e-8, RelTol = 1e-5).
Trace
Decomposition helpers
Real, Imag
[][]float64.
Commutators
Commutator
[m, n] = m·n - n·m.
AntiCommutator
{m, n} = m·n + n·m.
Building custom gates
Usematrix.New to define any unitary as a gate. Pass it to the simulator via qsim.G():
Tensor product for multi-qubit gates
Tensor product for multi-qubit gates
To apply a single-qubit gate to one qubit in an n-qubit register while leaving the others unchanged, tensor the gate with identity matrices:The simulator’s high-level gate methods do this automatically. Use
TensorProduct directly only when constructing composite gates manually.Apply vs MatMul ordering
Apply vs MatMul ordering
The two multiplication helpers have opposite argument order:
| Call | Result |
|---|---|
matrix.MatMul(A, B, C) | A·B·C |
matrix.Apply(A, B, C) | C·B·A |
MatMul follows standard mathematical matrix multiplication order.
Apply follows the circuit diagram reading order: A is applied first, C last.