1
1
//! Configuration for converting [`Circuit`]s into [`SerialCircuit`]
2
2
//!
3
- //! A configuration struct contains a list of custom encoders that define translations
3
+ //! A configuration struct contains a list of custom emitters that define translations
4
4
//! of HUGR operations and types into pytket primitives.
5
5
6
6
use std:: collections:: { BTreeSet , HashMap , VecDeque } ;
@@ -10,9 +10,9 @@ use hugr::ops::{ExtensionOp, Value};
10
10
use hugr:: types:: { SumType , Type , TypeEnum } ;
11
11
12
12
use crate :: serialize:: pytket:: extension:: {
13
- FloatEncoder , PreludeEncoder , RotationEncoder , Tk1OpEncoder , Tk2OpEncoder ,
13
+ FloatEmitter , PreludeEmitter , RotationEmitter , Tk1Emitter , Tk2Emitter ,
14
14
} ;
15
- use crate :: serialize:: pytket:: { Tk1ConvertError , Tk1Encoder } ;
15
+ use crate :: serialize:: pytket:: { PytketEmitter , Tk1ConvertError } ;
16
16
use crate :: Circuit ;
17
17
18
18
use super :: value_tracker:: RegisterCount ;
@@ -23,62 +23,61 @@ use itertools::Itertools;
23
23
24
24
/// Default encoder configuration for [`Circuit`]s.
25
25
///
26
- /// Contains encoders for std and tket2 operations.
26
+ /// Contains emitters for std and tket2 operations.
27
27
pub fn default_encoder_config < H : HugrView > ( ) -> Tk1EncoderConfig < H > {
28
- // TODO: Add std & tket2 encoders
29
28
let mut config = Tk1EncoderConfig :: new ( ) ;
30
- config. add_encoder ( PreludeEncoder ) ;
31
- config. add_encoder ( FloatEncoder ) ;
32
- config. add_encoder ( RotationEncoder ) ;
33
- config. add_encoder ( Tk1OpEncoder ) ;
34
- config. add_encoder ( Tk2OpEncoder ) ;
29
+ config. add_emitter ( PreludeEmitter ) ;
30
+ config. add_emitter ( FloatEmitter ) ;
31
+ config. add_emitter ( RotationEmitter ) ;
32
+ config. add_emitter ( Tk1Emitter ) ;
33
+ config. add_emitter ( Tk2Emitter ) ;
35
34
config
36
35
}
37
36
38
37
/// Configuration for converting [`Circuit`] into [`SerialCircuit`].
39
38
///
40
- /// Contains custom encoders that define translations for HUGR operations and types
41
- /// into pytket primitives.
39
+ /// Contains custom emitters that define translations for HUGR operations,
40
+ /// types, and consts into pytket primitives.
42
41
#[ derive( derive_more:: Debug ) ]
43
42
#[ debug( bounds( H : HugrView ) ) ]
44
43
pub struct Tk1EncoderConfig < H : HugrView > {
45
- /// Operation encoders
44
+ /// Operation emitters
46
45
#[ debug( skip) ]
47
- pub ( super ) encoders : Vec < Box < dyn Tk1Encoder < H > > > ,
48
- /// Pre-computed map from extension ids to corresponding encoders in
49
- /// `encoders `, identified by their index.
50
- #[ debug( "{:?}" , extension_encoders . keys( ) . collect_vec( ) ) ]
51
- extension_encoders : HashMap < ExtensionId , Vec < usize > > ,
52
- /// Extensions that request to be called for all operations.
53
- no_extension_encoders : Vec < usize > ,
46
+ pub ( super ) enmitters : Vec < Box < dyn PytketEmitter < H > > > ,
47
+ /// Pre-computed map from extension ids to corresponding emitters in
48
+ /// `emitters `, identified by their index.
49
+ #[ debug( "{:?}" , extension_emitters . keys( ) . collect_vec( ) ) ]
50
+ extension_emitters : HashMap < ExtensionId , Vec < usize > > ,
51
+ /// Emitters that request to be called for all operations.
52
+ no_extension_emitters : Vec < usize > ,
54
53
}
55
54
56
55
impl < H : HugrView > Tk1EncoderConfig < H > {
57
56
/// Create a new [`Tk1EncoderConfig`] with no encoders.
58
57
pub fn new ( ) -> Self {
59
58
Self {
60
- encoders : vec ! [ ] ,
61
- extension_encoders : HashMap :: new ( ) ,
62
- no_extension_encoders : vec ! [ ] ,
59
+ enmitters : vec ! [ ] ,
60
+ extension_emitters : HashMap :: new ( ) ,
61
+ no_extension_emitters : vec ! [ ] ,
63
62
}
64
63
}
65
64
66
65
/// Add an encoder to the configuration.
67
- pub fn add_encoder ( & mut self , encoder : impl Tk1Encoder < H > + ' static ) {
68
- let idx = self . encoders . len ( ) ;
66
+ pub fn add_emitter ( & mut self , encoder : impl PytketEmitter < H > + ' static ) {
67
+ let idx = self . enmitters . len ( ) ;
69
68
70
69
match encoder. extensions ( ) {
71
70
Some ( extensions) => {
72
71
for ext in extensions {
73
- self . extension_encoders . entry ( ext) . or_default ( ) . push ( idx) ;
72
+ self . extension_emitters . entry ( ext) . or_default ( ) . push ( idx) ;
74
73
}
75
74
}
76
75
// If the encoder does not specify an extension, it will be called
77
76
// for all operations.
78
- None => self . no_extension_encoders . push ( idx) ,
77
+ None => self . no_extension_emitters . push ( idx) ,
79
78
}
80
79
81
- self . encoders . push ( Box :: new ( encoder) ) ;
80
+ self . enmitters . push ( Box :: new ( encoder) ) ;
82
81
}
83
82
84
83
/// List the extensions supported by the encoders.
@@ -88,7 +87,7 @@ impl<H: HugrView> Tk1EncoderConfig<H> {
88
87
///
89
88
/// Use [`Tk1EncoderConfig::add_encoder`] to extend this list.
90
89
pub fn supported_extensions ( & self ) -> impl Iterator < Item = & ExtensionId > {
91
- self . extension_encoders . keys ( )
90
+ self . extension_emitters . keys ( )
92
91
}
93
92
94
93
/// Encode a HUGR operation using the registered custom encoders.
@@ -103,7 +102,7 @@ impl<H: HugrView> Tk1EncoderConfig<H> {
103
102
) -> Result < bool , Tk1ConvertError < H :: Node > > {
104
103
let mut result = false ;
105
104
let extension = op. def ( ) . extension_id ( ) ;
106
- for enc in self . encoders_for_extension ( extension) {
105
+ for enc in self . emitters_for_extension ( extension) {
107
106
if enc. op_to_pytket ( node, op, circ, encoder) ? {
108
107
result = true ;
109
108
break ;
@@ -146,7 +145,7 @@ impl<H: HugrView> Tk1EncoderConfig<H> {
146
145
}
147
146
TypeEnum :: Extension ( custom) => {
148
147
let type_ext = custom. extension ( ) ;
149
- for encoder in self . encoders_for_extension ( type_ext) {
148
+ for encoder in self . emitters_for_extension ( type_ext) {
150
149
if let Some ( count) = encoder. type_to_pytket ( custom) ? {
151
150
return Ok ( Some ( count) ) ;
152
151
}
@@ -185,7 +184,7 @@ impl<H: HugrView> Tk1EncoderConfig<H> {
185
184
Value :: Extension { e : opaque } => {
186
185
let type_exts = opaque. extension_reqs ( ) ;
187
186
let mut encoded = false ;
188
- for e in self . encoders_for_extensions ( & type_exts) {
187
+ for e in self . emitters_for_extensions ( & type_exts) {
189
188
if let Some ( vs) = e. const_to_pytket ( opaque, encoder) ? {
190
189
values. append ( vs) ;
191
190
encoded = true ;
@@ -202,40 +201,40 @@ impl<H: HugrView> Tk1EncoderConfig<H> {
202
201
Ok ( Some ( values) )
203
202
}
204
203
205
- /// Lists the encoders that can handle a given extension.
206
- fn encoders_for_extension (
204
+ /// Lists the emitters that can handle a given extension.
205
+ fn emitters_for_extension (
207
206
& self ,
208
207
ext : & ExtensionId ,
209
- ) -> impl Iterator < Item = & Box < dyn Tk1Encoder < H > > > {
210
- self . extension_encoders
208
+ ) -> impl Iterator < Item = & Box < dyn PytketEmitter < H > > > {
209
+ self . extension_emitters
211
210
. get ( ext)
212
211
. into_iter ( )
213
212
. flatten ( )
214
- . chain ( self . no_extension_encoders . iter ( ) )
215
- . map ( move |idx| & self . encoders [ * idx] )
213
+ . chain ( self . no_extension_emitters . iter ( ) )
214
+ . map ( move |idx| & self . enmitters [ * idx] )
216
215
}
217
216
218
- /// Lists the encoders that can handle a given set extensions.
219
- fn encoders_for_extensions (
217
+ /// Lists the emitters that can handle a given set extensions.
218
+ fn emitters_for_extensions (
220
219
& self ,
221
220
exts : & ExtensionSet ,
222
- ) -> impl Iterator < Item = & Box < dyn Tk1Encoder < H > > > {
223
- let encoder_ids : BTreeSet < usize > = exts
221
+ ) -> impl Iterator < Item = & Box < dyn PytketEmitter < H > > > {
222
+ let emitter_ids : BTreeSet < usize > = exts
224
223
. iter ( )
225
- . flat_map ( |ext| self . extension_encoders . get ( ext) . into_iter ( ) . flatten ( ) )
226
- . chain ( self . no_extension_encoders . iter ( ) )
224
+ . flat_map ( |ext| self . extension_emitters . get ( ext) . into_iter ( ) . flatten ( ) )
225
+ . chain ( self . no_extension_emitters . iter ( ) )
227
226
. copied ( )
228
227
. collect ( ) ;
229
- encoder_ids . into_iter ( ) . map ( move |idx| & self . encoders [ idx] )
228
+ emitter_ids . into_iter ( ) . map ( move |idx| & self . enmitters [ idx] )
230
229
}
231
230
}
232
231
233
232
impl < H : HugrView > Default for Tk1EncoderConfig < H > {
234
233
fn default ( ) -> Self {
235
234
Self {
236
- encoders : Default :: default ( ) ,
237
- extension_encoders : Default :: default ( ) ,
238
- no_extension_encoders : Default :: default ( ) ,
235
+ enmitters : Default :: default ( ) ,
236
+ extension_emitters : Default :: default ( ) ,
237
+ no_extension_emitters : Default :: default ( ) ,
239
238
}
240
239
}
241
240
}
0 commit comments