From a84b6394209e37620e45155a25b3755b38ba8515 Mon Sep 17 00:00:00 2001 From: hachem Date: Fri, 20 Sep 2024 09:25:19 +0200 Subject: basic quantum gate setup and quantum registers --- libmu/src/core/component.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 libmu/src/core/component.rs (limited to 'libmu/src/core/component.rs') diff --git a/libmu/src/core/component.rs b/libmu/src/core/component.rs new file mode 100644 index 0000000..bd18b52 --- /dev/null +++ b/libmu/src/core/component.rs @@ -0,0 +1,51 @@ +use crate::{ColumnVector, Complex, Matrix, VectorMatrix}; + +pub type QuantumRegister = ColumnVector>; +pub type QuantumBit = ColumnVector>; + +#[macro_export] +macro_rules! count { + () => { 0 }; + ($head:expr $(,$tail:expr)*) => { 1 + count!($( $tail ),*) }; +} + +#[macro_export] +macro_rules! qubit { + ($(($re:expr, $im:expr)),*) => { + { + let mut vector = Vec::new(); + $( + vector.push(complex!($re, $im)); + )* + QuantumBit::new(vector) + } + }; +} + +#[macro_export] +macro_rules! quantum_register { + ($($bit:expr),*) => { + { + const N: usize = count!($($bit),*); + let bits: [QuantumBit; N] = [$($bit),*]; + QuantumRegister::from(&bits) + } + }; +} + +impl QuantumBit { + pub fn get_state(&self) -> i32 { + (self[1] != Complex::new(0.0, 0.0)) as i32 + } +} + +impl QuantumRegister { + pub fn from(bits: &[QuantumBit]) -> QuantumRegister { + let matrices: Vec>> = bits.iter().map(|bit| bit.to_matrix()).collect(); + let mut result = matrices[0].clone(); + for matrix in &matrices[1..] { + result = result.kronecker(matrix); + } + ColumnVector::from_matrix(&result) + } +} -- cgit v1.3