From 82e938a96cc6b38ac1a75215cae56f100d1e7bb8 Mon Sep 17 00:00:00 2001 From: hachem Date: Fri, 27 Sep 2024 16:26:14 +0200 Subject: BasicRT first implementation --- libpsi-core/src/core/component.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'libpsi-core/src/core/component.rs') 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, } @@ -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) { self.bits = bits; } + + pub fn get_bits(&self) -> Vec { + 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> = self.state.mul_matrix(gate).unwrap(); - self.qubits[index] = result; - self.update(); + pub fn get_bits(&self) -> Vec { + self.qubits.clone() + } + + pub fn get_state(&self) -> ColumnVector> { + self.state.clone() + } + + pub fn apply(&self, gate: &QuantumGate) -> QuantumBit { + self.state.mul_matrix(gate).unwrap() } } -- cgit v1.3