Skip to content

Commit

Permalink
fix portgraph 0.13 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Jan 20, 2025
1 parent 7d250a5 commit 4eeec4c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jsonschema = "0.27.0"
lazy_static = "1.4.0"
num-rational = "0.4.1"
paste = "1.0"
petgraph = { version = "0.6.3", default-features = false }
petgraph = { version = "0.7.1", default-features = false }
proptest = "1.4.0"
proptest-derive = "0.5.0"
regex = "1.9.5"
Expand Down
52 changes: 36 additions & 16 deletions hugr-core/src/hugr/views/sibling_subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::mem;

use itertools::Itertools;
use portgraph::algorithms::ConvexChecker;
use portgraph::boundary::Boundary;
use portgraph::{view::Subgraph, Direction, PortView};
use thiserror::Error;

Expand Down Expand Up @@ -179,14 +180,8 @@ impl SiblingSubgraph {
) -> Result<Self, InvalidSubgraph> {
let pg = hugr.portgraph();

let to_pg = |(n, p): (Node, Port)| {
pg.port_index(n.pg_index(), p.pg_offset())
.expect("invalid port")
};

// Ordering of the edges here is preserved and becomes ordering of the signature.
let subpg =
Subgraph::new_subgraph(pg.clone(), combine_in_out(&inputs, &outputs).map(to_pg));
let subpg = Subgraph::new_subgraph(pg.clone(), make_boundary(hugr, &inputs, &outputs));
let nodes = subpg.nodes_iter().map_into().collect_vec();
validate_subgraph(hugr, &nodes, &inputs, &outputs)?;

Expand Down Expand Up @@ -414,7 +409,7 @@ impl SiblingSubgraph {
.is_some_and(|s| s.port_type(p).is_some())
});

if combine_in_out(&vec![out_order_ports], &in_order_ports)
if iter_io(&vec![out_order_ports], &in_order_ports)
.any(|(n, p)| is_order_edge(&replacement, n, p))
{
unimplemented!("Found state order edges in replacement graph");
Expand Down Expand Up @@ -485,15 +480,40 @@ impl SiblingSubgraph {
}
}

fn combine_in_out<'a>(
/// Returns an iterator over the input ports.
fn iter_incoming(inputs: &IncomingPorts) -> impl Iterator<Item = (Node, IncomingPort)> + '_ {
inputs.iter().flat_map(|part| part.iter().copied())
}

/// Returns an iterator over the output ports.
fn iter_outgoing(outputs: &OutgoingPorts) -> impl Iterator<Item = (Node, OutgoingPort)> + '_ {
outputs.iter().copied()
}

/// Returns an iterator over both incoming and outgoing ports.
fn iter_io<'a>(
inputs: &'a IncomingPorts,
outputs: &'a OutgoingPorts,
) -> impl Iterator<Item = (Node, Port)> + 'a {
inputs
.iter()
.flatten()
.map(|(n, p)| (*n, (*p).into()))
.chain(outputs.iter().map(|(n, p)| (*n, (*p).into())))
iter_incoming(inputs)
.map(|(n, p)| (n, Port::from(p)))
.chain(iter_outgoing(outputs).map(|(n, p)| (n, Port::from(p))))
}

fn make_boundary<'a>(
hugr: &impl HugrView,
inputs: &'a IncomingPorts,
outputs: &'a OutgoingPorts,
) -> Boundary {
let to_pg_index = |n: Node, p: Port| {
hugr.portgraph()
.port_index(n.pg_index(), p.pg_offset())
.unwrap()
};
Boundary::new(
iter_incoming(inputs).map(|(n, p)| to_pg_index(n, p.into())),
iter_outgoing(outputs).map(|(n, p)| to_pg_index(n, p.into())),
)
}

/// Precompute convexity information for a HUGR.
Expand Down Expand Up @@ -591,11 +611,11 @@ fn validate_subgraph<H: HugrView>(
}

// Check there are no linked "other" ports
if combine_in_out(inputs, outputs).any(|(n, p)| is_order_edge(hugr, n, p)) {
if iter_io(inputs, outputs).any(|(n, p)| is_order_edge(hugr, n, p)) {
unimplemented!("Connected order edges not supported at the boundary")
}

let boundary_ports = combine_in_out(inputs, outputs).collect_vec();
let boundary_ports = iter_io(inputs, outputs).collect_vec();
// Check that the boundary ports are all in the subgraph.
if let Some(&(n, p)) = boundary_ports.iter().find(|(n, _)| !node_set.contains(n)) {
Err(InvalidSubgraphBoundary::PortNodeNotInSet(n, p))?;
Expand Down

0 comments on commit 4eeec4c

Please sign in to comment.