-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
ObservablesArray docstring improvement #13423
base: main
Are you sure you want to change the base?
ObservablesArray docstring improvement #13423
Conversation
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 11794527861Details
💛 - Coveralls |
<class 'list'> | ||
>>> oa = ObservablesArray.coerce("Z") | ||
>>> print(type(oa.tolist())) | ||
<class 'dict'> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naive question, why is the output type a dict? I would expect a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, an observable can be a weighted sum of Paulis, so it's represented by a dictionary mapping the Pauli (or Pauli string) to its weight. See here.
"""Convert to a nested list""" | ||
"""Convert to a nested list. | ||
|
||
Similar to Numpy's `tolist` method, the level of nesting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sphinx needs the double `` to mark code snippets:
Similar to Numpy's `tolist` method, the level of nesting | |
Similar to Numpy's ``tolist`` method, the level of nesting |
case of dimension 0 the method returns a single observable | ||
instead of a list. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @gadial, in that case, would it incorrect to specify further something like this?
case of dimension 0 the method returns a single observable | |
instead of a list. | |
case of dimension 0 the method returns a single observable | |
(``dict`` in the case of a weighted sum of Paulis) instead of a list. |
Summary
Expands the docstring of
ObservablesArray.tolist
to make it clear it might return a scalar in the case the observables array is of dimension 0.Fixes #13402.
Details and comments
ObservablesArray.tolist
is based on numpy'stolist
method which returns a list with nesting level equal to the dimension of the numpy array being converted. When an observable array is initialized using a single observable (as opposed to a list containing a single observable) the internal representation of the array will be as a dimension 0 numpy array, resulting on a somewhat confusing return value:This PR clarifies the issue in the method's docstring.