VQE: Error while compiling Hamiltonian for CH4

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:

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:

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)

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

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!

1 Like

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

By the way, there are several resources of atomic coordinates online. Do you recommend using 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 ).

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 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. This may require creating our own MOL file, but has information for many different basis sets and experimental results.

1 Like