Pennylane takes too long to build a Hamiltonian

Hi @Einar_Gabbassov and thanks for using PennyLane to simulate this interesting molecule. The molecule is relatively large and it is more efficient to use the non-differentiable workflow of PennyLane, which uses OpenFermion-PySCF, to construct the molecular Hamiltonian. Please see here and here for more details.

The error you get is because of small imaginary components in the Hamiltonian coefficients. The default threshold for raising this error is defined here. We are working on making this default threshold slightly larger. In the meantime, you can directly construct your Hamiltonian using the following code. Please note that the atomic coordinates should be in atomic units and a conversion factor must be applied to the original values.

import pennylane as qml
from pennylane import numpy as np
import openfermion
from pennylane.qchem.openfermion_obs import meanfield, decompose


symbols = ["C", "C", "F", "F", "F", "H"]
coordinates = np.array(
        [
            0.0, 0.430631, 0.000000,
            -0.704353, -0.686847, 0.0,
            1.314791, 0.500587, 0.0,
            -0.554822, 1.630861, 0.0,
            -0.092517, -1.882156, 0.0,
            -1.780948, -0.706341, 0.0
        ]
    ) / 0.529177210903  # convert to Bohr


hf_file = meanfield(symbols, coordinates)
molecule = openfermion.MolecularData(filename=hf_file)
h_of = decompose(hf_file)
h_pl = qml.qchem.convert.import_operator(h_of, tol=1e09)  # use a larger threshold for the imaginary part

print(h_pl)

Please feel free to let us know if you have any further questions.

1 Like