Making Molecule Files for HFVQE

In this tutorial we describe the process of making the molecular data files necessary to run the HFVQE code. We focus on how to use the OpenFermion plugin modules to generate molecular files with canonical Hartree-Fock and generate integrals in a given atomic orbital basis set. We also provide helper functions to run variational Hartree-Fock simulating the experiment and generating initial parameters.

This tutorial will follow the code in recirq/hfvqe/molecular_data/ for constructing MolecularData objects and getting atomic orbital integrals.

In addition to the standard requirement of ReCirq and its dependencies, this notebook uses OpenFermion-pyscf (and pyscf) to compute some quantities. We install it below if you don't already have it.

View on QuantumAI Run in Google Colab View source on GitHub Download notebook

Setup

try:
    import recirq
except ImportError:
    !pip install --quiet git+https://github.com/quantumlib/ReCirq

try:
    import openfermionpyscf
except ImportError:
    !pip install --quiet openfermionpyscf~=0.5.0

Now we can import the packages required for this notebook.

import os
import numpy as np
import scipy

from recirq.hfvqe.molecular_data.molecular_data_construction import (
    h6_linear_molecule, h8_linear_molecule, 
    h10_linear_molecule, h12_linear_molecule, 
    get_ao_integrals)
from recirq.hfvqe.gradient_hf import rhf_minimization, rhf_func_generator
from recirq.hfvqe.objective import RestrictedHartreeFockObjective, generate_hamiltonian
def make_rhf_objective(molecule):
    S, Hcore, TEI = get_ao_integrals(molecule)
    _, X = scipy.linalg.eigh(Hcore, S)

    molecular_hamiltonian = generate_hamiltonian(
        general_basis_change(Hcore, X, (1, 0)),
        numpy.einsum('psqr', general_basis_change(TEI, X, (1, 0, 1, 0)),
                     molecule.nuclear_repulsion))

    rhf_objective = RestrictedHartreeFockObjective(molecular_hamiltonian,
                                                   molecule.n_electrons)
    return rhf_objective, S, Hcore, TEI

Hydrogen Chain MolecularData

We provide helper functions in the hfvqe module to generate the Hydrogen chain data. Each chain is constructed using OpenFermion and Psi4 via the OpenFermion-Psi4 plugin. We will use creating H6 with equal spacing between Hydrogen atoms as an example.

from openfermion import MolecularData, general_basis_change
from openfermionpyscf import run_pyscf
n_hydrogens = 6
bond_distance = 1.3 # in Angstroms
molecule = MolecularData(
    geometry=[('H', (0, 0, i * bond_distance)) for i in range(n_hydrogens)],
    charge=0,
    basis='6-31g',
    multiplicity=1,
    description=f"linear_r-{bond_distance}")

The previous lines set up the MolecularData file. We can now use pyscf to either run a full self-consistent-field Hartree-Fock calculation or get atomic integrals. Via Openfermion-Pyscf we provide an interface to running Hartree-Fock, coupled-cluster, second order perturbation theory, configuration-interaction singles-doubles, and full configuration interaction. Many of these methods depend on parameters such as convergence criteria or initial vectors in the subspace expansion. run_pyscf assumes common defaults which are appropriate for most systems. Below we will run default Hartree-Fock and CISD.

molecule = run_pyscf(molecule, run_scf=True, run_cisd=True)
print("Hartree-Fock energy:", molecule.hf_energy, 
      "\nCISD energy:", molecule.cisd_energy)
Hartree-Fock energy: -3.1025910099104914 
CISD energy: -3.2221034638297112

The MolecularData file holds almost all information that is required for post-Hartree-Fock correlated calculations. For example, we provide access to integrals as attributes of MolecularData.

print("Overlap Ints")
print(molecule.overlap_integrals)

