From 64d6a8c70b8d61a22136fd10f0c61ec349315694 Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 12 Oct 2024 17:10:12 +0200 Subject: Refactor 1: Numeric Traits --- Cargo.toml | 1 - examples/README.md | 12 ----- examples/simple_gates/Cargo.toml | 8 --- examples/simple_gates/src/main.rs | 10 ---- examples/visualizers/Cargo.toml | 8 --- examples/visualizers/src/main.rs | 14 ----- libpsi-core/src/lib.rs | 2 +- libpsi-core/src/maths/mod.rs | 6 +-- libpsi-core/src/maths/numeric.rs | 91 +++++++++++++++++++++++++++++++++ libpsi-core/src/maths/numeric_float.rs | 65 ----------------------- libpsi-core/src/maths/numeric_int.rs | 44 ---------------- libpsi-core/src/maths/numeric_traits.rs | 24 --------- 12 files changed, 94 insertions(+), 191 deletions(-) delete mode 100644 examples/README.md delete mode 100644 examples/simple_gates/Cargo.toml delete mode 100644 examples/simple_gates/src/main.rs delete mode 100644 examples/visualizers/Cargo.toml delete mode 100644 examples/visualizers/src/main.rs create mode 100644 libpsi-core/src/maths/numeric.rs delete mode 100644 libpsi-core/src/maths/numeric_float.rs delete mode 100644 libpsi-core/src/maths/numeric_int.rs delete mode 100644 libpsi-core/src/maths/numeric_traits.rs diff --git a/Cargo.toml b/Cargo.toml index 9076cbb..30c0f70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 + + ops::Mul + + ops::Sub + + ops::Div + + ops::Neg + + 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 { - 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 { - 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 - + ops::Mul - + ops::Sub - + ops::Div - + ops::Neg - + 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; -} -- cgit v1.3