-
Notifications
You must be signed in to change notification settings - Fork 7
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
refactor: use SiblingSubgraph in SimpleReplacement #517
Merged
+119
−65
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
85581ea
refactor: use SiblingSubgraph in SimpleReplacement
lmondada 526b442
Merge branch 'main' into refactor/use-subgraph-replace
lmondada 66f82a8
Add subgraph method
lmondada 8388389
Address comments
lmondada da966ba
Merge branch 'main' into refactor/use-subgraph-replace
lmondada 003e14f
Add node_count
lmondada 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,11 @@ pub struct SiblingSubgraph { | |
} | ||
|
||
/// The type of the incoming boundary of [`SiblingSubgraph`]. | ||
/// | ||
/// The nested vec represents a partition of the incoming boundary ports by | ||
/// input parameter. A set in the partition that has more than one element | ||
/// corresponds to an input parameter that is copied and useful multiple times | ||
/// in the subgraph. | ||
pub type IncomingPorts = Vec<Vec<(Node, Port)>>; | ||
/// The type of the outgoing boundary of [`SiblingSubgraph`]. | ||
pub type OutgoingPorts = Vec<(Node, Port)>; | ||
|
@@ -194,6 +199,58 @@ impl SiblingSubgraph { | |
}) | ||
} | ||
|
||
/// Create a subgraph from a set of nodes. | ||
/// | ||
/// The incoming boundary is given by the set of edges with a source | ||
/// not in nodes and a target in nodes. Conversely, the outgoing boundary | ||
/// is given by the set of edges with a source in nodes and a target not | ||
/// in nodes. | ||
/// | ||
/// The subgraph signature will be given by the types of the incoming and | ||
/// outgoing edges ordered by the node order in `nodes` and within each node | ||
/// by the port order. | ||
|
||
/// The in- and out-arity of the signature will match the | ||
/// number of incoming and outgoing edges respectively. In particular, the | ||
/// assumption is made that no two incoming edges have the same source | ||
/// (no copy nodes at the input bounary). | ||
pub fn try_from_nodes( | ||
nodes: impl Into<Vec<Node>>, | ||
hugr: &impl HugrView, | ||
) -> Result<Self, InvalidSubgraph> { | ||
let nodes = nodes.into(); | ||
let nodes_set = nodes.iter().copied().collect::<HashSet<_>>(); | ||
let incoming_edges = nodes | ||
.iter() | ||
.flat_map(|&n| hugr.node_inputs(n).map(move |p| (n, p))); | ||
let outgoing_edges = nodes | ||
.iter() | ||
.flat_map(|&n| hugr.node_outputs(n).map(move |p| (n, p))); | ||
let inputs = incoming_edges | ||
.filter(|&(n, p)| { | ||
if !hugr.is_linked(n, p) { | ||
return false; | ||
} | ||
let (out_n, _) = hugr.linked_ports(n, p).exactly_one().ok().unwrap(); | ||
!nodes_set.contains(&out_n) | ||
}) | ||
// Every incoming edge is its own input. | ||
.map(|p| vec![p]) | ||
.collect_vec(); | ||
let outputs = outgoing_edges | ||
.filter(|&(n, p)| { | ||
if !hugr.is_linked(n, p) { | ||
return false; | ||
} | ||
// TODO: what if there are multiple outgoing edges? | ||
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. Reference a github issue? 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. Done! |
||
// See https://github.com/CQCL-DEV/hugr/issues/518 | ||
let (in_n, _) = hugr.linked_ports(n, p).next().unwrap(); | ||
!nodes_set.contains(&in_n) | ||
}) | ||
.collect_vec(); | ||
Self::try_new(inputs, outputs, hugr) | ||
} | ||
|
||
/// An iterator over the nodes in the subgraph. | ||
pub fn nodes(&self) -> &[Node] { | ||
&self.nodes | ||
|
@@ -248,8 +305,6 @@ impl SiblingSubgraph { | |
hugr: &impl HugrView, | ||
replacement: Hugr, | ||
) -> Result<SimpleReplacement, InvalidReplacement> { | ||
let removal = self.nodes().iter().copied().collect(); | ||
|
||
let rep_root = replacement.root(); | ||
let dfg_optype = replacement.get_optype(rep_root); | ||
if !OpTag::Dfg.is_superset(dfg_optype.tag()) { | ||
|
@@ -300,8 +355,7 @@ impl SiblingSubgraph { | |
.collect(); | ||
|
||
Ok(SimpleReplacement::new( | ||
self.get_parent(hugr), | ||
removal, | ||
self.clone(), | ||
replacement, | ||
nu_inp, | ||
nu_out, | ||
|
@@ -609,7 +663,7 @@ mod tests { | |
|
||
let rep = sub.create_simple_replacement(&func, empty_dfg).unwrap(); | ||
|
||
assert_eq!(rep.removal.len(), 1); | ||
assert_eq!(rep.subgraph().nodes().len(), 1); | ||
|
||
assert_eq!(hugr.node_count(), 5); // Module + Def + In + CX + Out | ||
hugr.apply_rewrite(rep).unwrap(); | ||
|
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.
I'm puzzled why we convert
p
to a singleton vector here, i.e. why isIncomingPorts
a vector of vectors (not really pertinent to this PR though).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.
I've better documented this peculiarity. Let me know if this clarifies the matter.