13
13
# limitations under the License.
14
14
import math
15
15
import numbers
16
- from typing import cast , Dict , FrozenSet , Iterable , Iterator , List , Optional , Sequence , Union
16
+ from typing import cast , Iterable , Iterator , List , Optional , Sequence , Union
17
17
18
18
import numpy as np
19
19
import sympy
22
22
from cirq .qis import CliffordTableau
23
23
import tunits
24
24
25
- SUPPORTED_FUNCTIONS_FOR_LANGUAGE : Dict [Optional [str ], FrozenSet [str ]] = {
26
- '' : frozenset (),
27
- 'linear' : frozenset ({'add' , 'mul' }),
28
- 'exp' : frozenset ({'add' , 'mul' , 'pow' }),
29
- # None means any. Is used when inferring the language during serialization.
30
- None : frozenset ({'add' , 'mul' , 'pow' }),
31
- }
32
- MOST_PERMISSIVE_LANGUAGE = 'exp'
33
-
34
25
SUPPORTED_SYMPY_OPS = (sympy .Symbol , sympy .Add , sympy .Mul , sympy .Pow )
35
26
36
27
# Argument types for gates.
@@ -94,10 +85,7 @@ def _function_languages_from_arg(arg_proto: v2.program_pb2.Arg) -> Iterator[str]
94
85
95
86
96
87
def float_arg_to_proto (
97
- value : ARG_LIKE ,
98
- * ,
99
- arg_function_language : Optional [str ] = None ,
100
- out : Optional [v2 .program_pb2 .FloatArg ] = None ,
88
+ value : ARG_LIKE , * , out : Optional [v2 .program_pb2 .FloatArg ] = None
101
89
) -> v2 .program_pb2 .FloatArg :
102
90
"""Writes an argument value into an FloatArg proto.
103
91
@@ -108,9 +96,6 @@ def float_arg_to_proto(
108
96
Args:
109
97
value: The value to encode. This must be a float or compatible
110
98
sympy expression. Strings and repeated booleans are not allowed.
111
- arg_function_language: The language to use when encoding functions. If
112
- this is set to None, it will be set to the minimal language
113
- necessary to support the features that were actually used.
114
99
out: The proto to write the result into. Defaults to a new instance.
115
100
116
101
Returns:
@@ -121,29 +106,22 @@ def float_arg_to_proto(
121
106
if isinstance (value , FLOAT_TYPES ):
122
107
msg .float_value = float (value )
123
108
else :
124
- _arg_func_to_proto (value , arg_function_language , msg )
109
+ _arg_func_to_proto (value , msg )
125
110
126
111
return msg
127
112
128
113
129
114
def arg_to_proto (
130
- value : ARG_LIKE ,
131
- * ,
132
- arg_function_language : Optional [str ] = None ,
133
- out : Optional [v2 .program_pb2 .Arg ] = None ,
115
+ value : ARG_LIKE , * , out : Optional [v2 .program_pb2 .Arg ] = None
134
116
) -> v2 .program_pb2 .Arg :
135
117
"""Writes an argument value into an Arg proto.
136
118
137
119
Args:
138
120
value: The value to encode.
139
- arg_function_language: The language to use when encoding functions. If
140
- this is set to None, it will be set to the minimal language
141
- necessary to support the features that were actually used.
142
121
out: The proto to write the result into. Defaults to a new instance.
143
122
144
123
Returns:
145
- The proto that was written into as well as the `arg_function_language`
146
- that was used.
124
+ The proto that was written into.
147
125
148
126
Raises:
149
127
ValueError: if the object holds unsupported values.
@@ -188,42 +166,28 @@ def arg_to_proto(
188
166
elif isinstance (value , tunits .Value ):
189
167
msg .arg_value .value_with_unit .MergeFrom (value .to_proto ())
190
168
else :
191
- _arg_func_to_proto (value , arg_function_language , msg )
169
+ _arg_func_to_proto (value , msg )
192
170
193
171
return msg
194
172
195
173
196
174
def _arg_func_to_proto (
197
- value : ARG_LIKE ,
198
- arg_function_language : Optional [str ],
199
- msg : Union [v2 .program_pb2 .Arg , v2 .program_pb2 .FloatArg ],
175
+ value : ARG_LIKE , msg : Union [v2 .program_pb2 .Arg , v2 .program_pb2 .FloatArg ]
200
176
) -> None :
201
- def check_support (func_type : str ) -> str :
202
- if func_type not in supported :
203
- lang = repr (arg_function_language ) if arg_function_language is not None else '[any]'
204
- raise ValueError (
205
- f'Function type { func_type !r} not supported by arg_function_language { lang } '
206
- )
207
- return func_type
208
-
209
- if arg_function_language not in SUPPORTED_FUNCTIONS_FOR_LANGUAGE :
210
- raise ValueError (f'Unrecognized arg_function_language: { arg_function_language !r} ' )
211
- supported = SUPPORTED_FUNCTIONS_FOR_LANGUAGE [arg_function_language ]
212
-
213
177
if isinstance (value , sympy .Symbol ):
214
178
msg .symbol = str (value .free_symbols .pop ())
215
179
elif isinstance (value , sympy .Add ):
216
- msg .func .type = check_support ( 'add' )
180
+ msg .func .type = 'add'
217
181
for arg in value .args :
218
- arg_to_proto (arg , arg_function_language = arg_function_language , out = msg .func .args .add ())
182
+ arg_to_proto (arg , out = msg .func .args .add ())
219
183
elif isinstance (value , sympy .Mul ):
220
- msg .func .type = check_support ( 'mul' )
184
+ msg .func .type = 'mul'
221
185
for arg in value .args :
222
- arg_to_proto (arg , arg_function_language = arg_function_language , out = msg .func .args .add ())
186
+ arg_to_proto (arg , out = msg .func .args .add ())
223
187
elif isinstance (value , sympy .Pow ):
224
- msg .func .type = check_support ( 'pow' )
188
+ msg .func .type = 'pow'
225
189
for arg in value .args :
226
- arg_to_proto (arg , arg_function_language = arg_function_language , out = msg .func .args .add ())
190
+ arg_to_proto (arg , out = msg .func .args .add ())
227
191
else :
228
192
raise ValueError (
229
193
f"Unrecognized Sympy expression type: { type (value )} ."
@@ -233,10 +197,7 @@ def check_support(func_type: str) -> str:
233
197
234
198
235
199
def float_arg_from_proto (
236
- arg_proto : v2 .program_pb2 .FloatArg ,
237
- * ,
238
- arg_function_language : str ,
239
- required_arg_name : Optional [str ] = None ,
200
+ arg_proto : v2 .program_pb2 .FloatArg , * , required_arg_name : Optional [str ] = None
240
201
) -> Optional [FLOAT_ARG_LIKE ]:
241
202
"""Extracts a python value from an argument value proto.
242
203
@@ -245,8 +206,6 @@ def float_arg_from_proto(
245
206
246
207
Args:
247
208
arg_proto: The proto containing a serialized value.
248
- arg_function_language: The `arg_function_language` field from
249
- `Program.Language`.
250
209
required_arg_name: If set to `None`, the method will return `None` when
251
210
given an unset proto value. If set to a string, the method will
252
211
instead raise an error complaining that the value is missing in that
@@ -268,11 +227,7 @@ def float_arg_from_proto(
268
227
elif which == 'symbol' :
269
228
return sympy .Symbol (arg_proto .symbol )
270
229
elif which == 'func' :
271
- func = _arg_func_from_proto (
272
- arg_proto .func ,
273
- arg_function_language = arg_function_language ,
274
- required_arg_name = required_arg_name ,
275
- )
230
+ func = _arg_func_from_proto (arg_proto .func , required_arg_name = required_arg_name )
276
231
if func is None and required_arg_name is not None :
277
232
raise ValueError (
278
233
f'Arg { arg_proto .func } could not be processed for { required_arg_name } .'
@@ -287,17 +242,12 @@ def float_arg_from_proto(
287
242
288
243
289
244
def arg_from_proto (
290
- arg_proto : v2 .program_pb2 .Arg ,
291
- * ,
292
- arg_function_language : str ,
293
- required_arg_name : Optional [str ] = None ,
245
+ arg_proto : v2 .program_pb2 .Arg , * , required_arg_name : Optional [str ] = None
294
246
) -> Optional [ARG_RETURN_LIKE ]:
295
247
"""Extracts a python value from an argument value proto.
296
248
297
249
Args:
298
250
arg_proto: The proto containing a serialized value.
299
- arg_function_language: The `arg_function_language` field from
300
- `Program.Language`.
301
251
required_arg_name: If set to `None`, the method will return `None` when
302
252
given an unset proto value. If set to a string, the method will
303
253
instead raise an error complaining that the value is missing in that
@@ -342,11 +292,7 @@ def arg_from_proto(
342
292
return sympy .Symbol (arg_proto .symbol )
343
293
344
294
if which == 'func' :
345
- func = _arg_func_from_proto (
346
- arg_proto .func ,
347
- arg_function_language = arg_function_language ,
348
- required_arg_name = required_arg_name ,
349
- )
295
+ func = _arg_func_from_proto (arg_proto .func , required_arg_name = required_arg_name )
350
296
if func is not None :
351
297
return func
352
298
@@ -360,55 +306,22 @@ def arg_from_proto(
360
306
361
307
362
308
def _arg_func_from_proto (
363
- func : v2 .program_pb2 .ArgFunction ,
364
- * ,
365
- arg_function_language : str ,
366
- required_arg_name : Optional [str ] = None ,
309
+ func : v2 .program_pb2 .ArgFunction , * , required_arg_name : Optional [str ] = None
367
310
) -> Optional [ARG_RETURN_LIKE ]:
368
- supported = SUPPORTED_FUNCTIONS_FOR_LANGUAGE .get (arg_function_language )
369
- if supported is None :
370
- raise ValueError (f'Unrecognized arg_function_language: { arg_function_language !r} ' )
371
-
372
- if func .type not in supported :
373
- raise ValueError (
374
- f'Unrecognized function type { func .type !r} '
375
- f'for arg_function_language={ arg_function_language !r} '
376
- )
377
311
378
312
if func .type == 'add' :
379
313
return sympy .Add (
380
- * [
381
- arg_from_proto (
382
- a ,
383
- arg_function_language = arg_function_language ,
384
- required_arg_name = 'An addition argument' ,
385
- )
386
- for a in func .args
387
- ]
314
+ * [arg_from_proto (a , required_arg_name = 'An addition argument' ) for a in func .args ]
388
315
)
389
316
390
317
if func .type == 'mul' :
391
318
return sympy .Mul (
392
- * [
393
- arg_from_proto (
394
- a ,
395
- arg_function_language = arg_function_language ,
396
- required_arg_name = 'A multiplication argument' ,
397
- )
398
- for a in func .args
399
- ]
319
+ * [arg_from_proto (a , required_arg_name = 'A multiplication argument' ) for a in func .args ]
400
320
)
401
321
402
322
if func .type == 'pow' :
403
323
return sympy .Pow (
404
- * [
405
- arg_from_proto (
406
- a ,
407
- arg_function_language = arg_function_language ,
408
- required_arg_name = 'A power argument' ,
409
- )
410
- for a in func .args
411
- ]
324
+ * [arg_from_proto (a , required_arg_name = 'A power argument' ) for a in func .args ]
412
325
)
413
326
return None
414
327
@@ -420,9 +333,6 @@ def internal_gate_arg_to_proto(
420
333
421
334
Args:
422
335
value: The gate to encode.
423
- arg_function_language: The language to use when encoding functions. If
424
- this is set to None, it will be set to the minimal language
425
- necessary to support the features that were actually used.
426
336
out: The proto to write the result into. Defaults to a new instance.
427
337
428
338
Returns:
@@ -442,15 +352,11 @@ def internal_gate_arg_to_proto(
442
352
return msg
443
353
444
354
445
- def internal_gate_from_proto (
446
- msg : v2 .program_pb2 .InternalGate , arg_function_language : str
447
- ) -> InternalGate :
355
+ def internal_gate_from_proto (msg : v2 .program_pb2 .InternalGate ) -> InternalGate :
448
356
"""Extracts an InternalGate object from an InternalGate proto.
449
357
450
358
Args:
451
359
msg: The proto containing a serialized value.
452
- arg_function_language: The `arg_function_language` field from
453
- `Program.Language`.
454
360
455
361
Returns:
456
362
The deserialized InternalGate object.
@@ -460,7 +366,7 @@ def internal_gate_from_proto(
460
366
"""
461
367
gate_args = {}
462
368
for k , v in msg .gate_args .items ():
463
- gate_args [k ] = arg_from_proto (v , arg_function_language = arg_function_language )
369
+ gate_args [k ] = arg_from_proto (v )
464
370
return InternalGate (
465
371
gate_name = str (msg .name ),
466
372
gate_module = str (msg .module ),
@@ -476,9 +382,6 @@ def clifford_tableau_arg_to_proto(
476
382
"""Writes an CliffordTableau object into an CliffordTableau proto.
477
383
Args:
478
384
value: The gate to encode.
479
- arg_function_language: The language to use when encoding functions. If
480
- this is set to None, it will be set to the minimal language
481
- necessary to support the features that were actually used.
482
385
out: The proto to write the result into. Defaults to a new instance.
483
386
Returns:
484
387
The proto that was written into.
@@ -492,14 +395,10 @@ def clifford_tableau_arg_to_proto(
492
395
return msg
493
396
494
397
495
- def clifford_tableau_from_proto (
496
- msg : v2 .program_pb2 .CliffordTableau , arg_function_language : str
497
- ) -> CliffordTableau :
398
+ def clifford_tableau_from_proto (msg : v2 .program_pb2 .CliffordTableau ) -> CliffordTableau :
498
399
"""Extracts a CliffordTableau object from a CliffordTableau proto.
499
400
Args:
500
401
msg: The proto containing a serialized value.
501
- arg_function_language: The `arg_function_language` field from
502
- `Program.Language`.
503
402
Returns:
504
403
The deserialized InternalGate object.
505
404
"""
0 commit comments