Skip to main content

SparseStateVector

Struct SparseStateVector 

Source
pub struct SparseStateVector {
    pub n_qubits: usize,
    /* private fields */
}
Expand description

A sparse pure state: only nonzero basis amplitudes are stored.

Fields§

§n_qubits: usize

Number of qubits.

Implementations§

Source§

impl SparseStateVector

Source

pub fn new_zero_state(n_qubits: usize) -> QuantumResult<Self>

Construct the sparse |0…0⟩ state (a single populated key).

§Errors

Returns QuantumError::InvalidQubitCount for n_qubits == 0 or n_qubits > 30.

Source

pub fn from_dense(sv: &StateVector) -> Self

Build a sparse representation from a dense state vector (drops zeros).

Source

pub fn to_dense(&self) -> QuantumResult<StateVector>

Expand back to a dense StateVector.

§Errors

Propagates the dense constructor’s qubit-count validation.

Source

pub fn occupancy(&self) -> usize

Number of stored (nonzero) amplitudes.

Source

pub fn norm_sq(&self) -> f32

Squared norm Σ|a|².

Source

pub fn amp(&self, idx: usize) -> Complex<f32>

Amplitude at a basis index (zero if not stored).

Source

pub fn apply_1q( &mut self, qubit: usize, gate: &[[Complex<f32>; 2]; 2], ) -> QuantumResult<()>

Apply a single-qubit gate to qubit.

Iterates the current support, pairs each key with its qubit-flipped partner, and writes the mixed amplitudes into a fresh map (so concurrent reads of the old amplitudes are consistent).

§Errors

Returns QuantumError::QubitIndexOutOfRange if qubit >= n_qubits.

Source

pub fn apply_cnot(&mut self, ctrl: usize, tgt: usize) -> QuantumResult<()>

Apply a CNOT with the given control and target.

A CNOT only permutes basis indices (flips tgt when ctrl=1), so this is a pure key remap that exactly preserves occupancy.

§Errors

Returns QuantumError::QubitIndexOutOfRange for an out-of-range index or QuantumError::InvalidParameter if ctrl == tgt.

Source

pub fn apply_ccx( &mut self, c0: usize, c1: usize, tgt: usize, ) -> QuantumResult<()>

Apply a Toffoli (CCX): flips tgt when both controls are 1. Pure key remap.

§Errors

Returns an error for out-of-range or non-distinct qubit indices.

Source

pub fn normalize(&mut self)

Normalize to unit norm (no-op if ~zero).

Trait Implementations§

Source§

impl Clone for SparseStateVector

Source§

fn clone(&self) -> SparseStateVector

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SparseStateVector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.