Skip to content

Commit

Permalink
fix: Pattern matcher discriminating on opaqueOp description (#441)
Browse files Browse the repository at this point in the history
Ignores the `description` field (and the derived `signature`) when
comparing custom ops in the pattern matching.
  • Loading branch information
aborgna-q authored Jun 28, 2024
1 parent cb90863 commit 525b63f
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions tket2/src/portmatching/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use hugr::hugr::views::sibling_subgraph::{
InvalidReplacement, InvalidSubgraph, InvalidSubgraphBoundary, TopoConvexChecker,
};
use hugr::hugr::views::SiblingSubgraph;
use hugr::ops::{NamedOp, OpType};
use hugr::ops::{CustomOp, NamedOp, OpType};
use hugr::{HugrView, IncomingPort, Node, OutgoingPort, Port, PortIndex};
use itertools::Itertools;
use portgraph::algorithms::ConvexChecker;
Expand Down Expand Up @@ -45,24 +45,34 @@ pub(crate) struct MatchOp {
impl From<OpType> for MatchOp {
fn from(op: OpType) -> Self {
let op_name = op.name();
// Avoid encoding some operations if we know they can be uniquely
// identified by their name.
let encoded = match op {
OpType::Module(_) => None,
OpType::CustomOp(custom_op)
if custom_op
.as_extension_op()
.map(|ext| ext.args().is_empty())
.unwrap_or(false) =>
{
None
}
_ => rmp_serde::encode::to_vec(&op).ok(),
};
let encoded = encode_op(op);
Self { op_name, encoded }
}
}

/// Encode a unique identifier for an operation.
///
/// Avoids encoding some data if we know the operation can be uniquely
/// identified by their name.
fn encode_op(op: OpType) -> Option<Vec<u8>> {
match op {
OpType::Module(_) => None,
OpType::CustomOp(op) => {
let opaque = match op {
CustomOp::Extension(ext_op) => ext_op.make_opaque(),
CustomOp::Opaque(opaque) => *opaque,
};
let mut encoded: Vec<u8> = Vec::new();
// Ignore irrelevant fields
rmp_serde::encode::write(&mut encoded, opaque.extension()).ok()?;
rmp_serde::encode::write(&mut encoded, opaque.name()).ok()?;
rmp_serde::encode::write(&mut encoded, opaque.args()).ok()?;
Some(encoded)
}
_ => rmp_serde::encode::to_vec(&op).ok(),
}
}

/// A convex pattern match in a circuit.
///
/// The pattern is identified by a [`PatternID`] that can be used to retrieve the
Expand Down

0 comments on commit 525b63f

Please sign in to comment.