Skip to content

Commit a22e389

Browse files
authored
Merge pull request #175 from RichRick1/molham_test
Tutorials are running as expected.
2 parents e9f894a + c93792c commit a22e389

File tree

9 files changed

+157
-596
lines changed

9 files changed

+157
-596
lines changed

docs/molham.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Molecular Hamiltonian
2+
=====================
3+
MolHam class
4+
-----------------
5+
... autoclass:: moha.molkit.hamiltonians.MolHam
6+
:members:
7+
8+
.. automethod:: __init__

examples/molkit.ipynb

Lines changed: 53 additions & 574 deletions
Large diffs are not rendered by default.

moha/molkit/hamiltonians.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def __init__(self, one_body=None, two_body=None, zero_body=0):
4242

4343
def antisymmetrize(self):
4444
"""Apply proper antisymmetrization to two-electron integrals."""
45-
self.two_body = antisymmetrize_two_body(self.two_body, inplace=True)
45+
if not hasattr(self, "two_body_spin"):
46+
raise RuntimeError(
47+
"Call .spinize_H() first to compute spin-orbital form.")
48+
self.two_body_spin = antisymmetrize_two_body(self.two_body_spin,
49+
inplace=True)
4650

4751
def get_spin_blocks(self):
4852
"""Return the main spin blocks of the two-body spin-orbital tensor.

moha/molkit/utils/spinops.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def antisymmetrize_two_body(
2020
Returns
2121
-------
2222
np.ndarray
23-
Antisymmetrised tensor obeying
23+
Antisymmetrised tensor obeys
2424
2525
.. math::
2626
@@ -32,18 +32,28 @@ def antisymmetrize_two_body(
3232
3333
Notes
3434
-----
35-
The operation applied is
35+
https://vergil.chemistry.gatech.edu/static/content/permsymm.pdf
36+
37+
The input tensor obbeys the following 8-fold symmetry
3638
3739
.. math::
3840
39-
V^{\\sigma\\sigma\\sigma\\sigma}_{pqrs}
40-
\\;\\;\\leftarrow\\;\\;
41-
\\tfrac12\\,\\bigl(V^{\\sigma\\sigma\\sigma\\sigma}_{pqrs}
42-
-V^{\\sigma\\sigma\\sigma\\sigma}_{pqsr}\\bigr),
41+
V_{ijkl} = V_{jilk}
42+
= V_{klij}
43+
= V_{lkji}
44+
= V_{kjjl}
45+
= V_{lijk}
46+
= V_{ilkj}
47+
= V_{jklj}
48+
49+
The operation applied is
4350
44-
for ``σ = α`` and ``σ = β``. All other spin sectors are returned
45-
untouched.
51+
.. math::
52+
\langle ij\| kl\rangle=
53+
\langle ij\mid kl\rangle-\langle ij\mid lk\rangle
4654
55+
Keep in mind, that such operation produces terms of the form
56+
abba, baab, that are not present in the original tensor.
4757
"""
4858
if not inplace:
4959
tensor = tensor.copy()
@@ -54,15 +64,7 @@ def antisymmetrize_two_body(
5464

5565
n = nspin // 2 # number of spatial orbitals
5666

57-
# αααα block
58-
aa = tensor[:n, :n, :n, :n]
59-
aa -= np.swapaxes(aa, 2, 3)
60-
aa *= 0.5
61-
62-
# ββββ block
63-
bb = tensor[n:, n:, n:, n:]
64-
bb -= np.swapaxes(bb, 2, 3)
65-
bb *= 0.5
67+
tensor = tensor - np.einsum('pqrs->pqsr', tensor)
6668

6769
return tensor
6870

moha/molkit/utils/tests/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

moha/molkit/utils/tests/test_spinops.py

Whitespace-only changes.

moha/molkit/utils/tools.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,14 @@ def to_geminal(two_body=None, type='h2'):
5555
for s in range(r + 1, n):
5656
if type == 'rdm2':
5757
two_body_gem.append(
58-
0.5 * 4 * two_body[p, q, r, s]
58+
two_body[p, q, r, s]
5959
)
6060
elif type == 'h2':
6161
two_body_gem.append(
62-
0.5 * (
6362
two_body[p, q, r, s]
6463
- two_body[q, p, r, s]
6564
- two_body[p, q, s, r]
6665
+ two_body[q, p, s, r]
67-
)
6866
)
6967

7068
n_gem = n * (n - 1) // 2

moha/test/test_molham.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""Testing the MolHam class."""
2+
3+
import numpy as np
4+
from numpy.testing import assert_allclose, assert_equal
5+
from moha.molkit import utils
6+
from moha.molkit.hamiltonians import MolHam
7+
8+
9+
def test_antisymmetrize():
10+
"""Test antisymmetrization of two-electron integrals."""
11+
one_body = np.eye(2)
12+
two_body = np.zeros((2, 2, 2, 2))
13+
two_body[0, 0, 1, 1] = 1.0
14+
two_body[1, 1, 0, 0] = 1.0
15+
16+
mol_ham = MolHam(one_body=one_body, two_body=two_body)
17+
mol_ham.spinize_H()
18+
mol_ham.antisymmetrize()
19+
20+
expected_two_body_aa = np.zeros((2, 2, 2, 2))
21+
expected_two_body_aa[0, 0, 1, 1] = 1
22+
expected_two_body_aa[1, 1, 0, 0] = 1
23+
expected_two_body_aa[0, 1, 0, 1] = 0
24+
expected_two_body_aa[1, 0, 1, 0] = 0
25+
26+
assert_allclose(mol_ham.two_body[:2, :2, :2, :2], expected_two_body_aa)
27+
28+
29+
def test_to_geminal():
30+
"""Test conversion to geminal basis."""
31+
n_orb = 4
32+
two_body = np.zeros((2, 2, 2, 2))
33+
two_body[0, 0, 1, 1] = 1.0
34+
two_body[1, 1, 0, 0] = 1.0
35+
two_body[1, 0, 0, 1] = 1.0
36+
two_body[0, 1, 1, 0] = 1.0
37+
38+
geminal_true = np.array([
39+
[-2.0]
40+
])
41+
geminal = utils.tools.to_geminal(two_body, type='h2')
42+
assert_equal(geminal.shape, (1, 1))
43+
assert_allclose(geminal, geminal_true)
44+
45+
46+
def test_to_reduced_ham():
47+
"""Test conversion to reduced Hamiltonian."""
48+
n_orb = 2
49+
one_body = np.eye(n_orb)
50+
two_body = np.zeros((n_orb, n_orb, n_orb, n_orb))
51+
two_body[0, 0, 1, 1] = 1.0
52+
two_body[1, 1, 0, 0] = 1.0
53+
54+
mol_ham = MolHam(one_body=one_body, two_body=two_body)
55+
reduced_ham = mol_ham.to_reduced(n_elec=2)
56+
57+
# sum over the spin-orbital indices
58+
reduced_ham = reduced_ham[:2, :2, :2, :2] +\
59+
reduced_ham[2:, 2:, 2:, 2:] +\
60+
reduced_ham[:2, 2:, :2, 2:] +\
61+
reduced_ham[2:, :2, 2:, :2]
62+
reduced_ham *= 0.25
63+
64+
reduced_ham_true = 0.5 * two_body
65+
reduced_ham_true[0, 0, 0, 0] = 1
66+
reduced_ham_true[1, 1, 1, 1] = 1
67+
reduced_ham_true[0, 1, 0, 1] = 1
68+
reduced_ham_true[1, 0, 1, 0] = 1
69+
70+
assert_allclose(reduced_ham, reduced_ham_true)

website/_toc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ parts:
1212
- file: examples/mohagpt.ipynb
1313
- file: examples/gui.ipynb
1414
- file: examples/RG.ipynb
15+
- file: examples/molkit.ipynb
1516

1617
- caption: API
1718
chapters:

0 commit comments

Comments
 (0)