qiskit-community / qiskit-nature

Qiskit Nature is an open-source, quantum computing, framework for solving quantum mechanical natural science problems.
https://qiskit-community.github.io/qiskit-nature/
Apache License 2.0
301 stars 206 forks source link

Cannot use .interpret method with MinimumEigensolverResult #1067

Closed frankharkins closed 1 year ago

frankharkins commented 1 year ago

Environment

What is happening?

From the docs, it seems

ElectronicStructureProblem.interpret

Should accept a Qiskit MinimumEigensolverResult, but this is not the case.

How can we reproduce the issue?

Here's a minimal code example that shows the issue in context

from qiskit_nature.second_q.mappers import QubitConverter, ParityMapper
qubit_converter = QubitConverter(ParityMapper())

from qiskit_nature.second_q.drivers import PySCFDriver
driver = PySCFDriver(
    atom="H 0 0 0; H 0 0 0.72"
)
problem = driver.run()
hamiltonian = qubit_converter.convert(problem.second_q_ops()[0])

from qiskit.algorithms import NumPyMinimumEigensolver
sol = NumPyMinimumEigensolver().compute_minimum_eigenvalue(hamiltonian)
real_solution = problem.interpret(sol)

Output:

TypeError: Cannot construct an EigenstateResult from a result of type, <class 'qiskit.algorithms.minimum_eigen_solvers.minimum_eigen_solver.MinimumEigensolverResult'>.

What should happen?

I expect the code to run without error.

Any suggestions?

I'm a bit confused, but it seems there are two MinimumEigensolverResults. It looks like the class accepted in the code is

qiskit.algorithms.minimum_eigensolvers.minimum_eigensolver.MinimumEigensolverResult

whereas driver.run() NumPyMinimumEigensolver.compute_minimum_eigenvalue returns

qiskit.algorithms.minimum_eigen_solvers.minimum_eigen_solver.MinimumEigensolverResult
woodsp-ibm commented 1 year ago

The (minimum_)eigen_solvers in Terra were refactored to support primitives and are in (minimum_)eigensolvers folders (no underscore between eigen and solvers). The pre-existing algorithms are still there, currently pending_deprecation but are planned to be deprecated soon. Nature 0.5.0 in the refactored second_q code supports only the new primitive based algorithms. The new folder has the new algorithms and the new results. There is a Numpy classical solver there too compatible with this. So the import, for a compatible solver, should be

from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver

where they need to be imported from the new folder. (The import from qiskit.algorithms are the pre-existing algorithms - this has to stay that way because of compatibility).

from qiskit_nature.second_q.mappers import QubitConverter, ParityMapper
qubit_converter = QubitConverter(ParityMapper())

from qiskit_nature.second_q.drivers import PySCFDriver
driver = PySCFDriver(
    atom="H 0 0 0; H 0 0 0.72"
)
problem = driver.run()
hamiltonian = qubit_converter.convert(problem.second_q_ops()[0])

from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver
sol = NumPyMinimumEigensolver().compute_minimum_eigenvalue(hamiltonian)
real_solution = problem.interpret(sol)
frankharkins commented 1 year ago

@woodsp-ibm ok that makes sense. I've got it working now, thank you!