Skip to content

Commit 329f660

Browse files
kevinsungbabbush
authored andcommitted
Add wrapper to generate molecular Hamiltonian as InteractionOperator (#37)
* add generate_molecular_hamiltonian * lint
1 parent b5e4042 commit 329f660

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

openfermionpyscf/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
OpenFermion plugin to interface with PySCF.
1515
"""
1616

17-
try:
18-
from ._pyscf_molecular_data import PyscfMolecularData
19-
from ._run_pyscf import prepare_pyscf_molecule, run_pyscf
20-
except ImportError:
21-
raise Exception("Please install PySCF.")
17+
from ._pyscf_molecular_data import PyscfMolecularData
18+
19+
from ._run_pyscf import (
20+
generate_molecular_hamiltonian,
21+
prepare_pyscf_molecule,
22+
run_pyscf)
2223

2324
from ._version import __version__

openfermionpyscf/_run_pyscf.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
from __future__ import absolute_import
1616

17+
from functools import reduce
18+
1719
import numpy
1820
import pyscf
19-
from functools import reduce
20-
from pyscf import ci, cc, fci, mp
2121

22-
from openfermion.config import *
22+
from openfermion import MolecularData
2323
from openfermionpyscf import PyscfMolecularData
2424

2525

@@ -204,3 +204,54 @@ def run_pyscf(molecule,
204204
pyscf_molecular_data.__dict__.update(molecule.__dict__)
205205
pyscf_molecular_data.save()
206206
return pyscf_molecular_data
207+
208+
209+
def generate_molecular_hamiltonian(
210+
geometry,
211+
basis,
212+
multiplicity,
213+
charge=0,
214+
n_active_electrons=None,
215+
n_active_orbitals=None):
216+
"""Generate a molecular Hamiltonian with the given properties.
217+
218+
Args:
219+
geometry: A list of tuples giving the coordinates of each atom.
220+
An example is [('H', (0, 0, 0)), ('H', (0, 0, 0.7414))].
221+
Distances in angstrom. Use atomic symbols to
222+
specify atoms.
223+
basis: A string giving the basis set. An example is 'cc-pvtz'.
224+
Only optional if loading from file.
225+
multiplicity: An integer giving the spin multiplicity.
226+
charge: An integer giving the charge.
227+
n_active_electrons: An optional integer specifying the number of
228+
electrons desired in the active space.
229+
n_active_orbitals: An optional integer specifying the number of
230+
spatial orbitals desired in the active space.
231+
232+
Returns:
233+
The Hamiltonian as an InteractionOperator.
234+
"""
235+
236+
# Run electronic structure calculations
237+
molecule = run_pyscf(
238+
MolecularData(geometry, basis, multiplicity, charge)
239+
)
240+
241+
# Freeze core orbitals and truncate to active space
242+
if n_active_electrons is None:
243+
n_core_orbitals = 0
244+
occupied_indices = None
245+
else:
246+
n_core_orbitals = (molecule.n_electrons - n_active_electrons) // 2
247+
occupied_indices = list(range(n_core_orbitals))
248+
249+
if n_active_orbitals is None:
250+
active_indices = None
251+
else:
252+
active_indices = list(range(n_core_orbitals,
253+
n_core_orbitals + n_active_orbitals))
254+
255+
return molecule.get_molecular_hamiltonian(
256+
occupied_indices=occupied_indices,
257+
active_indices=active_indices)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
import openfermion
14+
import openfermionpyscf
15+
16+
17+
def test_load_molecular_hamiltonian():
18+
geometry = [('Li', (0., 0., 0.)), ('H', (0., 0., 1.4))]
19+
20+
lih_hamiltonian = openfermionpyscf.generate_molecular_hamiltonian(
21+
geometry, 'sto-3g', 1, 0, 2, 2)
22+
assert openfermion.count_qubits(lih_hamiltonian) == 4
23+
24+
lih_hamiltonian = openfermionpyscf.generate_molecular_hamiltonian(
25+
geometry, 'sto-3g', 1, 0, 2, 3)
26+
assert openfermion.count_qubits(lih_hamiltonian) == 6
27+
28+
lih_hamiltonian = openfermionpyscf.generate_molecular_hamiltonian(
29+
geometry, 'sto-3g', 1, 0, None, None)
30+
assert openfermion.count_qubits(lih_hamiltonian) == 12

0 commit comments

Comments
 (0)