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: allow disconnected outputs in SiblingSubgraph::from_node #1769

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
51 changes: 47 additions & 4 deletions hugr-core/src/hugr/views/sibling_subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,6 @@ impl SiblingSubgraph {
///
/// The subgraph signature will be given by signature of the node.
pub fn from_node(node: Node, hugr: &impl HugrView) -> Self {
// TODO once https://github.com/CQCL/portgraph/issues/155
Copy link
Member Author

Choose a reason for hiding this comment

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

this comment is no longer true since the two methods have different functionality

If we can extend the functionality of try_from_nodes that would be great: #1766

// is fixed we can just call try_from_nodes here.
// Until then, doing this saves a lot of work.
let nodes = vec![node];
let inputs = hugr
.node_inputs(node)
Expand All @@ -288,7 +285,17 @@ impl SiblingSubgraph {
.collect_vec();
let outputs = hugr
.node_outputs(node)
.filter_map(|p| hugr.is_linked(node, p).then_some((node, p)))
.filter_map(|p| {
// accept linked outputs or unlinked value outputs
{
hugr.is_linked(node, p)
|| hugr
.get_optype(node)
.port_kind(p)
.is_some_and(|k| k.is_value())
}
.then_some((node, p))
})
.collect_vec();

Self {
Expand Down Expand Up @@ -785,9 +792,11 @@ mod tests {
use cool_asserts::assert_matches;

use crate::builder::inout_sig;
use crate::hugr::Rewrite;
use crate::ops::Const;
use crate::std_extensions::arithmetic::float_types::{self, ConstF64};
use crate::std_extensions::logic::{self, LogicOp};
use crate::type_row;
use crate::utils::test_quantum_extension::{self, cx_gate, rz_f64};
use crate::{
builder::{
Expand Down Expand Up @@ -1155,4 +1164,38 @@ mod tests {
let subg = SiblingSubgraph::try_new_dataflow_subgraph(&view).unwrap();
assert_eq!(subg.nodes().len(), 2);
}

#[test]
fn test_unconnected() {
// test a replacement on a subgraph with a discarded output
let mut b = DFGBuilder::new(
Signature::new(bool_t(), type_row![])
// .with_prelude()
.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 mut h = b.finish_hugr_with_outputs([]).unwrap();

let subg = SiblingSubgraph::from_node(not_n.node(), &h);

assert_eq!(subg.nodes().len(), 1);
// TODO create a valid replacement
let replacement = {
let mut rep_b = DFGBuilder::new(
Signature::new_endo(bool_t())
.with_extension_delta(crate::std_extensions::logic::EXTENSION_ID),
)
.unwrap();
let inw = rep_b.input_wires().exactly_one().unwrap();

let not_n = rep_b.add_dataflow_op(LogicOp::Not, [inw]).unwrap();

rep_b.finish_hugr_with_outputs(not_n.outputs()).unwrap()
};
let rep = subg.create_simple_replacement(&h, replacement).unwrap();
rep.apply(&mut h).unwrap();
}
}
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
1 change: 0 additions & 1 deletion hugr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ bumpalo = { workspace = true, features = ["collections"] }
[[bench]]
name = "bench_main"
harness = false

Loading