Skip to content

Commit 9b46b82

Browse files
committed
Merge branch 'master' into u/maffoo/black-update
2 parents ab61253 + 9fd5af1 commit 9b46b82

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

cirq-core/cirq/protocols/pow_protocol.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Any, overload, TYPE_CHECKING, TypeVar, Union
15+
from typing import Any, cast, overload, TYPE_CHECKING, TypeVar, Union, Callable
1616

1717
if TYPE_CHECKING:
1818
import cirq
@@ -81,7 +81,7 @@ def pow(val: Any, exponent: Any, default: Any = RaiseTypeErrorIfNotProvided) ->
8181
TypeError: `val` doesn't have a __pow__ method (or that method returned
8282
NotImplemented) and no `default` value was specified.
8383
"""
84-
raiser = getattr(val, '__pow__', None)
84+
raiser = cast(Union[None, Callable], getattr(val, '__pow__', None))
8585
result = NotImplemented if raiser is None else raiser(exponent)
8686
if result is not NotImplemented:
8787
return result

cirq-core/cirq/study/result.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,16 @@ def dataframe_from_measurements(measurements: Mapping[str, np.ndarray]) -> pd.Da
142142
"""
143143
# Convert to a DataFrame with columns as measurement keys, rows as
144144
# repetitions and a big endian integer for individual measurements.
145-
converted_dict = {
146-
key: [value.big_endian_bits_to_int(m_vals) for m_vals in val]
147-
for key, val in measurements.items()
148-
}
149-
# Note that when a numpy array is produced from this data frame,
150-
# Pandas will try to use np.int64 as dtype, but will upgrade to
151-
# object if any value is too large to fit.
152-
return pd.DataFrame(converted_dict, dtype=np.int64)
145+
converted_dict = {}
146+
for key, bitstrings in measurements.items():
147+
_, n = bitstrings.shape
148+
dtype = object if n > 63 else np.int64
149+
basis = 2 ** np.arange(n, dtype=dtype)[::-1]
150+
converted_dict[key] = np.sum(basis * bitstrings, axis=1)
151+
152+
# Use objects to accomodate more than 64 qubits if needed.
153+
dtype = object if any(bs.shape[1] > 63 for _, bs in measurements.items()) else np.int64
154+
return pd.DataFrame(converted_dict, dtype=dtype)
153155

154156
@staticmethod
155157
@deprecated(

cirq-core/cirq/study/result_test.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def test_df():
156156
'c': np.array([[0], [0], [1], [0], [1]], dtype=bool),
157157
},
158158
)
159-
remove_end_measurements = pd.DataFrame(data={'ab': [1, 1, 2], 'c': [0, 1, 0]}, index=[1, 2, 3])
159+
remove_end_measurements = pd.DataFrame(
160+
data={'ab': [1, 1, 2], 'c': [0, 1, 0]}, index=[1, 2, 3], dtype=np.int64
161+
)
160162

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

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

168170

171+
def test_df_large():
172+
result = cirq.ResultDict(
173+
params=cirq.ParamResolver({}),
174+
measurements={
175+
'a': np.array([[0 for _ in range(76)]] * 10_000, dtype=bool),
176+
'd': np.array([[1 for _ in range(76)]] * 10_000, dtype=bool),
177+
},
178+
)
179+
180+
assert np.all(result.data['a'] == 0)
181+
assert np.all(result.data['d'] == 0xFFF_FFFFFFFF_FFFFFFFF)
182+
assert result.data['a'].dtype == object
183+
assert result.data['d'].dtype == object
184+
185+
169186
def test_histogram():
170187
result = cirq.ResultDict(
171188
params=cirq.ParamResolver({}),

0 commit comments

Comments
 (0)