pub struct Statevector {
pub amplitudes: Vec<(f64, f64)>,
pub n_qubits: usize,
}Expand description
N-qubit statevector: stores 2^n complex amplitudes as (re, im) pairs.
Qubit indexing convention: qubit 0 is the least significant bit.
So the basis state |b_{n-1} … b_1 b_0⟩ corresponds to index
b_0 + 2*b_1 + ... + 2^(n-1)*b_{n-1}.
Fields§
§amplitudes: Vec<(f64, f64)>Complex amplitudes: amplitudes[k] = (re, im) for basis state |k⟩
n_qubits: usizeNumber of qubits
Implementations§
Source§impl Statevector
impl Statevector
Sourcepub fn zero_state(n: usize) -> QcResult<Self>
pub fn zero_state(n: usize) -> QcResult<Self>
Create the zero state |0…0⟩ for n qubits.
Sourcepub fn norm_squared(&self) -> f64
pub fn norm_squared(&self) -> f64
Total norm squared: should remain 1.0 after any unitary operation.
Sourcepub fn apply_hadamard(&mut self, qubit: usize) -> QcResult<()>
pub fn apply_hadamard(&mut self, qubit: usize) -> QcResult<()>
Apply Hadamard gate to qubit.
H = (1/√2) [[1, 1], [1, -1]]
Pairs basis states that differ only in bit qubit.
Sourcepub fn apply_rz(&mut self, qubit: usize, theta: f64) -> QcResult<()>
pub fn apply_rz(&mut self, qubit: usize, theta: f64) -> QcResult<()>
Apply Rz(θ) gate to qubit.
Rz(θ) = [[e^{-iθ/2}, 0], [0, e^{iθ/2}]]
States with bit qubit = 0 get phase e^{-iθ/2}; states with bit = 1
get phase e^{+iθ/2}.
Sourcepub fn apply_rx(&mut self, qubit: usize, theta: f64) -> QcResult<()>
pub fn apply_rx(&mut self, qubit: usize, theta: f64) -> QcResult<()>
Apply Rx(θ) gate to qubit.
Rx(θ) = [[cos(θ/2), -i·sin(θ/2)], [-i·sin(θ/2), cos(θ/2)]]
Sourcepub fn apply_ry(&mut self, qubit: usize, theta: f64) -> QcResult<()>
pub fn apply_ry(&mut self, qubit: usize, theta: f64) -> QcResult<()>
Apply Ry(θ) gate to qubit.
Ry(θ) = [[cos(θ/2), -sin(θ/2)], [sin(θ/2), cos(θ/2)]]
Sourcepub fn apply_cnot(&mut self, control: usize, target: usize) -> QcResult<()>
pub fn apply_cnot(&mut self, control: usize, target: usize) -> QcResult<()>
Apply CNOT gate with control qubit and target qubit.
Flips the target qubit when the control qubit is |1⟩.
Sourcepub fn apply_rzz(&mut self, q1: usize, q2: usize, theta: f64) -> QcResult<()>
pub fn apply_rzz(&mut self, q1: usize, q2: usize, theta: f64) -> QcResult<()>
Apply Rzz(θ) gate to qubits q1 and q2.
Rzz(θ) = e^{-iθ/2 · Z⊗Z}
For basis state |b1, b2⟩:
- If b1 XOR b2 = 0 (same bits): phase e^{-iθ/2}
- If b1 XOR b2 = 1 (different bits): phase e^{+iθ/2}
Sourcepub fn expectation_zz(&self, q1: usize, q2: usize) -> QcResult<f64>
pub fn expectation_zz(&self, q1: usize, q2: usize) -> QcResult<f64>
Compute ⟨Z_i Z_j⟩ expectation value.
Z_i Z_j has eigenvalue +1 when bits i and j are equal, -1 otherwise.
Sourcepub fn expectation_z(&self, qubit: usize) -> QcResult<f64>
pub fn expectation_z(&self, qubit: usize) -> QcResult<f64>
Compute ⟨Z_k⟩ expectation value.
Z_k has eigenvalue +1 when bit k is 0, and -1 when bit k is 1.
Sourcepub fn most_probable_state(&self) -> usize
pub fn most_probable_state(&self) -> usize
Return the index of the basis state with the highest probability.
Sourcepub fn index_to_bits(&self, idx: usize) -> Vec<bool>
pub fn index_to_bits(&self, idx: usize) -> Vec<bool>
Decode a basis state index into a bitstring (qubit 0 = LSB).
Trait Implementations§
Source§impl Clone for Statevector
impl Clone for Statevector
Source§fn clone(&self) -> Statevector
fn clone(&self) -> Statevector
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 Statevector
impl RefUnwindSafe for Statevector
impl Send for Statevector
impl Sync for Statevector
impl Unpin for Statevector
impl UnsafeUnpin for Statevector
impl UnwindSafe for Statevector
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.