Skip to main content
The vector package provides the Vector type — a dense slice of complex128 values used to represent quantum state amplitudes. The q simulator builds its state vector from this type and applies gate matrices to it via Apply.
Most users interact with qubits through the Q simulator rather than the Vector type directly. You only need this package when working with low-level state manipulation or building utilities on top of the simulator internals.
Import path
import "github.com/itsubaki/q/math/vector"

Creating vectors

New

func New(z ...complex128) *Vector
Returns a new Vector from the given complex128 values.
v := vector.New(1+0i, 0+0i) // |0⟩ basis state
w := vector.New(0+0i, 1+0i) // |1⟩ basis state

Zero

func Zero(n int) *Vector
Returns a vector of length n with all elements set to 0+0i.
v := vector.Zero(4) // 4-element zero vector

Methods

Clone

func (v *Vector) Clone() *Vector
Returns a deep copy of v. Modifying the clone does not affect the original.
copy := v.Clone()

Dual

func (v *Vector) Dual() *Vector
Returns the dual (conjugate) vector — every element z becomes conj(z). Used internally when computing inner and outer products.

Mul

func (v *Vector) Mul(z complex128) *Vector
Returns a new vector with every element scaled by z.
scaled := v.Mul(0.5 + 0i)

Apply

func (v *Vector) Apply(m *matrix.Matrix) *Vector
Returns the matrix-vector product m|v⟩. This is how quantum gates are applied to a state vector.
// Apply the Hadamard matrix to |0⟩
result := v.Apply(h)

InnerProduct

func (v *Vector) InnerProduct(w *Vector) complex128
Returns ⟨w|v⟩ — the inner product of v with w. Computed as Σ vᵢ · conj(wᵢ).
// ⟨0|0⟩ = 1
zero := vector.New(1+0i, 0+0i)
zero.InnerProduct(zero) // (1+0i)

OuterProduct

func (v *Vector) OuterProduct(w *Vector) *matrix.Matrix
Returns |v⟩⟨w| — the outer product of v and w as a *matrix.Matrix. The result has len(v.Data) rows and len(w.Data) columns.
// |0⟩⟨0| projector
zero := vector.New(1+0i, 0+0i)
proj := zero.OuterProduct(zero)

TensorProduct

func (v *Vector) TensorProduct(w *Vector) *Vector
Returns the tensor (Kronecker) product v ⊗ w. For vectors of lengths p and q, the result has length p·q.
// |0⟩ ⊗ |1⟩ = |01⟩
zero := vector.New(1+0i, 0+0i)
one  := vector.New(0+0i, 1+0i)
zero.TensorProduct(one) // [0, 1, 0, 0]

Equal

func (v *Vector) Equal(w *Vector, tol ...float64) bool
Returns true if v and w have the same length and every pair of elements is within tolerance. Default tolerances come from the epsilon package (AbsTol = 1e-8, RelTol = 1e-5).
v.Equal(w)       // default tolerance
v.Equal(w, 1e-6) // custom absolute tolerance

Real / Imag

func (v *Vector) Real() []float64
func (v *Vector) Imag() []float64
Return the real and imaginary parts of each element as []float64.
v := vector.New(1+2i, 3+4i)
v.Real() // [1, 3]
v.Imag() // [2, 4]

String

func (v *Vector) String() string
Returns a string representation of the underlying data slice.
fmt.Println(v.String()) // [(1+0i) (0+0i)]

Package-level helpers

TensorProductN

func TensorProductN(v *Vector, n ...int) *Vector
Returns v ⊗ v ⊗ ... ⊗ v (n times). If n is omitted, returns v unchanged.
// |0⟩ ⊗ |0⟩ ⊗ |0⟩
zero := vector.New(1+0i, 0+0i)
zero3 := vector.TensorProductN(zero, 3) // length 8
OperationCallResult typeMeaning
Inner productv.InnerProduct(w)complex128⟨w|v⟩ — scalar overlap
Outer productv.OuterProduct(w)*matrix.Matrix|v⟩⟨w| — rank-1 matrix
The inner product collapses two vectors to a scalar; the outer product expands them into a matrix. For a normalised state |ψ⟩, ψ.OuterProduct(ψ) gives the density matrix of the pure state.
The q simulator stores the full quantum state as a Vector internally. When you call qsim.H(q0) or any other gate method, the simulator constructs the appropriate matrix and calls Apply on the state vector.You can inspect the amplitudes after a simulation run through qsim.State() rather than accessing the vector directly.

Build docs developers (and LLMs) love