Skip to content

Commit e934ad7

Browse files
committed
simplify measurements
1 parent 928c15a commit e934ad7

1 file changed

Lines changed: 20 additions & 24 deletions

File tree

src/lib.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ pub mod graphsim {
1313
use pyo3::prelude::*;
1414
use std::{
1515
collections::{HashMap, HashSet, VecDeque},
16-
fmt::{self, Debug, Display, Formatter},
17-
iter::repeat_n,
18-
ops::{Index, IndexMut, Mul},
16+
fmt::{Debug, Display, Formatter},
17+
iter::{once, repeat_n},
18+
ops::Mul,
1919
};
2020

2121
use rand::{
@@ -300,7 +300,7 @@ pub mod graphsim {
300300
}
301301

302302
let node_nbs = self.adjacent[node].clone();
303-
let other_nbs = self.adjacent[other].clone();
303+
let mut other_nbs = self.adjacent[other].clone();
304304

305305
let mut procced_edges: HashSet<(NodeIdx, NodeIdx)> = HashSet::new();
306306
for nval in node_nbs.iter() {
@@ -309,18 +309,20 @@ pub mod graphsim {
309309
true => (nval, oval),
310310
false => (oval, nval),
311311
};
312-
if nval != oval && !procced_edges.contains(&(combined.0, combined.1)) {
312+
if nval != oval {
313313
procced_edges.insert(combined);
314-
self.toggle_edge(combined.0, combined.1);
315314
}
316315
}
317316
}
318317

319-
let intersection: Vec<_> = node_nbs.intersection(&other_nbs).into_iter().collect();
320-
let ilen = intersection.len();
321-
for i in 0..ilen {
322-
for j in (i + 1)..ilen {
323-
self.toggle_edge(intersection[i], intersection[j]);
318+
for (i, j) in procced_edges {
319+
self.toggle_edge(i, j);
320+
}
321+
322+
other_nbs.intersect_with(&node_nbs);
323+
for (idx, i) in other_nbs.iter().enumerate() {
324+
for j in other_nbs.iter().skip(idx) {
325+
self.toggle_edge(i, j);
324326
}
325327
}
326328

@@ -335,21 +337,18 @@ pub mod graphsim {
335337
fn int_measure_y(&mut self, node: NodeIdx) -> MeasurementResult {
336338
let res = rand::rng().random();
337339

338-
for other in self.adjacent[node].clone().iter() {
340+
let adj = self.adjacent[node].clone();
341+
342+
for other in adj.iter() {
339343
match res {
340344
MeasurementResult::PlusOne => self.vop[other] = self.vop[other] * S_GATE,
341345
MeasurementResult::MinusOne => self.vop[other] = self.vop[other] * SDAG_GATE,
342346
}
343347
}
344348

345-
let adj: Vec<_> = self.adjacent[node].clone().into_iter().collect();
346-
let nlen = adj.len();
347-
348-
for i in 0..nlen {
349-
let nval = adj[i];
350-
for j in i + 1..=nlen {
351-
let oval = if j == nlen { node } else { adj[j] };
352-
self.toggle_edge(nval, oval);
349+
for (idx, i) in adj.iter().enumerate() {
350+
for j in adj.iter().skip(idx).chain(once(node)) {
351+
self.toggle_edge(i, j);
353352
}
354353
}
355354

@@ -643,10 +642,7 @@ pub mod graphsim {
643642
/// Simulate measurements on a set of `qubits` without modifying the real state.
644643
///
645644
/// Returns a map from qubit index to `Outcome` (result and axis used).
646-
pub fn peek_measure_set(
647-
&self,
648-
qubits: HashSet<NodeIdx>,
649-
) -> std::collections::HashMap<NodeIdx, Outcome> {
645+
pub fn peek_measure_set(&self, qubits: HashSet<NodeIdx>) -> HashMap<NodeIdx, Outcome> {
650646
let mut changeset = self.clone();
651647
qubits
652648
.iter()

0 commit comments

Comments
 (0)