Skip to content

Commit

Permalink
reduce decimal for probabilities_dict in Sampler (Qiskit#10596)
Browse files Browse the repository at this point in the history
* reduce decimal for probabilities_dict in Sampler

* adjust qaoa tests

* adjust qaoa tests

* reno

* Rolling everything back and just hook __repr__

Co-authored-by: Julien Gacon <[email protected]>

* remove reno

* QuasiDistribution.__repr__

---------

Co-authored-by: Julien Gacon <[email protected]>
Co-authored-by: Takashi Imamichi <[email protected]>
  • Loading branch information
3 people authored and rupeshknn committed Oct 9, 2023
1 parent 2ad7693 commit c19fce8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/quantum_info/operators/symplectic/pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class initialization (``Pauli('-iXYZ')``). A ``Pauli`` object can be
returned string for large numbers of qubits while :meth:`to_label`
will return the full string with no truncation. The default
truncation length is 50 characters. The default value can be
changed by setting the class `__truncate__` attribute to an integer
changed by setting the class ``__truncate__`` attribute to an integer
value. If set to ``0`` no truncation will be performed.
**Array Representation**
Expand Down
11 changes: 11 additions & 0 deletions qiskit/result/distributions/quasi.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ class QuasiDistribution(dict):
"""A dict-like class for representing quasi-probabilities."""

_bitstring_regex = re.compile(r"^[01]+$")
__ndigits__ = 15

def __init__(self, data, shots=None, stddev_upper_bound=None):
"""Builds a quasiprobability distribution object.
.. note::
The quasiprobability values might include floating-point errors.
``QuasiDistribution.__repr__`` rounds using :meth:`numpy.round`
and the parameter ``ndigits`` can be manipulated with the
class attribute ``__ndigits__``. The default is ``15``.
Parameters:
data (dict): Input quasiprobability data. Where the keys
represent a measured classical value and the value is a
Expand Down Expand Up @@ -141,3 +149,6 @@ def hex_probabilities(self):
def stddev_upper_bound(self):
"""Return an upper bound on standard deviation of expval estimator."""
return self._stddev_upper_bound

def __repr__(self):
return str({key: round(value, ndigits=self.__ndigits__) for key, value in self.items()})
7 changes: 7 additions & 0 deletions releasenotes/notes/decimals_for_sampler-5aaa56642cce23e0.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
upgrade:
- |
The :class:`~.QuasiDistribution` values might include floating-point errors.
``QuasiDistribution.__repr__`` rounds using :meth:`numpy.round`
and the parameter ``ndigits`` can be manipulated with the
class attribute ``__ndigits__``. The default is ``15``.

0 comments on commit c19fce8

Please sign in to comment.