Skip to content

Commit

Permalink
chore: Update hugr version
Browse files Browse the repository at this point in the history
  • Loading branch information
lmondada committed Oct 5, 2023
1 parent 61be578 commit 5d03c23
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ members = ["pyrs", "compile-rewriter", "taso-optimiser"]

[workspace.dependencies]

quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "af664e3" }
quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "09494f1" }
portgraph = { version = "0.9", features = ["serde"] }
pyo3 = { version = "0.19" }
itertools = { version = "0.11.0" }
Expand Down
9 changes: 6 additions & 3 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ fn update_signature<C: HugrMut + Circuit + ?Sized>(
};
let new_inp_op = Input::new(inp_types.clone());
let inp_exts = circ.get_nodetype(inp).input_extensions().cloned();
circ.replace_op(inp, NodeType::new(new_inp_op, inp_exts));
circ.replace_op(inp, NodeType::new(new_inp_op, inp_exts))
.unwrap();

// Update output node if necessary.
let out_types = out_index.map(|out_index| {
Expand All @@ -277,7 +278,8 @@ fn update_signature<C: HugrMut + Circuit + ?Sized>(
};
let new_out_op = Output::new(out_types.clone());
let inp_exts = circ.get_nodetype(out).input_extensions().cloned();
circ.replace_op(out, NodeType::new(new_out_op, inp_exts));
circ.replace_op(out, NodeType::new(new_out_op, inp_exts))
.unwrap();
out_types
});

Expand All @@ -291,7 +293,8 @@ fn update_signature<C: HugrMut + Circuit + ?Sized>(
}
let new_dfg_op = DFG { signature };
let inp_exts = circ.get_nodetype(circ.root()).input_extensions().cloned();
circ.replace_op(circ.root(), NodeType::new(new_dfg_op, inp_exts));
circ.replace_op(circ.root(), NodeType::new(new_dfg_op, inp_exts))
.unwrap();
}

impl<T> Circuit for T where T: HugrView {}
Expand Down
43 changes: 40 additions & 3 deletions src/portmatching/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use std::{
};

use super::{CircuitPattern, NodeID, PEdge, PNode};
use hugr::hugr::views::sibling_subgraph::{ConvexChecker, InvalidReplacement, InvalidSubgraph};
use hugr::hugr::views::sibling_subgraph::{
ConvexChecker, InvalidReplacement, InvalidSubgraph, InvalidSubgraphBoundary,
};
use hugr::hugr::PortIndex;
use hugr::{hugr::views::SiblingSubgraph, ops::OpType, Hugr, Node, Port};
use itertools::Itertools;
Expand Down Expand Up @@ -373,9 +375,14 @@ pub enum MatcherSerialisationError {
impl From<InvalidSubgraph> for InvalidPatternMatch {
fn from(value: InvalidSubgraph) -> Self {
match value {
InvalidSubgraph::NotConvex => InvalidPatternMatch::NotConvex,
// A non-convex subgraph might show itself as a disconnected boundary
// in the subgraph
InvalidSubgraph::NotConvex
| InvalidSubgraph::InvalidBoundary(
InvalidSubgraphBoundary::DisconnectedBoundaryPort(_, _),
) => InvalidPatternMatch::NotConvex,
InvalidSubgraph::EmptySubgraph => InvalidPatternMatch::EmptyMatch,
InvalidSubgraph::NoSharedParent | InvalidSubgraph::InvalidBoundary => {
InvalidSubgraph::NoSharedParent | InvalidSubgraph::InvalidBoundary(_) => {
InvalidPatternMatch::InvalidSubcircuit
}
}
Expand Down Expand Up @@ -449,6 +456,7 @@ fn handle_match_error<T>(match_res: Result<T, InvalidPatternMatch>, root: Node)
mod tests {
use hugr::Hugr;
use itertools::Itertools;
use rstest::{fixture, rstest};

use crate::utils::build_simple_circuit;
use crate::T2Op;
Expand All @@ -473,6 +481,26 @@ mod tests {
.unwrap()
}

#[fixture]
fn cx_cx_3() -> Hugr {
build_simple_circuit(3, |circ| {
circ.append(T2Op::CX, [0, 1]).unwrap();
circ.append(T2Op::CX, [2, 1]).unwrap();
Ok(())
})
.unwrap()
}

#[fixture]
fn cx_cx() -> Hugr {
build_simple_circuit(2, |circ| {
circ.append(T2Op::CX, [0, 1]).unwrap();
circ.append(T2Op::CX, [0, 1]).unwrap();
Ok(())
})
.unwrap()
}

#[test]
fn construct_matcher() {
let circ = h_cx();
Expand Down Expand Up @@ -503,4 +531,13 @@ mod tests {

assert_eq!(buf, buf2);
}

#[rstest]
fn cx_cx_replace_to_id(cx_cx: Hugr, cx_cx_3: Hugr) {
let p = CircuitPattern::try_from_circuit(&cx_cx_3).unwrap();
let m = PatternMatcher::from_patterns(vec![p]);

let matches = m.find_matches(&cx_cx);
assert_eq!(matches.len(), 0);
}
}
2 changes: 1 addition & 1 deletion src/portmatching/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl CircuitPattern {
}

/// Construct a pattern from a circuit.
pub fn try_from_circuit<C: Circuit>(circuit: &C) -> Result<Self, InvalidPattern> {
pub fn try_from_circuit(circuit: &impl Circuit) -> Result<Self, InvalidPattern> {
if circuit.num_gates() == 0 {
return Err(InvalidPattern::EmptyCircuit);
}
Expand Down

0 comments on commit 5d03c23

Please sign in to comment.