Skip to content

Commit

Permalink
debug assert the output order of subcircuit
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Jul 19, 2024
1 parent a30897e commit f90ccb8
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions tket2/src/passes/tuple_unpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,31 @@ fn remove_pack_unpack<T: HugrView>(
let mut nodes = unpack_nodes;
nodes.push(pack_node);
let subcirc = Subcircuit::try_from_nodes(nodes, circ).unwrap();
let subcirc_signature = subcirc.signature(circ);

// The output port order in `Subcircuit::try_from_nodes` is not too well defined.
// Check that the outputs are in the expected order.
if cfg!(debug) {
let tuple_type = Type::new_tuple(tuple_types.to_vec());
debug_assert!(
itertools::equal(
subcirc_signature.output().iter(),
tuple_types
.iter()
.cycle()
.take(num_unpack_outputs)
.chain(itertools::repeat_n(&tuple_type, num_other_outputs))
),
"Unpacked tuple values must come before tupled values"
);
}

let mut replacement = DFGBuilder::new(subcirc.signature(circ)).unwrap();
let mut replacement = DFGBuilder::new(subcirc_signature).unwrap();
let mut outputs = Vec::with_capacity(num_unpack_outputs + num_other_outputs);

// Wire the inputs directly to the unpack outputs
outputs.extend(replacement.input_wires().cycle().take(num_unpack_outputs));

// If needed, re-add the tuple pack node and connect its output to the tuple outputs.
if num_other_outputs > 0 {
let op = MakeTuple::new(tuple_types.to_vec().into());
Expand All @@ -110,9 +131,6 @@ fn remove_pack_unpack<T: HugrView>(
outputs.extend(std::iter::repeat(tuple).take(num_other_outputs))
}

// Wire the inputs directly to the unpack outputs
outputs.extend(replacement.input_wires().cycle().take(num_unpack_outputs));

let replacement = replacement
.finish_prelude_hugr_with_outputs(outputs)
.unwrap_or_else(|e| {
Expand Down

0 comments on commit f90ccb8

Please sign in to comment.