Skip to content

Commit

Permalink
Ok this one compiles
Browse files Browse the repository at this point in the history
I have been observing a small number of sizes of array-like data. So I am
trying to make these Array s i.e. with static size. But this is difficult.
I plan to replace these with Vec. This commit starts this.
  • Loading branch information
jlapeyre committed Sep 18, 2024
1 parent 2846750 commit c8e585c
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 16 deletions.
3 changes: 3 additions & 0 deletions crates/accelerate/src/xx_decompose/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]
#![allow(unused)]
// This code is part of Qiskit.
//
// (C) Copyright IBM 2022
Expand All @@ -19,3 +21,4 @@ pub mod utilities;
mod types;
mod weyl;
mod polytopes;
mod paths;
285 changes: 285 additions & 0 deletions crates/accelerate/src/xx_decompose/paths.rs
Original file line number Diff line number Diff line change
@@ -1 +1,286 @@
// from paths.py

use crate::xx_decompose::polytopes::{ Polytope, AlcoveDetails, AF};

/// Assembles a coordinate in the system used by `xx_region_polytope`.
fn get_augmented_coordinate(target_coordinate: &[f64; 3], strengths: &[f64]) -> Vec<f64> {
let (beta, strengths) = strengths.split_last().unwrap();
let mut strengths = Vec::from(strengths);
strengths.extend([0., 0.]);
strengths.sort_by(|a, b| a.partial_cmp(b).unwrap());
let ssum: f64 = strengths.iter().sum();
let n = strengths.len();
let interaction_coordinate = [ssum, strengths[n-1], strengths[n-2], *beta];
let mut target_coordinate = Vec::from(target_coordinate);
target_coordinate.extend(interaction_coordinate);
target_coordinate
}

fn make_polys1() {
let xx_lift_polytope: Vec<Polytope<'static, MLIFT>> =
vec! [
Polytope {
ineqs: &LIFT_INEQ1,
eqs: Some(&LIFT_EQ1),
alcove_details: Some((AlcoveDetails::Unreflected, AF::B3))
},
Polytope {
ineqs: &LIFT_INEQ2,
eqs: Some(&LIFT_EQ2),
alcove_details: Some((AlcoveDetails::Unreflected, AF::B1))
},
Polytope {
ineqs: &LIFT_INEQ3,
eqs: Some(&LIFT_EQ3),
alcove_details: Some((AlcoveDetails::Reflected, AF::B1))
},
Polytope {
ineqs: &LIFT_INEQ4,
eqs: Some(&LIFT_EQ4),
alcove_details: Some((AlcoveDetails::Reflected, AF::B3))
},
];

let xx_region_polytope: Vec<Polytope<'static, MREGION>> =
vec! [
Polytope {
ineqs: &REGION_INEQ1,
eqs: None,
alcove_details: Some((AlcoveDetails::Unreflected, AF::B3))
},
Polytope {
ineqs: &REGION_INEQ2,
eqs: None,
alcove_details: Some((AlcoveDetails::Reflected, AF::B3))
},
Polytope {
ineqs: &REGION_INEQ3,
eqs: None,
alcove_details: Some((AlcoveDetails::Unreflected, AF::B1))
},
Polytope {
ineqs: &REGION_INEQ4,
eqs: None,
alcove_details: Some((AlcoveDetails::Reflected, AF::B1))
},
];
}

const MLIFT: usize = 11;

