qiskit-nature icon indicating copy to clipboard operation
qiskit-nature copied to clipboard

Error when using NumPyMinimumEigensolver with an ElectronicStructureProblem

Open EliasCombarro opened this issue 2 years ago • 6 comments

I am trying to use NumPyMinimumEigensolver to solve an ElectronicStructureProblem, but I obtain an error.

My code is the following:

from qiskit_nature.drivers import Molecule from qiskit_nature.drivers.second_quantization import ElectronicStructureMoleculeDriver, ElectronicStructureDriverType from qiskit_nature.problems.second_quantization import ElectronicStructureProblem from qiskit_nature.converters.second_quantization import QubitConverter from qiskit_nature.mappers.second_quantization import JordanWignerMapper from qiskit_nature.algorithms import GroundStateEigensolver from qiskit.algorithms.minimum_eigensolvers import NumPyMinimumEigensolver

qconverter = QubitConverter(JordanWignerMapper())

npe = NumPyMinimumEigensolver() solver = GroundStateEigensolver(qconverter, npe)

mol = Molecule(geometry=[['H', [0., 0., -0.37]], ['H', [0., 0., 0.37]]])

driver = ElectronicStructureMoleculeDriver(mol, basis='sto3g', driver_type=ElectronicStructureDriverType.PYSCF) problem = ElectronicStructureProblem(driver) result = solver.solve(problem) print(result)

The error that I get is the following:


TypeError Traceback (most recent call last) in 18 driver_type=ElectronicStructureDriverType.PYSCF) 19 problem = ElectronicStructureProblem(driver) ---> 20 result = solver.solve(problem) 21 print(result)

2 frames /usr/local/lib/python3.7/dist-packages/qiskit_nature/results/eigenstate_result.py in combine(self, result) 164 """ 165 if result is None: --> 166 raise TypeError("Argument result expected.") 167 if result == self: 168 return

TypeError: Argument result expected.


Similar code works when using VQE or VQEUCCFactory as the solvers.

Thank you in advance

EliasCombarro avatar Oct 16 '22 18:10 EliasCombarro

The legacy code of Qiskit Nature which is not under qiskit_nature.second_q isnot guaranteed to work with the rewritten algorithms which were released as part of Qiskit Terra 0.22

Support for the primitive-based algorithms was added to the refactored Qiskit Nature stack under qiskit_nature.second_q in #849 and will be released as part of 0.5 which is planned for early November.

mrossinek avatar Oct 16 '22 18:10 mrossinek

Thank you very much for the quick answer. I am not sure what part of my code is legacy. Could you please help me with this? What are the correct imports and methods that I should be using?

Thanks so much in advance.

EliasCombarro avatar Oct 16 '22 19:10 EliasCombarro

If you have an installation of Qiskit Nature from source, you need to switch all Nature related imports to the qiskit_nature.second_q module. See also #814. This is still under heavy development and does not yet have finished documentation. A migration guide is also still in the works.

I suggest that instead of that you continue to use the NumPyMinimumEigensolver from qiskit.algorithms.minimum_eigen_solvers (note that extra _).

mrossinek avatar Oct 16 '22 19:10 mrossinek

I see, thanks so much. May I ask if the changes are only for the locations of the imports? Or will the profile of the methods also change? I am writing a book using Qiskit code and I would not like the code in the book to be badly out of date.

Thanks in advance

EliasCombarro avatar Oct 16 '22 19:10 EliasCombarro

There will also be functional changes. Especially everything around drivers, operators, properties, problems and transformers will change a LOT. The algorithms will change slightly to be adapted to the primitive-based algorithms as released in Qiskit Terra 0.22. The circuit module has gotten some minor API changes.

mrossinek avatar Oct 16 '22 19:10 mrossinek

Well, that's too bad for me :/ Are there also changes coming to other modules, such as qiskit optimization or qiskit machine learning?

EliasCombarro avatar Oct 16 '22 19:10 EliasCombarro

Changes to ML and Opt at this time are around support for the primitives. ML has some new Neural Networks and Quantum Kernel logic done now using primitives. Algos are being updated too as needed. Work in Opt has yet to start. You can look at the 0.5.0 milestones in the respective repos to see the issues/PRs going into the next releases.

woodsp-ibm avatar Oct 17 '22 21:10 woodsp-ibm

Thanks so much for the info!

EliasCombarro avatar Oct 17 '22 21:10 EliasCombarro