Skip to content

Commit

Permalink
applied Julien suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
MozammilQ committed Nov 17, 2024
1 parent 477e62e commit f5a92c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
6 changes: 4 additions & 2 deletions crates/accelerate/src/synthesis/linear/pmh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ pub fn synth_cnot_count_full_pmh(
let arrayview = matrix.as_array();
let mat: Array2<bool> = arrayview.to_owned();
let num_qubits = mat.nrows();
let section_size: Option<usize> =
section_size.and_then(|num| if num >= 0 { Some(num as usize) } else { None });

let instructions = synth_pmh(mat, section_size);
CircuitData::from_standard_gates(py, num_qubits as u32, instructions, Param::Float(0.0))
Expand All @@ -169,7 +171,7 @@ pub fn synth_cnot_count_full_pmh(
type Instruction = (StandardGate, SmallVec<[Param; 3]>, SmallVec<[Qubit; 2]>);
pub fn synth_pmh(
mat: Array2<bool>,
section_size: Option<i64>,
section_size: Option<usize>,
) -> impl DoubleEndedIterator<Item = Instruction> {
let mut mat = mat;
let num_qubits = mat.nrows(); // is a quadratic matrix
Expand All @@ -181,7 +183,7 @@ pub fn synth_pmh(
// until ~100 qubits.
let alpha = 0.56;
let blocksize = match section_size {
Some(section_size) => section_size as usize,
Some(section_size) => section_size,
None => std::cmp::max(2, (alpha * (num_qubits as f64).log2()).floor() as usize),
};

Expand Down
24 changes: 13 additions & 11 deletions crates/accelerate/src/synthesis/linear_phase/cnot_phase_synth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ pub fn synth_cnot_phase_aam(
angles: &Bound<PyList>,
section_size: Option<i64>,
) -> PyResult<CircuitData> {
// converting to Option<usize>
let section_size: Option<usize> =
section_size.and_then(|num| if num >= 0 { Some(num as usize) } else { None });

let s = cnots.as_array().to_owned();
let s = s.mapv(|x| x != 0);
let num_qubits = s.nrows();

let mut rust_angles = angles
Expand All @@ -80,7 +85,8 @@ pub fn synth_cnot_phase_aam(
})
.collect::<Vec<String>>();

let mut state = Array2::<u8>::eye(num_qubits);
let state = Array2::<u8>::eye(num_qubits);
let mut state = state.mapv(|x| x != 0);

let mut s_cpy = s.clone();
let mut q = vec![(
Expand Down Expand Up @@ -146,9 +152,7 @@ pub fn synth_cnot_phase_aam(
keep_iterating = false;
}
while qubit_idx < num_qubits {
if (qubit_idx != _ep)
&& (_s.row(qubit_idx).sum() as usize == _s.row(qubit_idx).len())
{
if (qubit_idx != _ep) && !_s.row(qubit_idx).iter().any(|&b| !b) {
if !cx_gate_done && !phase_loop_on {
keep_iterating = true;
cx_gate_done = true;
Expand Down Expand Up @@ -221,8 +225,8 @@ pub fn synth_cnot_phase_aam(
.axis_iter(numpy::ndarray::Axis(0))
.map(|row| {
std::cmp::max(
row.iter().filter(|&&x| x == 0).count(),
row.iter().filter(|&&x| x == 1).count(),
row.iter().filter(|&&x| !x).count(),
row.iter().filter(|&&x| x).count(),
)
})
.collect();
Expand All @@ -245,10 +249,10 @@ pub fn synth_cnot_phase_aam(
let mut cnots1_t_shape = (0_usize, 0_usize);
cnots1_t_shape.1 = cnots0_t_shape.1;
for cols in _s.columns() {
if cols[_j] == 0 {
if !cols[_j] {
cnots0_t_shape.0 += 1;
cnots0_t.append(&mut cols.to_vec());
} else if cols[_j] == 1 {
} else {
cnots1_t_shape.0 += 1;
cnots1_t.append(&mut cols.to_vec());
}
Expand Down Expand Up @@ -283,9 +287,7 @@ pub fn synth_cnot_phase_aam(
} // end 'outer_loop

if phase_done && cx_phase_done && !pmh_init_done {
synth_pmh_iter = Some(Box::new(
synth_pmh(state.mapv(|x| x != 0), section_size).rev(),
));
synth_pmh_iter = Some(Box::new(synth_pmh(state.clone(), section_size).rev()));
pmh_init_done = true;
}

Expand Down

0 comments on commit f5a92c9

Please sign in to comment.