diff options
| author | hachem <im@hachem.wtf> | 2024-10-12 17:10:12 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2024-10-12 17:10:12 +0200 |
| commit | 64d6a8c70b8d61a22136fd10f0c61ec349315694 (patch) | |
| tree | 7eace7d8b31ea4c44617cf7f599f385aa69dc26a | |
| parent | 483e9e6ce9fbb1440992e916ec743ce0eff0ab5a (diff) | |
Refactor 1: Numeric Traits
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/README.md | 12 | ||||
| -rw-r--r-- | examples/simple_gates/Cargo.toml | 8 | ||||
| -rw-r--r-- | examples/simple_gates/src/main.rs | 10 | ||||
| -rw-r--r-- | examples/visualizers/Cargo.toml | 8 | ||||
| -rw-r--r-- | examples/visualizers/src/main.rs | 14 | ||||
| -rw-r--r-- | libpsi-core/src/lib.rs | 2 | ||||
| -rw-r--r-- | libpsi-core/src/maths/mod.rs | 6 | ||||
| -rw-r--r-- | libpsi-core/src/maths/numeric.rs | 91 | ||||
| -rw-r--r-- | libpsi-core/src/maths/numeric_float.rs | 65 | ||||
| -rw-r--r-- | libpsi-core/src/maths/numeric_int.rs | 44 | ||||
| -rw-r--r-- | libpsi-core/src/maths/numeric_traits.rs | 24 |
12 files changed, 94 insertions, 191 deletions
@@ -4,5 +4,4 @@ members = [ "libpsi-core", "libpsi-qasm", "libpsi-visualizer", - "examples/*" ]
\ No newline at end of file diff --git a/examples/README.md b/examples/README.md deleted file mode 100644 index 47cbfd5..0000000 --- a/examples/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# Examples -In this directory, you will find a series of directories providing examples on how to use all the $\psi$ libraries. Since this entire project is based on workspaces, all you have to do use the following command: -```sh -$ cargo run -p [example name] -``` - -For instance: -```sh -$ cargo run -p simple_gates -$ cargo run -p visualizers -... -```
\ No newline at end of file diff --git a/examples/simple_gates/Cargo.toml b/examples/simple_gates/Cargo.toml deleted file mode 100644 index 503d31c..0000000 --- a/examples/simple_gates/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "simple_gates" -version = "0.1.0" -edition = "2021" - -[dependencies] -libpsi-core = { path = "../../libpsi-core" } -libpsi-visualizer = { path = "../../libpsi-visualizer" }
\ No newline at end of file diff --git a/examples/simple_gates/src/main.rs b/examples/simple_gates/src/main.rs deleted file mode 100644 index f48c935..0000000 --- a/examples/simple_gates/src/main.rs +++ /dev/null @@ -1,10 +0,0 @@ -use libpsi_core::*; - -fn main() { - let mut circuit = QuantumCircuit::new(2, 2); - circuit.apply(&gates::HADAMARD, &[], 0); - circuit.apply(&gates::CNOT, &[0], 1); - - let runtime = BasicRT::new(&circuit); - runtime.execute(1); -} diff --git a/examples/visualizers/Cargo.toml b/examples/visualizers/Cargo.toml deleted file mode 100644 index ab34ffe..0000000 --- a/examples/visualizers/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "visualizers" -version = "0.1.0" -edition = "2021" - -[dependencies] -libpsi-core = { path = "../../libpsi-core" } -libpsi-visualizer = { path = "../../libpsi-visualizer" }
\ No newline at end of file diff --git a/examples/visualizers/src/main.rs b/examples/visualizers/src/main.rs deleted file mode 100644 index d1fdb6e..0000000 --- a/examples/visualizers/src/main.rs +++ /dev/null @@ -1,14 +0,0 @@ -use libpsi_core::*; -use libpsi_visualizer::*; - -fn main() { - let mut circuit = QuantumCircuit::new(2, 2); - circuit.apply(&gates::HADAMARD, &[], 0); - circuit.apply(&gates::CNOT, &[0], 1); - - let mut visualizer = HorizontalCLIVisualizer::new(&circuit); - visualizer.render(); - - let runtime = BasicRT::new(&circuit); - runtime.execute(1024); -} diff --git a/libpsi-core/src/lib.rs b/libpsi-core/src/lib.rs index 0be80cc..91b853e 100644 --- a/libpsi-core/src/lib.rs +++ b/libpsi-core/src/lib.rs @@ -4,7 +4,7 @@ pub mod runtimes; pub use maths::complex::*; pub use maths::matrix::*; -pub use maths::numeric_traits::*; +pub use maths::numeric::*; pub use maths::vector::*; pub use core::circuit::*; diff --git a/libpsi-core/src/maths/mod.rs b/libpsi-core/src/maths/mod.rs index a2b9b84..32c8f9c 100644 --- a/libpsi-core/src/maths/mod.rs +++ b/libpsi-core/src/maths/mod.rs @@ -4,13 +4,11 @@ pub mod complex_ops; pub mod matrix; pub mod matrix_ops; -pub mod numeric_float; -pub mod numeric_int; -pub mod numeric_traits; +pub mod numeric; pub mod vector; pub mod vector_ops; pub use complex::*; pub use matrix::*; -pub use numeric_traits::*; +pub use numeric::*; diff --git a/libpsi-core/src/maths/numeric.rs b/libpsi-core/src/maths/numeric.rs new file mode 100644 index 0000000..e6f3247 --- /dev/null +++ b/libpsi-core/src/maths/numeric.rs @@ -0,0 +1,91 @@ +use crate::Complex; +use core::ops; + +macro_rules! impl_numeric { + ($($t:ty),*) => { + $( + impl Numeric for $t { + fn zero() -> Self { + 0 as $t + } + + fn one() -> Self { + 1 as $t + } + } + )* + }; +} + +macro_rules! impl_float { + ($($t:ty, $sqrt_fn:path, $atan2_fn:path),*) => { + $( + impl Float for $t { + fn sqrt(self) -> Self { + $sqrt_fn(self) + } + + fn atan2(y: Self, x: Self) -> Self { + $atan2_fn(y, x) + } + } + )* + }; +} + +macro_rules! impl_complex { + ($($t:ty, $sqrt_fn:path, $atan2_fn:path, $cos_fn:path, $sin_fn:path),*) => { + $( + impl Float for Complex<$t> { + fn sqrt(self) -> Self { + let r = self.abs(); + let theta = self.phase(); + + let sqrt_r = $sqrt_fn(r); + let sqrt_theta = theta / 2.0; + + Complex::new( + sqrt_r * $cos_fn(sqrt_theta), + sqrt_r * $sin_fn(sqrt_theta), + ) + } + + fn atan2(y: Self, x: Self) -> Self { + Complex::new( + $atan2_fn(y.real, x.real), + $atan2_fn(y.imaginary, x.imaginary), + ) + } + } + )* + }; +} + +pub trait Numeric: + Copy + + PartialOrd + + ops::Add<Output = Self> + + ops::Mul<Output = Self> + + ops::Sub<Output = Self> + + ops::Div<Output = Self> + + ops::Neg<Output = Self> + + ops::AddAssign + + ops::SubAssign + + ops::MulAssign + + ops::DivAssign +{ + fn zero() -> Self; + fn one() -> Self; +} + +impl_numeric!(i32, i64, f32, f64); +impl_float!(f32, libm::sqrtf, libm::atan2f); +impl_float!(f64, libm::sqrt, libm::atan2); +impl_complex!(f32, libm::sqrtf, libm::atan2f, libm::cosf, libm::sinf); +impl_complex!(f64, libm::sqrt, libm::atan2, libm::cos, libm::sin); + +pub trait Integer: Numeric {} +pub trait Float: Numeric { + fn sqrt(self) -> Self; + fn atan2(y: Self, x: Self) -> Self; +} diff --git a/libpsi-core/src/maths/numeric_float.rs b/libpsi-core/src/maths/numeric_float.rs deleted file mode 100644 index 3591f24..0000000 --- a/libpsi-core/src/maths/numeric_float.rs +++ /dev/null @@ -1,65 +0,0 @@ -use super::{Complex, Float}; - -impl Float for f32 { - fn sqrt(self) -> Self { - libm::sqrtf(self) - } - - fn atan2(y: Self, x: Self) -> Self { - libm::atan2f(y, x) - } -} - -impl Float for f64 { - fn sqrt(self) -> Self { - libm::sqrt(self) - } - - fn atan2(y: Self, x: Self) -> Self { - libm::atan2(y, x) - } -} - -impl Float for Complex<f32> { - fn sqrt(self) -> Self { - let r = self.abs(); - let theta = self.phase(); - - let sqrt_r = libm::sqrtf(r); - let sqrt_theta = theta / 2.0; - - Complex::new( - sqrt_r * libm::cosf(sqrt_theta), - sqrt_r * libm::sinf(sqrt_theta), - ) - } - - fn atan2(y: Self, x: Self) -> Self { - Complex::new( - libm::atan2f(y.real, x.real), - libm::atan2f(y.imaginary, x.imaginary), - ) - } -} - -impl Float for Complex<f64> { - fn sqrt(self) -> Self { - let r = self.abs(); - let theta = self.phase(); - - let sqrt_r = libm::sqrt(r); - let sqrt_theta = theta / 2.0; - - Complex::new( - sqrt_r * libm::cos(sqrt_theta), - sqrt_r * libm::sin(sqrt_theta), - ) - } - - fn atan2(y: Self, x: Self) -> Self { - Complex::new( - libm::atan2(y.real, x.real), - libm::atan2(y.imaginary, x.imaginary), - ) - } -} diff --git a/libpsi-core/src/maths/numeric_int.rs b/libpsi-core/src/maths/numeric_int.rs deleted file mode 100644 index a9ecec7..0000000 --- a/libpsi-core/src/maths/numeric_int.rs +++ /dev/null @@ -1,44 +0,0 @@ -use super::{Integer, Numeric}; - -impl Integer for i64 {} -impl Integer for i32 {} - -impl Numeric for i32 { - fn zero() -> Self { - 0 - } - - fn one() -> Self { - 1 - } -} - -impl Numeric for i64 { - fn zero() -> Self { - 0 - } - - fn one() -> Self { - 1 - } -} - -impl Numeric for f32 { - fn zero() -> Self { - 0.0 - } - - fn one() -> Self { - 1.0 - } -} - -impl Numeric for f64 { - fn zero() -> Self { - 0.0 - } - - fn one() -> Self { - 1.0 - } -} diff --git a/libpsi-core/src/maths/numeric_traits.rs b/libpsi-core/src/maths/numeric_traits.rs deleted file mode 100644 index dd7fdec..0000000 --- a/libpsi-core/src/maths/numeric_traits.rs +++ /dev/null @@ -1,24 +0,0 @@ -use core::ops; - -pub trait Numeric: - Copy - + PartialOrd - + ops::Add<Output = Self> - + ops::Mul<Output = Self> - + ops::Sub<Output = Self> - + ops::Div<Output = Self> - + ops::Neg<Output = Self> - + ops::AddAssign - + ops::SubAssign - + ops::MulAssign - + ops::DivAssign -{ - fn zero() -> Self; - fn one() -> Self; -} - -pub trait Integer: Numeric {} -pub trait Float: Numeric { - fn sqrt(self) -> Self; - fn atan2(y: Self, x: Self) -> Self; -} |
