Skip to content

Commit 7a834a0

Browse files
authored
Remove arg_function_language (#7092)
* Remove arg_function_language - We have no need for this parameter and it is pretty much always set to the most permissive parameter. - Removes this parameter to simplify serialization. * Fix some typecheck stuff. * Fix test * Address comments.
1 parent 96583fe commit 7a834a0

File tree

11 files changed

+252
-629
lines changed

11 files changed

+252
-629
lines changed

cirq-google/cirq_google/api/v2/program.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ option java_multiple_files = true;
1111
// A quantum program.
1212
message Program {
1313
// The language in which the program is written.
14-
Language language = 1;
14+
Language language = 1 [deprecated=true];
1515

1616
// Programs can be specified by a circuit or a schedule.
1717
oneof program {
@@ -143,7 +143,7 @@ message Language {
143143
//
144144
// Valid names for the arg function language can be found in
145145
// cirq/google/arg_func_langs.py
146-
string arg_function_language = 2;
146+
string arg_function_language = 2 [deprecated=true];
147147
}
148148

149149
// Argument that is constrained to a float or symbolic expression

cirq-google/cirq_google/api/v2/program_pb2.py

Lines changed: 124 additions & 120 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cirq-google/cirq_google/ops/internal_gate_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def test_internal_gate_with_custom_function_round_trip():
111111

112112
msg = arg_func_langs.internal_gate_arg_to_proto(gate)
113113

114-
new_gate = arg_func_langs.internal_gate_from_proto(msg, arg_func_langs.MOST_PERMISSIVE_LANGUAGE)
114+
new_gate = arg_func_langs.internal_gate_from_proto(msg)
115115

116116
func_proto = new_gate.custom_args['func'].function_interpolation_data
117117

cirq-google/cirq_google/ops/internal_tag.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ def to_proto(self, msg: Optional[program_pb2.Tag] = None) -> program_pb2.Tag:
6868
msg.internal_tag.tag_name = self.name
6969
msg.internal_tag.tag_package = self.package
7070
for k, v in self.tag_args.items():
71-
arg_func_langs.arg_to_proto(
72-
v, out=msg.internal_tag.tag_args[k], arg_function_language='exp'
73-
)
71+
arg_func_langs.arg_to_proto(v, out=msg.internal_tag.tag_args[k])
7472
return msg
7573

7674
@staticmethod
@@ -83,7 +81,7 @@ def from_proto(msg: program_pb2.Tag) -> 'InternalTag':
8381

8482
kw_dict = {}
8583
for k, v in msg.internal_tag.tag_args.items():
86-
kw_dict[k] = arg_func_langs.arg_from_proto(v, arg_function_language='exp')
84+
kw_dict[k] = arg_func_langs.arg_from_proto(v)
8785

8886
return InternalTag(
8987
name=msg.internal_tag.tag_name, package=msg.internal_tag.tag_package, **kw_dict

cirq-google/cirq_google/serialization/arg_func_langs.py

Lines changed: 24 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414
import math
1515
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
1717

1818
import numpy as np
1919
import sympy
@@ -22,15 +22,6 @@
2222
from cirq.qis import CliffordTableau
2323
import tunits
2424

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-
3425
SUPPORTED_SYMPY_OPS = (sympy.Symbol, sympy.Add, sympy.Mul, sympy.Pow)
3526

3627
# Argument types for gates.
@@ -94,10 +85,7 @@ def _function_languages_from_arg(arg_proto: v2.program_pb2.Arg) -> Iterator[str]
9485

9586

9687
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
10189
) -> v2.program_pb2.FloatArg:
10290
"""Writes an argument value into an FloatArg proto.
10391
@@ -108,9 +96,6 @@ def float_arg_to_proto(
10896
Args:
10997
value: The value to encode. This must be a float or compatible
11098
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.
11499
out: The proto to write the result into. Defaults to a new instance.
115100
116101
Returns:
@@ -121,29 +106,22 @@ def float_arg_to_proto(
121106
if isinstance(value, FLOAT_TYPES):
122107
msg.float_value = float(value)
123108
else:
124-
_arg_func_to_proto(value, arg_function_language, msg)
109+
_arg_func_to_proto(value, msg)
125110

126111
return msg
127112

128113

129114
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
134116
) -> v2.program_pb2.Arg:
135117
"""Writes an argument value into an Arg proto.
136118
137119
Args:
138120
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.
142121
out: The proto to write the result into. Defaults to a new instance.
143122
144123
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.
147125
148126
Raises:
149127
ValueError: if the object holds unsupported values.
@@ -188,42 +166,28 @@ def arg_to_proto(
188166
elif isinstance(value, tunits.Value):
189167
msg.arg_value.value_with_unit.MergeFrom(value.to_proto())
190168
else:
191-
_arg_func_to_proto(value, arg_function_language, msg)
169+
_arg_func_to_proto(value, msg)
192170

193171
return msg
194172

195173

196174
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]
200176
) -> 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-
213177
if isinstance(value, sympy.Symbol):
214178
msg.symbol = str(value.free_symbols.pop())
215179
elif isinstance(value, sympy.Add):
216-
msg.func.type = check_support('add')
180+
msg.func.type = 'add'
217181
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())
219183
elif isinstance(value, sympy.Mul):
220-
msg.func.type = check_support('mul')
184+
msg.func.type = 'mul'
221185
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())
223187
elif isinstance(value, sympy.Pow):
224-
msg.func.type = check_support('pow')
188+
msg.func.type = 'pow'
225189
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())
227191
else:
228192
raise ValueError(
229193
f"Unrecognized Sympy expression type: {type(value)}."
@@ -233,10 +197,7 @@ def check_support(func_type: str) -> str:
233197

234198

235199
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
240201
) -> Optional[FLOAT_ARG_LIKE]:
241202
"""Extracts a python value from an argument value proto.
242203
@@ -245,8 +206,6 @@ def float_arg_from_proto(
245206
246207
Args:
247208
arg_proto: The proto containing a serialized value.
248-
arg_function_language: The `arg_function_language` field from
249-
`Program.Language`.
250209
required_arg_name: If set to `None`, the method will return `None` when
251210
given an unset proto value. If set to a string, the method will
252211
instead raise an error complaining that the value is missing in that
@@ -268,11 +227,7 @@ def float_arg_from_proto(
268227
elif which == 'symbol':
269228
return sympy.Symbol(arg_proto.symbol)
270229
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)
276231
if func is None and required_arg_name is not None:
277232
raise ValueError(
278233
f'Arg {arg_proto.func} could not be processed for {required_arg_name}.'
@@ -287,17 +242,12 @@ def float_arg_from_proto(
287242

288243

289244
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
294246
) -> Optional[ARG_RETURN_LIKE]:
295247
"""Extracts a python value from an argument value proto.
296248
297249
Args:
298250
arg_proto: The proto containing a serialized value.
299-
arg_function_language: The `arg_function_language` field from
300-
`Program.Language`.
301251
required_arg_name: If set to `None`, the method will return `None` when
302252
given an unset proto value. If set to a string, the method will
303253
instead raise an error complaining that the value is missing in that
@@ -342,11 +292,7 @@ def arg_from_proto(
342292
return sympy.Symbol(arg_proto.symbol)
343293

344294
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)
350296
if func is not None:
351297
return func
352298

