Skip to content

Commit

Permalink
feat: Add SiblingSubgraph::from_node
Browse files Browse the repository at this point in the history
fixes #152
  • Loading branch information
doug-q committed Nov 13, 2024
1 parent 0c430d1 commit 12e14f8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
31 changes: 31 additions & 0 deletions hugr-core/src/hugr/views/sibling_subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ impl SiblingSubgraph {
Self::try_new_with_checker(inputs, outputs, hugr, checker)
}

/// Create a subgraph from a single node.
///
/// `SiblingSubgraph::from_node(n,h)` is equivalent to
/// `SiblingSubgraph::try_from_nodes([n],h).unwrap()`. A single node always
/// forms a convex subgraph, so `from_node` is infaliible.
///
/// Panics if `node` is not present in `hugr`.
pub fn from_node(node: Node, hugr: &impl HugrView) -> Self {
// We use a custom ConvexChecker here, but once we have
// https://github.com/CQCL/portgraph/issues/152 we have the option
// to use the TopoConvexChecker. In that case this line becomes
// `Self::try_from_nodes([node], hugr).unwrap()`
Self::try_from_nodes_with_checker(vec![node], hugr, &SingleNodeConvexChecker)
.expect("invalid node")
}

/// An iterator over the nodes in the subgraph.
pub fn nodes(&self) -> &[Node] {
&self.nodes
Expand Down Expand Up @@ -476,6 +492,21 @@ impl<'g, Base: HugrView> ConvexChecker for TopoConvexChecker<'g, Base> {
}
}

struct SingleNodeConvexChecker;

impl ConvexChecker for SingleNodeConvexChecker {
fn is_convex(
&self,
nodes: impl IntoIterator<Item = portgraph::NodeIndex>,
_inputs: impl IntoIterator<Item = portgraph::PortIndex>,
_outputs: impl IntoIterator<Item = portgraph::PortIndex>,
) -> bool {
let nodes = nodes.into_iter().collect_vec();
assert_eq!(1, nodes.len());
true
}
}

/// The type of all ports in the iterator.
///
/// If the array is empty or a port does not exist, returns `None`.
Expand Down
3 changes: 1 addition & 2 deletions hugr-passes/src/const_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ fn fold_op(
})
.unzip();
let replacement = const_graph(consts, reg);
let sibling_graph = SiblingSubgraph::try_from_nodes([op_node], hugr)
.expect("Operation should form valid subgraph.");
let sibling_graph = SiblingSubgraph::from_node(op_node, hugr);

let simple_replace = SimpleReplacement::new(
sibling_graph,
Expand Down
2 changes: 1 addition & 1 deletion hugr-passes/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn lower_ops(
replacements
.into_iter()
.map(|(node, replacement)| {
let subcirc = SiblingSubgraph::try_from_nodes([node], hugr)?;
let subcirc = SiblingSubgraph::from_node(node, hugr);
let rw = subcirc.create_simple_replacement(hugr, replacement)?;
let mut repls = hugr.apply_rewrite(rw)?;
debug_assert_eq!(repls.len(), 1);
Expand Down

0 comments on commit 12e14f8

Please sign in to comment.