Package com.quantum4j.core.math
Class StateVector
java.lang.Object
com.quantum4j.core.math.StateVector
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 Summary
ConstructorsConstructorDescriptionStateVector(int numQubits) Create a new state vector with the specified number of qubits. -
Method Summary
Modifier and TypeMethodDescriptioncopy()Create a deep copy of this state vector.intGet the dimension of the state vector (2^numQubits).Complex[]Get the amplitude array (for direct manipulation by gates).intGet the number of qubits.Measure all qubits in the computational basis.intmeasureOne(int qubit) Measure a single qubit in the computational basis.
-
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
Get the amplitude array (for direct manipulation by gates).- Returns:
- the complex amplitude array
-
copy
Create a deep copy of this state vector.- Returns:
- a new StateVector with the same amplitudes
-
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
-