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)