@@ -360,55 +306,22 @@ def arg_from_proto(
360306

361307

362308
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
367310
) -> 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-
)
377311

378312
if func.type == 'add':
379313
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]
388315
)
389316

390317
if func.type == 'mul':
391318
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]
400320
)
401321

402322
if func.type == 'pow':
403323
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]
412325
)
413326
return None
414327

@@ -420,9 +333,6 @@ def internal_gate_arg_to_proto(
420333
421334
Args:
422335
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.
426336
out: The proto to write the result into. Defaults to a new instance.
427337
428338
Returns:
@@ -442,15 +352,11 @@ def internal_gate_arg_to_proto(
442352
return msg
443353

444354

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:
448356
"""Extracts an InternalGate object from an InternalGate proto.
449357
450358
Args:
451359
msg: The proto containing a serialized value.
452-
arg_function_language: The `arg_function_language` field from
453-
`Program.Language`.
454360
455361
Returns:
456362
The deserialized InternalGate object.
@@ -460,7 +366,7 @@ def internal_gate_from_proto(
460366
"""
461367
gate_args = {}
462368
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)
464370
return InternalGate(
465371
gate_name=str(msg.name),
466372
gate_module=str(msg.module),
@@ -476,9 +382,6 @@ def clifford_tableau_arg_to_proto(
476382
"""Writes an CliffordTableau object into an CliffordTableau proto.
477383
Args:
478384
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.
482385
out: The proto to write the result into. Defaults to a new instance.
483386
Returns:
484387
The proto that was written into.
@@ -492,14 +395,10 @@ def clifford_tableau_arg_to_proto(
492395
return msg
493396

494397

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:
498399
"""Extracts a CliffordTableau object from a CliffordTableau proto.
499400
Args:
500401
msg: The proto containing a serialized value.
501-
arg_function_language: The `arg_function_language` field from
502-
`Program.Language`.
503402
Returns:
504403
The deserialized InternalGate object.
505404
"""

0 commit comments

Comments
 (0)