Skip to content

Commit 5fc9cd8

Browse files
authored
refactor!: Remove node parameter from Circuit (#824)
The update to `hugr 0.15` added a `Node` associated type to `HugrView`. Over here, that got implemented as a new type parameter in `Circuit`, ```rust struct Circuit<T = Hugr, N = Node> ``` All `impl` blocks however restrain it to the specific hugr support, ```rust impl<T: HugrView> Circuit<T, T::Node> { ... } ``` Given how both types are connected, we should be using `T::Node` directly since an unrelated node type is not usable here. This PR changes the definition to ```rust pub struct Circuit<T: HugrView = Hugr> ``` As this is a breaking change, it can wait until the next breaking release. BREAKING CHANGE: Removed node type parameter from `Circuit`
1 parent 8d3be98 commit 5fc9cd8

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

tket2/src/circuit.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ use self::units::{filter, LinearUnit, Units};
3434

3535
/// A quantum circuit, represented as a function in a HUGR.
3636
#[derive(Debug, Clone)]
37-
pub struct Circuit<T = Hugr, N = Node> {
37+
pub struct Circuit<T: HugrView = Hugr> {
3838
/// The HUGR containing the circuit.
3939
hugr: T,
4040
/// The parent node of the circuit.
4141
///
4242
/// This is checked at runtime to ensure that the node is a DFG node.
43-
parent: N,
43+
parent: T::Node,
4444
}
4545

46-
impl<T: Default + HugrView> Default for Circuit<T, T::Node> {
46+
impl<T: Default + HugrView> Default for Circuit<T> {
4747
fn default() -> Self {
4848
let hugr = T::default();
4949
let parent = hugr.root();
@@ -82,7 +82,7 @@ fn issue_1496_remains() {
8282
assert_eq!("Noop", NoopDef.name())
8383
}
8484

85-
impl<T: HugrView> Circuit<T, T::Node> {
85+
impl<T: HugrView> Circuit<T> {
8686
/// Create a new circuit from a HUGR and a node.
8787
///
8888
/// # Errors
@@ -289,7 +289,7 @@ impl<T: HugrView> Circuit<T, T::Node> {
289289
}
290290
}
291291

292-
impl<T: HugrView<Node = Node>> Circuit<T, Node> {
292+
impl<T: HugrView<Node = Node>> Circuit<T> {
293293
/// Ensures the circuit contains an owned HUGR.
294294
pub fn to_owned(&self) -> Circuit<Hugr> {
295295
let hugr = self.hugr.base_hugr().clone();
@@ -362,7 +362,7 @@ impl<T: HugrView<Node = Node>> Circuit<T, Node> {
362362
}
363363
}
364364

365-
impl<T: HugrView> From<T> for Circuit<T, T::Node> {
365+
impl<T: HugrView> From<T> for Circuit<T> {
366366
fn from(hugr: T) -> Self {
367367
let parent = hugr.root();
368368
Self::new(hugr, parent)
@@ -535,7 +535,7 @@ pub enum CircuitMutError {
535535
/// Shift ports in range (free_port + 1 .. max_ind) by -1.
536536
fn shift_ports<C: HugrMut + ?Sized>(
537537
circ: &mut C,
538-
node: Node,
538+
node: C::Node,
539539
free_port: impl Into<Port>,
540540
max_ind: usize,
541541
) -> Result<Port, hugr::hugr::HugrError> {

tket2/src/circuit/command.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use hugr::types::{EdgeKind, Type, TypeRow};
2222
pub use hugr::{CircuitUnit, Direction, Node, Port, PortIndex, Wire};
2323

2424
/// An operation applied to specific wires.
25-
pub struct Command<'circ, T> {
25+
pub struct Command<'circ, T: HugrView> {
2626
/// The circuit.
2727
circ: &'circ Circuit<T>,
2828
/// The operation node.
@@ -237,7 +237,7 @@ type NodeWalker = pv::Topo<Node, HashSet<Node>>;
237237
// TODO: this can only be made generic over node type once `SiblingGraph` is
238238
// generic over node type. See https://github.com/CQCL/hugr/issues/1926
239239
#[derive(Clone)]
240-
pub struct CommandIterator<'circ, T> {
240+
pub struct CommandIterator<'circ, T: HugrView> {
241241
/// The circuit.
242242
circ: &'circ Circuit<T>,
243243
/// A view of the top-level region of the circuit.

tket2/src/circuit/units.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<N: HugrNode> Units<OutgoingPort, N, DefaultUnitLabeller> {
8484
/// This iterator will yield all units originating from the circuit's input
8585
/// node.
8686
#[inline]
87-
pub(super) fn new_circ_input<T: HugrView<Node = N>>(circuit: &Circuit<T, N>) -> Self {
87+
pub(super) fn new_circ_input<T: HugrView<Node = N>>(circuit: &Circuit<T>) -> Self {
8888
Self::new_outgoing(circuit, circuit.input_node(), DefaultUnitLabeller)
8989
}
9090
}
@@ -96,7 +96,7 @@ where
9696
/// Create a new iterator over the units originating from node.
9797
#[inline]
9898
pub(super) fn new_outgoing<T: HugrView<Node = N>>(
99-
circuit: &Circuit<T, N>,
99+
circuit: &Circuit<T>,
100100
node: N,
101101
unit_labeller: UL,
102102
) -> Self {
@@ -111,7 +111,7 @@ where
111111
/// Create a new iterator over the units terminating on the node.
112112
#[inline]
113113
pub(super) fn new_incoming<T: HugrView<Node = N>>(
114-
circuit: &Circuit<T, N>,
114+
circuit: &Circuit<T>,
115115
node: N,
116116
unit_labeller: UL,
117117
) -> Self {
@@ -127,7 +127,7 @@ where
127127
/// Create a new iterator over the units of a node.
128128
#[inline]
129129
fn new_with_dir<T: HugrView<Node = N>>(
130-
circuit: &Circuit<T, N>,
130+
circuit: &Circuit<T>,
131131
node: N,
132132
direction: Direction,
133133
unit_labeller: UL,
@@ -151,9 +151,9 @@ where
151151
// We should revisit it once this is reworked on the HUGR side.
152152
//
153153
// TODO: EdgeKind::Function is not currently supported.
154-
fn init_types<T: HugrView<Node = N>>(
155-
circuit: &Circuit<T, N>,
156-
node: N,
154+
fn init_types<T: HugrView>(
155+
circuit: &Circuit<T>,
156+
node: T::Node,
157157
direction: Direction,
158158
) -> TypeRow {
159159
let hugr = circuit.hugr();

tket2/src/rewrite.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<N: HugrNode> Subcircuit<N> {
3737
/// Create a new subcircuit induced from a set of nodes.
3838
pub fn try_from_nodes(
3939
nodes: impl Into<Vec<N>>,
40-
circ: &Circuit<impl HugrView<Node = N>, N>,
40+
circ: &Circuit<impl HugrView<Node = N>>,
4141
) -> Result<Self, InvalidSubgraph<N>> {
4242
let subgraph = SiblingSubgraph::try_from_nodes(nodes, circ.hugr())?;
4343
Ok(Self { subgraph })
@@ -54,7 +54,7 @@ impl<N: HugrNode> Subcircuit<N> {
5454
}
5555

5656
/// The signature of the subcircuit.
57-
pub fn signature(&self, circ: &Circuit<impl HugrView<Node = N>, N>) -> Signature {
57+
pub fn signature(&self, circ: &Circuit<impl HugrView<Node = N>>) -> Signature {
5858
self.subgraph.signature(circ.hugr())
5959
}
6060
}

tket2/src/serialize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::{Circuit, CircuitError};
3232
#[allow(unused)]
3333
const METADATA_ENTRYPOINT: &str = "TKET2.entrypoint";
3434

35-
impl<T: HugrView> Circuit<T, T::Node> {
35+
impl<T: HugrView> Circuit<T> {
3636
/// Store the circuit as a HUGR envelope, using the given configuration.
3737
///
3838
/// If the circuit is not a function in a module-rooted HUGR, a new module

0 commit comments

Comments
 (0)