Skip to content

Commit

Permalink
fix: Portmatching not matching const edges (#444)
Browse files Browse the repository at this point in the history
Patterns with constant value definitions failed at construction time,
because the code only read the wire types from the dataflow signature
(which does not include const wires).
  • Loading branch information
aborgna-q authored Jun 28, 2024
1 parent 8208324 commit cb90863
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
19 changes: 11 additions & 8 deletions tket2/src/portmatching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
pub mod matcher;
pub mod pattern;

use hugr::types::EdgeKind;
use hugr::{HugrView, OutgoingPort};
use itertools::Itertools;
pub use matcher::{PatternMatch, PatternMatcher};
Expand Down Expand Up @@ -98,6 +99,7 @@ enum PEdge {
}

#[derive(Debug, Clone, Error)]
#[non_exhaustive]
enum InvalidEdgeProperty {
/// The port is linked to multiple edges.
#[error("port {0:?} is linked to multiple edges")]
Expand All @@ -106,8 +108,8 @@ enum InvalidEdgeProperty {
#[error("port {0:?} is not linked to any edge")]
NoLinkedEdge(Port),
/// The port does not have a type.
#[error("port {0:?} does not have a type")]
UntypedPort(Port),
#[error("{0}:{1} does not have a type")]
UntypedPort(Node, Port),
}

impl PEdge {
Expand All @@ -127,12 +129,13 @@ impl PEdge {
if hugr.get_optype(dst_node).tag() == OpTag::Input {
return Ok(Self::InputEdge { src });
}
let port_type = hugr
.signature(node)
.unwrap()
.port_type(src)
.cloned()
.ok_or(InvalidEdgeProperty::UntypedPort(src))?;

// Get the port type for either value or constant ports.
let port_type = match hugr.get_optype(node).port_kind(src) {
Some(EdgeKind::Value(typ)) => typ,
Some(EdgeKind::Const(typ)) => typ,
_ => return Err(InvalidEdgeProperty::UntypedPort(node, src)),
};
let is_reversible = type_is_linear(&port_type);
Ok(Self::InternalEdge {
src,
Expand Down
9 changes: 7 additions & 2 deletions tket2/src/portmatching/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ impl CircuitPattern {
for in_offset in 0..cmd.input_count() {
let in_offset: IncomingPort = in_offset.into();
let edge_prop = PEdge::try_from_port(cmd.node(), in_offset.into(), circuit)
.expect("Invalid HUGR");
.unwrap_or_else(|e| panic!("Invalid HUGR, {e}"));
let (prev_node, prev_port) = hugr
.linked_outputs(cmd.node(), in_offset)
.exactly_one()
.expect("invalid HUGR");
.unwrap_or_else(|_| {
panic!(
"{} input port {in_offset} does not have a single neighbour",
cmd.node()
)
});
let prev_node = match edge_prop {
PEdge::InternalEdge { .. } => NodeID::HugrNode(prev_node),
PEdge::InputEdge { .. } => NodeID::new_copy(prev_node, prev_port),
Expand Down

0 comments on commit cb90863

Please sign in to comment.