aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2024-09-20 09:25:19 +0200
committerhachem <im@hachem.wtf>2024-09-20 09:25:19 +0200
commita84b6394209e37620e45155a25b3755b38ba8515 (patch)
tree4839f145111fda4086311d8ff1417f698ca30212
parentaffb34897a4761197df78bdccdb2da599ceb537a (diff)
basic quantum gate setup and quantum registers
-rw-r--r--libmu/src/core/bit.rs10
-rw-r--r--libmu/src/core/component.rs51
-rw-r--r--libmu/src/core/gate.rs3
-rw-r--r--libmu/src/core/mod.rs3
-rw-r--r--libmu/src/lib.rs3
-rw-r--r--libmu/src/maths/matrix.rs3
-rw-r--r--libmu/src/maths/vector.rs17
-rw-r--r--mu/src/main.rs15
8 files changed, 67 insertions, 38 deletions
diff --git a/libmu/src/core/bit.rs b/libmu/src/core/bit.rs
deleted file mode 100644
index a5bce51..0000000
--- a/libmu/src/core/bit.rs
+++ /dev/null
@@ -1,10 +0,0 @@
-use crate::{ColumnVector, Complex, QuantumGate};
-
-pub type QuantumBit = ColumnVector<Complex<f64>>;
-pub type ClassicalBit = bool;
-
-impl QuantumBit {
- pub fn apply_gate(&mut self, _gate: &QuantumGate) {
- todo!();
- }
-}
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<Complex<f64>>;
+pub type QuantumBit = ColumnVector<Complex<f64>>;
+
+#[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<Matrix<Complex<f64>>> = 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)
+ }
+}
diff --git a/libmu/src/core/gate.rs b/libmu/src/core/gate.rs
deleted file mode 100644
index b2e18c0..0000000
--- a/libmu/src/core/gate.rs
+++ /dev/null
@@ -1,3 +0,0 @@
-use crate::{Complex, Matrix};
-
-pub type QuantumGate = Matrix<Complex<f64>>;
diff --git a/libmu/src/core/mod.rs b/libmu/src/core/mod.rs
index 63ee71b..9cea807 100644
--- a/libmu/src/core/mod.rs
+++ b/libmu/src/core/mod.rs
@@ -1,2 +1 @@
-pub mod bit;
-pub mod gate;
+pub mod component;
diff --git a/libmu/src/lib.rs b/libmu/src/lib.rs
index ad64816..9cbd644 100644
--- a/libmu/src/lib.rs
+++ b/libmu/src/lib.rs
@@ -6,5 +6,4 @@ pub use maths::matrix::*;
pub use maths::numeric_types::*;
pub use maths::vector::*;
-pub use core::gate::*;
-pub use core::bit::*;
+pub use core::component::*;
diff --git a/libmu/src/maths/matrix.rs b/libmu/src/maths/matrix.rs
index 9932895..388f858 100644
--- a/libmu/src/maths/matrix.rs
+++ b/libmu/src/maths/matrix.rs
@@ -24,6 +24,7 @@ macro_rules! matrix {
}};
}
+#[derive(Clone)]
pub struct Matrix<T: Float> {
pub data: Vec<T>,
pub rows: usize,
@@ -78,7 +79,7 @@ impl<T: Float> Matrix<T> {
for l in 0..other.cols {
let result_row = i * other.rows + k;
let result_col = j * other.cols + l;
- result.set(result_row, result_col, self_val * other.get(k, l))
+ result.set(result_row, result_col, self_val.clone() * other.get(k, l));
}
}
}
diff --git a/libmu/src/maths/vector.rs b/libmu/src/maths/vector.rs
index c97b415..0c13e4c 100644
--- a/libmu/src/maths/vector.rs
+++ b/libmu/src/maths/vector.rs
@@ -362,14 +362,13 @@ impl<T: Float + fmt::Display> fmt::Display for RowVector<T> {
impl<T: Float + fmt::Display> fmt::Display for ColumnVector<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(
- f,
- "[{}]",
- self.0
- .iter()
- .map(|x| x.to_string())
- .collect::<Vec<String>>()
- .join(", ")
- )
+ write!(f, "[")?;
+ for (i, x) in self.0.iter().enumerate() {
+ if i > 0 {
+ write!(f, ",\n ")?;
+ }
+ write!(f, "{}", x)?;
+ }
+ write!(f, "]")
}
}
diff --git a/mu/src/main.rs b/mu/src/main.rs
index a254401..7bbc3e7 100644
--- a/mu/src/main.rs
+++ b/mu/src/main.rs
@@ -1,15 +1,8 @@
use libmu::*;
fn main() {
- #[rustfmt::skip]
- let matrix = matrix![[1.0, 2.0, 3.0];
- [4.0, 5.0, 6.0];
- [7.0, 8.0, 9.0]];
- let vector = row_vector![1.0, 2.0, 3.0];
-
- let result = vector.mul_matrix(&matrix).unwrap();
-
- println!("Matrix: \n{}", matrix);
- println!("Vector: \n{}", vector);
- println!("Result: \n{}", result);
+ let bit_1 = qubit![(1.0, 2.0), (1.0, 2.0)];
+ let bit_2 = qubit![(1.0, 2.0), (1.0, 2.0)];
+ let register: QuantumRegister = quantum_register![bit_1, bit_2];
+ println!("{}", register);
}