@@ -9,12 +9,13 @@ pub use config::{default_encoder_config, Tk1EncoderConfig};
9
9
use hugr:: envelope:: EnvelopeConfig ;
10
10
use hugr:: hugr:: views:: SiblingSubgraph ;
11
11
use hugr:: package:: Package ;
12
+ use hugr_core:: hugr:: internal:: PortgraphNodeMap ;
12
13
pub use value_tracker:: {
13
14
RegisterCount , TrackedBit , TrackedParam , TrackedQubit , TrackedValue , TrackedValues ,
14
15
ValueTracker ,
15
16
} ;
16
17
17
- use hugr:: ops:: { NamedOp , OpTrait , OpType } ;
18
+ use hugr:: ops:: { OpTrait , OpType } ;
18
19
use hugr:: types:: EdgeKind ;
19
20
20
21
use std:: borrow:: Cow ;
@@ -23,8 +24,6 @@ use std::sync::Arc;
23
24
24
25
use hugr:: { HugrView , Wire } ;
25
26
use itertools:: Itertools ;
26
- use portgraph:: algorithms:: TopoSort ;
27
- use portgraph:: LinkView ;
28
27
use tket_json_rs:: circuit_json:: { self , SerialCircuit } ;
29
28
use unsupported_tracker:: UnsupportedTracker ;
30
29
@@ -93,34 +92,15 @@ impl<H: HugrView> Tk1EncoderContext<H> {
93
92
) -> Result < ( ) , Tk1ConvertError < H :: Node > > {
94
93
// Normally we'd use `SiblingGraph` here, but it doesn't support generic node types.
95
94
// See https://github.com/CQCL/hugr/issues/2010
96
- let hugr = circ. hugr ( ) ;
97
- let root = circ. parent ( ) ;
98
- let portgraph = hugr. portgraph ( ) ;
99
- let hierarchy = hugr. hierarchy ( ) ;
100
- let region =
101
- portgraph:: view:: FlatRegion :: new ( & portgraph, & hierarchy, hugr. get_pg_index ( root) ) ;
102
-
103
- // Collect all initial nodes in the region (nodes with no predecessors).
104
- let initials: Vec < _ > = hugr
105
- . children ( root)
106
- . filter_map ( |node| {
107
- let pg_node = hugr. get_pg_index ( node) ;
108
- region
109
- . input_links ( pg_node)
110
- . next ( )
111
- . is_none ( )
112
- . then_some ( pg_node)
113
- } )
114
- . collect ( ) ;
115
-
116
- let topo: TopoSort < ' _ , _ , HashSet < _ > > =
117
- portgraph:: algorithms:: toposort ( region, initials, portgraph:: Direction :: Outgoing ) ;
95
+ let ( region, node_map) = circ. hugr ( ) . region_portgraph ( circ. parent ( ) ) ;
118
96
let io_nodes = circ. io_nodes ( ) ;
97
+
119
98
// TODO: Use weighted topological sort to try and explore unsupported
120
99
// ops first (that is, ops with no available emitter in `self.config`),
121
100
// to ensure we group them as much as possible.
122
- for pg_node in topo {
123
- let node = hugr. get_node ( pg_node) ;
101
+ let mut topo = petgraph:: visit:: Topo :: new ( & region) ;
102
+ while let Some ( pg_node) = topo. next ( & region) {
103
+ let node = node_map. from_portgraph ( pg_node) ;
124
104
if io_nodes. contains ( & node) {
125
105
// I/O nodes are handled by `new` and `finish`.
126
106
continue ;
@@ -310,7 +290,7 @@ impl<H: HugrView> Tk1EncoderContext<H> {
310
290
tk1_optype : tket_json_rs:: OpType ,
311
291
node : H :: Node ,
312
292
circ : & Circuit < H > ,
313
- output_params : impl FnOnce ( OutputParamArgs ) -> Vec < String > ,
293
+ output_params : impl FnOnce ( OutputParamArgs < ' _ > ) -> Vec < String > ,
314
294
) -> Result < ( ) , Tk1ConvertError < H :: Node > > {
315
295
self . emit_node_command (
316
296
node,
@@ -345,7 +325,7 @@ impl<H: HugrView> Tk1EncoderContext<H> {
345
325
& mut self ,
346
326
node : H :: Node ,
347
327
circ : & Circuit < H > ,
348
- output_params : impl FnOnce ( OutputParamArgs ) -> Vec < String > ,
328
+ output_params : impl FnOnce ( OutputParamArgs < ' _ > ) -> Vec < String > ,
349
329
make_operation : impl FnOnce ( usize , usize , & [ String ] ) -> tket_json_rs:: circuit_json:: Operation ,
350
330
) -> Result < ( ) , Tk1ConvertError < H :: Node > > {
351
331
let TrackedValues {
@@ -411,7 +391,7 @@ impl<H: HugrView> Tk1EncoderContext<H> {
411
391
& mut self ,
412
392
node : H :: Node ,
413
393
circ : & Circuit < H > ,
414
- output_params : impl FnOnce ( OutputParamArgs ) -> Vec < String > ,
394
+ output_params : impl FnOnce ( OutputParamArgs < ' _ > ) -> Vec < String > ,
415
395
) -> Result < ( ) , Tk1ConvertError < H :: Node > > {
416
396
let input_values = self . get_input_values ( node, circ) ?;
417
397
let output_counts = self . node_output_values ( node, circ) ?;
@@ -432,23 +412,23 @@ impl<H: HugrView> Tk1EncoderContext<H> {
432
412
if input_values. qubits . len ( ) != total_out_count. qubits {
433
413
return Err ( Tk1ConvertError :: custom ( format ! (
434
414
"Mismatched number of input and output qubits while trying to emit a transparent operation for {}. We have {} inputs but {} outputs." ,
435
- circ. hugr( ) . get_optype( node) . name ( ) ,
415
+ circ. hugr( ) . get_optype( node) ,
436
416
input_values. qubits. len( ) ,
437
417
total_out_count. qubits,
438
418
) ) ) ;
439
419
}
440
420
if input_values. bits . len ( ) != total_out_count. bits {
441
421
return Err ( Tk1ConvertError :: custom ( format ! (
442
422
"Mismatched number of input and output bits while trying to emit a transparent operation for {}. We have {} inputs but {} outputs." ,
443
- circ. hugr( ) . get_optype( node) . name ( ) ,
423
+ circ. hugr( ) . get_optype( node) ,
444
424
input_values. bits. len( ) ,
445
425
total_out_count. bits,
446
426
) ) ) ;
447
427
}
448
428
if out_params. len ( ) != total_out_count. params {
449
429
return Err ( Tk1ConvertError :: custom ( format ! (
450
430
"Not enough parameters in the input values for a {}. Expected {} but got {}." ,
451
- circ. hugr( ) . get_optype( node) . name ( ) ,
431
+ circ. hugr( ) . get_optype( node) ,
452
432
total_out_count. params,
453
433
out_params. len( )
454
434
) ) ) ;
@@ -506,7 +486,6 @@ impl<H: HugrView> Tk1EncoderContext<H> {
506
486
507
487
let unsupported_hugr = subgraph. extract_subgraph ( circ. hugr ( ) , & subcircuit_id) ;
508
488
let payload = Package :: from_hugr ( unsupported_hugr)
509
- . unwrap ( )
510
489
. store_str ( EnvelopeConfig :: text ( ) )
511
490
. unwrap ( ) ;
512
491
@@ -677,7 +656,7 @@ impl<H: HugrView> Tk1EncoderContext<H> {
677
656
circ : & Circuit < H > ,
678
657
qubits : & mut impl Iterator < Item = TrackedQubit > ,
679
658
input_params : & [ String ] ,
680
- output_params : impl FnOnce ( OutputParamArgs ) -> Vec < String > ,
659
+ output_params : impl FnOnce ( OutputParamArgs < ' _ > ) -> Vec < String > ,
681
660
wire_filter : impl Fn ( Wire < H :: Node > ) -> bool ,
682
661
) -> Result < TrackedValues , Tk1ConvertError < H :: Node > > {
683
662
let output_counts = self . node_output_values ( node, circ) ?;
@@ -693,7 +672,7 @@ impl<H: HugrView> Tk1EncoderContext<H> {
693
672
if out_params. len ( ) != total_out_count. params {
694
673
return Err ( Tk1ConvertError :: custom ( format ! (
695
674
"Not enough parameters in the input values for a {}. Expected {} but got {}." ,
696
- circ. hugr( ) . get_optype( node) . name ( ) ,
675
+ circ. hugr( ) . get_optype( node) ,
697
676
total_out_count. params,
698
677
out_params. len( )
699
678
) ) ) ;
@@ -752,8 +731,7 @@ impl<H: HugrView> Tk1EncoderContext<H> {
752
731
} else if Some ( out_port) == static_output {
753
732
let EdgeKind :: Const ( ty) = op. static_output ( ) . unwrap ( ) else {
754
733
return Err ( Tk1ConvertError :: custom ( format ! (
755
- "Cannot emit a static output for a {}." ,
756
- op. name( )
734
+ "Cannot emit a static output for a {op}."
757
735
) ) ) ;
758
736
} ;
759
737
ty
@@ -772,8 +750,7 @@ impl<H: HugrView> Tk1EncoderContext<H> {
772
750
let wire = hugr:: Wire :: new ( node, out_port) ;
773
751
let Some ( count) = self . config ( ) . type_to_pytket ( & ty) ? else {
774
752
return Err ( Tk1ConvertError :: custom ( format ! (
775
- "Found an unsupported type while encoding a {}." ,
776
- op. name( )
753
+ "Found an unsupported type while encoding a {op}."
777
754
) ) ) ;
778
755
} ;
779
756
wire_counts. push ( ( wire, count) ) ;
0 commit comments