From dc1bd68e673a41311e176bc5599e751cfa76a69a Mon Sep 17 00:00:00 2001 From: Cassandra Granade Date: Fri, 1 Apr 2022 08:43:39 -0700 Subject: [PATCH] Fix a few warnings and add new macro to simplify tests. --- src/Simulation/qdk_sim_rs/src/c_api.rs | 2 +- .../src/linalg/decompositions/lu.rs | 7 +++-- src/Simulation/qdk_sim_rs/src/linalg/inv.rs | 2 +- src/Simulation/qdk_sim_rs/src/utils.rs | 28 +++++++++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/Simulation/qdk_sim_rs/src/c_api.rs b/src/Simulation/qdk_sim_rs/src/c_api.rs index b43ef513a33..70703cbf4b7 100644 --- a/src/Simulation/qdk_sim_rs/src/c_api.rs +++ b/src/Simulation/qdk_sim_rs/src/c_api.rs @@ -329,7 +329,7 @@ pub extern "C" fn get_noise_model(sim_id: usize, noise_model_json: *mut *const c as_capi_err(|| { let state = STATE .lock() - .map_err(|e| { + .map_err(|_| { // Note that as per https://github.com/dtolnay/anyhow/issues/81#issuecomment-609247231, // common practice is for poison errors to indicate that the containing thread // has been irrevocably corrupted and must panic. diff --git a/src/Simulation/qdk_sim_rs/src/linalg/decompositions/lu.rs b/src/Simulation/qdk_sim_rs/src/linalg/decompositions/lu.rs index 92b79ff511f..d697690c1ae 100644 --- a/src/Simulation/qdk_sim_rs/src/linalg/decompositions/lu.rs +++ b/src/Simulation/qdk_sim_rs/src/linalg/decompositions/lu.rs @@ -214,6 +214,7 @@ mod tests { use ndarray::{array, Array2, OwnedRepr}; use crate::{ + c64, error::QdkSimError, linalg::decompositions::{LUDecomposable, LU}, }; @@ -260,9 +261,9 @@ mod tests { // [ 0. +0.j , 0. +0.j , // -2.26666667-1.13333333j]])) let mtx: Array2 = array![ - [c64::new(-1.0, 0.0), c64::new(0.0, 1.0), c64::new(-2.0, 0.0)], - [c64::new(3.0, 0.0), c64::new(0.0, 0.0), c64::new(0.0, -4.0)], - [c64::new(-1.0, 0.0), c64::new(5.0, 0.0), c64::new(-1.0, 0.0)] + [c64!(-1.0), c64!(1.0 i), c64!(-2.0)], + [c64!(3.0), c64!(0.0), c64!(-4.0 i)], + [c64!(-1.0), c64!(5.0), c64!(-1.0)] ]; let lu: LU> = mtx.lu()?; diff --git a/src/Simulation/qdk_sim_rs/src/linalg/inv.rs b/src/Simulation/qdk_sim_rs/src/linalg/inv.rs index 4925bbcdddc..4b42ed80679 100644 --- a/src/Simulation/qdk_sim_rs/src/linalg/inv.rs +++ b/src/Simulation/qdk_sim_rs/src/linalg/inv.rs @@ -1,5 +1,5 @@ use cauchy::Scalar; -use ndarray::{Array1, Array2, Data, OwnedRepr, RawData}; +use ndarray::{Array1, Array2, OwnedRepr}; use crate::linalg::decompositions::{LUDecomposable, LUDecomposition}; diff --git a/src/Simulation/qdk_sim_rs/src/utils.rs b/src/Simulation/qdk_sim_rs/src/utils.rs index c6d11ce569c..5ed14a7f412 100644 --- a/src/Simulation/qdk_sim_rs/src/utils.rs +++ b/src/Simulation/qdk_sim_rs/src/utils.rs @@ -123,3 +123,31 @@ pub fn phase_product(row1: &ArrayView1, row2: &ArrayView1) -> bool { ((if r1 { 2 } else { 0 }) + (if r2 { 2 } else { 0 }) + acc) % 4 == 2 } + +/// Macro for quickly declaring complex numbers of type [`cauchy::c64`]. +/// +/// # Example +/// ``` +/// # use qdk_sim::c64; +/// # use cauchy::c64; +/// let re_unit = c64!(1.0); +/// assert_eq!(re_unit, c64::new(1.0, 0.0)); +/// +/// let im_unit = c64!(1.0 i); +/// assert_eq!(im_unit, c64::new(0.0, 1.0)); +/// +/// let z = c64!(1.0 + 2.0 i); +/// assert_eq!(z, c64::new(1.0, 2.0)); +/// ``` +#[macro_export] +macro_rules! c64 { + ($re:literal) => { + c64::new($re, 0.0) + }; + ($re:literal + $im:literal i) => { + c64::new($re, $im) + }; + ($im:literal i) => { + c64::new(0.0, $im) + }; +}