# Classical algorithm to find the ground state of a Hamiltonian

Hi!

Is there any classical algorithm implemented in PennyLane that allows to find the ground state of a Hamiltonian? I would like to check the results that I am getting with VQE, because I am not really sure they are correct.

Hi @combarro, we have some quantum datasets released with our latest version of PennyLane (v0.27).
You can find them here on our website under the “Quantum Machine Learning” tab. If you go to Chemical Systems you will find the optimal geometry for some molecules, and you can then get the fci_energy or the vqe_energy.

An example code you could use is:

``````import pennylane as qml
H2datasets = qml.data.load("qchem", molname="H2", basis="STO-3G", bondlength=0.742)
print('fci_energy',H2datasets[0].fci_energy)
print('vqe_energy',H2datasets[0].vqe_energy)
``````

Please let me know if this is what you were looking for!

Hi! Thanks, this is useful, but not exactly what I’m looking for.

I have a Hamiltonian (that, incidentally, I obtained from a certain molecule with a certain geometry) and I want to find its ground state with a classical algorithm to check if VQE is indeed finding the ground state. Is it possible to do that with PennyLane?

Hi @combarro, I remember that I did code a classical algorithm in PennyLane for finding a ground state a long time ago, but I can’t find it anymore. So it can be done. The advantage now with the datasets that I shared is that you don’t need to code everything from scratch. You can use the fci_energy for instance, which is computed classically, to compare and determine whether you’re getting the right outcome. All you need to do is choose the molecule, the bondlength and run the code I shared before.

Please let me know if you have any further questions!

Hi!

I tried your code on Google Colab and it gives me the following error:

``````ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-41c3c3e79b6d> in <module>
1 import pennylane as qml
2
----> 3 H2datasets = qml.data.load("qchem", molname="H2", basis="STO-3G", bondlength=0.742)
4
5 print('fci_energy',H2datasets[0].fci_energy)

8 frames
/usr/local/lib/python3.8/dist-packages/dill/_dill.py in find_class(self, module, name)
407             return type(None) #XXX: special case: NoneType missing
408         if module == 'dill.dill': module = 'dill._dill'
--> 409         return StockUnpickler.find_class(self, module, name)
410
411     def __init__(self, *args, **kwds):

ModuleNotFoundError: No module named 'scipy.sparse._csr'

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------'''``````

Hi @combarro ,

This seems like a problem with some of the libraries you have installed.
What is your output for `qml.about()`?

You need PennyLane v0.27 or higher and you need to install the zstd and dill libraries. You can do `!pip install zstd dill`

To upgrade your PennyLane version you can do `!pip install pennylane --upgrade`

It’s possible that the issue is with the Scipy library. I’m using v1.9.3 and it works.

You could also try running your code on the notebooks on Xanadu Cloud.

Please let me know if this helps!

Hi!

It works on Xanadu Cloud (but I had to install PennyLane 0.27.0, because I was getting an error when calling “qml.data”).

Anyway, I am not sure how to use the datasets in qml.data. For H2, it is clear because there is only parameter in the geometry: the distance between the two atoms. But for molecules like H20 I am not sure what the “bondlength” parameter means. I have a geometry like this one:

symbols = [‘H’, ‘O’, ‘H’]
geometry = np.array([-0.0399, -0.0038, 0.0, 1.5780, 0.8540, 0.0, 2.7909, -0.5159, 0.0], requires_grad = False)

How do I translate that to a bond length?

Hi @combarro, yes, this only works on PennyLane versions 0.27 and up.

For H2O it would be similar to H2. You define the bond length between H and O.

Here’s an example with the optimal bond length.

``````import pennylane as qml
H2Odatasets = qml.data.load("qchem", molname="H2O", basis="STO-3G", bondlength=0.958)
print('fci_energy',H2Odatasets[0].fci_energy)
print('vqe_energy',H2Odatasets[0].vqe_energy)
``````

For the coordinates that you have you can calculate the distance from each H to the O. The distance isn’t perfectly symmetrical but it’s about 1.83. This value isn’t available in the datasets but 1.82 is available. You can use this value as an approximation.

If needed you could then modify your coordinates so that the bondlength is 1.82.

[0, 0, 0.0, 0, 1.82, 0.0, np.sin(104.5*np.pi/180)1.82, 1.82-np.cos(104.5np.pi/180)*1.82, 0.0]

I hope this helps you!