Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected result.data implementation. #5153

Merged
merged 9 commits into from
Mar 28, 2022
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