# VQE: Error while compiling Hamiltonian for CH4

**URL:** <https://discuss.pennylane.ai/t/vqe-error-while-compiling-hamiltonian-for-ch4/3443>\
**Category:** PennyLane Help\
**Created:** [September 19, 2023, 7:48am UTC](https://discuss.pennylane.ai/t/vqe-error-while-compiling-hamiltonian-for-ch4/3443 "2023-09-19T07:48:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Den](https://avatars.discourse-cdn.com/v4/letter/d/ce7236/32.png) [@Den](https://discuss.pennylane.ai/u/Den)\
**Post date:** [September 19, 2023, 7:48am UTC](https://discuss.pennylane.ai/t/vqe-error-while-compiling-hamiltonian-for-ch4/3443/1 "2023-09-19T07:48:56Z")

</div>

When I try to calculate the Hamiltonian of H2 and F2, I see no error, but I do when I try to calculate it for CH4. Can you help me?

My code:

```auto
import pennylane as qml
from pennylane import numpy as np
from pennylane.numpy import pi, array, cos, sin, sqrt

def Hsave(mol, fname=''):
    symbols = mol['sym']
    coordinates = mol['coo']
    electrons = mol['Ne']

    H, qubits = qml.qchem.molecular_hamiltonian(symbols, coordinates)
    #print("Number of qubits = ", qubits)
    #print("The Hamiltonian is ", H)

    # Saving Hamiltonian to file. // Skipped for demo
    #f = open(fname, 'w')
    #f.write( str(H) )
    #f.close()
    
    return H, qubits

A2AU = 1.8897259885789 # Conversion factor from Angstrom to Atomic Units

# Data of CH4 molecule with coordinates from molview.org
CH4 = { 'mol': 'CH4', 
         'sym': 'CHHH' , 
         'coo':A2AU*np.array([[0.0, 0.0, 0] , 
                         [0.5541, 0.7996, 0.4965],
                        [0.6833, -0.8134, -0.2536],
                        [-0.7782, -0.3735, 0.6692],
                        [-0.4593 , 0.3874, -0.9121] ]), 
         'Ne': 10 }

mymol = CH4
#path = 'C:\\Users\\MYUSER\\Documents\\'
#Hfname = path + 'Hamiltonian'+ mymol['mol']

# Here is where the error message appears
H, qubits = Hsave(mymol)

```

The error message:

```auto
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in Hsave
  File "C:\Users\MYUSER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pennylane\qchem\openfermion_obs.py", line 955, in molecular_hamiltonian
    if args is None and isinstance(geometry_dhf, qml.numpy.tensor):
UnboundLocalError: local variable 'geometry_dhf' referenced before assignment

```

**Packages versions:**  
Platform info: Windows-10-10.0.22621-SP0  
Python version: 3.9.13  
Numpy version: 1.23.5  
Scipy version: 1.10.1  
Installed devices:

- default.gaussian (PennyLane-0.31.1)
- default.mixed (PennyLane-0.31.1)
- default.qubit (PennyLane-0.31.1)
- default.qubit.autograd (PennyLane-0.31.1)
- default.qubit.jax (PennyLane-0.31.1)
- default.qubit.tf (PennyLane-0.31.1)
- default.qubit.torch (PennyLane-0.31.1)
- default.qutrit (PennyLane-0.31.1)
- null.qubit (PennyLane-0.31.1)
- qiskit.aer (PennyLane-qiskit-0.30.0)
- qiskit.basicaer (PennyLane-qiskit-0.30.0)
- qiskit.ibmq (PennyLane-qiskit-0.30.0)
- qiskit.ibmq.circuit\_runner (PennyLane-qiskit-0.30.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.30.0)
- lightning.qubit (PennyLane-Lightning-0.31.0)

---

<div class="post-metadata">

**Author:** ![Guillermo\_Alonso](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/guillermo_alonso/32/816_2.png) [@Guillermo\_Alonso](https://discuss.pennylane.ai/u/Guillermo_Alonso)\
**Post date:** [September 19, 2023, 12:37pm UTC](https://discuss.pennylane.ai/t/vqe-error-while-compiling-hamiltonian-for-ch4/3443/2 "2023-09-19T12:37:02Z")

</div>

Hi @Den , thanks for the question 🙂  
I would say that you forgot a Hydrogen when defining the symbols.

```python
CH4 = { 'mol': 'CH4', 
         'sym': 'CHHHH' , 
         'coo':A2AU*np.array([[0.0, 0.0, 0] , 
                         [0.5541, 0.7996, 0.4965],
                        [0.6833, -0.8134, -0.2536],
                        [-0.7782, -0.3735, 0.6692],
                        [-0.4593 , 0.3874, -0.9121] ]), 
         'Ne': 10 }

```

with an extra “H” it is working on my side. I hope that helps!

---

<div class="post-metadata">

**Author:** ![Den](https://avatars.discourse-cdn.com/v4/letter/d/ce7236/32.png) [@Den](https://discuss.pennylane.ai/u/Den)\
**Post date:** [September 19, 2023, 5:34pm UTC](https://discuss.pennylane.ai/t/vqe-error-while-compiling-hamiltonian-for-ch4/3443/3 "2023-09-19T17:34:29Z")

</div>

Very true. My bad, my dear.  
Now it’s working.

---

<div class="post-metadata">

**Author:** ![Den](https://avatars.discourse-cdn.com/v4/letter/d/ce7236/32.png) [@Den](https://discuss.pennylane.ai/u/Den)\
**Post date:** [September 19, 2023, 5:50pm UTC](https://discuss.pennylane.ai/t/vqe-error-while-compiling-hamiltonian-for-ch4/3443/4 "2023-09-19T17:50:34Z")

</div>

By the way, there are several resources of atomic coordinates online. Do you recommend using _[molview.org](http://molview.org)_ to generate a MOL file with the coordinates?

I noticed that their coordinates for atoms of H2 are (-0.38, 0.38) A (which is ~( -0.71809, 0.71809) au), which is a little greater than the one used in Pennylane’s demo on VQE with H2 ( (-0.6614, 0.6614) au ).

---

<div class="post-metadata">

**Author:** ![Diego](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/diego/32/1663_2.png) [@Diego](https://discuss.pennylane.ai/u/Diego)\
**Post date:** [September 19, 2023, 6:38pm UTC](https://discuss.pennylane.ai/t/vqe-error-while-compiling-hamiltonian-for-ch4/3443/5 "2023-09-19T18:38:45Z")

</div>

Hi Den,

Variation in the coordinates of the atoms may be due to the use of different basis sets (e.g. STO-3G, 6-31G) in the simulation. Different basis sets have different optimal (lowest energy) positions for the atoms.

[molview.org](http://molview.org) is a quick and easy way to get a MOL file. For more details but more effortful search, we can use the [Computational Chemistry Comparison and Benchmark Database](https://cccbdb.nist.gov/geom1x.asp). This may require creating our own MOL file, but has information for [many different basis sets](https://cccbdb.nist.gov/geom2x.asp) and [experimental results](https://cccbdb.nist.gov/expgeom2x.asp).
