Skip to content

Commit 52fc658

Browse files
lmondadaacl-cqc
andauthored
feat: Add CopyableExpressionAST (#1209)
I want to define a `Subcircuit` as a combination of `Interval`s on resource paths (the parts with linear, non-copyable data), and ASTs of fully classical, copyable data. - PR #1205 was opened to refine the notion of `Interval` - this PR creates the `CopyableExpressionAST` type that corresponds to ASTs of classical data in hugr graphs. --------- Co-authored-by: Alan Lawrence <[email protected]>
1 parent d34b013 commit 52fc658

File tree

4 files changed

+517
-2
lines changed

4 files changed

+517
-2
lines changed

tket/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub mod passes;
5454
pub mod resource;
5555
pub mod rewrite;
5656
pub mod serialize;
57+
pub mod subcircuit;
5758

5859
#[cfg(feature = "portmatching")]
5960
pub mod portmatching;

tket/src/resource/scope.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! tracking within a specific region of a HUGR, computing resource paths and
55
//! providing efficient lookup of circuit units associated with ports.
66
7-
use std::collections::BTreeSet;
7+
use std::collections::{BTreeSet, VecDeque};
88
use std::{cmp, iter};
99

1010
use crate::resource::flow::{DefaultResourceFlow, ResourceFlow};
@@ -16,7 +16,7 @@ use hugr::hugr::views::sibling_subgraph::InvalidSubgraph;
1616
use hugr::hugr::views::SiblingSubgraph;
1717
use hugr::ops::OpTrait;
1818
use hugr::types::Signature;
19-
use hugr::{Direction, HugrView, IncomingPort, Port, PortIndex, Wire};
19+
use hugr::{Direction, HugrView, IncomingPort, OutgoingPort, Port, PortIndex, Wire};
2020
use hugr_core::hugr::internal::PortgraphNodeMap;
2121
use indexmap::map::Entry;
2222
use indexmap::IndexMap;
@@ -267,6 +267,41 @@ impl<H: HugrView> ResourceScope<H> {
267267
self.nodes().contains(&next_node).then_some(next_node)
268268
})
269269
}
270+
271+
/// Whether any of the ends are reachable from any of the starts, within
272+
/// `self`.
273+
///
274+
/// Any nodes outside of `self` are ignored.
275+
pub fn any_reachable_from(
276+
&self,
277+
starts: impl IntoIterator<Item = (H::Node, OutgoingPort)>,
278+
ends: impl IntoIterator<Item = (H::Node, IncomingPort)>,
279+
) -> bool {
280+
let end_nodes = BTreeSet::from_iter(ends.into_iter().map(|(n, _)| n));
281+
let Some(max_end) = end_nodes.iter().filter_map(|&n| self.get_position(n)).max() else {
282+
return false;
283+
};
284+
let mut visited_nodes = BTreeSet::new();
285+
286+
let mut curr_nodes = VecDeque::from_iter(
287+
starts
288+
.into_iter()
289+
.flat_map(|(n, p)| self.hugr().linked_inputs(n, p))
290+
.map(|(n, _)| n),
291+
);
292+
293+
while let Some(node) = curr_nodes.pop_front() {
294+
if self.get_position(node).is_none_or(|p| p > max_end) || !visited_nodes.insert(node) {
295+
continue;
296+
}
297+
if end_nodes.contains(&node) {
298+
return true;
299+
}
300+
curr_nodes.extend(self.hugr().output_neighbours(node));
301+
}
302+
303+
false
304+
}
270305
}
271306

272307
impl<H: Clone + HugrView<Node = hugr::Node>> ResourceScope<H> {

tket/src/subcircuit.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! Placeholder file until subcircuit is merged, see
2+
//! [https://github.com/CQCL/tket2/pull/1054](https://github.com/CQCL/tket2/pull/1054)
3+
4+
mod expression;
5+
pub use expression::CopyableExpr;

0 commit comments

Comments
 (0)