Skip to main content

QuantumState

Struct QuantumState 

Source
pub struct QuantumState { /* private fields */ }
Expand description

Quantum state represented as a state vector of 2^n complex amplitudes.

Implementations§

Source§

impl QuantumState

Source

pub fn new(num_qubits: u32) -> Result<Self>

Create the |00…0> state for num_qubits qubits.

Source

pub fn new_with_seed(num_qubits: u32, seed: u64) -> Result<Self>

Create the |00…0> state with a deterministic seed for reproducibility.

Source

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.

Source

pub fn num_qubits(&self) -> u32

Source

pub fn num_amplitudes(&self) -> usize

Source

pub fn state_vector(&self) -> &[Complex]

Source

pub fn amplitudes_mut(&mut self) -> &mut [Complex]

Get mutable access to the raw amplitude array.

§Safety

Caller must maintain normalisation after mutation.

Source

pub fn probabilities(&self) -> Vec<f64>

|amplitude|^2 for each basis state.

Source

pub fn probability_of_qubit(&self, qubit: QubitIndex) -> f64

Probability that qubit is in state |1>.

Source

pub fn measurement_record(&self) -> &[MeasurementOutcome]

Source

pub fn estimate_memory(num_qubits: u32) -> usize

Estimated memory (in bytes) needed for a state of num_qubits qubits.

Source

pub fn apply_gate(&mut self, gate: &Gate) -> Result<Vec<MeasurementOutcome>>

Apply a gate to the state, returning any measurement outcomes.

Source

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.

Source

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.

Source

pub fn measure(&mut self, qubit: QubitIndex) -> Result<MeasurementOutcome>

Measure a single qubit projectively.

  1. Compute P(qubit = 0).
  2. Sample the outcome from the distribution.
  3. Collapse the state vector (zero out the other branch).
  4. Renormalise.
Source

pub fn measure_all(&mut self) -> Result<Vec<MeasurementOutcome>>

Measure all qubits sequentially (qubit 0 first).

Source

pub fn reset_qubit(&mut self, qubit: QubitIndex) -> Result<()>

Reset a qubit to |0>.

Implemented as “measure, then flip if result was |1>”.

Source

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].

Source

pub fn expectation_hamiltonian(&self, h: &Hamiltonian) -> f64

Compute <psi| H |psi> for a Hamiltonian H = sum_k c_k P_k.

Source

pub fn normalize(&mut self)

Renormalise the state vector so that sum |a_i|^2 = 1.

Source

pub fn fidelity(&self, other: &QuantumState) -> f64

State fidelity: |<self|other>|^2.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V