Skip to content

Commit

Permalink
Add strucures and check compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapeyre committed Sep 17, 2024
1 parent f3500df commit 2846750
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 27 deletions.
3 changes: 3 additions & 0 deletions crates/accelerate/src/xx_decompose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

/// Conventions in journal article and Python code
/// A "program" means a unitary opeator.
pub mod circuits;
pub mod decomposer;
pub mod utilities;
Expand Down
27 changes: 23 additions & 4 deletions crates/accelerate/src/xx_decompose/polytopes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use crate::xx_decompose::types::Coordinate;
use crate::xx_decompose::utilities::EPSILON;

/// The raw data underlying a ConvexPolytope. Describes a single /// polytope, specified by families of `inequalities` and `equalities`, each
/// entry of which respectively corresponds to
///
Expand All @@ -6,12 +9,28 @@
/// and
///
/// equalities[j][0] + sum_i equalities[j][i] * xi == 0.
struct ConvexPolytopeData {
struct ConvexPolytopeData<const MI:usize, const NI: usize, const ME:usize, const NE: usize> {
inequalities: [[f64; MI]; NI],
equalities: [[f64; ME]; NE],
name: String,
}

/// The raw data of a union of convex polytopes.
struct PolytopeData {
data: Vec<ConvexPolytopeData>,
struct PolytopeData<const MI:usize, const NI: usize, const ME:usize, const NE: usize> {
convex_subpolytopes: Vec<ConvexPolytopeData<MI, NI, ME, NE>>,
}

// TODO: In the original, this is not a class-instance method. Could be I think.
/// Tests whether `polytope` contains `point.
fn polytope_has_element<const MI:usize, const NI: usize, const ME:usize, const NE: usize>
(polytope: ConvexPolytopeData<MI, NI, ME, NE>, point: &[f64; MI - 1]) -> bool {
polytope.inequalities
.iter()
.all(|ie| (-EPSILON <= ie[0] + point.iter().zip(&ie[1..]).map(|(p, i)| p * i).sum::<f64>()))
// &&
// polytope.equalities
// .iter()
// .all(|e| (e[0] + point.iter().zip(&e[1..]).map(|(p, i)| p * i).sum::<f64>() <= EPSILON))
}

/// Describes those two-qubit programs accessible to a given sequence of XX-type interactions.
Expand All @@ -27,7 +46,7 @@ struct XXPolytope {
impl XXPolytope {

// Method add_strength appears in the original Python
// But it is only called in the test suite.
// But it is only called in the test suite.
// fn add_strength() {}
}

Expand Down
20 changes: 12 additions & 8 deletions crates/accelerate/src/xx_decompose/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ pub(crate) struct Coordinate {

impl Coordinate {

pub(crate) fn reflect(&self, scalars: &[i32; 3]) -> Coordinate {
pub(crate) fn reflect(&self, scalars: &[f64; 3]) -> Coordinate {
Coordinate {
data: [self.data[0] * (scalars[0]) as f64,
self.data[1] * (scalars[1]) as f64,
self.data[2] * (scalars[2]) as f64,]
data: [self.data[0] * (scalars[0]),
self.data[1] * (scalars[1]),
self.data[2] * (scalars[2]),]
}
}

pub(crate) fn shift(&self, scalars: &[i32; 3]) -> Coordinate {
pub(crate) fn shift(&self, scalars: &[f64; 3]) -> Coordinate {
let pi2 = PI / 2.0;
Coordinate {
data: [pi2 * self.data[0] + (scalars[0]) as f64,
pi2 * self.data[1] + (scalars[1]) as f64,
pi2 * self.data[2] + (scalars[2]) as f64,]
data: [pi2 * self.data[0] + (scalars[0]),
pi2 * self.data[1] + (scalars[1]),
pi2 * self.data[2] + (scalars[2]),]
}
}

Expand All @@ -77,6 +77,10 @@ impl Coordinate {
let d = self.data[i as usize] - other.data[j as usize];
(d.abs() % PI).abs()
}

pub(crate) fn iter(&self) -> std::slice::Iter<'_, f64> {
self.data.iter()
}
}

// Forward indexing into `Coordinate` to the field `data`.
Expand Down
3 changes: 2 additions & 1 deletion crates/accelerate/src/xx_decompose/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ pub(crate) const EPSILON: f64 = 1e-6;
use std::f64::consts::PI;

// The logic in `safe_acos` is copied from the Python original.
// The following comment is copied as well.
// The following one-line comment is copied from the Python as well.
// TODO: THIS IS A STOPGAP!!!
//
/// Has the same behavior as `f64::acos` except that an
/// argument a bit greater than `1` or less than `-1` is
/// valid and returns the value at `1` or `-1`.
Expand Down
28 changes: 14 additions & 14 deletions crates/accelerate/src/xx_decompose/weyl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ pub(crate) static REFLECTION_NAMES: [ReflectionName; 4] = [ReflectionName::NoRef
// where reflection scalars (a, b, c) model the map (x, y, z) |-> (ax, by, cz),
// global phase is a complex unit, and gate constructors are applied in sequence
// and by conjugation to the first qubit and are passed pi as a parameter.
static reflection_options: [(&[i32; 3], Complex64, &[StandardGate]); 4] =
[(&[1, 1, 1], C_ONE, &[]), // 0
(&[-1, -1, 1], C_ONE, &[StandardGate::RZGate]), // 1
(&[-1, 1, -1], C_ONE, &[StandardGate::RYGate]), // 2
(&[1, -1, -1], C_ONE, &[StandardGate::RXGate]), // 3
static reflection_options: [(&[f64; 3], Complex64, &[StandardGate]); 4] =
[(&[1., 1., 1.], C_ONE, &[]), // 0
(&[-1., -1., 1.], C_ONE, &[StandardGate::RZGate]), // 1
(&[-1., 1., -1.], C_ONE, &[StandardGate::RYGate]), // 2
(&[1., -1., -1.], C_ONE, &[StandardGate::RXGate]), // 3
];


Expand All @@ -56,15 +56,15 @@ pub(crate) static SHIFT_NAMES: [ShiftName; 8] = [
ShiftName::XYZShift,
];

static shift_options: [(&[i32; 3], Complex64, &[StandardGate]); 8] =
[(&[0, 0, 0], C_ONE, &[]),
(&[0, 0, 1], IM, &[StandardGate::RZGate]),
(&[0, 1, 0], M_IM, &[StandardGate::RYGate]),
(&[0, 1, 1], C_ONE, &[StandardGate::RYGate, StandardGate::RZGate]),
(&[1, 0, 0], M_IM, &[StandardGate::RXGate]),
(&[1, 0, 1], C_ONE, &[StandardGate::RXGate, StandardGate::RZGate]),
(&[1, 1, 0], C_M_ONE, &[StandardGate::RXGate, StandardGate::RYGate]),
(&[1, 1, 1], M_IM, &[StandardGate::RXGate, StandardGate::RYGate, StandardGate::RZGate]),
static shift_options: [(&[f64; 3], Complex64, &[StandardGate]); 8] =
[(&[0., 0., 0.], C_ONE, &[]),
(&[0., 0., 1.], IM, &[StandardGate::RZGate]),
(&[0., 1., 0.], M_IM, &[StandardGate::RYGate]),
(&[0., 1., 1.], C_ONE, &[StandardGate::RYGate, StandardGate::RZGate]),
(&[1., 0., 0.], M_IM, &[StandardGate::RXGate]),
(&[1., 0., 1.], C_ONE, &[StandardGate::RXGate, StandardGate::RZGate]),
(&[1., 1., 0.], C_M_ONE, &[StandardGate::RXGate, StandardGate::RYGate]),
(&[1., 1., 1.], M_IM, &[StandardGate::RXGate, StandardGate::RYGate, StandardGate::RZGate]),
];


Expand Down

0 comments on commit 2846750

Please sign in to comment.