print()
print("One-electron integrals")
print(molecule.one_body_integrals)
Overlap Ints
[[1.00000000e+00 6.58292049e-01 1.07450262e-01 2.97950190e-01
  2.92936786e-04 2.79155456e-02 1.86658314e-08 5.52805483e-04
  2.50532789e-14 2.34306448e-06 7.06186034e-22 2.12361769e-09]
 [6.58292049e-01 1.00000000e+00 2.97950190e-01 6.14673658e-01
  2.79155456e-02 1.42750753e-01 5.52805483e-04 1.25256830e-02
  2.34306448e-06 4.15253811e-04 2.12361769e-09 5.20133758e-06]
 [1.07450262e-01 2.97950190e-01 1.00000000e+00 6.58292049e-01
  1.07450262e-01 2.97950190e-01 2.92936786e-04 2.79155456e-02
  1.86658314e-08 5.52805483e-04 2.50532789e-14 2.34306448e-06]
 [2.97950190e-01 6.14673658e-01 6.58292049e-01 1.00000000e+00
  2.97950190e-01 6.14673658e-01 2.79155456e-02 1.42750753e-01
  5.52805483e-04 1.25256830e-02 2.34306448e-06 4.15253811e-04]
 [2.92936786e-04 2.79155456e-02 1.07450262e-01 2.97950190e-01
  1.00000000e+00 6.58292049e-01 1.07450262e-01 2.97950190e-01
  2.92936786e-04 2.79155456e-02 1.86658314e-08 5.52805483e-04]
 [2.79155456e-02 1.42750753e-01 2.97950190e-01 6.14673658e-01
  6.58292049e-01 1.00000000e+00 2.97950190e-01 6.14673658e-01
  2.79155456e-02 1.42750753e-01 5.52805483e-04 1.25256830e-02]
 [1.86658314e-08 5.52805483e-04 2.92936786e-04 2.79155456e-02
  1.07450262e-01 2.97950190e-01 1.00000000e+00 6.58292049e-01
  1.07450262e-01 2.97950190e-01 2.92936786e-04 2.79155456e-02]
 [5.52805483e-04 1.25256830e-02 2.79155456e-02 1.42750753e-01
  2.97950190e-01 6.14673658e-01 6.58292049e-01 1.00000000e+00
  2.97950190e-01 6.14673658e-01 2.79155456e-02 1.42750753e-01]
 [2.50532789e-14 2.34306448e-06 1.86658314e-08 5.52805483e-04
  2.92936786e-04 2.79155456e-02 1.07450262e-01 2.97950190e-01
  1.00000000e+00 6.58292049e-01 1.07450262e-01 2.97950190e-01]
 [2.34306448e-06 4.15253811e-04 5.52805483e-04 1.25256830e-02
  2.79155456e-02 1.42750753e-01 2.97950190e-01 6.14673658e-01
  6.58292049e-01 1.00000000e+00 2.97950190e-01 6.14673658e-01]
 [7.06186034e-22 2.12361769e-09 2.50532789e-14 2.34306448e-06
  1.86658314e-08 5.52805483e-04 2.92936786e-04 2.79155456e-02
  1.07450262e-01 2.97950190e-01 1.00000000e+00 6.58292049e-01]
 [2.12361769e-09 5.20133758e-06 2.34306448e-06 4.15253811e-04
  5.52805483e-04 1.25256830e-02 2.79155456e-02 1.42750753e-01
  2.97950190e-01 6.14673658e-01 6.58292049e-01 1.00000000e+00]]

