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 tolist() doesn't return list #13402

Open
jyu00 opened this issue Nov 6, 2024 · 2 comments · May be fixed by #13423
Open

ObservablesArray tolist() doesn't return list #13402

jyu00 opened this issue Nov 6, 2024 · 2 comments · May be fixed by #13423
Labels
bug Something isn't working mod: primitives Related to the Primitives module

Comments

@jyu00
Copy link
Contributor

jyu00 commented Nov 6, 2024

Environment

  • Qiskit version: 1.2.4
  • Python version: 3.11
  • Operating system:

What is happening?

The tolist() method in ObservablesArray returns a dictionary instead of a list when there is only 1 observable.

How can we reproduce the issue?

from qiskit.primitives.containers.observables_array import ObservablesArray

oa = ObservablesArray.coerce("Z")
print(type(oa.tolist()))

What should happen?

Both the method name and type hint suggests it should return a list

Any suggestions?

No response

@jyu00 jyu00 added bug Something isn't working mod: primitives Related to the Primitives module labels Nov 6, 2024
@gadial
Copy link
Contributor

gadial commented Nov 7, 2024

It's not clear whether this is indeed a bug or the expected behavior, since tolist is a numpy method which generates a nested list with ndim levels.

In the example above, the observable array is of dimension 0, so returning a scalar is actually expected. Compare with

from qiskit.primitives.containers.observables_array import ObservablesArray

oa = ObservablesArray.coerce("Z")
print(type(oa.tolist()))

oa = ObservablesArray.coerce(["Z"])
print(type(oa.tolist()))

Where the second print is indeed a list.

So we have two possible choices:

  1. Keep the current behavior as it corresponds to how in general tolist behaves.
  2. Do the one-line fix such that both the examples above return a one-dimensional list.

I tend to view 1 as the better possibility, to keep our usage of tolist as standard as possible; what is your opinion? I guess this issue arose from some specific problem in primitives/runtime?

@jyu00
Copy link
Contributor Author

jyu00 commented Nov 7, 2024

This was prompted by some partner trying to implement an Estimator-like interface.

I'm not against option 1, but if that's the approach, the type hint should be list | dict, and docstring should mention it could return a scalar (dictionary) if the array is 0d (similar to the docstring for numpy.tolist)

@gadial gadial linked a pull request Nov 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: primitives Related to the Primitives module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants