-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftfci.py
185 lines (160 loc) · 5.82 KB
/
ftfci.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
183
184
185
# Copyright 2016-2023 Chong Sun (sunchong137@gmail.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/usr/bin/env python
import numpy
import pyscf.lib
from pyscf.fci import cistring
from pyscf.fci import direct_spin1
import ftlanczos as flan
from smpl import smpl_hilbert as ftsmpl
def kernel(h1e, g2e, norb, nelec):
h2e = direct_spin1.absorb_h1e(h1e, g2e, norb, nelec, .5)
if isinstance(nelec, (int, numpy.integer)):
neleca = nelec//2
else:
neleca = nelec[0]
na = cistring.num_strings(norb, neleca)
ci0 = numpy.zeros((na,na))
ci0[0,0] = 1
def hop(c):
hc = direct_spin1.contract_2e(h2e, c, norb, nelec)
return hc.reshape(-1)
hdiag = direct_spin1.make_hdiag(h1e, g2e, norb, nelec)
precond = lambda x, e, *args: x/(hdiag-e+1e-4)
e, c = pyscf.lib.davidson(hop, ci0.reshape(-1), precond)
return e, c
def kernel_ft(h1e, g2e, norb, nelec, T, m=50, nsamp=100, Tmin=10e-4):
'''E at temperature T
'''
if T < Tmin:
e, c = kernel(h1e, g2e, norb, nelec)
return e
h2e = direct_spin1.absorb_h1e(h1e, g2e, norb, nelec, .5)
if isinstance(nelec, (int, numpy.integer)):
nelecb = nelec//2
neleca = nelec - nelecb
else:
neleca, nelecb = nelec
na = cistring.num_strings(norb, neleca)
nb = cistring.num_strings(norb, nelecb)
def vecgen(n1=na, n2=nb):
ci0 = numpy.random.randn(n1, n2)
return ci0.reshape(-1)
def hop(c):
hc = direct_spin1.contract_2e(h2e, c, norb, nelec)
return hc.reshape(-1)
E = flan.ftlan_E(hop, vecgen, T, m, nsamp)
return E
def kernel_ft_smpl(h1e, g2e, norb, nelec, T, vecgen = 0, m=50, nsmpl = 250, nblk = 10, Tmin=10e-4, nrotation = 200):
if T < Tmin:
e, c = kernel(h1e, g2e, norb, nelec)
return e
disp = numpy.exp(T) * 0.1 # displacement
h2e = direct_spin1.absorb_h1e(h1e, g2e, norb, nelec, .5)
if isinstance(nelec, (int, numpy.integer)):
nelecb = nelec//2
neleca = nelec - nelecb
else:
neleca, nelecb = nelec
na = cistring.num_strings(norb, neleca)
nb = cistring.num_strings(norb, nelecb)
ci0 = numpy.random.randn(na, nb)
def hop(c):
hc = direct_spin1.contract_2e(h2e, c, norb, nelec)
return hc.reshape(-1)
E, dev, ar = ftsmpl(hop, ci0, T, flan.ftlan_E1c, nsamp = nsmpl, dr = disp, genci=vecgen, nblock = nblk, nrot = nrotation)
# ar is the acceptance ratio
return E, dev, ar
def ft_rdm1s(h1e, g2e, norb, nelec, T, m=50, nsamp=40, Tmin=10e-4):
'''rdm of spin a and b at temperature T
'''
if T < Tmin:
e, c = kernel(h1e, g2e, norb, nelec)
rdma, rdmb = direct_spin1.make_rdm1s(c, norb, nelec)
return rdma, rdmb
h2e = direct_spin1.absorb_h1e(h1e, g2e, norb, nelec, .5)
if isinstance(nelec, (int, numpy.integer)):
nelecb = nelec//2
neleca = nelec - nelecb
else:
neleca, nelecb = nelec
na = cistring.num_strings(norb, neleca)
nb = cistring.num_strings(norb, nelecb)
def vecgen(n1=na, n2=nb):
ci0 = numpy.random.randn(n1, n2)
# ci0[0, 0] = 1.
return ci0.reshape(-1)
def hop(c):
hc = direct_spin1.contract_2e(h2e, c, norb, nelec)
return hc.reshape(-1)
def qud(v1, v2):
dma, dmb = direct_spin1.trans_rdm1s(v1, v2, norb, nelec)
return dma, dmb
# rdma, rdmb = flan.ht_rdm1s(qud, hop, vecgen, T, norb, m, nsamp)
rdma, rdmb = flan.ftlan_rdm1s(qud, hop, vecgen, T, norb, m, nsamp)
return rdma, rdmb
def ft_rdm1(h1e, g2e, norb, nelec, T, m=50, nsamp=40):
rdma, rdmb = ft_rdm1s(h1e, g2e, norb, nelec, T, m, nsamp)
return rdma+rdmb
if __name__ == '__main__':
from functools import reduce
from pyscf import gto
from pyscf import scf
from pyscf import ao2mo
mol = gto.Mole()
mol.verbose = 0
mol.output = None
mol.atom = [
['H', ( 1.,-1. , 0. )],
['H', ( 0.,-1. ,-1. )],
['H', ( 1.,-0.5 ,-1. )],
['H', ( 0.,-0. ,-1. )],
['H', ( 1.,-0.5 , 0. )],
['H', ( 0., 1. , 1. )],
]
mol.basis = 'sto-3g'
mol.build()
m = scf.RHF(mol)
m.kernel()
norb = m.mo_coeff.shape[1]
nelec = mol.nelectron - 2
ne = mol.nelectron - 2
nelec = (nelec//2, nelec-nelec//2)
h1e = reduce(numpy.dot, (m.mo_coeff.T, m.get_hcore(), m.mo_coeff))
eri = ao2mo.incore.general(m._eri, (m.mo_coeff,)*4, compact=False)
eri = eri.reshape(norb,norb,norb,norb)
e1, ci0 = kernel(h1e, eri, norb, ne) #FCI kernel
print "T = 0, E = ", e1
rdma0, rdmb0 = direct_spin1.make_rdm1s(ci0, norb, nelec)
print "*********************"
print "zero rdm:\n", rdma0, "\n", rdmb0
print "*********************"
rdma, rdmb = ft_rdm1s(h1e, eri, norb, nelec, 10., 10, 10)
print rdma, "\n", rdmb
# print numpy.sum(numpy.diag(rdma))
# print "T = 0, E = %10.10f"%e1
e2 = kernel_ft(h1e, eri, norb, nelec, 0.1, 40, 20)
print "E(T) = %10.10f"%e2
e3 = kernel_ft_smpl(h1e, eri, norb, nelec, 0.1, )
print "E(T)_smpl = %10.10f"%e3
'''
f = open("data/E-T_3.dat", "w")
f.write("%2.4f %2.10f\n"%(0., e1))
for i in range(30):
T = 0.1+0.2*i
e2 = kernel_ft(h1e, eri, norb, nelec, T) # finite FCI kernel
print "%2.4f %2.10f"%(T, e2)
f.write("%2.4f %2.10f\n"%(T, e2))
f.close()
'''