Skip to content

Commit 98019fe

Browse files
e-zzShellyGarion
andauthored
fix: Statevector.to_dict(decimals= 3) does not round results and ignores the decimals keyword (Qiskit#12113)
* fix: `SparsePauliOp.to_dict(decimals= 3)` does not return rounded results * test: add `test_to_dict_decimals` * add release note * refactor: release note and test * style(lint): formatted with black --------- Co-authored-by: Shelly Garion <46566946+ShellyGarion@users.noreply.github.com>
1 parent 429a9c0 commit 98019fe

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

qiskit/quantum_info/states/quantum_state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def _vector_to_dict(vec, dims, decimals=None, string_labels=False):
400400

401401
# Make dict of tuples
402402
if string_labels:
403-
return dict(zip(kets, vec[inds]))
403+
return dict(zip(kets, vals[inds]))
404404

405405
return {tuple(ket): val for ket, val in zip(kets, vals[inds])}
406406

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
The method :meth:`.Statevector.to_dict` will now respect its ``decimals`` keyword argument, which it previously ignored.

test/python/quantum_info/states/test_statevector.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ def test_to_dict(self):
486486
target[key] = 2 * i + j + 1
487487
self.assertDictAlmostEqual(target, vec.to_dict())
488488

489+
def test_to_dict_decimals(self):
490+
"""Test to_dict method with decimals argument."""
491+
decimal = 3
492+
sv = np.array([1 / np.sqrt(2), 0, 0, -1 / np.sqrt(2)], dtype=np.complex128)
493+
vec_rounded = Statevector(sv).to_dict(decimals=decimal)
494+
expected = {"00": 0.707, "11": -0.707}
495+
self.assertEqual(vec_rounded, expected)
496+
489497
def test_probabilities_product(self):
490498
"""Test probabilities method for product state"""
491499

0 commit comments

Comments
 (0)