-
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
ObservablesArray docstring improvement #13423
Conversation
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 12646326577Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - 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.
Co-authored-by: Elena Peña Tapia <[email protected]>
Co-authored-by: Elena Peña Tapia <[email protected]>
@@ -98,7 +98,24 @@ def __repr__(self): | |||
return prefix + array + suffix | |||
|
|||
def tolist(self) -> 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.
Oh I just realized on re-review that this return annotation also needs to be updated
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, done in c968189
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.