Skip to content

Commit 75f0dd3

Browse files
authored
Add bytes to cirq_google proto (#7201)
* Add bytes to cirq_google proto - Adds bytes to the cirq_google Arg proto - Adds hooks in arg_func_langs to serialize and deserialize it. * Changed bytes to b
1 parent 4b87002 commit 75f0dd3

File tree

5 files changed

+67
-57
lines changed

5 files changed

+67
-57
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ message ArgValue {
421421
RepeatedString string_values = 7;
422422
tunits.Value value_with_unit = 8;
423423
bool bool_value = 9;
424+
bytes bytes_value = 10;
424425
}
425426
}
426427

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ def arg_to_proto(
133133
msg.arg_value.bool_value = bool(value)
134134
elif isinstance(value, FLOAT_TYPES):
135135
msg.arg_value.float_value = float(value)
136+
elif isinstance(value, bytes):
137+
msg.arg_value.bytes_value = value
136138
elif isinstance(value, str):
137139
msg.arg_value.string_value = value
138140
elif isinstance(value, (list, tuple, np.ndarray)):
@@ -291,6 +293,8 @@ def arg_from_proto(
291293
return [str(v) for v in arg_value.string_values.values]
292294
if which_val == 'value_with_unit':
293295
return tunits.Value.from_proto(arg_value.value_with_unit)
296+
if which_val == 'bytes_value':
297+
return bytes(arg_value.bytes_value)
294298
raise ValueError(f'Unrecognized value type: {which_val!r}')
295299

296300
if which == 'symbol':

cirq-google/cirq_google/serialization/arg_func_langs_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import base64
1516
import inspect
1617
from typing import Dict
1718

@@ -55,6 +56,7 @@ def _json_format_kwargs() -> Dict[str, bool]:
5556
[
5657
(1.0, {'arg_value': {'float_value': 1.0}}),
5758
(1, {'arg_value': {'float_value': 1.0}}),
59+
(b'abcdef', {'arg_value': {'bytes_value': base64.b64encode(b'abcdef').decode("ascii")}}),
5860
('abc', {'arg_value': {'string_value': 'abc'}}),
5961
(True, {'arg_value': {'bool_value': True}}),
6062
([True, False], {'arg_value': {'bool_values': {'values': [True, False]}}}),

0 commit comments

Comments
 (0)