pub struct QuantumState { /* private fields */ }Expand description
Quantum state represented as a state vector of 2^n complex amplitudes.
Implementations§
Source§impl QuantumState
impl QuantumState
Sourcepub fn new_with_seed(num_qubits: u32, seed: u64) -> Result<Self>
pub fn new_with_seed(num_qubits: u32, seed: u64) -> Result<Self>
Create the |00…0> state with a deterministic seed for reproducibility.
Sourcepub fn from_amplitudes(amps: Vec<Complex>, num_qubits: u32) -> Result<Self>
pub fn from_amplitudes(amps: Vec<Complex>, num_qubits: u32) -> Result<Self>
Construct a state from an explicit amplitude vector.
Validates that amps.len() == 2^num_qubits.
pub fn num_qubits(&self) -> u32
pub fn num_amplitudes(&self) -> usize
pub fn state_vector(&self) -> &[Complex]
Sourcepub fn amplitudes_mut(&mut self) -> &mut [Complex]
pub fn amplitudes_mut(&mut self) -> &mut [Complex]
Get mutable access to the raw amplitude array.
§Safety
Caller must maintain normalisation after mutation.
Sourcepub fn probabilities(&self) -> Vec<f64>
pub fn probabilities(&self) -> Vec<f64>
|amplitude|^2 for each basis state.
Sourcepub fn probability_of_qubit(&self, qubit: QubitIndex) -> f64
pub fn probability_of_qubit(&self, qubit: QubitIndex) -> f64
Probability that qubit is in state |1>.
pub fn measurement_record(&self) -> &[MeasurementOutcome]
Sourcepub fn estimate_memory(num_qubits: u32) -> usize
pub fn estimate_memory(num_qubits: u32) -> usize
Estimated memory (in bytes) needed for a state of num_qubits qubits.
Sourcepub fn apply_gate(&mut self, gate: &Gate) -> Result<Vec<MeasurementOutcome>>
pub fn apply_gate(&mut self, gate: &Gate) -> Result<Vec<MeasurementOutcome>>
Apply a gate to the state, returning any measurement outcomes.
Sourcepub fn apply_single_qubit_gate(
&mut self,
qubit: QubitIndex,
matrix: &[[Complex; 2]; 2],
)
pub fn apply_single_qubit_gate( &mut self, qubit: QubitIndex, matrix: &[[Complex; 2]; 2], )
Apply a 2x2 unitary matrix to the given qubit.
For each pair of amplitudes where the qubit bit is 0 (index i)
versus 1 (index j = i + step), we apply the matrix transformation.
Sourcepub fn apply_two_qubit_gate(
&mut self,
q1: QubitIndex,
q2: QubitIndex,
matrix: &[[Complex; 4]; 4],
)
pub fn apply_two_qubit_gate( &mut self, q1: QubitIndex, q2: QubitIndex, matrix: &[[Complex; 4]; 4], )
Apply a 4x4 unitary matrix to qubits q1 and q2.
Matrix row/column index = q1_bit * 2 + q2_bit.
Sourcepub fn measure(&mut self, qubit: QubitIndex) -> Result<MeasurementOutcome>
pub fn measure(&mut self, qubit: QubitIndex) -> Result<MeasurementOutcome>
Measure a single qubit projectively.
- Compute P(qubit = 0).
- Sample the outcome from the distribution.
- Collapse the state vector (zero out the other branch).
- Renormalise.
Sourcepub fn measure_all(&mut self) -> Result<Vec<MeasurementOutcome>>
pub fn measure_all(&mut self) -> Result<Vec<MeasurementOutcome>>
Measure all qubits sequentially (qubit 0 first).
Sourcepub fn reset_qubit(&mut self, qubit: QubitIndex) -> Result<()>
pub fn reset_qubit(&mut self, qubit: QubitIndex) -> Result<()>
Reset a qubit to |0>.
Implemented as “measure, then flip if result was |1>”.
Sourcepub fn expectation_value(&self, pauli: &PauliString) -> f64
pub fn expectation_value(&self, pauli: &PauliString) -> f64
Compute <psi| P |psi> for a Pauli string P.
For each basis state |i>, we compute P|i> = phase * |j>, then accumulate conj(amp[j]) * phase * amp[i].
Sourcepub fn expectation_hamiltonian(&self, h: &Hamiltonian) -> f64
pub fn expectation_hamiltonian(&self, h: &Hamiltonian) -> f64
Compute <psi| H |psi> for a Hamiltonian H = sum_k c_k P_k.
Sourcepub fn fidelity(&self, other: &QuantumState) -> f64
pub fn fidelity(&self, other: &QuantumState) -> f64
State fidelity: |<self|other>|^2.