/
test_contract.py
139 lines (119 loc) · 5.56 KB
/
test_contract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
## vi: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
## ---------------------------------------------------------------------
##
## Copyright (C) 2019 by the adcc authors
##
## This file is part of adcc.
##
## adcc is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## adcc is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with adcc. If not, see <http://www.gnu.org/licenses/>.
##
## ---------------------------------------------------------------------
import unittest
import numpy as np
from numpy.testing import assert_allclose
from adcc import contract, empty_like, nosym_like
from adcc.testdata.cache import cache
# Note: The tuples "in C++" refer to the (N_CONTR_IDCS, DIMA, DIMB)
# tuple in the IF_DIMENSIONS_MATCH_EXECUTE preprocessor macro
# of the TensorImpl::contract_to function in adccore.
class TestContract(unittest.TestCase):
def base_test(self, contr, a, b, out):
a.set_random()
b.set_random()
out.set_random()
ref = np.einsum(contr, a.to_ndarray(), b.to_ndarray())
contract(contr, a, b, out)
assert_allclose(out.to_ndarray(), ref, rtol=1e-10, atol=1e-14)
out2 = contract(contr, a, b)
assert_allclose(out2.to_ndarray(), ref, rtol=1e-10, atol=1e-14)
def test_contract_1_2_1_ij(self): # (1,1,2) in C++
refstate = cache.refstate["h2o_sto3g"]
a = nosym_like(refstate.orbital_energies("o1"))
b = nosym_like(refstate.fock("o1v1"))
out = nosym_like(refstate.orbital_energies("v1"))
self.base_test("i,ij->j", a, b, out)
def test_contract_1_2_1_ji(self): # (1,1,2) in C++
refstate = cache.refstate["h2o_sto3g"]
a = nosym_like(refstate.orbital_energies("o1"))
b = nosym_like(refstate.fock("v1o1"))
out = nosym_like(refstate.orbital_energies("v1"))
self.base_test("i,ji->j", a, b, out)
def test_contract_2_1_1(self): # (1,2,1) in C++
refstate = cache.refstate["h2o_sto3g"]
a = empty_like(refstate.fock("o1v1"))
b = empty_like(refstate.orbital_energies("v1"))
out = nosym_like(refstate.orbital_energies("o1"))
self.base_test("ij,j->i", a, b, out)
def test_contract_2_2_2(self): # (1, 2, 2) in C++
refstate = cache.refstate["h2o_sto3g"]
a = nosym_like(refstate.fock("o1v1"))
b = nosym_like(refstate.fock("v1v1"))
out = nosym_like(refstate.fock("o1v1"))
self.base_test("ij,jk->ik", a, b, out)
def test_contract_2_4_4(self): # (1, 2, 4) in C++
refstate = cache.refstate["h2o_sto3g"]
a = empty_like(refstate.fock("o1o1"))
b = empty_like(refstate.eri("o1v1o1v1"))
out = nosym_like(refstate.eri("o1o1v1v1"))
self.base_test("ij,iklm->jlkm", a, b, out)
def test_contract_2_4_2(self): # (2, 2, 4) in C++
refstate = cache.refstate["h2o_sto3g"]
a = empty_like(refstate.fock("o1o1"))
b = empty_like(refstate.eri("o1v1o1v1"))
out = nosym_like(refstate.fock("v1v1"))
self.base_test("ij,ikjl->kl", a, b, out)
def test_contract_4_2_4(self): # (1, 4, 2) in C++
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.eri("o1o1v1v1"))
b = empty_like(refstate.fock("v1v1"))
out = nosym_like(refstate.eri("o1v1o1v1"))
self.base_test("ijkl,km->iljm", a, b, out)
def test_contract_4_2_4_perm(self): # (1, 4, 2) in C++
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.eri("o1o1v1v1"))
b = empty_like(refstate.fock("v1v1"))
out = nosym_like(refstate.eri("o1v1o1v1"))
self.base_test("ijkl,km->imjl", a, b, out)
# oovv vv ovov
def test_contract_4_2_2(self): # (2, 4, 2) in C++
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.eri("o1o1v1v1"))
b = empty_like(refstate.fock("o1v1"))
out = nosym_like(refstate.fock("v1o1"))
self.base_test("ijkl,jk->li", a, b, out)
def test_contract_4_4_4_oipj(self): # (2, 4, 4) in C++
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.eri("o1o1v1v1"))
b = empty_like(refstate.eri("v1v1v1v1"))
out = nosym_like(refstate.eri("o1v1o1v1"))
self.base_test("opvw,vijw->oipj", a, b, out)
def test_contract_4_4_4_ojpi(self): # (2, 4, 4) in C++
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.eri("o1o1v1v1"))
b = empty_like(refstate.eri("v1v1v1v1"))
out = nosym_like(refstate.eri("o1v1o1v1"))
self.base_test("opvw,vijw->ojpi", a, b, out)
def test_contract_4_4_4_pjoi(self): # (2, 4, 4) in C++
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.eri("o1o1v1v1"))
b = empty_like(refstate.eri("v1v1v1v1"))
out = nosym_like(refstate.eri("o1v1o1v1"))
self.base_test("opvw,vijw->pjoi", a, b, out)
def test_contract_4_4_2(self): # (3, 4, 4) in C++
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.eri("o1v1o1v1"))
b = empty_like(refstate.eri("o1o1o1v1"))
out = nosym_like(refstate.fock("o1v1"))
self.base_test("iajb,jikb->ka", a, b, out)