aboutsummaryrefslogtreecommitdiff
path: root/libmu/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'libmu/src/core')
-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
4 files changed, 52 insertions, 15 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;