Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cirq-google/cirq_google/api/v2/sweeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

import numbers
from typing import Any, Callable, cast, TYPE_CHECKING

import sympy
Expand All @@ -31,10 +32,10 @@ def _build_sweep_const(value: Any) -> run_context_pb2.ConstValue:
"""Build the sweep const message from a value."""
if value is None:
return run_context_pb2.ConstValue(is_none=True)
elif isinstance(value, float):
return run_context_pb2.ConstValue(float_value=value)
elif isinstance(value, int):
return run_context_pb2.ConstValue(int_value=value)
elif isinstance(value, numbers.Integral):
return run_context_pb2.ConstValue(int_value=int(value))
elif isinstance(value, numbers.Real):
return run_context_pb2.ConstValue(float_value=float(value))
elif isinstance(value, str):
return run_context_pb2.ConstValue(string_value=value)
elif isinstance(value, tunits.Value):
Expand Down
7 changes: 7 additions & 0 deletions cirq-google/cirq_google/api/v2/sweeps_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from copy import deepcopy
from typing import Iterator

import numpy as np
import pytest
import sympy
import tunits
Expand Down Expand Up @@ -418,3 +419,9 @@ def test_tunits_round_trip(sweep):
msg = v2.sweep_to_proto(sweep)
recovered = v2.sweep_from_proto(msg)
assert sweep == recovered


@pytest.mark.parametrize('value', [np.float32(3.14), np.int64(5)])
def test_const_sweep_with_numpy_types_roundtrip(value):
sweep = cirq.Points('const', [value])
assert v2.sweep_from_proto(v2.sweep_to_proto(sweep)) == sweep