Class StateVector

java.lang.Object
com.quantum4j.core.math.StateVector

public final class StateVector extends Object
Mutable representation of a quantum state vector.

\n * A state vector represents the amplitudes of all computational basis states for a system of n qubits. The dimension is 2^n and amplitudes are stored as complex numbers. Initially, all states are in |00...0⟩.

  • Constructor Details

    • StateVector

      public StateVector(int numQubits)
      Create a new state vector with the specified number of qubits.

      The state is initialized to |00...0⟩ (all zeros).

      Parameters:
      numQubits - number of qubits (must be ≥ 1)
      Throws:
      IllegalArgumentException - if numQubits ≤ 0
  • Method Details

    • getNumQubits

      public int getNumQubits()
      Get the number of qubits.
      Returns:
      the qubit count
    • dimension

      public int dimension()
      Get the dimension of the state vector (2^numQubits).
      Returns:
      the state vector dimension
    • getAmplitudes

      public Complex[] getAmplitudes()
      Get the amplitude array (for direct manipulation by gates).
      Returns:
      the complex amplitude array
    • copy

      public StateVector copy()
      Create a deep copy of this state vector.
      Returns:
      a new StateVector with the same amplitudes
    • measureAll

      public String measureAll()
      Measure all qubits in the computational basis.

      Collapses the state to the measured outcome (all amplitudes except the chosen basis state are set to zero). Returns the outcome as a bitstring.

      Returns:
      the measurement outcome as a bitstring (e.g., "110")
      Throws:
      IllegalStateException - if the state vector has zero norm
    • measureOne

      public int measureOne(int qubit)
      Measure a single qubit in the computational basis.

      Collapses the qubit to the measured outcome (0 or 1) and updates the amplitudes accordingly. Other qubits are not affected by the measurement.

      Parameters:
      qubit - the qubit index to measure
      Returns:
      0 or 1, the measurement outcome
      Throws:
      IllegalArgumentException - if qubit index is out of range