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: Inconsistent behaviour in SiblingSubgraph::from_nodes #2011

Merged
merged 1 commit into from
Mar 21, 2025
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
44 changes: 37 additions & 7 deletions hugr-core/src/hugr/views/sibling_subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl<N: HugrNode> SiblingSubgraph<N> {
.collect_vec();
validate_subgraph(hugr, &nodes, &inputs, &outputs)?;

if !subpg.is_convex_with_checker(checker) {
if nodes.len() > 1 && !subpg.is_convex_with_checker(checker) {
return Err(InvalidSubgraph::NotConvex);
}

Expand Down Expand Up @@ -238,12 +238,9 @@ impl<N: HugrNode> SiblingSubgraph<N> {
) -> Result<Self, InvalidSubgraph<N>> {
let nodes = nodes.into();

// If there's one or less nodes, we don't need to check convexity.
match nodes.as_slice() {
[] => return Err(InvalidSubgraph::EmptySubgraph),
[node] => return Ok(Self::from_node(*node, hugr)),
_ => {}
};
if nodes.is_empty() {
return Err(InvalidSubgraph::EmptySubgraph);
}

let nodes_set = nodes.iter().copied().collect::<HashSet<_>>();
let incoming_edges = nodes
Expand Down Expand Up @@ -1239,4 +1236,37 @@ mod tests {
let rep = subg.create_simple_replacement(&h, replacement).unwrap();
rep.apply(&mut h).unwrap();
}

/// Test the behaviour of the sibling subgraph when built from a single node.
#[test]
fn single_node_subgraph() {
// A hugr with a single NOT operation, with disconnected output.
let mut b = DFGBuilder::new(
Signature::new(bool_t(), type_row![])
.with_extension_delta(crate::std_extensions::logic::EXTENSION_ID),
)
.unwrap();
let inw = b.input_wires().exactly_one().unwrap();
let not_n = b.add_dataflow_op(LogicOp::Not, [inw]).unwrap();
// Unconnected output, discarded
let h = b.finish_hugr_with_outputs([]).unwrap();

// When built with `from_node`, the subgraph's signature is the same as the node's.
// (bool input, bool output)
let subg = SiblingSubgraph::from_node(not_n.node(), &h);
assert_eq!(subg.nodes().len(), 1);
assert_eq!(
subg.signature(&h).io(),
Signature::new(vec![bool_t()], vec![bool_t()]).io()
);

// `from_nodes` is different, is it only uses incoming and outgoing edges to compute the signature.
// In this case, the output is disconnected, so it is not part of the subgraph signature.
let subg = SiblingSubgraph::try_from_nodes([not_n.node()], &h).unwrap();
assert_eq!(subg.nodes().len(), 1);
assert_eq!(
subg.signature(&h).io(),
Signature::new(vec![bool_t()], vec![]).io()
);
}
}
Loading