Skip to content

Commit

Permalink
ensure round still works
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryoris committed Mar 10, 2023
1 parent b22df7c commit 6b3c30a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
6 changes: 4 additions & 2 deletions qiskit/quantum_info/states/densitymatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,13 @@ def probabilities(self, qargs=None, decimals=None):
probs = self._subsystem_probabilities(
np.abs(self.data.diagonal()), self._op_shape.dims_l(), qargs=qargs
)
if decimals is not None:
probs = probs.round(decimals=decimals)

# to account for roundoff errors, we renormalize and clip
probs = np.clip(probs / np.sum(probs), a_min=0, a_max=1)

if decimals is not None:
probs = probs.round(decimals=decimals)

return probs

def reset(self, qargs=None):
Expand Down
6 changes: 4 additions & 2 deletions qiskit/quantum_info/states/statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,13 @@ def probabilities(self, qargs=None, decimals=None):
probs = self._subsystem_probabilities(
np.abs(self.data) ** 2, self._op_shape.dims_l(), qargs=qargs
)
if decimals is not None:
probs = probs.round(decimals=decimals)

# to account for roundoff errors, we renormalize and clip
probs = np.clip(probs / np.sum(probs), a_min=0, a_max=1)

if decimals is not None:
probs = probs.round(decimals=decimals)

return probs

def reset(self, qargs=None):
Expand Down
12 changes: 12 additions & 0 deletions test/python/quantum_info/states/test_densitymatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,18 @@ def test_clip_probabilities(self):
self.assertTrue(np.allclose(dm.probabilities(), [1, 0], atol=0))
self.assertDictAlmostEqual(dm.probabilities_dict(), {"0": 1, "1": 0}, delta=0)

def test_round_probabilities(self):
"""Test probabilities are correctly rounded.
This is good to test to ensure clipping, renormalizing and rounding work together.
"""
p = np.sqrt(1 / 3)
amplitudes = [p, p, p, 0]
dm = DensityMatrix(np.outer(amplitudes, amplitudes))
expected = [0.33, 0.33, 0.33, 0]

self.assertTrue(np.allclose(dm.probabilities(decimals=2), expected))


if __name__ == "__main__":
unittest.main()
11 changes: 11 additions & 0 deletions test/python/quantum_info/states/test_statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,17 @@ def test_clip_probabilities(self):
self.assertTrue(np.allclose(sv.probabilities(), [1, 0], atol=0))
self.assertDictAlmostEqual(sv.probabilities_dict(), {"0": 1, "1": 0}, delta=0)

def test_round_probabilities(self):
"""Test probabilities are correctly rounded.
This is good to test to ensure clipping, renormalizing and rounding work together.
"""
p = np.sqrt(1 / 3)
sv = Statevector([p, p, p, 0])
expected = [0.33, 0.33, 0.33, 0]

self.assertTrue(np.allclose(sv.probabilities(decimals=2), expected))


if __name__ == "__main__":
unittest.main()

0 comments on commit 6b3c30a

Please sign in to comment.