Skip to content

Commit

Permalink
nonempty in sibling subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
Cobord committed Mar 28, 2024
1 parent fbe8b52 commit e689e91
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
3 changes: 2 additions & 1 deletion quantinuum-hugr/src/hugr/hugrmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use core::panic;
use std::collections::HashMap;
use std::ops::Range;

use nonempty::NonEmpty;
use portgraph::view::{NodeFilter, NodeFiltered};
use portgraph::{LinkMut, NodeIndex, PortMut, PortView, SecondaryMap};

Expand Down Expand Up @@ -365,7 +366,7 @@ impl<T: RootTagged<RootHandle = Node> + AsMut<Hugr>> HugrMut for T {
subgraph: &SiblingSubgraph,
) -> HashMap<Node, Node> {
// Create a portgraph view with the explicit list of nodes defined by the subgraph.
let portgraph: NodeFiltered<_, NodeFilter<&[Node]>, &[Node]> =
let portgraph: NodeFiltered<_, NodeFilter<&NonEmpty<Node>>, &NonEmpty<Node>> =
NodeFiltered::new_node_filtered(
other.portgraph(),
|node, ctx| ctx.contains(&node.into()),
Expand Down
3 changes: 1 addition & 2 deletions quantinuum-hugr/src/hugr/rewrite/simple_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ pub enum SimpleReplacementError {
#[cfg(test)]
pub(in crate::hugr::rewrite) mod test {
use itertools::Itertools;
use nonempty::NonEmpty;
use rstest::{fixture, rstest};
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -622,7 +621,7 @@ pub(in crate::hugr::rewrite) mod test {
replacement.remove_node(in_);
replacement.remove_node(out);
Replacement {
removal: NonEmpty::from_slice(s.subgraph.nodes()).unwrap(),
removal: s.subgraph.nodes().clone(),
replacement,
adoptions: HashMap::new(),
mu_inp,
Expand Down
38 changes: 17 additions & 21 deletions quantinuum-hugr/src/hugr/views/sibling_subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::collections::HashSet;
use std::mem;

use itertools::Itertools;
use nonempty::NonEmpty;
use portgraph::algorithms::ConvexChecker;
use portgraph::{view::Subgraph, Direction, PortView};
use thiserror::Error;
Expand Down Expand Up @@ -54,7 +55,7 @@ use crate::{Hugr, IncomingPort, Node, OutgoingPort, Port, SimpleReplacement};
#[derive(Clone, Debug)]
pub struct SiblingSubgraph {
/// The nodes of the induced subgraph.
nodes: Vec<Node>,
nodes: NonEmpty<Node>,
/// The input ports of the subgraph.
///
/// Grouped by input parameter. Each port must be unique and belong to a
Expand Down Expand Up @@ -101,16 +102,12 @@ impl SiblingSubgraph {
let (inputs, outputs) = get_input_output_ports(dfg_graph);

validate_subgraph(dfg_graph, &nodes, &inputs, &outputs)?;

if nodes.is_empty() {
Err(InvalidSubgraph::EmptySubgraph)
} else {
Ok(Self {
nodes,
inputs,
outputs,
})
}
let nodes = NonEmpty::from_vec(nodes).ok_or(InvalidSubgraph::EmptySubgraph)?;
Ok(Self {
nodes,
inputs,
outputs,
})
}

/// Create a new convex sibling subgraph from input and output boundaries.
Expand Down Expand Up @@ -192,6 +189,8 @@ impl SiblingSubgraph {
return Err(InvalidSubgraph::NotConvex);
}

let nodes = NonEmpty::from_vec(nodes).ok_or(InvalidSubgraph::EmptySubgraph)?;

Ok(Self {
nodes,
inputs,
Expand Down Expand Up @@ -269,7 +268,7 @@ impl SiblingSubgraph {
}

/// An iterator over the nodes in the subgraph.
pub fn nodes(&self) -> &[Node] {
pub fn nodes(&self) -> &NonEmpty<Node> {
&self.nodes
}

Expand Down Expand Up @@ -715,15 +714,12 @@ mod tests {
fn from_sibling_graph(sibling_graph: &impl HugrView) -> Result<Self, InvalidSubgraph> {
let root = sibling_graph.root();
let nodes = sibling_graph.children(root).collect_vec();
if nodes.is_empty() {
Err(InvalidSubgraph::EmptySubgraph)
} else {
Ok(Self {
nodes,
inputs: Vec::new(),
outputs: Vec::new(),
})
}
let nodes = NonEmpty::from_vec(nodes).ok_or(InvalidSubgraph::EmptySubgraph)?;
Ok(Self {
nodes,
inputs: Vec::new(),
outputs: Vec::new(),
})
}
}

Expand Down

0 comments on commit e689e91

Please sign in to comment.