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: usizeNumber of qubits
Implementations§
Source§impl QuantumState
impl QuantumState
Sourcepub fn new(amplitudes: Array1<QuantumAmplitude>) -> SpatialResult<Self>
pub fn new(amplitudes: Array1<QuantumAmplitude>) -> SpatialResult<Self>
Sourcepub fn zero_state(numqubits: usize) -> Self
pub fn zero_state(numqubits: usize) -> Self
Sourcepub fn uniform_superposition(numqubits: usize) -> Self
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
Sourcepub fn measure(&self) -> usize
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
Sourcepub fn probability(&self, state: usize) -> f64
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)
Sourcepub fn hadamard(&mut self, qubit: usize) -> SpatialResult<()>
pub fn hadamard(&mut self, qubit: usize) -> SpatialResult<()>
Sourcepub fn phase_rotation(&mut self, qubit: usize, angle: f64) -> SpatialResult<()>
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 toangle- Rotation angle in radians
§Errors
Returns SpatialError::InvalidInput if the qubit index is out of range
Sourcepub fn controlled_rotation(
&mut self,
control: usize,
target: usize,
angle: f64,
) -> SpatialResult<()>
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 qubittarget- Index of the target qubitangle- Rotation angle in radians
§Errors
Returns SpatialError::InvalidInput if either qubit index is out of range
Sourcepub fn pauli_x(&mut self, qubit: usize) -> SpatialResult<()>
pub fn pauli_x(&mut self, qubit: usize) -> SpatialResult<()>
Sourcepub fn pauli_y(&mut self, qubit: usize) -> SpatialResult<()>
pub fn pauli_y(&mut self, qubit: usize) -> SpatialResult<()>
Sourcepub fn pauli_z(&mut self, qubit: usize) -> SpatialResult<()>
pub fn pauli_z(&mut self, qubit: usize) -> SpatialResult<()>
Sourcepub fn num_qubits(&self) -> usize
pub fn num_qubits(&self) -> usize
Get number of qubits
Sourcepub fn num_states(&self) -> usize
pub fn num_states(&self) -> usize
Get number of basis states (2^n)
Sourcepub fn is_normalized(&self) -> bool
pub fn is_normalized(&self) -> bool
Check if the state is normalized
Sourcepub fn amplitude(&self, state: usize) -> Option<QuantumAmplitude>
pub fn amplitude(&self, state: usize) -> Option<QuantumAmplitude>
Get the amplitude for a specific basis state
Sourcepub fn set_amplitude(
&mut self,
state: usize,
amplitude: QuantumAmplitude,
) -> SpatialResult<()>
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
impl Clone for QuantumState
Source§fn clone(&self) -> QuantumState
fn clone(&self) -> QuantumState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for QuantumState
impl RefUnwindSafe for QuantumState
impl Send for QuantumState
impl Sync for QuantumState
impl Unpin for QuantumState
impl UnwindSafe for QuantumState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.