Skip to content

Commit

Permalink
Merge branch 'master' into u/maffoo/black-update
Browse files Browse the repository at this point in the history
  • Loading branch information
maffoo committed Mar 28, 2022
2 parents ab61253 + 9fd5af1 commit 9b46b82
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cirq-core/cirq/protocols/pow_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, overload, TYPE_CHECKING, TypeVar, Union
from typing import Any, cast, overload, TYPE_CHECKING, TypeVar, Union, Callable

if TYPE_CHECKING:
import cirq
Expand Down Expand Up @@ -81,7 +81,7 @@ def pow(val: Any, exponent: Any, default: Any = RaiseTypeErrorIfNotProvided) ->
TypeError: `val` doesn't have a __pow__ method (or that method returned
NotImplemented) and no `default` value was specified.
"""
raiser = getattr(val, '__pow__', None)
raiser = cast(Union[None, Callable], getattr(val, '__pow__', None))
result = NotImplemented if raiser is None else raiser(exponent)
if result is not NotImplemented:
return result
Expand Down
18 changes: 10 additions & 8 deletions cirq-core/cirq/study/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ def dataframe_from_measurements(measurements: Mapping[str, np.ndarray]) -> pd.Da
"""
# Convert to a DataFrame with columns as measurement keys, rows as
# repetitions and a big endian integer for individual measurements.
converted_dict = {
key: [value.big_endian_bits_to_int(m_vals) for m_vals in val]
for key, val in measurements.items()
}
# Note that when a numpy array is produced from this data frame,
# Pandas will try to use np.int64 as dtype, but will upgrade to
# object if any value is too large to fit.
return pd.DataFrame(converted_dict, dtype=np.int64)
converted_dict = {}
for key, bitstrings in measurements.items():
_, n = bitstrings.shape
dtype = object if n > 63 else np.int64
basis = 2 ** np.arange(n, dtype=dtype)[::-1]
converted_dict[key] = np.sum(basis * bitstrings, axis=1)

# Use objects to accomodate more than 64 qubits if needed.
dtype = object if any(bs.shape[1] > 63 for _, bs in measurements.items()) else np.int64
return pd.DataFrame(converted_dict, dtype=dtype)

@staticmethod
@deprecated(
Expand Down
19 changes: 18 additions & 1 deletion cirq-core/cirq/study/result_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ def test_df():
'c': np.array([[0], [0], [1], [0], [1]], dtype=bool),
},
)
remove_end_measurements = pd.DataFrame(data={'ab': [1, 1, 2], 'c': [0, 1, 0]}, index=[1, 2, 3])
remove_end_measurements = pd.DataFrame(
data={'ab': [1, 1, 2], 'c': [0, 1, 0]}, index=[1, 2, 3], dtype=np.int64
)

pd.testing.assert_frame_equal(result.data.iloc[1:-1], remove_end_measurements)

Expand All @@ -166,6 +168,21 @@ def test_df():
assert df.c.value_counts().to_dict() == {0: 3, 1: 2}


def test_df_large():
result = cirq.ResultDict(
params=cirq.ParamResolver({}),
measurements={
'a': np.array([[0 for _ in range(76)]] * 10_000, dtype=bool),
'd': np.array([[1 for _ in range(76)]] * 10_000, dtype=bool),
},
)

assert np.all(result.data['a'] == 0)
assert np.all(result.data['d'] == 0xFFF_FFFFFFFF_FFFFFFFF)
assert result.data['a'].dtype == object
assert result.data['d'].dtype == object


def test_histogram():
result = cirq.ResultDict(
params=cirq.ParamResolver({}),
Expand Down

0 comments on commit 9b46b82

Please sign in to comment.