One-electron integrals
[[-1.87432473e+00 -7.24478626e-16 -1.16409397e-01 -4.45583605e-16
  -6.15079850e-02  4.11069749e-16 -1.25731117e-01  4.10276506e-16
  -5.89300819e-02  3.83527060e-16  2.71519013e-03  6.83767065e-16]
 [-6.15250874e-16 -1.69304317e+00 -5.80535128e-16  1.65551634e-01
   1.51196459e-16 -4.41325654e-02 -1.43729952e-15  1.00494695e-01
   2.35340695e-15 -8.00967125e-02  9.74768549e-16 -6.99099285e-03]
 [-1.16409397e-01 -3.78979857e-16 -1.61481611e+00 -3.82350886e-16
   1.42975424e-01  5.98664665e-16  1.99561985e-02  3.89925228e-16
  -9.93297282e-02 -3.26463066e-15 -8.38903468e-02  3.93227776e-15]
 [-3.97506447e-16  1.65551634e-01 -4.46012495e-16 -1.47182278e+00
   3.58593611e-16 -1.55554532e-01  1.17871923e-16  6.81485313e-02
  -1.75697305e-15  1.47405321e-01 -7.00716639e-15 -4.83500512e-02]
 [-6.15079850e-02 -1.35790980e-16  1.42975424e-01  3.27091786e-16
  -1.29158630e+00 -2.09347898e-15  5.71950545e-02  4.27751144e-15
  -1.32456629e-01  9.05861678e-16  1.56350374e-01 -6.06366584e-15]
 [ 5.35967863e-16 -4.41325654e-02  5.62231733e-16 -1.55554532e-01
  -2.10850033e-15 -1.24806929e+00 -3.00067699e-15 -1.25035349e-01
  -2.96347997e-16 -1.02022892e-01 -9.72872290e-15 -2.06045664e-01]
 [-1.25731117e-01 -1.52315598e-15  1.99561985e-02  1.47676484e-16
   5.71950545e-02 -3.09349448e-15 -5.80093413e-01  2.71396532e-15
  -1.60790101e-01 -3.34670386e-15 -2.49740558e-02  2.53648386e-15]
 [ 2.73603518e-16  1.00494695e-01  6.01437149e-16  6.81485313e-02
   4.33932659e-15 -1.25035349e-01  2.77123805e-15 -4.13964912e-01
  -1.55022004e-15  9.28607464e-02 -5.47802989e-15 -4.59666892e-02]
 [-5.89300819e-02  2.75459657e-15 -9.93297282e-02 -1.86321159e-15
  -1.32456629e-01 -3.86401336e-16 -1.60790101e-01 -1.55496725e-15
  -4.68339016e-01  3.81499500e-15 -3.67277351e-02  1.46412987e-15]
 [ 2.30456780e-16 -8.00967125e-02 -3.50391740e-15  1.47405321e-01
   1.01572494e-15 -1.02022892e-01 -3.34377041e-15  9.28607464e-02
   3.99095816e-15 -5.21989682e-01 -1.36488187e-16  3.96032256e-02]
 [ 2.71519013e-03  5.54156150e-16 -8.38903468e-02 -6.82460330e-15
   1.56350374e-01 -9.41316135e-15 -2.49740558e-02 -5.60841547e-15
  -3.67277351e-02 -4.39354242e-16 -5.04870294e-01  6.09078740e-16]
 [-1.57776255e-17 -6.99099285e-03  4.07823164e-15 -4.83500512e-02
  -6.38640187e-15 -2.06045664e-01  2.64377639e-15 -4.59666892e-02
   1.71310164e-15  3.96032256e-02  1.02256902e-15 -4.15306261e-01]]

For the Hartree-Fock experiment we will need to get the atomic basis integrals from the molecular integrals. We can use the identity $C^{\dagger}SC = I$ to reverse the transformation on the one and two electron integrals.

oei_mo,  tei_mo = molecule.one_body_integrals, molecule.two_body_integrals
C = molecule.canonical_orbitals
S = molecule.overlap_integrals
oei_ao = general_basis_change(oei_mo, C.conj().T @ S, key=(1, 0))
print(oei_ao)
[[-1.18698197e+00 -1.09398249e+00 -2.90382070e-01 -5.73478152e-01
  -1.91588194e-03 -7.08618226e-02 -2.34028697e-07 -1.97458486e-03
  -5.37517440e-13 -1.18105831e-05 -3.07827985e-16 -1.46391009e-08]
 [-1.09398249e+00 -1.30863711e+00 -6.31325886e-01 -9.69723130e-01
  -7.72940958e-02 -2.75295274e-01 -2.08668247e-03 -2.98813560e-02
  -1.21151858e-05 -1.23417943e-03 -1.46391010e-08 -1.91822048e-05]
 [-2.90382070e-01 -6.31325886e-01 -1.51260669e+00 -1.30790890e+00
  -3.09964749e-01 -6.50637018e-01 -1.94571033e-03 -7.66467737e-02
  -2.34897055e-07 -2.06566594e-03 -5.37638634e-13 -1.21151858e-05]
 [-5.73478152e-01 -9.69723130e-01 -1.30790890e+00 -1.61455088e+00
  -6.69270617e-01 -1.08041337e+00 -7.82781452e-02 -2.89819977e-01
  -2.06566594e-03 -3.04640626e-02 -1.18105831e-05 -1.23417943e-03]
 [-1.91588194e-03 -7.72940958e-02 -3.09964749e-01 -6.69270617e-01
  -1.61437154e+00 -1.37489989e+00 -3.14983314e-01 -6.74320764e-01
  -1.94571033e-03 -7.82781452e-02 -2.34028698e-07 -2.08668247e-03]
 [-7.08618226e-02 -2.75295274e-01 -6.50637018e-01 -1.08041337e+00
  -1.37489989e+00 -1.71629957e+00 -6.74320764e-01 -1.10900856e+00
  -7.66467737e-02 -2.89819977e-01 -1.97458486e-03 -2.98813560e-02]
 [-2.34028697e-07 -2.08668247e-03 -1.94571033e-03 -7.82781452e-02
  -3.14983314e-01 -6.74320764e-01 -1.61437154e+00 -1.37489989e+00
  -3.09964749e-01 -6.69270617e-01 -1.91588194e-03 -7.72940958e-02]
 [-1.97458486e-03 -2.98813560e-02 -7.66467737e-02 -2.89819977e-01
  -6.74320764e-01 -1.10900856e+00 -1.37489989e+00 -1.71629957e+00
  -6.50637018e-01 -1.08041337e+00 -7.08618226e-02 -2.75295274e-01]
 [-5.37619614e-13 -1.21151858e-05 -2.34897054e-07 -2.06566594e-03
  -1.94571033e-03 -7.66467737e-02 -3.09964749e-01 -6.50637018e-01
  -1.51260669e+00 -1.30790890e+00 -2.90382070e-01 -6.31325886e-01]
 [-1.18105831e-05 -1.23417943e-03 -2.06566594e-03 -3.04640626e-02
  -7.82781452e-02 -2.89819977e-01 -6.69270617e-01 -1.08041337e+00
  -1.30790890e+00 -1.61455088e+00 -5.73478152e-01 -9.69723130e-01]
 [-6.90538940e-16 -1.46391011e-08 -5.37734828e-13 -1.18105831e-05
  -2.34028698e-07 -1.97458486e-03 -1.91588194e-03 -7.08618226e-02
  -2.90382070e-01 -5.73478152e-01 -1.18698197e+00 -1.09398249e+00]
 [-1.46391012e-08 -1.91822048e-05 -1.21151858e-05 -1.23417943e-03
  -2.08668247e-03 -2.98813560e-02 -7.72940958e-02 -2.75295274e-01
  -6.31325886e-01 -9.69723130e-01 -1.09398249e+00 -1.30863711e+00]]
