Skip to content

Commit eddb281

Browse files
authored
Add boolean support to cirq_google protos (#7177)
* Add boolean support to cirq_google protos * Add np.bool. * Fix type issue.
1 parent ea1679d commit eddb281

5 files changed

Lines changed: 67 additions & 58 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ message ArgValue {
420420
RepeatedDouble double_values = 6;
421421
RepeatedString string_values = 7;
422422
tunits.Value value_with_unit = 8;
423+
bool bool_value = 9;
423424
}
424425
}
425426

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

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

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

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

cirq-google/cirq_google/serialization/arg_func_langs.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ def arg_to_proto(
128128
"""
129129
msg = v2.program_pb2.Arg() if out is None else out
130130

131-
if isinstance(value, FLOAT_TYPES):
131+
if isinstance(value, (bool, np.bool_)):
132+
msg.arg_value.bool_value = bool(value)
133+
elif isinstance(value, FLOAT_TYPES):
132134
msg.arg_value.float_value = float(value)
133135
elif isinstance(value, str):
134136
msg.arg_value.string_value = value
@@ -274,6 +276,8 @@ def arg_from_proto(
274276
if math.ceil(result) == math.floor(result):
275277
result = int(result)
276278
return result
279+
if which_val == 'bool_value':
280+
return bool(arg_value.bool_value)
277281
if which_val == 'bool_values':
278282
return list(arg_value.bool_values.values)
279283
if which_val == 'string_value':

cirq-google/cirq_google/serialization/arg_func_langs_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def _json_format_kwargs() -> Dict[str, bool]:
5757
(1.0, {'arg_value': {'float_value': 1.0}}),
5858
(1, {'arg_value': {'float_value': 1.0}}),
5959
('abc', {'arg_value': {'string_value': 'abc'}}),
60+
(True, {'arg_value': {'bool_value': True}}),
6061
([True, False], {'arg_value': {'bool_values': {'values': [True, False]}}}),
6162
([42.9, 3.14], {'arg_value': {'double_values': {'values': [42.9, 3.14]}}}),
6263
([3, 8], {'arg_value': {'int64_values': {'values': ['3', '8']}}}),

0 commit comments

Comments
 (0)