Skip to content
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

Merged
merged 6 commits into from
Jan 7, 2025

Conversation

gadial
Copy link
Contributor

@gadial gadial commented Nov 12, 2024

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's tolist 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:

>>> from qiskit.primitives.containers.observables_array import ObservablesArray
>>> oa = ObservablesArray.coerce(["Z"])
>>> print(type(oa.tolist()))
<class 'list'>
>>> oa = ObservablesArray.coerce("Z")
>>> print(type(oa.tolist()))
<class 'dict'>

This PR clarifies the issue in the method's docstring.

@gadial gadial requested review from a team as code owners November 12, 2024 09:36
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core
  • @ajavadia
  • @levbishop
  • @t-imamichi

@coveralls
Copy link

coveralls commented Nov 12, 2024

Pull Request Test Coverage Report for Build 12646326577

Warning: 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

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • 814 unchanged lines in 57 files lost coverage.
  • Overall coverage increased (+0.005%) to 88.94%

Files with Coverage Reduction New Missed Lines %
qiskit/circuit/library/quantum_volume.py 1 93.18%
qiskit/primitives/statevector_sampler.py 1 99.07%
qiskit/primitives/base/base_estimator.py 1 96.97%
qiskit/converters/circuit_to_instruction.py 1 98.18%
qiskit/transpiler/passes/routing/commuting_2q_gate_routing/commuting_2q_gate_router.py 1 99.11%
qiskit/circuit/classical/types/ordering.py 1 98.31%
qiskit/pulse/instructions/directives.py 1 97.14%
crates/accelerate/src/unitary_synthesis.rs 1 92.2%
qiskit/pulse/transforms/alignments.py 1 96.27%
crates/accelerate/src/circuit_library/quantum_volume.rs 1 96.67%
Totals Coverage Status
Change from base Build 11784569909: 0.005%
Covered Lines: 79420
Relevant Lines: 89296

💛 - Coveralls

<class 'list'>
>>> oa = ObservablesArray.coerce("Z")
>>> print(type(oa.tolist()))
<class 'dict'>
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@1ucian0 1ucian0 added the documentation Something is not clear or an error documentation label Dec 8, 2024
ihincks
ihincks previously approved these changes Jan 6, 2025
@@ -98,7 +98,24 @@ def __repr__(self):
return prefix + array + suffix

def tolist(self) -> list:
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done in c968189

@gadial gadial requested a review from ihincks January 7, 2025 10:01
@gadial gadial added this pull request to the merge queue Jan 7, 2025
Merged via the queue into Qiskit:main with commit b9cba9f Jan 7, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Something is not clear or an error documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ObservablesArray tolist() doesn't return list
6 participants