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

NumPyEigensolver does not support all BaseOperator instances. #9091

Closed
declanmillar opened this issue Nov 7, 2022 · 1 comment · Fixed by #9101
Closed

NumPyEigensolver does not support all BaseOperator instances. #9091

declanmillar opened this issue Nov 7, 2022 · 1 comment · Fixed by #9101
Assignees
Labels
bug Something isn't working mod: algorithms Related to the Algorithms module

Comments

@declanmillar
Copy link
Member

Environment

  • Qiskit Terra version: 3ce1737
  • Python version: 3.10.6
  • Operating system: macOS 13.0

What is happening?

NumPyEigensolver and by extension NumPyMinimumEigensolver do not support all BaseOperator instances, in particular SparsePauliOp. This is because it requires the data attribute, which Operator exposes but e.g. SparsePauliOp does not.

How can we reproduce the issue?

from qiskit.algorithms.eigensolvers import NumPyEigensolver
from qiskit.quantum_info import SparsePauliOp

op = SparsePauliOp("Z")
npe = NumPyEigensolver()
result = npe.compute_eigenvalues(op)

Output:

Traceback (most recent call last):
  File "/Users/declanmillar/Projects/qiskit/qiskit-terra/test.py", line 10, in <module>
    result = npe.compute_eigenvalues(op)
  File "/Users/declanmillar/Projects/qiskit/qiskit-terra/qiskit/algorithms/eigensolvers/numpy_eigensolver.py", line 248, in compute_eigenvalues
    self._solve(operator)
  File "/Users/declanmillar/Projects/qiskit/qiskit-terra/qiskit/algorithms/eigensolvers/numpy_eigensolver.py", line 144, in _solve
    if operator.data.all() == operator.data.conj().T.all():
AttributeError: 'SparsePauliOp' object has no attribute 'data'

What should happen?

The code should compute the eigenvalues without error, similarly to:

from qiskit.algorithms.eigensolvers import NumPyEigensolver
from qiskit.quantum_info import SparsePauliOp
from qiskit.opflow import PauliSumOp

op = SparsePauliOp("Z")
op = PauliSumOp(op)
npe = NumPyEigensolver()
result = npe.compute_eigenvalues(op)

or:

from qiskit.algorithms.eigensolvers import NumPyEigensolver
from qiskit.quantum_info import SparsePauliOp, Operator

op = SparsePauliOp("Z")
op = Operator(op.to_matrix())
npe = NumPyEigensolver()
result = npe.compute_eigenvalues(op)

Any suggestions?

Generalize the computation inside NumPyEigensolver to avoid the data attribute, perhaps via to_matrix() or, if possible, a sparse implementation that works for BaseOperator instances.

@declanmillar declanmillar added the bug Something isn't working label Nov 7, 2022
@declanmillar
Copy link
Member Author

I'm happy to work on this issue myself.

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: algorithms Related to the Algorithms module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants