diff options
| author | hachem <im@hachem.wtf> | 2024-09-27 16:26:14 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2024-09-27 16:26:14 +0200 |
| commit | 82e938a96cc6b38ac1a75215cae56f100d1e7bb8 (patch) | |
| tree | e7d1536731e607ac5cf2c7675817d1bd2986764b /libpsi-core/src/core/component.rs | |
| parent | 1070a7ee073471af8442826cec54e720be266c7c (diff) | |
BasicRT first implementation
Diffstat (limited to 'libpsi-core/src/core/component.rs')
| -rw-r--r-- | libpsi-core/src/core/component.rs | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/libpsi-core/src/core/component.rs b/libpsi-core/src/core/component.rs index 00462e5..09095ec 100644 --- a/libpsi-core/src/core/component.rs +++ b/libpsi-core/src/core/component.rs @@ -1,3 +1,5 @@ +use rand::Rng; + use crate::{complex, ColumnVector, Complex, Matrix, Vector, VectorMatrix}; use core::ops; @@ -34,6 +36,7 @@ macro_rules! quantum_register { }; } +#[derive(Clone)] pub struct ClassicalRegister { bits: Vec<i32>, } @@ -51,7 +54,10 @@ impl QuantumBit { let alpha_norm = alpha_abs * alpha_abs; let beta_norm = beta_abs * beta_abs; - if alpha_norm > beta_norm { + let mut rng = rand::thread_rng(); + let random_value = rng.gen_range(0.0..(alpha_norm as f32 + beta_norm as f32)); + + if random_value < alpha_norm as f32 { 0 } else { 1 @@ -77,6 +83,10 @@ impl ClassicalRegister { pub fn set_bits(&mut self, bits: Vec<i32>) { self.bits = bits; } + + pub fn get_bits(&self) -> Vec<i32> { + self.bits.clone() + } } impl QuantumRegister { @@ -105,10 +115,16 @@ impl QuantumRegister { classical_register.set_bits(self.qubits.iter().map(|qubit| qubit.measure()).collect()); } - pub fn apply(&mut self, gate: &QuantumGate, index: usize) { - let result: ColumnVector<Complex<f64>> = self.state.mul_matrix(gate).unwrap(); - self.qubits[index] = result; - self.update(); + pub fn get_bits(&self) -> Vec<QuantumBit> { + self.qubits.clone() + } + + pub fn get_state(&self) -> ColumnVector<Complex<f64>> { + self.state.clone() + } + + pub fn apply(&self, gate: &QuantumGate) -> QuantumBit { + self.state.mul_matrix(gate).unwrap() } } |
