Skip to content

Commit de70bd6

Browse files
committed
Add new Subcircuit
1 parent 4a6a211 commit de70bd6

File tree

9 files changed

+1111
-30
lines changed

9 files changed

+1111
-30
lines changed

tket/src/circuit.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ pub use command::{Command, CommandIterator};
1414
pub use hash::CircuitHash;
1515
use hugr::extension::prelude::{NoopDef, TupleOpDef};
1616
use hugr::extension::simple_op::MakeOpDef;
17-
use hugr::hugr::views::ExtractionResult;
17+
use hugr::hugr::views::{ExtractionResult, SiblingSubgraph};
18+
use hugr::ops::handle::DataflowParentID;
1819
use itertools::Either::{Left, Right};
1920

2021
use derive_more::{Display, Error, From};
@@ -354,6 +355,14 @@ impl<T: HugrView<Node = Node>> Circuit<T> {
354355
{
355356
self.commands().map(|cmd| op_cost(cmd.optype())).sum()
356357
}
358+
359+
/// The subgraph containing the entire circuit.
360+
pub fn subgraph(&self) -> SiblingSubgraph
361+
where
362+
T: Clone,
363+
{
364+
SiblingSubgraph::try_new_dataflow_subgraph::<_, DataflowParentID>(self.hugr()).unwrap()
365+
}
357366
}
358367

359368
impl<T: HugrView> From<T> for Circuit<T> {

tket/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub mod passes;
5252
pub mod resource;
5353
pub mod rewrite;
5454
pub mod serialize;
55+
pub mod subcircuit;
5556

5657
#[cfg(feature = "portmatching")]
5758
pub mod portmatching;
@@ -65,3 +66,4 @@ pub use circuit::{Circuit, CircuitError, CircuitMutError};
6566
pub use hugr;
6667
pub use hugr::Hugr;
6768
pub use ops::{op_matches, symbolic_constant_op, Pauli, TketOp};
69+
pub use subcircuit::Subcircuit;

tket/src/resource.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,26 @@
3939
//! Use a [`SiblingSubgraph`] to define a region of a `HUGR`, within which
4040
//! resources should be tracked. You can then construct a resource-tracked scope
4141
//! using [`ResourceScope::new`].
42+
//!
43+
//! [`SiblingSubgraph`]: hugr::hugr::views::SiblingSubgraph
4244
4345
// Public API exports
4446
pub use flow::{DefaultResourceFlow, ResourceFlow, UnsupportedOp};
47+
pub use interval::{Interval, InvalidInterval};
4548
pub use scope::{ResourceScope, ResourceScopeConfig};
4649
pub use types::{CopyableValueId, OpValue, Position, ResourceAllocator, ResourceId};
4750

4851
// Internal modules
4952
mod flow;
53+
mod interval;
5054
mod scope;
5155
mod types;
5256

5357
#[cfg(test)]
54-
mod tests {
58+
pub(crate) mod tests {
5559
use hugr::{
5660
builder::{DFGBuilder, Dataflow, DataflowHugr},
5761
extension::prelude::qb_t,
58-
hugr::views::SiblingSubgraph,
59-
ops::handle::DataflowParentID,
6062
types::Signature,
6163
CircuitUnit, Hugr,
6264
};
@@ -67,13 +69,14 @@ mod tests {
6769
use crate::{
6870
extension::rotation::{rotation_type, ConstRotation},
6971
resource::scope::tests::ResourceScopeReport,
70-
TketOp,
72+
utils::build_simple_circuit,
73+
Circuit, TketOp,
7174
};
7275

7376
use super::ResourceScope;
7477

7578
// Gate being commuted has a non-linear input
76-
fn circ(n_qubits: usize, add_rz: bool, add_const_rz: bool) -> Hugr {
79+
pub fn cx_rz_circuit(n_qubits: usize, add_rz: bool, add_const_rz: bool) -> Hugr {
7780
let build = || {
7881
let out_qb_row = vec![qb_t(); n_qubits];
7982
let mut inp_qb_row = out_qb_row.clone();
@@ -123,6 +126,18 @@ mod tests {
123126
build().unwrap()
124127
}
125128

129+
/// A two-qubit circuit with `n_cx` CNOTs.
130+
pub fn cx_circuit(n_cx: usize) -> Hugr {
131+
build_simple_circuit(2, |circ| {
132+
for _ in 0..n_cx {
133+
circ.append(TketOp::CX, [0, 1])?;
134+
}
135+
Ok(())
136+
})
137+
.unwrap()
138+
.into_hugr()
139+
}
140+
126141
#[rstest]
127142
#[case(2, false, false)]
128143
#[case(2, true, false)]
@@ -137,9 +152,8 @@ mod tests {
137152
#[case] add_rz: bool,
138153
#[case] add_const_rz: bool,
139154
) {
140-
let circ = circ(n_qubits, add_rz, add_const_rz);
141-
let subgraph =
142-
SiblingSubgraph::try_new_dataflow_subgraph::<_, DataflowParentID>(&circ).unwrap();
155+
let circ = cx_rz_circuit(n_qubits, add_rz, add_const_rz);
156+
let subgraph = Circuit::from(&circ).subgraph();
143157
let scope = ResourceScope::new(&circ, subgraph);
144158
let info = ResourceScopeReport::from(&scope);
145159

0 commit comments

Comments
 (0)