From 7d000c56fc9d3b497714231c1581c448311db9da Mon Sep 17 00:00:00 2001 From: Luca Mondada Date: Thu, 21 Sep 2023 17:08:18 +0100 Subject: [PATCH] Address comments --- src/portmatching.rs | 20 +++++++++++++------- src/portmatching/matcher.rs | 7 ++----- src/portmatching/pattern.rs | 14 ++++++-------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/portmatching.rs b/src/portmatching.rs index 30096f26..423c612c 100644 --- a/src/portmatching.rs +++ b/src/portmatching.rs @@ -65,13 +65,16 @@ impl PEdge { circ: &impl Circuit, ) -> Result { let src = port; - let (dst_node, dst) = circ.linked_ports(node, src).exactly_one().map_err(|e| { - if e.size_hint().0 > 0 { - InvalidEdgeProperty::AmbiguousEdge(src) - } else { - InvalidEdgeProperty::NoLinkedEdge(src) - } - })?; + let (dst_node, dst) = circ + .linked_ports(node, src) + .exactly_one() + .map_err(|mut e| { + if e.next().is_some() { + InvalidEdgeProperty::AmbiguousEdge(src) + } else { + InvalidEdgeProperty::NoLinkedEdge(src) + } + })?; if circ.get_optype(dst_node).tag() == OpTag::Input { return Ok(Self::InputEdge { src }); } @@ -120,6 +123,9 @@ impl portmatching::EdgeProperty for PEdge { /// /// A node is either a real node in the HUGR graph or a hidden copy node /// that is identified by its node and outgoing port. +/// +/// A NodeID::CopyNode can only be found as a target of a PEdge::InputEdge +/// property. Furthermore, a NodeID::CopyNode never has a node property. #[derive( Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize, )] diff --git a/src/portmatching/matcher.rs b/src/portmatching/matcher.rs index 133a5db6..960ea346 100644 --- a/src/portmatching/matcher.rs +++ b/src/portmatching/matcher.rs @@ -13,7 +13,7 @@ use hugr::{hugr::views::SiblingSubgraph, ops::OpType, Hugr, Node, Port}; use itertools::Itertools; use portmatching::{ automaton::{LineBuilder, ScopeAutomaton}, - PatternID, + EdgeProperty, PatternID, }; use thiserror::Error; @@ -385,10 +385,7 @@ fn compatible_offsets(e1: &PEdge, e2: &PEdge) -> bool { let PEdge::InternalEdge { dst: dst1, .. } = e1 else { return false; }; - let src2 = match *e2 { - PEdge::InternalEdge { src, .. } => src, - PEdge::InputEdge { src, .. } => src, - }; + let src2 = e2.offset_id(); dst1.direction() != src2.direction() && dst1.index() == src2.index() } diff --git a/src/portmatching/pattern.rs b/src/portmatching/pattern.rs index 1b63f018..60b0ab1c 100644 --- a/src/portmatching/pattern.rs +++ b/src/portmatching/pattern.rs @@ -100,14 +100,12 @@ impl CircuitPattern { ) .map(|m| { m.into_iter() - .filter_map(|(node_p, node_c)| { - let NodeID::HugrNode(node_p) = node_p else { - return None; - }; - let NodeID::HugrNode(node_c) = node_c else { - return None; - }; - Some((node_p, node_c)) + .filter_map(|(node_p, node_c)| match (node_p, node_c) { + (NodeID::HugrNode(node_p), NodeID::HugrNode(node_c)) => { + Some((node_p, node_c)) + } + (NodeID::CopyNode(..), NodeID::CopyNode(..)) => None, + _ => panic!("Invalid match map"), }) .collect() })