/
test_einsum.py
182 lines (153 loc) · 6.63 KB
/
test_einsum.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/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 einsum, empty_like, nosym_like
from adcc.testdata.cache import cache
import pytest
class TestEinsum(unittest.TestCase):
def base_test(self, contr, *arr):
arr = [a.set_random() for a in arr]
ref = np.einsum(contr, *[a.to_ndarray() for a in arr])
out = einsum(contr, *arr)
assert_allclose(out.to_ndarray(), ref, rtol=1e-10, atol=1e-14)
def test_einsum_1_1_0(self):
refstate = cache.refstate["h2o_sto3g"]
a = nosym_like(refstate.orbital_energies("o1"))
b = nosym_like(refstate.orbital_energies("v1"))
self.base_test("i,j->ji", a, b)
def test_einsum_2_2_0(self):
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.fock("o1o1"))
b = empty_like(refstate.fock("o1v1"))
self.base_test("ij,ka->kjai", a, b)
def test_einsum_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"))
self.base_test("i,ij->j", a, b)
def test_einsum_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"))
self.base_test("i,ji->j", a, b)
def test_einsum_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"))
self.base_test("ij,j->i", a, b)
def test_einsum_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"))
self.base_test("ij,jk->ik", a, b)
def test_einsum_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"))
self.base_test("ij,iklm->jlkm", a, b)
def test_einsum_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"))
self.base_test("ij,ikjl->kl", a, b)
def test_einsum_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"))
self.base_test("ijkl,km->iljm", a, b)
def test_einsum_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"))
self.base_test("ijkl,km->imjl", a, b)
# oovv vv ovov
def test_einsum_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"))
self.base_test("ijkl,jk->li", a, b)
def test_einsum_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"))
self.base_test("opvw,vijw->oipj", a, b)
def test_einsum_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"))
self.base_test("opvw,vijw->ojpi", a, b)
def test_einsum_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"))
self.base_test("opvw,vijw->pjoi", a, b)
def test_einsum_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"))
self.base_test("iajb,jikb->ka", a, b)
def test_diagonal_1(self):
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.eri("o1v1o1v1"))
self.base_test("iaia->ia", a)
def test_diagonal_2(self):
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.eri("o1v1o1v1"))
self.base_test("iaia->ai", a)
def test_diagonal_3(self):
refstate = cache.refstate["h2o_sto3g"]
a = empty_like(refstate.ovov)
self.base_test("iaja->aij", a)
def test_diagonal_4(self):
refstate = cache.refstate["h2o_sto3g"]
a = empty_like(refstate.ovov)
self.base_test("iaja->ija", a)
def test_diagonal_5(self):
refstate = cache.refstate["cn_sto3g"]
a = empty_like(refstate.ovov)
b = empty_like(refstate.foo)
self.base_test("laib,il->aib", a, b)
def test_diagonal_6(self):
refstate = cache.refstate["h2o_sto3g"]
a = empty_like(refstate.fov)
b = empty_like(refstate.fvo)
self.base_test("ai,ia->i", a, b)
def test_diagonal_7(self):
refstate = cache.refstate["h2o_sto3g"]
a = empty_like(refstate.ovov)
b = empty_like(refstate.fvv)
self.base_test("iaib,ba->ia", a, b)
def test_partial_trace(self):
refstate = cache.refstate["h2o_sto3g"]
a = empty_like(refstate.ovov)
b = empty_like(refstate.fvv)
with pytest.raises(NotImplementedError, match=r"Partial traces.*"):
einsum("iaib,ba->a", a, b)
def test_thc(self):
refstate = cache.refstate["h2o_sto3g"]
a = nosym_like(refstate.foo)
b = nosym_like(refstate.fov)
c = nosym_like(refstate.foo)
self.base_test("ij,ia,ik->jka", a, b, c)