Skip to main content

StateVector

Struct StateVector 

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

The state of an n-qubit register as 2^n complex amplitudes.

Gates change the state in place. Reuse one StateVector across runs: reset does not allocate. See the module docs for the qubit order.

Implementations§

Source§

impl StateVector

Source

pub fn new(qubits: usize) -> Result<Self, TooManyQubits>

Makes an n-qubit register in the state |0…0⟩.

§Errors

Returns TooManyQubits when qubits is more than MAX_QUBITS.

Source

pub fn qubits(&self) -> usize

Returns the number of qubits.

Source

pub fn states(&self) -> usize

Returns the number of basis states, 2^n.

Source

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

Returns the amplitudes, indexed by basis state.

Source

pub fn reset(&mut self)

Sets the register back to |0…0⟩ without a new allocation.

Source

pub fn h(&mut self, qubit: usize)

Applies the Hadamard gate to qubit.

§Panics

Panics when qubit is not less than qubits.

Source

pub fn x(&mut self, qubit: usize)

Applies the Pauli-X (NOT) gate to qubit.

§Panics

Panics when qubit is not less than qubits.

Source

pub fn z(&mut self, qubit: usize)

Applies the Pauli-Z gate to qubit: the |1⟩ component changes sign.

§Panics

Panics when qubit is not less than qubits.

Source

pub fn phase(&mut self, qubit: usize, angle: f64)

Applies the phase gate diag(1, e^(i·angle)) to qubit, with angle in radians.

§Panics

Panics when qubit is not less than qubits.

Source

pub fn mcz(&mut self, qubits: impl IntoIterator<Item = usize>)

Applies the multi-controlled Z gate on qubits: a basis state changes sign when all of these qubits are 1.

The gate is symmetric, so no qubit is special as the target. One qubit gives Z. An empty set changes the sign of every state, a global phase without physical effect.

§Panics

Panics when a qubit is not less than qubits.

Source

pub fn probabilities(&self) -> impl Iterator<Item = f64> + '_

Returns the measurement probability of each basis state, in index order.

Source

pub fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> usize

Measures all qubits once and returns the basis-state index, without a state collapse.

The draw comes from rng, so a fixed seed gives a reproducible result. When rounding leaves the probabilities a little below 1 in total, the last index takes the rest.

Trait Implementations§

Source§

impl Clone for StateVector

Source§

fn clone(&self) -> Self

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 StateVector

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.