@@ -8,35 +8,18 @@ use std::collections::{BTreeSet, HashMap, VecDeque};
8
8
9
9
use hugr:: extension:: { ExtensionId , ExtensionSet } ;
10
10
use hugr:: ops:: { ExtensionOp , Value } ;
11
- use hugr:: types:: { SumType , Type , TypeEnum } ;
11
+ use hugr:: types:: { SumType , Type } ;
12
12
13
13
use crate :: serialize:: pytket:: encoder:: EncodeStatus ;
14
- use crate :: serialize:: pytket:: extension:: {
15
- set_bits_op, BoolEmitter , FloatEmitter , PreludeEmitter , RotationEmitter , Tk1Emitter , Tk2Emitter ,
16
- } ;
14
+ use crate :: serialize:: pytket:: extension:: { set_bits_op, PytketTypeTranslator , RegisterCount } ;
17
15
use crate :: serialize:: pytket:: { PytketEmitter , Tk1ConvertError } ;
18
16
use crate :: Circuit ;
19
17
20
- use super :: value_tracker:: RegisterCount ;
21
- use super :: { Tk1EncoderContext , TrackedValues } ;
22
- use hugr:: extension:: prelude:: bool_t;
18
+ use super :: super :: encoder:: { Tk1EncoderContext , TrackedValues } ;
19
+ use super :: TypeTranslatorSet ;
23
20
use hugr:: HugrView ;
24
21
use itertools:: Itertools ;
25
22
26
- /// Default encoder configuration for [`Circuit`]s.
27
- ///
28
- /// Contains emitters for std and tket2 operations.
29
- pub fn default_encoder_config < H : HugrView > ( ) -> Tk1EncoderConfig < H > {
30
- let mut config = Tk1EncoderConfig :: new ( ) ;
31
- config. add_emitter ( PreludeEmitter ) ;
32
- config. add_emitter ( BoolEmitter ) ;
33
- config. add_emitter ( FloatEmitter ) ;
34
- config. add_emitter ( RotationEmitter ) ;
35
- config. add_emitter ( Tk1Emitter ) ;
36
- config. add_emitter ( Tk2Emitter ) ;
37
- config
38
- }
39
-
40
23
/// Configuration for converting [`Circuit`] into
41
24
/// [`tket_json_rs::circuit_json::SerialCircuit`].
42
25
///
@@ -54,16 +37,14 @@ pub struct Tk1EncoderConfig<H: HugrView> {
54
37
extension_emitters : HashMap < ExtensionId , Vec < usize > > ,
55
38
/// Emitters that request to be called for all operations.
56
39
no_extension_emitters : Vec < usize > ,
40
+ /// Set of type translators used to translate HUGR types into pytket registers.
41
+ type_translators : TypeTranslatorSet ,
57
42
}
58
43
59
44
impl < H : HugrView > Tk1EncoderConfig < H > {
60
45
/// Create a new [`Tk1EncoderConfig`] with no encoders.
61
46
pub fn new ( ) -> Self {
62
- Self {
63
- emitters : vec ! [ ] ,
64
- extension_emitters : HashMap :: new ( ) ,
65
- no_extension_emitters : vec ! [ ] ,
66
- }
47
+ Self :: default ( )
67
48
}
68
49
69
50
/// Add an encoder to the configuration.
@@ -84,6 +65,14 @@ impl<H: HugrView> Tk1EncoderConfig<H> {
84
65
self . emitters . push ( Box :: new ( encoder) ) ;
85
66
}
86
67
68
+ /// Add a type translator to the configuration.
69
+ pub fn add_type_translator (
70
+ & mut self ,
71
+ translator : impl PytketTypeTranslator + Send + Sync + ' static ,
72
+ ) {
73
+ self . type_translators . add_type_translator ( translator) ;
74
+ }
75
+
87
76
/// List the extensions supported by the encoders.
88
77
///
89
78
/// Some encoders may not specify an extension, in which case they will be called
@@ -98,7 +87,7 @@ impl<H: HugrView> Tk1EncoderConfig<H> {
98
87
///
99
88
/// Returns `true` if the operation was successfully converted and no further
100
89
/// encoders should be called.
101
- pub ( super ) fn op_to_pytket (
90
+ pub fn op_to_pytket (
102
91
& self ,
103
92
node : H :: Node ,
104
93
op : & ExtensionOp ,
@@ -116,57 +105,12 @@ impl<H: HugrView> Tk1EncoderConfig<H> {
116
105
Ok ( result)
117
106
}
118
107
119
- /// Translate a HUGR type into a count of qubits, bits, and parameters,
120
- /// using the registered custom encoders.
121
- ///
122
- /// Only tuple sums, bools, and custom types are supported.
123
- /// Other types will return `None`.
124
- pub fn type_to_pytket (
125
- & self ,
126
- typ : & Type ,
127
- ) -> Result < Option < RegisterCount > , Tk1ConvertError < H :: Node > > {
128
- match typ. as_type_enum ( ) {
129
- TypeEnum :: Sum ( sum) => {
130
- if typ == & bool_t ( ) {
131
- return Ok ( Some ( RegisterCount {
132
- qubits : 0 ,
133
- bits : 1 ,
134
- params : 0 ,
135
- } ) ) ;
136
- }
137
- if let Some ( tuple) = sum. as_tuple ( ) {
138
- let count: Result < Option < RegisterCount > , Tk1ConvertError < H :: Node > > = tuple
139
- . iter ( )
140
- . map ( |ty| {
141
- match ty. clone ( ) . try_into ( ) {
142
- Ok ( ty) => Ok ( self . type_to_pytket ( & ty) ?) ,
143
- // Sum types with row variables (variable tuple lengths) are not supported.
144
- Err ( _) => Ok ( None ) ,
145
- }
146
- } )
147
- . sum ( ) ;
148
- return count;
149
- }
150
- }
151
- TypeEnum :: Extension ( custom) => {
152
- let type_ext = custom. extension ( ) ;
153
- for encoder in self . emitters_for_extension ( type_ext) {
154
- if let Some ( count) = encoder. type_to_pytket ( custom) ? {
155
- return Ok ( Some ( count) ) ;
156
- }
157
- }
158
- }
159
- _ => { }
160
- }
161
- Ok ( None )
162
- }
163
-
164
108
/// Encode a const value into the pytket context using the registered custom
165
109
/// encoders.
166
110
///
167
111
/// Returns the values associated to the loaded constant, or `None` if the
168
112
/// constant could not be encoded.
169
- pub ( super ) fn const_to_pytket (
113
+ pub fn const_to_pytket (
170
114
& self ,
171
115
value : & Value ,
172
116
encoder : & mut Tk1EncoderContext < H > ,
@@ -216,6 +160,15 @@ impl<H: HugrView> Tk1EncoderConfig<H> {
216
160
Ok ( Some ( values) )
217
161
}
218
162
163
+ /// Translate a HUGR type into a count of qubits, bits, and parameters,
164
+ /// using the registered custom translator.
165
+ ///
166
+ /// Only tuple sums, bools, and custom types are supported.
167
+ /// Other types will return `None`.
168
+ pub fn type_to_pytket ( & self , typ : & Type ) -> Option < RegisterCount > {
169
+ self . type_translators . type_to_pytket ( typ)
170
+ }
171
+
219
172
/// Lists the emitters that can handle a given extension.
220
173
fn emitters_for_extension (
221
174
& self ,
@@ -250,6 +203,7 @@ impl<H: HugrView> Default for Tk1EncoderConfig<H> {
250
203
emitters : Default :: default ( ) ,
251
204
extension_emitters : Default :: default ( ) ,
252
205
no_extension_emitters : Default :: default ( ) ,
206
+ type_translators : TypeTranslatorSet :: default ( ) ,
253
207
}
254
208
}
255
209
}
0 commit comments