-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtucker_inner.jl
493 lines (390 loc) · 18.2 KB
/
tucker_inner.jl
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
"""
build_sigma!(sigma_vector::BSTstate, ci_vector::BSTstate, cluster_ops, clustered_ham)
"""
function build_sigma_serial!(sigma_vector::BSTstate{T,N,R}, ci_vector::BSTstate{T,N,R}, cluster_ops, clustered_ham;
nbody=4, cache=false) where {T,N,R}
#={{{=#
for (fock_bra, configs_bra) in sigma_vector
for (fock_ket, configs_ket) in ci_vector
fock_trans = fock_bra - fock_ket
# check if transition is connected by H
haskey(clustered_ham, fock_trans) == true || continue
for (config_bra, coeff_bra) in configs_bra
for (config_ket, coeff_ket) in configs_ket
for term in clustered_ham[fock_trans]
length(term.clusters) <= nbody || continue
check_term(term, fock_bra, config_bra, fock_ket, config_ket) || continue
#coeff_bra.core .= form_sigma_block!(term, cluster_ops, fock_bra, config_bra,
# fock_ket, config_ket,
# coeff_bra, coeff_ket,
# cache=cache)
tmp = form_sigma_block!(term, cluster_ops, fock_bra, config_bra,
fock_ket, config_ket,
coeff_bra, coeff_ket,
cache=cache)
for r in 1:R
coeff_bra.core[r] .= tmp[r]
end
end
end
end
end
end
return
#=}}}=#
end
"""
build_sigma!(sigma_vector::BSTstate, ci_vector::BSTstate, cluster_ops, clustered_ham)
"""
function cache_hamiltonian_old(sigma_vector::BSTstate, ci_vector::BSTstate, cluster_ops, clustered_ham; nbody=4)
#={{{=#
println(" Cache hamiltonian terms")
for (fock_bra, configs_bra) in sigma_vector
for (fock_ket, configs_ket) in ci_vector
fock_trans = fock_bra - fock_ket
# check if transition is connected by H
haskey(clustered_ham, fock_trans) == true || continue
for (config_bra, coeff_bra) in configs_bra
for (config_ket, coeff_ket) in configs_ket
for term in clustered_ham[fock_trans]
length(term.clusters) <= nbody || continue
check_term(term, fock_bra, config_bra, fock_ket, config_ket) || continue
cache_key = OperatorConfig((fock_bra, fock_ket, config_bra, config_ket))
term.cache[cache_key] = build_dense_H_term(term, cluster_ops,
fock_bra, config_bra, coeff_bra,
fock_ket, config_ket, coeff_ket)
end
end
end
end
end
return
#=}}}=#
end
function cache_hamiltonian(bra::BSTstate{T,N,R}, ket::BSTstate{T,N,R}, cluster_ops, clustered_ham; nbody=4, verbose=0, blas=false) where {T,N,R}
#={{{=#
# it seems like this is quite a bit faster when turned off:
#if blas
# TensorOperations.enable_blas()
#else
# TensorOperations.disable_blas()
#end
keys_to_loop = [keys(clustered_ham.trans)...]
# set up scratch arrays
nscr = 10
scr_f = Vector{Vector{Vector{T}} }()
for tid in 1:Threads.nthreads()
tmp = Vector{Vector{T}}()
[push!(tmp, zeros(T,100000)) for i in 1:nscr]
push!(scr_f, tmp)
end
if verbose>0
@printf(" %-50s", " Number of threaded jobs:")
println(length(keys_to_loop))
end
Threads.@threads for ftrans in keys_to_loop
scr = scr_f[Threads.threadid()]
terms = clustered_ham[ftrans]
for term in terms
length(term.clusters) <= nbody || continue
for (fock_ket, configs_ket) in ket
#fock_bra = [fock_ket.config...]
#for (cii,ci) in enumerate(term.clusters)
# fock_bra[ci.idx] = (fock_ket[ci.idx][1] + ftrans[cii][1], fock_ket[ci.idx][2] + ftrans[cii][2])
#end
fock_bra = ftrans + fock_ket
#fock_bra = FockConfig(fock_bra)
haskey(bra.data, fock_bra) == true || continue
for (config_ket, tuck_ket) in configs_ket
for (config_bra, tuck_bra) in bra[fock_bra]
check_term(term, fock_bra, config_bra, fock_ket, config_ket) || continue
cache_key = OperatorConfig((fock_bra, fock_ket, config_bra, config_ket))
#if term isa ClusteredTerm4B
# @btime op = build_dense_H_term($term, $cluster_ops, $fock_bra, $config_bra, $tuck_bra,
# $fock_ket, $config_ket, $tuck_ket, $scr)
# error("please stop")
#end
term.cache[cache_key] = build_dense_H_term(term, cluster_ops,
fock_bra, config_bra, tuck_bra,
fock_ket, config_ket, tuck_ket,
scr)
#term.cache[cache_key] = build_dense_H_term(term, cluster_ops,
# fock_bra, config_bra, tuck_bra,
# fock_ket, config_ket, tuck_ket)
end
end
end
end
end
end
#=}}}=#
"""
build_sigma_parallel!(sigma_vector::BSTstate, ci_vector::BSTstate, cluster_ops, clustered_ham)
"""
function build_sigma!(sigma_vector::BSTstate{T,N,R}, ci_vector::BSTstate{T,N,R}, cluster_ops, clustered_ham; nbody=4, cache=false, verbose=1) where {T,N,R}
#={{{=#
verbose < 2 || @printf(" in build_sigma!")
verbose < 2 || println(" length of sigma vector: ", length(sigma_vector))
flush(stdout)
jobs = []
output = [[] for i in 1:Threads.nthreads()]
for (fock_bra, configs_bra) in sigma_vector
for (config_bra, tuck_bra) in configs_bra
push!(jobs, [fock_bra, config_bra])
end
end
# set up scratch arrays
nscr = 10
scr_f = Vector{Vector{Vector{T}} }()
for tid in 1:Threads.nthreads()
tmp = Vector{Vector{T}}()
[push!(tmp, zeros(T,1000)) for i in 1:nscr]
push!(scr_f, tmp)
end
function do_job(job)
fock_bra = job[1]
config_bra = job[2]
coeff_bra = sigma_vector[fock_bra][config_bra]
for (fock_ket, configs_ket) in ci_vector
fock_trans = fock_bra - fock_ket
# check if transition is connected by H
haskey(clustered_ham, fock_trans) == true || continue
for (config_ket, coeff_ket) in configs_ket
for term in clustered_ham[fock_trans]
length(term.clusters) <= nbody || continue
check_term(term, fock_bra, config_bra, fock_ket, config_ket) || continue
# these methods dispatched on type of term
#coeff_bra.core .= form_sigma_block!(term, cluster_ops, fock_bra, config_bra,
# fock_ket, config_ket,
# coeff_bra, coeff_ket,
# cache=cache)
out = form_sigma_block!(term, cluster_ops, fock_bra, config_bra,
fock_ket, config_ket,
coeff_bra, coeff_ket,
scr_f[Threads.threadid()],
cache=cache)
push!(output[Threads.threadid()], (fock_bra, config_bra, out))
end
end
end
end
Threads.@threads for job in jobs
#for job in jobs
do_job(job)
end
flush(stdout)
for tid in output
for out in tid
fock_bra = out[1]
config_bra = out[2]
core = out[3]
add!(sigma_vector[fock_bra][config_bra].core, core)
end
end
return
#=}}}=#
end
#
# form_sigma_block computes the action of the term on a Tucker compressed state,
# projected into the space defined by bra. This is used to work with H within a subspace defined by a compression
#
#
function form_sigma_block!(term::C,
cluster_ops::Vector{ClusterOps{T}},
fock_bra::FockConfig, bra::TuckerConfig,
fock_ket::FockConfig, ket::TuckerConfig,
coeffs_bra::Tucker{T,N,R}, coeffs_ket::Tucker{T,N,R},
scr_f::Vector{Vector{T}};
cache=false ) where {T,N,R, C<:ClusteredTerm}
#={{{=#
check_term(term, fock_bra, bra, fock_ket, ket) || throw(Exception)
#
# determine sign from rearranging clusters if odd number of operators
state_sign = compute_terms_state_sign(term, fock_ket)
# todo: add in 2e integral tucker decomposition and compress gamma along 1st index first
op = Array{T}[]
cache_key = OperatorConfig((fock_bra, fock_ket, bra, ket))
#if cache && haskey(term.cache, cache_key)
if cache
#
# read the dense H term
op = term.cache[cache_key]
else
#cache == false || println(" couldn't find:", cache_key)
#
# build the dense H term
op = build_dense_H_term(term, cluster_ops, fock_bra, bra, coeffs_bra, fock_ket, ket, coeffs_ket, scr_f)
#if term isa ClusteredTerm4B
# @btime op = build_dense_H_term($term, $cluster_ops, $fock_bra, $bra, $coeffs_bra, $fock_ket, $ket, $coeffs_ket, $scr_f)
# error("please stop")
#end
#if cache
# term.cache[cache_key] = op
#end
end
#if term isa ClusteredTerm2B
# display(term)
# @btime contract_dense_H_with_state($term, $op, $state_sign, $coeffs_bra, $coeffs_ket)
# @btime contract_dense_H_with_state_tensor($term, $op, $state_sign, $coeffs_bra, $coeffs_ket)
# @btime contract_dense_H_with_state_ncon($term, $op, $state_sign, $coeffs_bra, $coeffs_ket)
# #error("please stop")
#end
return contract_dense_H_with_state(term, op, state_sign, coeffs_bra, coeffs_ket)
#return contract_dense_H_with_state_tensor(term, op, state_sign, coeffs_bra, coeffs_ket)
#return contract_dense_H_with_state_ncon(term, op, state_sign, coeffs_bra, coeffs_ket)
end
#=}}}=#
#
# form_sigma_block computes the action of the term on a Tucker compressed state,
# projected into the space defined by bra. This is used to work with H within a subspace defined by a compression
#
#
function form_sigma_block!(term::C,
cluster_ops::Vector{ClusterOps{T}},
fock_bra::FockConfig, bra::TuckerConfig,
fock_ket::FockConfig, ket::TuckerConfig,
coeffs_bra::Tucker{T,N,R}, coeffs_ket::Tucker{T,N,R};
cache=false ) where {T,N,R, C<:ClusteredTerm}
#={{{=#
check_term(term, fock_bra, bra, fock_ket, ket) || throw(Exception)
#
# determine sign from rearranging clusters if odd number of operators
state_sign = compute_terms_state_sign(term, fock_ket)
# todo: add in 2e integral tucker decomposition and compress gamma along 1st index first
op = Array{T}[]
cache_key = OperatorConfig((fock_bra, fock_ket, bra, ket))
#if cache && haskey(term.cache, cache_key)
if cache
#
# read the dense H term
op = term.cache[cache_key]
else
#cache == false || println(" couldn't find:", cache_key)
#
# build the dense H term
op = build_dense_H_term(term, cluster_ops, fock_bra, bra, coeffs_bra, fock_ket, ket, coeffs_ket)
#if term isa ClusteredTerm4B
# @btime op = build_dense_H_term($term, $cluster_ops, $fock_bra, $bra, $coeffs_bra, $fock_ket, $ket, $coeffs_ket, $scr_f)
# error("please stop")
#end
#if cache
# term.cache[cache_key] = op
#end
end
#if term isa ClusteredTerm2B
# display(term)
# @btime contract_dense_H_with_state($term, $op, $state_sign, $coeffs_bra, $coeffs_ket)
# @btime contract_dense_H_with_state_tensor($term, $op, $state_sign, $coeffs_bra, $coeffs_ket)
# @btime contract_dense_H_with_state_ncon($term, $op, $state_sign, $coeffs_bra, $coeffs_ket)
# #error("please stop")
#end
return contract_dense_H_with_state(term, op, state_sign, coeffs_bra, coeffs_ket)
#return contract_dense_H_with_state_tensor(term, op, state_sign, coeffs_bra, coeffs_ket)
#return contract_dense_H_with_state_ncon(term, op, state_sign, coeffs_bra, coeffs_ket)
end
#=}}}=#
function _compress_local_operator(gamma, Ul::Matrix{T}, Ur::Matrix{T}) where T
# this is way slower than @tensor
#={{{=#
# gamma has 3 indices (orbital indices, cluster indices (left), cluster indices (right)
#
# out(i,jp) = gamma(p,I,J) Ul(I,i)
out = Ul' * reshape(permutedims(gamma, [2,3,1]), size(gamma,2), size(gamma,3)*size(gamma,1))
#
# out(j,pi) = out(J,pi) Ur(J,j)
out = Ur' * reshape(out', size(gamma,3), size(gamma,1)*size(Ul,2))
# out(j,pi) -> out(p,i,j)
return reshape(out', size(gamma,1), size(Ul,2), size(Ur,2))
# # out(i,pJ) = gamma(I,pJ) U(I,i)
# out = Ul' * unfold(gamma, 2)
# # out(ip,J)
# out = reshape(out, size(out,1) * size(gamma,1), size(gamma,3))
#
# # out(ip,j) = gamma(ip,J) U(J,j)
# out = out * Ur
# # out(i,p,j)
# out = reshape(out, size(Ul,2), size(gamma,1), size(Ur,2))
#
# # out(p,i,j)
# return permutedims(out, [2,1,3])
end
#=}}}=#
function calc_bound(term::ClusteredTerm1B,
cluster_ops::Vector{ClusterOps{T}},
fock_bra::FockConfig, bra::TuckerConfig,
fock_ket::FockConfig, ket::TuckerConfig,
coeffs_ket::Tucker{T,N,R};
prescreen=1e-4) where {T,N,R}
c1 = term.clusters[1]
bound1 = norm(term.ints)*norm(coeffs_ket.core)
if bound1 < prescreen
return false
end
return true
end
function calc_bound(term::ClusteredTerm2B,
cluster_ops::Vector{ClusterOps{T}},
fock_bra::FockConfig, bra::TuckerConfig,
fock_ket::FockConfig, ket::TuckerConfig,
coeffs_ket::Tucker{T,N,R};
prescreen=1e-4) where {T,N,R}
c1 = term.clusters[1]
c2 = term.clusters[2]
#@views gamma1 = cluster_ops[c1.idx][term.ops[1]][(fock_bra[c1.idx],fock_ket[c1.idx])][:,bra[c1.idx],ket[c1.idx]]
#@views gamma2 = cluster_ops[c2.idx][term.ops[2]][(fock_bra[c2.idx],fock_ket[c2.idx])][:,bra[c2.idx],ket[c2.idx]]
bound1 = norm(term.ints)*norm(coeffs_ket.core)
#bound1 *= norm(gamma1)*norm(gamma2)
bound1 *= norm(coeffs_ket.factors[c1.idx])*norm(coeffs_ket.factors[c2.idx])
if bound1 < prescreen
return false
end
return true
end
function calc_bound(term::ClusteredTerm3B,
cluster_ops::Vector{ClusterOps{T}},
fock_bra::FockConfig, bra::TuckerConfig,
fock_ket::FockConfig, ket::TuckerConfig,
coeffs_ket::Tucker{T,N,R};
prescreen=1e-4) where {T,N,R}
c1 = term.clusters[1]
c2 = term.clusters[2]
c3 = term.clusters[3]
@views gamma1 = cluster_ops[c1.idx][term.ops[1]][(fock_bra[c1.idx],fock_ket[c1.idx])][:,bra[c1.idx],ket[c1.idx]]
@views gamma2 = cluster_ops[c2.idx][term.ops[2]][(fock_bra[c2.idx],fock_ket[c2.idx])][:,bra[c2.idx],ket[c2.idx]]
@views gamma3 = cluster_ops[c3.idx][term.ops[3]][(fock_bra[c3.idx],fock_ket[c3.idx])][:,bra[c3.idx],ket[c3.idx]]
bound1 = norm(term.ints)*norm(coeffs_ket.core)
#bound1 *= norm(gamma1)*norm(gamma2)*norm(gamma3)
bound1 *= norm(coeffs_ket.factors[c1.idx])*norm(coeffs_ket.factors[c2.idx])*norm(coeffs_ket.factors[c3.idx])
if bound1 < prescreen
return false
end
return true
end
function calc_bound(term::ClusteredTerm4B,
cluster_ops::Vector{ClusterOps{T}},
fock_bra::FockConfig, bra::TuckerConfig,
fock_ket::FockConfig, ket::TuckerConfig,
coeffs_ket::Tucker{T,N,R};
prescreen=1e-4) where {T,N,R}
c1 = term.clusters[1]
c2 = term.clusters[2]
c3 = term.clusters[3]
c4 = term.clusters[4]
@views gamma1 = cluster_ops[c1.idx][term.ops[1]][(fock_bra[c1.idx],fock_ket[c1.idx])][:,bra[c1.idx],ket[c1.idx]]
@views gamma2 = cluster_ops[c2.idx][term.ops[2]][(fock_bra[c2.idx],fock_ket[c2.idx])][:,bra[c2.idx],ket[c2.idx]]
@views gamma3 = cluster_ops[c3.idx][term.ops[3]][(fock_bra[c3.idx],fock_ket[c3.idx])][:,bra[c3.idx],ket[c3.idx]]
@views gamma4 = cluster_ops[c4.idx][term.ops[4]][(fock_bra[c4.idx],fock_ket[c4.idx])][:,bra[c4.idx],ket[c4.idx]]
bound1 = norm(term.ints)*norm(coeffs_ket.core)
#bound1 *= norm(gamma1)*norm(gamma2)*norm(gamma3)*norm(gamma4)
bound1 *= norm(coeffs_ket.factors[c1.idx])*norm(coeffs_ket.factors[c2.idx])*norm(coeffs_ket.factors[c3.idx])*norm(coeffs_ket.factors[c4.idx])
if bound1 < prescreen
return false
end
return true
end
function Base.copyto!(a::Tuple{Array{T,N}}, b) where {T,N}
length(a) == length(b) || throw(DimensionMismatch)
for i in 1:length(a)
a[i] .= b[i]
end
end