print(oei_mo)
[[-1.87432473e+00 -7.24478626e-16 -1.16409397e-01 -4.45583605e-16
  -6.15079850e-02  4.11069749e-16 -1.25731117e-01  4.10276506e-16
  -5.89300819e-02  3.83527060e-16  2.71519013e-03  6.83767065e-16]
 [-6.15250874e-16 -1.69304317e+00 -5.80535128e-16  1.65551634e-01
   1.51196459e-16 -4.41325654e-02 -1.43729952e-15  1.00494695e-01
   2.35340695e-15 -8.00967125e-02  9.74768549e-16 -6.99099285e-03]
 [-1.16409397e-01 -3.78979857e-16 -1.61481611e+00 -3.82350886e-16
   1.42975424e-01  5.98664665e-16  1.99561985e-02  3.89925228e-16
  -9.93297282e-02 -3.26463066e-15 -8.38903468e-02  3.93227776e-15]
 [-3.97506447e-16  1.65551634e-01 -4.46012495e-16 -1.47182278e+00
   3.58593611e-16 -1.55554532e-01  1.17871923e-16  6.81485313e-02
  -1.75697305e-15  1.47405321e-01 -7.00716639e-15 -4.83500512e-02]
 [-6.15079850e-02 -1.35790980e-16  1.42975424e-01  3.27091786e-16
  -1.29158630e+00 -2.09347898e-15  5.71950545e-02  4.27751144e-15
  -1.32456629e-01  9.05861678e-16  1.56350374e-01 -6.06366584e-15]
 [ 5.35967863e-16 -4.41325654e-02  5.62231733e-16 -1.55554532e-01
  -2.10850033e-15 -1.24806929e+00 -3.00067699e-15 -1.25035349e-01
  -2.96347997e-16 -1.02022892e-01 -9.72872290e-15 -2.06045664e-01]
 [-1.25731117e-01 -1.52315598e-15  1.99561985e-02  1.47676484e-16
   5.71950545e-02 -3.09349448e-15 -5.80093413e-01  2.71396532e-15
  -1.60790101e-01 -3.34670386e-15 -2.49740558e-02  2.53648386e-15]
 [ 2.73603518e-16  1.00494695e-01  6.01437149e-16  6.81485313e-02
   4.33932659e-15 -1.25035349e-01  2.77123805e-15 -4.13964912e-01
  -1.55022004e-15  9.28607464e-02 -5.47802989e-15 -4.59666892e-02]
 [-5.89300819e-02  2.75459657e-15 -9.93297282e-02 -1.86321159e-15
  -1.32456629e-01 -3.86401336e-16 -1.60790101e-01 -1.55496725e-15
  -4.68339016e-01  3.81499500e-15 -3.67277351e-02  1.46412987e-15]
 [ 2.30456780e-16 -8.00967125e-02 -3.50391740e-15  1.47405321e-01
   1.01572494e-15 -1.02022892e-01 -3.34377041e-15  9.28607464e-02
   3.99095816e-15 -5.21989682e-01 -1.36488187e-16  3.96032256e-02]
 [ 2.71519013e-03  5.54156150e-16 -8.38903468e-02 -6.82460330e-15
   1.56350374e-01 -9.41316135e-15 -2.49740558e-02 -5.60841547e-15
  -3.67277351e-02 -4.39354242e-16 -5.04870294e-01  6.09078740e-16]
 [-1.57776255e-17 -6.99099285e-03  4.07823164e-15 -4.83500512e-02
  -6.38640187e-15 -2.06045664e-01  2.64377639e-15 -4.59666892e-02
   1.71310164e-15  3.96032256e-02  1.02256902e-15 -4.15306261e-01]]
