From 5d03c23a970372e3afc172610edc5be744370d23 Mon Sep 17 00:00:00 2001 From: Luca Mondada Date: Thu, 5 Oct 2023 16:16:26 +0200 Subject: [PATCH] chore: Update hugr version --- Cargo.toml | 2 +- src/circuit.rs | 9 +++++--- src/portmatching/matcher.rs | 43 ++++++++++++++++++++++++++++++++++--- src/portmatching/pattern.rs | 2 +- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 85c4770f..1283ca25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/src/circuit.rs b/src/circuit.rs index 3ae2b8a1..7d00611b 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -262,7 +262,8 @@ fn update_signature( }; 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| { @@ -277,7 +278,8 @@ fn update_signature( }; 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 }); @@ -291,7 +293,8 @@ fn update_signature( } 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 Circuit for T where T: HugrView {} diff --git a/src/portmatching/matcher.rs b/src/portmatching/matcher.rs index 2da3fa45..8577e2b3 100644 --- a/src/portmatching/matcher.rs +++ b/src/portmatching/matcher.rs @@ -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; @@ -373,9 +375,14 @@ pub enum MatcherSerialisationError { impl From 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 } } @@ -449,6 +456,7 @@ fn handle_match_error(match_res: Result, root: Node) mod tests { use hugr::Hugr; use itertools::Itertools; + use rstest::{fixture, rstest}; use crate::utils::build_simple_circuit; use crate::T2Op; @@ -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(); @@ -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); + } } diff --git a/src/portmatching/pattern.rs b/src/portmatching/pattern.rs index 60b0ab1c..fce1e05e 100644 --- a/src/portmatching/pattern.rs +++ b/src/portmatching/pattern.rs @@ -33,7 +33,7 @@ impl CircuitPattern { } /// Construct a pattern from a circuit. - pub fn try_from_circuit(circuit: &C) -> Result { + pub fn try_from_circuit(circuit: &impl Circuit) -> Result { if circuit.num_gates() == 0 { return Err(InvalidPattern::EmptyCircuit); }