Skip to content

Commit

Permalink
Corrected result.data implementation. (quantumlib#5153)
Browse files Browse the repository at this point in the history
When looking into quantumlib#4774 auto type promotion to objects raised a ValueError and was not tested. This PR fixes this and introduces the intended behavior as well as boosting performance slightly when qubit counts are < 64.
  • Loading branch information
MichaelBroughton authored Mar 28, 2022
1 parent 2530747 commit 4956eae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
18 changes: 10 additions & 8 deletions 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/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 4956eae

Please sign in to comment.