q5m.js - Quantum Computing Library - v0.1.1
    Preparing search index...

    Class QubitState<TQubit>

    Represents a pure quantum state of qubits with memory optimization and integrated single-qubit operations.

    QubitState is the unified quantum state representation in q5m.js, supporting both single qubits and arbitrary multi-qubit systems with automatic optimization for large systems. It provides both the functionality of the original Qubit class for single-qubit operations and the multi-qubit capabilities.

    State Representation: |ψ⟩ = Σᵢ αᵢ|i⟩ where |i⟩ are computational basis states and Σᵢ|αᵢ|² = 1

    Memory Strategies:

    • Dense representation: Full quantum amplitude vector for small systems (<12 qubits)
    • Sparse representation: Map-based storage for large or sparse systems
    • Automatic switching based on sparsity and system size

    Single Qubit Features:

    • Static factory methods for common states (|0⟩, |1⟩, |+⟩, |−⟩)
    • Bloch sphere parameterization
    • Direct amplitude specification
    • Measurement and state collapse
    • Phase-aware state comparison

    Computational Basis Ordering (Big-Endian): For n qubits, basis states are ordered as |q_{n-1} ... q_1 q_0⟩. The index i of the state vector corresponds to the integer value of the bit string.

    Type Parameters

    Hierarchy (View Summary)

    Implements

    Index

    Accessors

    • get isDense(): boolean

      Flag indicating whether dense or sparse representation is currently used (backward compatibility)

      Returns boolean

    Constructors

    • Creates a QubitState with the specified number of qubits.

      The constructor automatically selects the internal representation (dense or sparse) based on the system size and the provided state vector's sparsity. For systems with 8 or more qubits, sparse representation is enabled by default to conserve memory.

      Type Parameters

      Parameters

      • numQubits: number

        Number of qubits in the system. Must be a positive integer.

      • OptionalstateVector: Complex[]

        Optional initial state vector of complex amplitudes. If not provided, defaults to the |0...0⟩ state. The length must be 2^numQubits.

      • OptionaloptimizeMemory: boolean

        Manually enable or disable sparse representation. Defaults to true for 8 or more qubits.

      • Optionalmaterial: TQubit

      Returns QubitState<TQubit>

      If numQubits is not a positive integer.

      If stateVector is provided and its length is not equal to 2^numQubits.

    Methods

    • Returns the full array of quantum amplitudes of the quantum state. Uses memoization to reduce memory allocations for repeated access.

      Returns Complex[]

      Array of quantum amplitudes.

      Do not modify the returned array directly.

    • Returns the probability of measuring a specific computational basis state.

      Parameters

      • basisIndex: number

      Returns number

    • Determine if CSR representation should be used.

      Parameters

      • size: number
      • sparsity: number
      • nonZeroCount: number
      • sparseMemory: number
      • csrMemory: number

      Returns boolean

    • Protected

      Standard dense matrix-vector multiplication. Uses optimized implementation from math/vector-matrix.ts

      Parameters

      • unitaryMatrix: Matrix

        Dense unitary matrix

      Returns Complex[]

      New state vector after multiplication

    • Protected

      Sparse matrix-vector multiplication for sparse Map representation. Uses optimized implementation from math/vector-matrix.ts

      Parameters

      • unitaryMatrix: Matrix

        Dense unitary matrix

      Returns Complex[]

      New state vector after multiplication

    • Creates a QubitState representing a specific computational basis state.

      Parameters

      • numQubits: number

        Number of qubits in the system

      • basisIndex: number

        Index of the basis state (0 to 2^numQubits - 1)

      Returns QubitState

      New QubitState in the specified basis state

      If basisIndex is out of range

    • Creates a QubitState from a binary string representation.

      Parameters

      • bitString: string

        Binary string (e.g., "101" for |101⟩)

      Returns QubitState

      New QubitState representing the specified basis state

      If bitString contains non-binary characters

    • Returns the quantum amplitude for a specific basis state. Implementation of Q5mState.amplitude() method.

      Parameters

      • basisIndex: number

        Index of the computational basis state

      Returns Complex

      Quantum amplitude for the specified basis state

      If basisIndex is out of range

    • Calculates the state space dimension for the given number of quantum units. For qubits (2-level systems): 2^n.

      Parameters

      • numQubits: number

      Returns number

    • Calculates the trace distance between this quantum state and another.

      Trace distance is a metric for distinguishing quantum states, ranging from 0 to 1. For pure states, D(|ψ⟩, |φ⟩) = √(1 - |⟨ψ|φ⟩|²)

      Parameters

      • other: QubitState

        The other quantum state to compare with

      Returns number

      The trace distance between 0 (identical) and 1 (maximally different)

      If the states have different dimensions

    • Calculates the inner product (overlap) between this quantum state and another.

      The overlap ⟨ψ|φ⟩ is the complex inner product between two state vectors.

      Parameters

      • other: QubitState

        The other quantum state to calculate overlap with

      Returns Complex

      The complex overlap value ⟨this|other⟩

      If the states have different dimensions

    • Checks if this quantum state is approximately equal to another.

      Two states are considered equal if their fidelity is sufficiently close to 1.

      Parameters

      • other: QubitState

        The other quantum state to compare with

      • tolerance: number = 1e-10

        The tolerance for comparison (default: 1e-10)

      Returns boolean

      True if the states are approximately equal

    • Returns a string representation of the quantum state.

      Parameters

      • precision: number = 3

        Decimal precision for amplitudes.

      • threshold: number = 1e-10

        Minimum amplitude magnitude to display.

      Returns string

    • Calculates the purity of the quantum state. For pure states (which QubitState represents), purity is always 1.

      Returns number

    • Checks if this quantum system represents a pure state. QubitState always represents pure states, so this always returns true.

      Returns boolean

    • Computes the von Neumann entropy of the quantum state. For pure states (which QubitState represents), the entropy is always 0.

      Returns number

    • Creates a QubitState from Bloch sphere angles.

      Parameters

      • theta: number

        Polar angle (0 to π)

      • phi: number = 0

        Azimuthal angle (0 to 2π)

      Returns QubitState

      New QubitState instance corresponding to the specified angles

    • Performs a projective measurement on a single qubit in the computational basis. This operation is stochastic and collapses the qubit's state to either |0⟩ or |1⟩.

      Returns ZeroOne

      The measurement outcome, either 0 or 1

      If this state is not a single-qubit state

    Properties

    stateCount: number

    The total number of basis states in the Hilbert space (its dimension).

    numQuantum: number

    The number of quantum units (e.g., qubits, qutrits) in this state.

    material: TQubit

    The quantum material associated with this state.

    stateVector?: Complex[]

    The state vector containing quantum amplitudes for each basis state. Optional for sparse representations.

    sparseAmplitudes?: Map<number, Complex>

    Sparse representation using Map for memory optimization

    csrData?: CSRFormat

    CSR (Compressed Sparse Row) representation for large sparse states

    optimizedCSRData?: OptimizedCSRFormat

    Optimized CSR representation using TypedArrays

    rep: RepType = RepType.DENSE

    Current representation type

    sparseConfig: SparseConfig = DEFAULT_SPARSE_CONFIG

    Configuration for sparse optimization