Skip to content

Commit 7498419

Browse files
ElePTmtreinish
andauthored
Apply inline suggestions from Matt's code review
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
1 parent eb815d1 commit 7498419

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

crates/accelerate/src/unitary_synthesis.rs

+14-20
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,7 @@ fn get_2q_decomposers_from_target(
712712
op.operation.matrix(&op.params).unwrap().view(),
713713
None,
714714
None,
715-
)
716-
.unwrap()
715+
)?
717716
.a;
718717
let mut fidelity_value = match available_2q_props.get(name) {
719718
Some(&(_, error)) => 1.0 - error.unwrap_or(0.0),
@@ -781,17 +780,15 @@ fn get_2q_decomposers_from_target(
781780
&embodiments_dict,
782781
pi2_decomposer,
783782
))?;
784-
let decomposer_gate = decomposer.getattr("gate")?;
783+
let decomposer_gate = decomposer.getattr(intern!(py, "gate"))?;
785784

786-
// .getattr("name")?
787-
// .extract::<String>()?;
788785

789786
decomposers.push(DecomposerElement {
790787
decomposer: DecomposerType::XXDecomposer(decomposer.into()),
791-
decomp_gate: decomposer_gate.getattr("name")?.extract::<String>()?,
788+
decomp_gate: decomposer_gate.getattr(intern!(py, "name"))?.extract::<String>()?,
792789
decomp_gate_params: Some(
793790
decomposer_gate
794-
.getattr("params")?
791+
.getattr(intern!(py, "params"))?
795792
.extract::<SmallVec<[Param; 3]>>()?,
796793
),
797794
});
@@ -816,7 +813,7 @@ fn preferred_direction(
816813
let reverse_qubits: SmallVec<[PhysicalQubit; 2]> = qubits.iter().rev().copied().collect();
817814

818815
let compute_cost =
819-
|lengths: bool, q_tuple: &SmallVec<[PhysicalQubit; 2]>, in_cost: f64| -> PyResult<f64> {
816+
|lengths: bool, q_tuple: &[PhysicalQubit; 2], in_cost: f64| -> PyResult<f64> {
820817
let cost = match target.qargs_for_operation_name(&decomposer.decomp_gate) {
821818
Ok(_) => match target[&decomposer.decomp_gate].get(Some(q_tuple)) {
822819
Some(Some(_props)) => {
@@ -903,14 +900,15 @@ fn synth_su4(
903900
let synth_dag = match &decomposer_2q.decomposer {
904901
// the output will be a dag in the relative basis
905902
DecomposerType::XXDecomposer(decomposer) => {
906-
let mut kwargs = HashMap::<&str, bool>::new();
907-
kwargs.insert("approximate", is_approximate);
908-
kwargs.insert("use_dag", true);
903+
let kwargs: HashMap<&str, bool> = [
904+
("approximate", is_approximate),
905+
("use_dag", true)
906+
].iter().collect();
909907
// can we avoid cloning the matrix to pass it to python?
910908
decomposer
911909
.call_method_bound(
912910
py,
913-
"__call__",
911+
intern!(py, "__call__"),
914912
(su4_mat.clone().into_pyarray_bound(py),),
915913
Some(&kwargs.into_py_dict_bound(py)),
916914
)?
@@ -1025,16 +1023,12 @@ fn reversed_synth_su4(
10251023
let mut su4_mat_mm = su4_mat.clone();
10261024

10271025
// Swap rows 1 and 2
1028-
let row_1 = su4_mat_mm.slice(s![1, ..]).to_owned();
1029-
let row_2 = su4_mat_mm.slice(s![2, ..]).to_owned();
1030-
su4_mat_mm.slice_mut(s![1, ..]).assign(&row_2);
1031-
su4_mat_mm.slice_mut(s![2, ..]).assign(&row_1);
1026+
let (mut row_1, mut row_2) = su4_mat_mm.multi_slice_mut((s![1, ..], s![2, ..]));
1027+
azip!((x in &mut row_1, y in &mut row2) (*x, *y) = (*y, *x))
10321028

10331029
// Swap columns 1 and 2
1034-
let col_1 = su4_mat_mm.slice(s![.., 1]).to_owned();
1035-
let col_2 = su4_mat_mm.slice(s![.., 2]).to_owned();
1036-
su4_mat_mm.slice_mut(s![.., 1]).assign(&col_2);
1037-
su4_mat_mm.slice_mut(s![.., 2]).assign(&col_1);
1030+
let (mut col_1, mut col_2) = su4_mat_mm.multi_slice_mut((s![.., 1], s![.., 2]));
1031+
azip!((x in &mut col_1, y in &mut col_2) (*x, *y) = (*y, *x))
10381032

10391033
let synth_dag = match &decomposer_2q.decomposer {
10401034
DecomposerType::XXDecomposer(decomposer) => {

0 commit comments

Comments
 (0)