Skip to content

Commit

Permalink
Run rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapeyre committed Oct 16, 2023
1 parent a3ab146 commit ca07f86
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion crates/accelerate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ mod sabre_swap;
mod sampled_exp_val;
mod sparse_pauli_op;
mod stochastic_swap;
mod vf2_layout;
mod two_qubit_decompose;
mod utils;
mod vf2_layout;

#[inline]
pub fn getenv_use_multiple_threads() -> bool {
Expand Down
77 changes: 46 additions & 31 deletions crates/accelerate/src/two_qubit_decompose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@
// of real components and one of imaginary components.
// In order to avoid copying we want to use `MatRef<c64>` or `MatMut<c64>`.

use num_complex::Complex;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
use pyo3::Python;
use num_complex::{Complex};
use std::f64::consts::PI;

//use numpy::{ToPyArray, PyArray};
use numpy::PyReadonlyArray2;
use faer::{Mat, MatRef, mat, Scale};
use faer::Faer;
use faer::{mat, Mat, MatRef, Scale};
use faer_core::c64;
use numpy::PyReadonlyArray2;
//use faer_core::ComplexField; // for access to fields of struct
use faer::IntoFaerComplex;

use crate::utils;

// 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};
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 };

// FIXME: Find a way to compute these matrices at compile time.
fn transform_from_magic_basis(unitary : 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.adjoint();
fn transform_from_magic_basis(unitary: 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.adjoint();
_b_nonnormalized_dagger * unitary * _b_nonnormalized
}

Expand All @@ -52,11 +54,11 @@ fn transform_from_magic_basis(unitary : Mat<c64>) -> Mat<c64> {
// upstream.

pub trait PowF {
fn powf(self, pow : f64) -> c64;
fn powf(self, pow: f64) -> c64;
}

impl PowF for c64 {
fn powf(self, pow : f64) -> c64 {
fn powf(self, pow: f64) -> c64 {
c64::from(Complex::<f64>::from(self).powf(pow))
}
}
Expand All @@ -73,19 +75,24 @@ impl Arg for c64 {
}

// FIXME: full of ineffciencies
fn __weyl_coordinates(unitary : MatRef<c64>) -> (f64, f64, f64) {
fn __weyl_coordinates(unitary: MatRef<c64>) -> (f64, f64, f64) {
let pi = PI;
let pi2 = PI / 2.0;
let pi4 = PI / 4.0;
let uscaled = Scale(C1 / unitary.determinant().powf(0.25)) * unitary;
let uup = transform_from_magic_basis(uscaled);
let uad = uup.transpose();
let mut darg : Vec<_> = (uad * &uup)
.eigenvalues().iter()
.map(|x: &c64| - x.arg() / 2.0).collect();
let mut darg: Vec<_> = (uad * &uup)
.eigenvalues()
.iter()
.map(|x: &c64| -x.arg() / 2.0)
.collect();
darg[3] = -darg[0] - darg[1] - darg[2];
let mut cs : Vec<_> = (0..3).map(|i| utils::modulo((darg[i] + darg[3]) / 2.0, 2.0 * pi)).collect();
let cstemp : Vec<f64> = cs.iter()
let mut cs: Vec<_> = (0..3)
.map(|i| utils::modulo((darg[i] + darg[3]) / 2.0, 2.0 * pi))
.collect();
let cstemp: Vec<f64> = cs
.iter()
.map(|x| utils::modulo(*x, pi2))
.map(|x| x.min(pi2 - x))
.collect();
Expand Down Expand Up @@ -122,35 +129,43 @@ fn __weyl_coordinates(unitary : MatRef<c64>) -> (f64, f64, f64) {
(cs[1], cs[0], cs[2])
}


// For debugging. We can remove this later
#[pyfunction]
#[pyo3(text_signature = "(unitary, /")]
pub fn _weyl_coordinates(unitary : PyReadonlyArray2<Complex<f64>>) -> (f64, f64, f64) {
pub fn _weyl_coordinates(unitary: PyReadonlyArray2<Complex<f64>>) -> (f64, f64, f64) {
let u = unitary.as_array().into_faer_complex();
__weyl_coordinates(u)
}

#[pyfunction]
#[pyo3(text_signature = "(basis_b, basis_fidelity, unitary, /")]
pub fn _num_basis_gates(basis_b : f64, basis_fidelity : f64, unitary : PyReadonlyArray2<Complex<f64>>) -> usize {
pub fn _num_basis_gates(
basis_b: f64,
basis_fidelity: f64,
unitary: PyReadonlyArray2<Complex<f64>>,
) -> usize {
let u = unitary.as_array().into_faer_complex();
__num_basis_gates(basis_b, basis_fidelity, u)
}

fn __num_basis_gates(basis_b : f64, basis_fidelity : f64, unitary : MatRef<c64>) -> usize {
fn __num_basis_gates(basis_b: f64, basis_fidelity: f64, unitary: MatRef<c64>) -> usize {
let (a, b, c) = __weyl_coordinates(unitary);
let pi4 = PI / 4.0;
let traces = [
c64::new(4.0 * (a.cos() * b.cos() * c.cos()), 4.0 * (a.sin() * b.sin() * c.sin())),
c64::new(4.0 * (pi4 - a).cos() * (basis_b - b).cos() * c.cos(),
4.0 * (pi4 - a).sin() * (basis_b - b).sin() * c.sin()),
c64::new(
4.0 * (a.cos() * b.cos() * c.cos()),
4.0 * (a.sin() * b.sin() * c.sin()),
),
c64::new(
4.0 * (pi4 - a).cos() * (basis_b - b).cos() * c.cos(),
4.0 * (pi4 - a).sin() * (basis_b - b).sin() * c.sin(),
),
c64::new(4.0 * c.cos(), 0.0),
c64::new(4.0, 0.0)
c64::new(4.0, 0.0),
];
let mut imax : usize = 0;
let mut imax: usize = 0;
let mut max_fid = 0.0;
// for i in 0..4 {
// for i in 0..4 {
for (i, trace) in traces.iter().enumerate() {
let fid = trace_to_fid(*trace) * basis_fidelity.powi(i as i32);
if fid > max_fid {
Expand All @@ -168,7 +183,7 @@ fn myabs2(z: c64) -> f64 {

/// Average gate fidelity is :math:`Fbar = (d + |Tr (Utarget \\cdot U^dag)|^2) / d(d+1)`
/// M. Horodecki, P. Horodecki and R. Horodecki, PRA 60, 1888 (1999)
fn trace_to_fid(trace : c64) -> f64 {
fn trace_to_fid(trace: c64) -> f64 {
(4.0 + myabs2(trace)) / 20.0
}

Expand Down

0 comments on commit ca07f86

Please sign in to comment.