# Use pyscf to get atomic integrals and compare to transformed integrals from above
pyscf_mol = molecule._pyscf_data['mol']
t = pyscf_mol.intor('int1e_kin')
v = pyscf_mol.intor('int1e_nuc')
hcore = t + v
assert np.allclose(hcore, oei_ao)

The two-electron integrals can also be acquired from the pyscf_mol object or we can use the general_basis_change to transform the two-electron integrals back into the AO basis.

# Transform the two-electron integrals
tei_ao = general_basis_change(tei_mo, C.conj().T @ S, key=(1, 1, 0, 0))  

# re-ordering for chem->physics storage of the integrals
eri_ao = np.einsum('ijkl->iklj', pyscf_mol.intor('int2e', aosym='s1'))
assert np.allclose(tei_ao, eri_ao)
assert not np.allclose(tei_ao, tei_mo)

We also provide a function in recirq.hfvqe.molecular_data.molecular_data_construction that uses psi4 to generate atomic integrals for the HF-VQE study. Once the atomic orbital integrals are obtained we can perform the first step in setting up the HF-VQE experiment. This involes transforming the integrals to the core-orbital basis and building an RestrictedHartreeFockObjective.

# diagonalize the AO-core Hamiltonian
_, X = scipy.linalg.eigh(oei_ao, S)  

obi = general_basis_change(oei_ao, X, (1, 0))
tbi = np.einsum('psqr', general_basis_change(pyscf_mol.intor('int2e', aosym='s1'), X, (1, 0, 1, 0)))
molecular_hamiltonian = generate_hamiltonian(obi, tbi,
                                             molecule.nuclear_repulsion)

rhf_objective = RestrictedHartreeFockObjective(molecular_hamiltonian,
                                               molecule.n_electrons)

To get initial parameters we can simulate the Hartree-Fock experiment which is performing variational-Hartree-Fock theory.

from recirq.hfvqe.gradient_hf import rhf_minimization, rhf_func_generator
# Uses conjugate gradient to solve non-linear hartree-fock functional:
scipy_result = rhf_minimization(rhf_objective, verbose=True)
Optimization terminated successfully.
         Current function value: -3.102591
         Iterations: 12
         Function evaluations: 25
         Gradient evaluations: 25
print(molecule.hf_energy, scipy_result.fun)
-3.1025910099104914 -3.1025910099026444
print("Initial Parameters for HF-VQE Study ", scipy_result.x)
Initial Parameters for HF-VQE Study  [-1.00143752e-20 -4.73763008e-01  6.60620466e-20 -1.36102260e-02
 -2.09955868e-20  4.44832962e-01  2.42756497e-20 -2.59144562e-01
  8.36642335e-21  1.17062375e-01 -5.73771734e-21  7.61172516e-03
  1.68201040e-20  9.47442448e-02 -9.67248528e-21 -2.71237051e-04
  2.45981516e-20 -7.30836704e-02  1.28872365e-19 -4.10271133e-02
 -4.24308432e-20 -4.52955414e-03 -6.62091725e-21 -8.54284651e-02
 -4.12248294e-20 -4.71103578e-02  3.39533252e-20]
# Explicity build kappa matrix
from recirq.hfvqe.circuits import rhf_params_to_matrix
import matplotlib.pyplot as plt
kappa = rhf_params_to_matrix(scipy_result.x, len(rhf_objective.occ) + len(rhf_objective.virt), rhf_objective.occ,
            rhf_objective.virt)
plt.imshow(kappa)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x7f8bdc75d4a8>

png