QuantumState

Struct QuantumState 

Source
pub struct QuantumState {
    pub amplitudes: Array1<QuantumAmplitude>,
    pub numqubits: usize,
}
Expand description

Quantum state vector representation

A quantum state is represented as a vector of complex amplitudes in the computational basis. The state |ψ⟩ = Σᵢ αᵢ|i⟩ where αᵢ are the complex amplitudes and |i⟩ are the computational basis states.

§Properties

  • The state is normalized: Σᵢ |αᵢ|² = 1
  • The number of amplitudes must be a power of 2 (2ⁿ for n qubits)
  • Supports common quantum gates and operations

§Example

use scirs2_spatial::quantum_inspired::concepts::QuantumState;
use scirs2_core::ndarray::Array1;
use scirs2_core::numeric::Complex64;

// Create a 2-qubit zero state |00⟩
let zero_state = QuantumState::zero_state(2);
assert_eq!(zero_state.num_qubits(), 2);
assert_eq!(zero_state.probability(0), 1.0);

// Create uniform superposition |+⟩⊗²
let superposition = QuantumState::uniform_superposition(2);
assert_eq!(superposition.probability(0), 0.25);

Fields§

§amplitudes: Array1<QuantumAmplitude>

Amplitudes for each basis state

§numqubits: usize

Number of qubits

Implementations§

Source§

impl QuantumState

Source

pub fn new(amplitudes: Array1<QuantumAmplitude>) -> SpatialResult<Self>

Create a new quantum state with given amplitudes

§Arguments
  • amplitudes - Complex amplitudes for each computational basis state
§Returns

A new QuantumState if the amplitudes vector has a power-of-2 length

§Errors

Returns SpatialError::InvalidInput if the number of amplitudes is not a power of 2

Source

pub fn zero_state(numqubits: usize) -> Self

Create a quantum state in computational basis |0⟩⊗ⁿ

Creates an n-qubit state where all qubits are in the |0⟩ state. This corresponds to the basis state |00…0⟩.

§Arguments
  • numqubits - Number of qubits in the state
§Returns

A new QuantumState in the |0⟩⊗ⁿ state

Source

pub fn uniform_superposition(numqubits: usize) -> Self

Create a uniform superposition state |+⟩⊗ⁿ

Creates an n-qubit state where each qubit is in the |+⟩ = (|0⟩ + |1⟩)/√2 state. This results in a uniform superposition over all 2ⁿ computational basis states.

§Arguments
  • numqubits - Number of qubits in the state
§Returns

A new QuantumState in uniform superposition

Source

pub fn measure(&self) -> usize

Measure the quantum state and collapse to classical state

Performs a measurement in the computational basis, collapsing the quantum state to a classical state according to the Born rule. The probability of measuring state |i⟩ is |αᵢ|².

§Returns

The index of the measured computational basis state

Source

pub fn probability(&self, state: usize) -> f64

Get the probability of measuring a specific state

Calculates the probability of measuring the quantum state in a specific computational basis state according to the Born rule.

§Arguments
  • state - Index of the computational basis state
§Returns

Probability of measuring the given state (between 0.0 and 1.0)

Source

pub fn hadamard(&mut self, qubit: usize) -> SpatialResult<()>

Apply Hadamard gate to specific qubit

The Hadamard gate creates superposition by mapping:

  • |0⟩ → (|0⟩ + |1⟩)/√2
  • |1⟩ → (|0⟩ - |1⟩)/√2
§Arguments
  • qubit - Index of the qubit to apply the gate to (0-indexed)
§Errors

Returns SpatialError::InvalidInput if the qubit index is out of range

Source

pub fn phase_rotation(&mut self, qubit: usize, angle: f64) -> SpatialResult<()>

Apply phase rotation gate

The phase rotation gate applies a phase e^(iθ) to the |1⟩ component of the specified qubit, leaving the |0⟩ component unchanged.

§Arguments
  • qubit - Index of the qubit to apply the gate to
  • angle - Rotation angle in radians
§Errors

Returns SpatialError::InvalidInput if the qubit index is out of range

Source

pub fn controlled_rotation( &mut self, control: usize, target: usize, angle: f64, ) -> SpatialResult<()>

Apply controlled rotation between two qubits

Applies a rotation to the target qubit conditioned on the control qubit being in the |1⟩ state. This creates entanglement between the qubits.

§Arguments
  • control - Index of the control qubit
  • target - Index of the target qubit
  • angle - Rotation angle in radians
§Errors

Returns SpatialError::InvalidInput if either qubit index is out of range

Source

pub fn pauli_x(&mut self, qubit: usize) -> SpatialResult<()>

Apply Pauli-X gate (bit flip) to specific qubit

The Pauli-X gate performs a bit flip: |0⟩ ↔ |1⟩

§Arguments
  • qubit - Index of the qubit to apply the gate to
§Errors

Returns SpatialError::InvalidInput if the qubit index is out of range

Source

pub fn pauli_y(&mut self, qubit: usize) -> SpatialResult<()>

Apply Pauli-Y gate to specific qubit

The Pauli-Y gate performs: |0⟩ → i|1⟩, |1⟩ → -i|0⟩

§Arguments
  • qubit - Index of the qubit to apply the gate to
§Errors

Returns SpatialError::InvalidInput if the qubit index is out of range

Source

pub fn pauli_z(&mut self, qubit: usize) -> SpatialResult<()>

Apply Pauli-Z gate (phase flip) to specific qubit

The Pauli-Z gate performs a phase flip: |0⟩ → |0⟩, |1⟩ → -|1⟩

§Arguments
  • qubit - Index of the qubit to apply the gate to
§Errors

Returns SpatialError::InvalidInput if the qubit index is out of range

Source

pub fn num_qubits(&self) -> usize

Get number of qubits

Source

pub fn num_states(&self) -> usize

Get number of basis states (2^n)

Source

pub fn is_normalized(&self) -> bool

Check if the state is normalized

Source

pub fn normalize(&mut self)

Normalize the quantum state

Source

pub fn amplitude(&self, state: usize) -> Option<QuantumAmplitude>

Get the amplitude for a specific basis state

Source

pub fn set_amplitude( &mut self, state: usize, amplitude: QuantumAmplitude, ) -> SpatialResult<()>

Set the amplitude for a specific basis state

Trait Implementations§

Source§

impl Clone for QuantumState

Source§

fn clone(&self) -> QuantumState

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for QuantumState

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
Source§

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

Source§

fn vzip(self) -> V