Skip to content

Commit

Permalink
Finish applying ownership suggestion from Ray
Browse files Browse the repository at this point in the history
  • Loading branch information
ElePT committed Oct 3, 2024
1 parent 388c744 commit eaaeb81
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions crates/accelerate/src/unitary_synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,11 @@ fn synth_su4_sequence(
_ => panic!(),
};
if synth_dir != preferred_dir {
reversed_synth_su4_sequence(su4_mat, decomposer_2q, approximation_degree)
reversed_synth_su4_sequence(
su4_mat.clone(),
decomposer_2q,
approximation_degree,
)
} else {
Ok(sequence)
}
Expand All @@ -962,22 +966,22 @@ fn synth_su4_sequence(
}

fn reversed_synth_su4_sequence(
su4_mat: Array2<Complex64>,
mut su4_mat: Array2<Complex64>,
decomposer_2q: &DecomposerElement,
approximation_degree: Option<f64>,
) -> PyResult<TwoQubitUnitarySequence> {
let is_approximate = approximation_degree.is_none() || approximation_degree.unwrap() != 1.0;
// Swap rows 1 and 2
let (mut row_1, mut row_2) = su4_mat_mm.multi_slice_mut((s![1, ..], s![2, ..]));
let (mut row_1, mut row_2) = su4_mat.multi_slice_mut((s![1, ..], s![2, ..]));
azip!((x in &mut row_1, y in &mut row_2) (*x, *y) = (*y, *x));

// Swap columns 1 and 2
let (mut col_1, mut col_2) = su4_mat_mm.multi_slice_mut((s![.., 1], s![.., 2]));
let (mut col_1, mut col_2) = su4_mat.multi_slice_mut((s![.., 1], s![.., 2]));
azip!((x in &mut col_1, y in &mut col_2) (*x, *y) = (*y, *x));

let mut synth =
if let DecomposerType::TwoQubitBasisDecomposer(decomp) = &decomposer_2q.decomposer {
decomp.call_inner(su4_mat_mm.view(), None, is_approximate, None)?
decomp.call_inner(su4_mat.view(), None, is_approximate, None)?
} else {
panic!("reversed_synth_su4_sequence should only be called for TwoQubitBasisDecomposer.")
};
Expand Down Expand Up @@ -1041,7 +1045,12 @@ fn synth_su4_dag(
_ => panic!("There are no more than 2 possible synth directions."),
};
if synth_dir != preferred_dir {
reversed_synth_su4_dag(py, su4_mat, decomposer_2q, approximation_degree)
reversed_synth_su4_dag(
py,
su4_mat.clone(),
decomposer_2q,
approximation_degree,
)
} else {
Ok(synth_dag)
}
Expand All @@ -1053,19 +1062,18 @@ fn synth_su4_dag(

fn reversed_synth_su4_dag(
py: Python<'_>,
su4_mat: &Array2<Complex64>,
mut su4_mat: Array2<Complex64>,
decomposer_2q: &DecomposerElement,
approximation_degree: Option<f64>,
) -> PyResult<DAGCircuit> {
let is_approximate = approximation_degree.is_none() || approximation_degree.unwrap() != 1.0;
let mut su4_mat_mm = su4_mat.clone();

// Swap rows 1 and 2
let (mut row_1, mut row_2) = su4_mat_mm.multi_slice_mut((s![1, ..], s![2, ..]));
let (mut row_1, mut row_2) = su4_mat.multi_slice_mut((s![1, ..], s![2, ..]));
azip!((x in &mut row_1, y in &mut row_2) (*x, *y) = (*y, *x));

// Swap columns 1 and 2
let (mut col_1, mut col_2) = su4_mat_mm.multi_slice_mut((s![.., 1], s![.., 2]));
let (mut col_1, mut col_2) = su4_mat.multi_slice_mut((s![.., 1], s![.., 2]));
azip!((x in &mut col_1, y in &mut col_2) (*x, *y) = (*y, *x));

let synth_dag = if let DecomposerType::XXDecomposer(decomposer) = &decomposer_2q.decomposer {
Expand All @@ -1075,7 +1083,7 @@ fn reversed_synth_su4_dag(
decomposer
.call_bound(
py,
(su4_mat_mm.clone().into_pyarray_bound(py),),
(su4_mat.clone().into_pyarray_bound(py),),
Some(&kwargs.into_py_dict_bound(py)),
)?
.extract::<DAGCircuit>(py)?
Expand Down

0 comments on commit eaaeb81

Please sign in to comment.