-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat: Support copies in pattern matching #127
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ use std::{ | |
path::{Path, PathBuf}, | ||
}; | ||
|
||
use super::{CircuitPattern, PEdge, PNode}; | ||
use super::{CircuitPattern, NodeID, PEdge, PNode}; | ||
use hugr::hugr::views::sibling_subgraph::{ConvexChecker, InvalidReplacement, InvalidSubgraph}; | ||
use hugr::{hugr::views::SiblingSubgraph, ops::OpType, Hugr, Node, Port}; | ||
use itertools::Itertools; | ||
|
@@ -30,6 +30,7 @@ use crate::{ | |
/// Matchable operations in a circuit. | ||
/// | ||
/// We currently support [`T2Op`] and a the HUGR load constant operation. | ||
// TODO: Support OpType::Const, but blocked by use of F64 (Eq support required) | ||
#[derive( | ||
Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize, | ||
)] | ||
|
@@ -257,11 +258,11 @@ impl PatternMatcher { | |
) -> Vec<PatternMatch> { | ||
self.automaton | ||
.run( | ||
root, | ||
root.into(), | ||
// Node weights (none) | ||
validate_weighted_node(circ), | ||
validate_circuit_node(circ), | ||
// Check edge exist | ||
validate_unweighted_edge(circ), | ||
validate_circuit_edge(circ), | ||
) | ||
.filter_map(|pattern_id| { | ||
handle_match_error( | ||
|
@@ -380,28 +381,51 @@ impl From<InvalidSubgraph> for InvalidPatternMatch { | |
} | ||
} | ||
|
||
fn compatible_offsets((_, pout): &(Port, Port), (pin, _): &(Port, Port)) -> bool { | ||
pout.direction() != pin.direction() && pout.index() == pin.index() | ||
fn compatible_offsets(e1: &PEdge, e2: &PEdge) -> bool { | ||
let PEdge::InternalEdge { dst: dst1, .. } = e1 else { | ||
return false; | ||
}; | ||
let src2 = match *e2 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use offset_id ? |
||
PEdge::InternalEdge { src, .. } => src, | ||
PEdge::InputEdge { src, .. } => src, | ||
}; | ||
dst1.direction() != src2.direction() && dst1.index() == src2.index() | ||
} | ||
|
||
/// Check if an edge `e` is valid in a portgraph `g` without weights. | ||
pub(crate) fn validate_unweighted_edge( | ||
/// Returns a predicate checking that an edge at `src` satisfies `prop` in `circ`. | ||
pub(super) fn validate_circuit_edge( | ||
circ: &impl Circuit, | ||
) -> impl for<'a> Fn(Node, &'a PEdge) -> Option<Node> + '_ { | ||
move |src, &(src_port, tgt_port)| { | ||
let (next_node, _) = circ | ||
.linked_ports(src, src_port) | ||
.find(|&(_, tgt)| tgt == tgt_port)?; | ||
Some(next_node) | ||
) -> impl for<'a> Fn(NodeID, &'a PEdge) -> Option<NodeID> + '_ { | ||
move |src, &prop| { | ||
let NodeID::HugrNode(src) = src else { | ||
return None; | ||
}; | ||
match prop { | ||
PEdge::InternalEdge { | ||
src: src_port, | ||
dst: dst_port, | ||
.. | ||
} => { | ||
let (next_node, next_port) = circ.linked_ports(src, src_port).exactly_one().ok()?; | ||
(dst_port == next_port).then_some(NodeID::HugrNode(next_node)) | ||
} | ||
PEdge::InputEdge { src: src_port } => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doc the invariant if you want |
||
let (next_node, next_port) = circ.linked_ports(src, src_port).exactly_one().ok()?; | ||
Some(NodeID::CopyNode(next_node, next_port)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Check if a node `n` is valid in a weighted portgraph `g`. | ||
pub(crate) fn validate_weighted_node( | ||
/// Returns a predicate checking that `node` satisfies `prop` in `circ`. | ||
pub(crate) fn validate_circuit_node( | ||
circ: &impl Circuit, | ||
) -> impl for<'a> Fn(Node, &PNode) -> bool + '_ { | ||
move |v, prop| { | ||
let v_weight = MatchOp::try_from(circ.get_optype(v).clone()); | ||
) -> impl for<'a> Fn(NodeID, &PNode) -> bool + '_ { | ||
move |node, prop| { | ||
let NodeID::HugrNode(node) = node else { | ||
return false; | ||
}; | ||
let v_weight = MatchOp::try_from(circ.get_optype(node).clone()); | ||
v_weight.is_ok_and(|w| &w == prop) | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check if you can use peek instead