Skip to content

Commit

Permalink
Adding weyl chamber code in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapeyre committed Oct 13, 2023
1 parent 33cd338 commit 05e5067
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
10 changes: 2 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/accelerate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ features = ["rayon"]
[dependencies.indexmap]
version = "2.0.1"
features = ["rayon"]

[dependencies.faer]
version = "0.12"

[dependencies.faer-core]
version = "0.12"
1 change: 1 addition & 0 deletions crates/accelerate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mod sampled_exp_val;
mod sparse_pauli_op;
mod stochastic_swap;
mod vf2_layout;
mod two_qubit_decompose;

#[inline]
pub fn getenv_use_multiple_threads() -> bool {
Expand Down
71 changes: 71 additions & 0 deletions crates/accelerate/src/two_qubit_decompose.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use pyo3::Python;
use num_complex::Complex;

use numpy::PyReadonlyArray2;
//use faer::complex_native::c64;
use faer::{Mat, mat, Scale};
use faer::Faer;
use faer_core::c64;

// FIXME: zero and one exist but I cant find the right incantation
const c0 : c64 = c64 {re: 0.0, im: 0.0};
const c1 : c64 = c64 {re: 1.0, im: 0.0};
const c1im : c64 = c64 { re: 0.0, im: 1.0};

fn transform_to_magic_basis(U : Mat<c64>) -> Mat<c64> {
let _B_nonnormalized : Mat<c64> = mat![[c1, c1im, c0, c0],
[c0, c0, c1im, c1],
[c0, c0, c1im, -c1],
[c1, -c1im, c0, c0]];
let _B_nonnormalized_dagger = Scale(c64 {re: 0.5, im: 0.0}) * _B_nonnormalized.conjugate();
return _B_nonnormalized * U * _B_nonnormalized_dagger;
}

fn transform_from_magic_basis(U : Mat<c64>) -> Mat<c64> {
let _B_nonnormalized : Mat<c64> = mat![[c1, c1im, c0, c0],
[c0, c0, c1im, c1],
[c0, c0, c1im, -c1],
[c1, -c1im, c0, c0]];
let _B_nonnormalized_dagger = Scale(c64 {re: 0.5, im: 0.0}) * _B_nonnormalized.conjugate();
return _B_nonnormalized_dagger * U * _B_nonnormalized;
}

// FIXME: make some kind of trait
fn powf(base: c64, pow : f64) -> c64 {
return c64::from(Complex::<f64>::from(base).powf(pow));
}

pub trait Arg {
fn arg(self) -> f64;
}

impl Arg for c64 {
fn arg(self) -> f64 {
let c = Complex::<f64>::from(self);
return c.arg();
}
}

// FIXME: full of ineffciencies
fn weyl_coordinates(umat : Mat<c64>) {
let pi = std::f64::consts::PI;
let pi2 = std::f64::consts::PI / 2.0;
let pi4 = std::f64::consts::PI / 4.0;
let uscaled = Scale(c1 / powf(umat.determinant(), 0.25)) * umat;
let a = c0.arg();
let uup = transform_from_magic_basis(uscaled);
let uad = uup.adjoint();
let d : Vec<c64> = (&uad * &uup).eigenvalues();
let mut darg : Vec<_> = d.iter().map(|x| - x.arg() / 2.0).collect();
darg[3] = -darg[0] - darg[1] - darg[2];
let mut cs = vec![0.0; 3];
for i in 0..2 {
cs[i] = (darg[i] + darg[3]) / 2.0 % (2.0 * pi);
}
let mut cstemp : Vec<f64> = cs.iter().map(|x| x % pi2).collect();
for i in 0..3 {
cstemp[i] = cstemp[i].min(cstemp[i] - pi2);
}
}

0 comments on commit 05e5067

Please sign in to comment.