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: NonConvex error on SiblingSubgraph::from_nodes with multiports #1295

Merged
merged 2 commits into from
Jul 11, 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
47 changes: 40 additions & 7 deletions hugr-core/src/hugr/views/sibling_subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,8 @@ impl SiblingSubgraph {
.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?
// See https://github.com/CQCL/hugr/issues/518
let (in_n, _) = hugr.linked_ports(n, p).next().unwrap();
!nodes_set.contains(&in_n)
hugr.linked_ports(n, p)
.any(|(n1, _)| !nodes_set.contains(&n1))
})
.collect_vec();
Self::try_new_with_checker(inputs, outputs, hugr, checker)
Expand Down Expand Up @@ -792,6 +787,7 @@ mod tests {
Ok((hugr, func_id.node()))
}

/// A bool to bool hugr with three subsequent NOT gates.
fn build_3not_hugr() -> Result<(Hugr, Node), BuildError> {
let mut mod_builder = ModuleBuilder::new();
let func = mod_builder.declare("test", FunctionType::new_endo(type_row![BOOL_T]).into())?;
Expand All @@ -808,6 +804,26 @@ mod tests {
Ok((hugr, func_id.node()))
}

/// A bool to (bool, bool) with multiports.
fn build_multiport_hugr() -> Result<(Hugr, Node), BuildError> {
let mut mod_builder = ModuleBuilder::new();
let func = mod_builder.declare(
"test",
FunctionType::new(type_row![BOOL_T], type_row![BOOL_T, BOOL_T]).into(),
)?;
let func_id = {
let mut dfg = mod_builder.define_declaration(&func)?;
let [b0] = dfg.input_wires_arr();
let [b1] = dfg.add_dataflow_op(NotOp, [b0])?.outputs_arr();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one should never use Result in tests, always unwrap. The error is better when it fails is my usual reason, but now I have another. The error path gets "partial coverage"!

let [b2] = dfg.add_dataflow_op(NotOp, [b1])?.outputs_arr();
dfg.finish_with_outputs([b1, b2])?
};
let hugr = mod_builder
.finish_prelude_hugr()
.map_err(|e| -> BuildError { e.into() })?;
Ok((hugr, func_id.node()))
}

/// A HUGR with a copy
fn build_hugr_classical() -> Result<(Hugr, Node), BuildError> {
let mut mod_builder = ModuleBuilder::new();
Expand Down Expand Up @@ -979,6 +995,23 @@ mod tests {
);
}

/// A subgraphs mixed with multiports caused a NonConvex error.
/// https://github.com/CQCL/hugr/issues/1294
#[test]
fn convex_multiports() {
let (hugr, func_root) = build_multiport_hugr().unwrap();
let [inp, out] = hugr.get_io(func_root).unwrap();
let not1 = hugr.output_neighbours(inp).exactly_one().unwrap();
let not2 = hugr
.output_neighbours(not1)
.filter(|&n| n != out)
.exactly_one()
.unwrap();

let subgraph = SiblingSubgraph::try_from_nodes([not1, not2], &hugr).unwrap();
assert_eq!(subgraph.nodes(), [not1, not2]);
}

#[test]
fn invalid_boundary() {
let (hugr, func_root) = build_hugr().unwrap();
Expand Down
Loading