static LIFT_INEQ1: [[i32; MLIFT]; 29] = [
[0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, -1, 0, -1, 0, 0, 0, 0, 0, 0, 0],
[0, -1, -1, -1, 0, 0, 0, 1, 0, 0, 0],
[0, 1, -1, -1, 0, 0, 0, 1, -2, 0, 0],
[0, 0, -1, 0, 0, 0, 0, 1, -1, -1, 0],
[0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[1, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, -1, -1, -1, 1, 0, 0, 1],
[0, 0, 0, 0, 1, -1, -1, 1, -2, 0, 1],
[0, 0, 0, 0, 1, -1, -1, 1, 0, 0, -1],
[0, 0, 0, 0, 0, 0, -1, 1, -1, -1, 1],
[0, 0, 0, 0, 0, 0, -1, 1, -1, 0, 0],
[0, -1, -1, 0, 1, 1, 0, 0, 0, 0, 1],
[2, -1, -1, 0, -1, -1, 0, 0, 0, 0, -1],
[0, 1, 1, 0, -1, -1, 0, 0, 0, 0, 1],
[0, -1, 1, 0, 1, -1, 0, 0, 0, 0, 1],
[0, 1, -1, 0, -1, 1, 0, 0, 0, 0, 1],
[0, 1, -1, 0, 1, -1, 0, 0, 0, 0, -1],
];

static LIFT_EQ1: [[i32; MLIFT]; 1] = [[0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0]];
// equalities=[[0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0]],


static LIFT_INEQ2: [[i32; MLIFT]; 29] = [
[0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, -1, 0, -1, 0, 0, 0, 0, 0, 0, 0],
[0, -1, -1, -1, 0, 0, 0, 1, 0, 0, 0],
[0, -1, -1, 1, 0, 0, 0, 1, -2, 0, 0],
[0, 0, -1, 0, 0, 0, 0, 1, -1, -1, 0],
[0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[1, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, -1, -1, -1, 1, 0, 0, 1],
[0, 0, 0, 0, 1, -1, -1, 1, -2, 0, 1],
[0, 0, 0, 0, 1, -1, -1, 1, 0, 0, -1],
[0, 0, 0, 0, 0, 0, -1, 1, -1, -1, 1],
[0, 0, 0, 0, 0, 0, -1, 1, -1, 0, 0],
[0, -1, -1, 0, 0, 1, 1, 0, 0, 0, 1],
[2, -1, -1, 0, 0, -1, -1, 0, 0, 0, -1],
[0, 1, 1, 0, 0, -1, -1, 0, 0, 0, 1],
[0, -1, 1, 0, 0, 1, -1, 0, 0, 0, 1],
[0, 1, -1, 0, 0, -1, 1, 0, 0, 0, 1],
[0, 1, -1, 0, 0, 1, -1, 0, 0, 0, -1],
];

static LIFT_EQ2: [[i32; MLIFT]; 1] = [[0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0]];


static LIFT_INEQ3: [[i32; MLIFT]; 29] = [
[0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, -1, 0, -1, 0, 0, 0, 0, 0, 0, 0],
[1, -1, -1, -1, 0, 0, 0, 1, -2, 0, 0],
[-1, -1, -1, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, -1, 0, 0, 0, 0, 1, -1, -1, 0],
[0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[1, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0],
[-1, 0, 0, 0, 1, -1, -1, 1, 0, 0, 1],
[1, 0, 0, 0, -1, -1, -1, 1, 0, 0, -1],
[1, 0, 0, 0, -1, -1, -1, 1, -2, 0, 1],
[0, 0, 0, 0, 0, 0, -1, 1, -1, -1, 1],
[0, 0, 0, 0, 0, 0, -1, 1, -1, 0, 0],
[0, -1, -1, 0, 0, 1, 1, 0, 0, 0, 1],
[2, -1, -1, 0, 0, -1, -1, 0, 0, 0, -1],
[0, 1, 1, 0, 0, -1, -1, 0, 0, 0, 1],
[0, -1, 1, 0, 0, 1, -1, 0, 0, 0, 1],
[0, 1, -1, 0, 0, -1, 1, 0, 0, 0, 1],
[0, 1, -1, 0, 0, 1, -1, 0, 0, 0, -1],
];

static LIFT_EQ3: [[i32; MLIFT]; 1] = [[0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0]];

static LIFT_INEQ4: [[i32; MLIFT]; 29] = [
[0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, -1, 0, -1, 0, 0, 0, 0, 0, 0, 0],
[1, -1, -1, -1, 0, 0, 0, 1, -2, 0, 0],
[-1, 1, -1, -1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, -1, 0, 0, 0, 0, 1, -1, -1, 0],
[0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[1, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0],
[-1, 0, 0, 0, 1, -1, -1, 1, 0, 0, 1],
[1, 0, 0, 0, -1, -1, -1, 1, 0, 0, -1],
[1, 0, 0, 0, -1, -1, -1, 1, -2, 0, 1],
[0, 0, 0, 0, 0, 0, -1, 1, -1, -1, 1],
[0, 0, 0, 0, 0, 0, -1, 1, -1, 0, 0],
[0, -1, -1, 0, 1, 1, 0, 0, 0, 0, 1],
[2, -1, -1, 0, -1, -1, 0, 0, 0, 0, -1],
[0, 1, 1, 0, -1, -1, 0, 0, 0, 0, 1],
[0, -1, 1, 0, 1, -1, 0, 0, 0, 0, 1],
[0, 1, -1, 0, -1, 1, 0, 0, 0, 0, 1],
[0, 1, -1, 0, 1, -1, 0, 0, 0, 0, -1],
];

static LIFT_EQ4: [[i32; MLIFT]; 1] = [[0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0]];

const MREGION: usize = 8;

static REGION_INEQ1: [[i32; MREGION]; 15] = [
[0, 0, 0, 0, 0, 1, -1, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, -2, 0, 0],
[1, 0, 0, 0, 0, 0, 0, -2],
[0, 0, 1, -1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, -1, -1, 0],
[1, -1, -1, 0, 0, 0, 0, 0],
[0, -1, -1, -1, 1, 0, 0, 1],
[0, 1, -1, 0, 0, 0, 0, 0],
[0, 1, -1, -1, 1, -2, 0, 1],
[0, 1, -1, -1, 1, 0, 0, -1],
[0, 0, 0, -1, 1, -1, 0, 0],
[0, 0, -1, 0, 1, -1, -1, 1],
[0, 0, 0, 0, 0, 0, 0, 1],
];

static REGION_INEQ2: [[i32; MREGION]; 15] = [
[0, 0, 0, 0, 0, 1, -1, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, -2, 0, 0],
[1, 0, 0, 0, 0, 0, 0, -2],
[0, 0, 1, -1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, -1, -1, 0],
[1, -1, -1, 0, 0, 0, 0, 0],
[1, -1, -1, -1, 1, -2, 0, 1],
[0, 1, -1, 0, 0, 0, 0, 0],
[-1, 1, -1, -1, 1, 0, 0, 1],
[1, -1, -1, -1, 1, 0, 0, -1],
[0, 0, 0, -1, 1, -1, 0, 0],
[0, 0, -1, 0, 1, -1, -1, 1],
[0, 0, 0, 0, 0, 0, 0, 1],
];

static REGION_INEQ3: [[i32; MREGION]; 16] = [
[0, 0, 0, 0, 0, 1, -1, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, -2, 0, 0],
[1, 0, 0, 0, 0, 0, 0, -2],
[0, 1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[1, -1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, -1, -1, 0],
[0, 1, -1, -1, 1, -2, 0, 1],
[0, -1, -1, -1, 1, 0, 0, 1],
[0, 0, 1, -1, 0, 0, 0, 0],
[1, -1, 1, -1, 0, 0, 0, -1],
[0, 1, 1, -1, 1, -2, 0, -1],
[0, -1, 1, -1, 1, 0, 0, -1],
[0, 0, 0, -1, 1, -1, -1, 1],
[0, 0, 0, 0, 0, 0, 0, 1],

];

static REGION_INEQ4: [[i32; MREGION]; 16] = [
[0, 0, 0, 0, 0, 1, -1, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 0, -2, 0, 0],
[1, 0, 0, 0, 0, 0, 0, -2],
[0, 1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[1, -1, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, -1, -1, 0],
[-1, 1, -1, -1, 1, 0, 0, 1],
[1, -1, -1, -1, 1, -2, 0, 1],
[0, 0, 1, -1, 0, 0, 0, 0],
[1, -1, 1, -1, 0, 0, 0, -1],
[-1, 1, 1, -1, 1, 0, 0, -1],
[1, -1, 1, -1, 1, -2, 0, -1],
[0, 0, 0, -1, 1, -1, -1, 1],
[0, 0, 0, 0, 0, 0, 0, 1],

];
65 changes: 53 additions & 12 deletions crates/accelerate/src/xx_decompose/polytopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,65 @@ use crate::xx_decompose::utilities::EPSILON;
/// and
///
/// equalities[j][0] + sum_i equalities[j][i] * xi == 0.
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 names of the four explicit ConvexPolytopeData are below.
// These occur in both xx_lift... and xx_region... But the order is slightly different.
// Combinations obey:
// "ah" and "B3" are always together
// "af" and "B1" are always together
// So we use just B1 and B3 to distinguish the pairs.
// "A unreflected", "B unreflected" and "slant" always appear together.
// "A reflected", "B reflected" and "strength" always appear together.
// We label these triples with just "reflected" and "unreflected".
//
// If this code is modified so that the characteristics of the polytopes
// changes in the future, the simplifications mentioned above may not hold.
//
// "A unreflected ∩ ah slant ∩ al frustrum ∩ B alcove ∩ B unreflected ∩ AF=B3"
// "A reflected ∩ ah strength ∩ al frustrum ∩ B alcove ∩ B reflected ∩ AF=B3"
// "A unreflected ∩ af slant ∩ al frustrum ∩ B alcove ∩ B unreflected ∩ AF=B1"
// "A reflected ∩ af strength ∩ al frustrum ∩ B alcove ∩ B reflected ∩ AF=B1"

//#[allow(non_camel_case_types)]
pub(crate) enum AlcoveDetails {
Reflected,
Unreflected,
}

// B2 is also referenced in the Python code.
// But no data carries a "tag" B2, so that code is never used.
pub(crate) enum AF {
B1,
B3,
}

/// The raw data of a union of convex polytopes.
struct PolytopeData<const MI:usize, const NI: usize, const ME:usize, const NE: usize> {
convex_subpolytopes: Vec<ConvexPolytopeData<MI, NI, ME, NE>>,
static polys: Vec<Polytope<'static, 11>> = vec! [ ];

pub(crate) struct Polytope<'a, const NCOLS: usize> {
pub(crate) ineqs: &'a [[i32; NCOLS]],
pub(crate) eqs: Option<&'a[[i32; NCOLS]]>,
pub(crate) alcove_details: Option<(AlcoveDetails, AF)>
}

pub(crate) struct ConvexPolytopeData<'a, const MI:usize, const NI: usize, const ME:usize, const NE: usize> {
pub(crate) inequalities: [[i32; MI]; NI],
pub(crate) equalities: [[i32; ME]; NE],
pub(crate) name: &'a str,
}

// /// The raw data of a union of convex polytopes.
// pub(crate) struct PolytopeData<'a, const MI:usize, const NI: usize, const ME:usize, const NE: usize, const NC: usize> {
// pub(crate) convex_subpolytopes: [ConvexPolytopeData<'a, MI, NI, ME, NE>; NC],
// }

// 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: ConvexPolytopeData<MI, NI, ME, NE>, point: &Vec<f64>) -> bool {
polytope.inequalities
.iter()
.all(|ie| (-EPSILON <= ie[0] + point.iter().zip(&ie[1..]).map(|(p, i)| p * i).sum::<f64>()))
.all(|ie| (-EPSILON <= ie[0] as f64 + point.iter().zip(&ie[1..]).map(|(p, i)| p * *i as f64).sum::<f64>()))
// &&
// polytope.equalities
// .iter()
Expand Down Expand Up @@ -99,7 +140,7 @@ static A1: [[[f64; 3]; 1] ; 7] =

[[ 0., 0., -1.]]];

static A1inv: [[[f64; 1]; 3] ; 7] =
static A1INV: [[[f64; 1]; 3] ; 7] =
[[[ 0.5 ],
[-0.5 ],
[ 0. ]],
Expand Down Expand Up @@ -198,7 +239,7 @@ static A2: [[[f64; 3]; 2]; 21] =
// A3 collects all triples of rows in A
// A3inv collects the pseudo-inverse of each 3x3 matrix.

static A2inv: [[[f64; 2]; 3]; 21] =
static A2INV: [[[f64; 2]; 3]; 21] =
[[[ 0.6666666666666666, 0.3333333333333333],
[-0.3333333333333333, 0.3333333333333333],
[-0.3333333333333333, -0.6666666666666666]],
Expand Down Expand Up @@ -425,7 +466,7 @@ static A3: [[[f64; 3]; 3]; 35] =
[ 1., -1., -1.],
[ 0., 0., -1.]]];

static A3inv: [[[f64; 3]; 3]; 35] =
static A3INV: [[[f64; 3]; 3]; 35] =
[[[ 1. , 1. , 1. ],
[-0. , 1. , 1. ],
[-0. , -0. , 1. ]],
Expand Down
Loading

0 comments on commit c8e585c

Please sign in to comment.