Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pattern matcher discriminating on opaqueOp description #441

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading