QState represents a quantum state vector. It stores a vector of Complex amplitudes and provides utilities for normalization, conjugation, and inner products.
import { QState } from "@masabando/quantum-gates"Create a state from an array of numbers or Complex values. By default, the state is treated as a column vector.
import { QState, Complex } from "@masabando/quantum-gates"const ket0 = new QState([1, 0])const ket1 = new QState([0, 1])const psi = new QState([ new Complex(1 / Math.sqrt(2), 0), new Complex(0, 1 / Math.sqrt(2)),])QState can represent either a column vector (|ψ⟩) or a row vector (⟨ψ|). Many operations require the correct orientation.
const ket = new QState([1, 0], true) // column (default)const bra = ket.dagger() // rowUse scale to multiply amplitudes by a scalar.magnitude returns the vector norm, and normalize returns a normalized state.
const s = new QState([2, 0])console.log(s.magnitude())const sn = s.normalize()const half = sn.scale(0.5)These methods return new states.
transpose(): swaps row and column orientationconjugate(): element-wise complex conjugationdagger(): conjugate transpose (conjugate().transpose())const bra = ket.dagger()const ket2 = bra.dagger()Compute an inner product ⟨ψ|φ⟩ using applyState. The left operand must be a row vector and the right operand must be a column vector.
const psi = new QState([1, 0])const phi = new QState([0, 1])const inner = psi.dagger().applyState(phi)console.log(inner.toString())Apply a matrix to a state using applyMatrix. This operation follows the orientation rules used in the implementation.
// row vector × matrixconst braNext = bra.applyMatrix(matrix)For a single-qubit state (column vector), xyz returns the Bloch sphere coordinates computed from Pauli expectations.
const psi = new QState([1, 0])const { x, y, z } = psi.xyzconsole.log(x, y, z)QState does not automatically normalize the vector.