-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathlocaloperator.jl
More file actions
565 lines (499 loc) · 22.3 KB
/
localoperator.jl
File metadata and controls
565 lines (499 loc) · 22.3 KB
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# Contraction of local operators on arbitrary lattice locations
# -------------------------------------------------------------
import MPSKit: tensorexpr
# currently need this because MPSKit restricts tensor names to symbols
_totuple(t) = t isa Tuple ? t : tuple(t)
MPSKit.tensorexpr(ex::Expr, inds::Tuple) = Expr(:ref, ex, _totuple(inds)...)
function MPSKit.tensorexpr(ex::Expr, indout, indin)
return Expr(
:typed_vcat, ex, Expr(:row, _totuple(indout)...), Expr(:row, _totuple(indin)...)
)
end
function tensorlabel(args...)
return Symbol(ntuple(i -> iseven(i) ? :_ : args[(i + 1) >> 1], 2 * length(args) - 1)...)
end
envlabel(args...) = tensorlabel(:χ, args...)
virtuallabel(args...) = tensorlabel(:D, args...)
physicallabel(args...) = tensorlabel(:d, args...)
"""
$(SIGNATURES)
Contract a local operator `O` on the PEPS `peps` at the indices `inds` using the environment `env`.
This works by generating the appropriate contraction on a rectangular patch with its corners
specified by `inds`. The `peps` is contracted with `O` from above and below, and the PEPS-operator
sandwich is surrounded with the appropriate environment tensors.
"""
function contract_local_operator(
inds::NTuple{N, CartesianIndex{2}},
O::AbstractTensorMap{T, S, N, N},
ket::InfinitePEPS, bra::InfinitePEPS,
env::CTMRGEnv,
) where {T, S, N}
static_inds = Val.(inds)
return _contract_local_operator(static_inds, O, ket, bra, env)
end
function contract_local_operator(
inds::NTuple{N, Tuple{Int, Int}},
O::AbstractTensorMap{T, S, N, N},
ket::InfinitePEPS, bra::InfinitePEPS,
env::CTMRGEnv,
) where {T, S, N}
return contract_local_operator(CartesianIndex.(inds), O, ket, bra, env)
end
# This implements the contraction of an operator acting on sites `inds`.
# The generated function ensures that we can use @tensor to write dynamic contractions (and maximize performance).
function _contract_corner_expr(rowrange, colrange)
rmin, rmax = extrema(rowrange)
cmin, cmax = extrema(colrange)
gridsize = (rmax - rmin + 1, cmax - cmin + 1)
C_NW = :(env.corners[NORTHWEST, mod1($(rmin - 1), end), mod1($(cmin - 1), end)])
corner_NW = tensorexpr(C_NW, envlabel(WEST, 0), envlabel(NORTH, 0))
C_NE = :(env.corners[NORTHEAST, mod1($(rmin - 1), end), mod1($(cmax + 1), end)])
corner_NE = tensorexpr(C_NE, envlabel(NORTH, gridsize[2]), envlabel(EAST, 0))
C_SE = :(env.corners[SOUTHEAST, mod1($(rmax + 1), end), mod1($(cmax + 1), end)])
corner_SE = tensorexpr(C_SE, envlabel(EAST, gridsize[1]), envlabel(SOUTH, gridsize[2]))
C_SW = :(env.corners[SOUTHWEST, mod1($(rmax + 1), end), mod1($(cmin - 1), end)])
corner_SW = tensorexpr(C_SW, envlabel(SOUTH, 0), envlabel(WEST, gridsize[1]))
return corner_NW, corner_NE, corner_SE, corner_SW
end
function _contract_edge_expr(rowrange, colrange)
rmin, rmax = extrema(rowrange)
cmin, cmax = extrema(colrange)
gridsize = (rmax - rmin + 1, cmax - cmin + 1)
edges_N = map(1:gridsize[2]) do i
E_N = :(env.edges[NORTH, mod1($(rmin - 1), end), mod1($(cmin + i - 1), end)])
return tensorexpr(
E_N,
(
envlabel(NORTH, i - 1),
virtuallabel(NORTH, :ket, i),
virtuallabel(NORTH, :bra, i),
),
envlabel(NORTH, i),
)
end
edges_E = map(1:gridsize[1]) do i
E_E = :(env.edges[EAST, mod1($(rmin + i - 1), end), mod1($(cmax + 1), end)])
return tensorexpr(
E_E,
(
envlabel(EAST, i - 1),
virtuallabel(EAST, :ket, i),
virtuallabel(EAST, :bra, i),
),
envlabel(EAST, i),
)
end
edges_S = map(1:gridsize[2]) do i
E_S = :(env.edges[SOUTH, mod1($(rmax + 1), end), mod1($(cmin + i - 1), end)])
return tensorexpr(
E_S,
(
envlabel(SOUTH, i),
virtuallabel(SOUTH, :ket, i),
virtuallabel(SOUTH, :bra, i),
),
envlabel(SOUTH, i - 1),
)
end
edges_W = map(1:gridsize[1]) do i
E_W = :(env.edges[WEST, mod1($(rmin + i - 1), end), mod1($(cmin - 1), end)])
return tensorexpr(
E_W,
(envlabel(WEST, i), virtuallabel(WEST, :ket, i), virtuallabel(WEST, :bra, i)),
envlabel(WEST, i - 1),
)
end
return edges_N, edges_E, edges_S, edges_W
end
function _contract_state_expr(rowrange, colrange, cartesian_inds = nothing)
rmin, rmax = extrema(rowrange)
cmin, cmax = extrema(colrange)
gridsize = (rmax - rmin + 1, cmax - cmin + 1)
return map((:bra, :ket)) do side
return map(Iterators.product(1:gridsize[1], 1:gridsize[2])) do (i, j)
inds_id = if isnothing(cartesian_inds)
nothing
else
findfirst(==(CartesianIndex(rmin + i - 1, cmin + j - 1)), cartesian_inds)
end
physical_label = if isnothing(inds_id)
physicallabel(i, j)
else
physicallabel(:O, side, inds_id)
end
return tensorexpr(
:($(side)[mod1($(rmin + i - 1), end), mod1($(cmin + j - 1), end)]),
(physical_label,),
(
if i == 1
virtuallabel(NORTH, side, j)
else
virtuallabel(:vertical, side, i - 1, j)
end,
if j == gridsize[2]
virtuallabel(EAST, side, i)
else
virtuallabel(:horizontal, side, i, j)
end,
if i == gridsize[1]
virtuallabel(SOUTH, side, j)
else
virtuallabel(:vertical, side, i, j)
end,
if j == 1
virtuallabel(WEST, side, i)
else
virtuallabel(:horizontal, side, i, j - 1)
end,
),
)
end
end
end
function _contract_pepo_state_expr(rowrange, colrange, cartesian_inds = nothing)
rmin, rmax = extrema(rowrange)
cmin, cmax = extrema(colrange)
gridsize = (rmax - rmin + 1, cmax - cmin + 1)
return map((:bra, :ket)) do side
return map(Iterators.product(1:gridsize[1], 1:gridsize[2])) do (i, j)
inds_id = if isnothing(cartesian_inds)
nothing
else
findfirst(==(CartesianIndex(rmin + i - 1, cmin + j - 1)), cartesian_inds)
end
physical_label_in = if isnothing(inds_id)
physicallabel(:in, i, j)
else
physicallabel(:Oopen, inds_id)
end
physical_label_out = if isnothing(inds_id)
physicallabel(:out, i, j)
else
physicallabel(:O, side, inds_id)
end
return tensorexpr(
if side == :ket
:(twistdual(ket[mod1($(rmin + i - 1), end), mod1($(cmin + j - 1), end)], (1, 2)))
else
:(bra[mod1($(rmin + i - 1), end), mod1($(cmin + j - 1), end)])
end,
(physical_label_out, physical_label_in),
(
if i == 1
virtuallabel(NORTH, side, j)
else
virtuallabel(:vertical, side, i - 1, j)
end,
if j == gridsize[2]
virtuallabel(EAST, side, i)
else
virtuallabel(:horizontal, side, i, j)
end,
if i == gridsize[1]
virtuallabel(SOUTH, side, j)
else
virtuallabel(:vertical, side, i, j)
end,
if j == 1
virtuallabel(WEST, side, i)
else
virtuallabel(:horizontal, side, i, j - 1)
end,
),
)
end
end
end
@generated function _contract_local_operator(
inds::NTuple{N, Val},
O::AbstractTensorMap{T, S, N, N},
ket::InfinitePEPS, bra::InfinitePEPS,
env::CTMRGEnv,
) where {T, S, N}
cartesian_inds = collect(CartesianIndex{2}, map(x -> x.parameters[1], inds.parameters)) # weird hack to extract information from Val
allunique(cartesian_inds) ||
throw(ArgumentError("Indices should not overlap: $cartesian_inds."))
rowrange = getindex.(cartesian_inds, 1)
colrange = getindex.(cartesian_inds, 2)
corner_NW, corner_NE, corner_SE, corner_SW = _contract_corner_expr(rowrange, colrange)
edges_N, edges_E, edges_S, edges_W = _contract_edge_expr(rowrange, colrange)
operator = tensorexpr(
:O,
ntuple(i -> physicallabel(:O, :bra, i), N),
ntuple(i -> physicallabel(:O, :ket, i), N),
)
bra, ket = _contract_state_expr(rowrange, colrange, cartesian_inds)
multiplication_ex = Expr(
:call, :*,
corner_NW, corner_NE, corner_SE, corner_SW,
edges_N..., edges_E..., edges_S..., edges_W...,
ket..., map(x -> Expr(:call, :conj, x), bra)...,
operator,
)
returnex = quote
@autoopt @tensor $multiplication_ex
end
return macroexpand(@__MODULE__, returnex)
end
"""
$(SIGNATURES)
Contract a local norm of the PEPS `peps` around indices `inds`.
This works analogously to [`contract_local_operator`](@ref) by generating the contraction
on a rectangular patch based on `inds` but replacing the operator with an identity such
that the PEPS norm is computed. (Note that this is not the physical norm of the state.)
"""
function contract_local_norm(
inds::NTuple{N, CartesianIndex{2}}, ket::InfinitePEPS, bra::InfinitePEPS, env::CTMRGEnv
) where {N}
static_inds = Val.(inds)
return _contract_local_norm(static_inds, ket, bra, env)
end
function contract_local_norm(
inds::NTuple{N, Tuple{Int, Int}}, ket::InfinitePEPS, bra::InfinitePEPS, env::CTMRGEnv
) where {N}
return contract_local_norm(CartesianIndex.(inds), ket, bra, env)
end
@generated function _contract_local_norm(
inds::NTuple{N, Val}, ket::InfinitePEPS, bra::InfinitePEPS, env::CTMRGEnv
) where {N}
cartesian_inds = collect(CartesianIndex{2}, map(x -> x.parameters[1], inds.parameters)) # weird hack to extract information from Val
allunique(cartesian_inds) || throw(ArgumentError("Indices should not overlap: $cartesian_inds."))
rowrange = getindex.(cartesian_inds, 1)
colrange = getindex.(cartesian_inds, 2)
corner_NW, corner_NE, corner_SE, corner_SW = _contract_corner_expr(rowrange, colrange)
edges_N, edges_E, edges_S, edges_W = _contract_edge_expr(rowrange, colrange)
bra, ket = _contract_state_expr(rowrange, colrange)
multiplication_ex = Expr(
:call, :*,
corner_NW, corner_NE, corner_SE, corner_SW,
edges_N..., edges_E..., edges_S..., edges_W...,
ket..., map(x -> Expr(:call, :conj, x), bra)...,
)
returnex = quote
@autoopt @tensor $multiplication_ex
end
return macroexpand(@__MODULE__, returnex)
end
@doc """
$(SIGNATURES)
Construct the reduced density matrix `ρ` of the PEPS `peps` with open indices `inds` using the environment `env`.
Alternatively, construct the reduced density matrix `ρ` of a full density matrix PEPO with open indices `inds` using the environment `env`.
This works by generating the appropriate contraction on a rectangular patch with its corners
specified by `inds`. The result is normalized such that `tr(ρ) = 1`.
""" reduced_densitymatrix
function reduced_densitymatrix(
inds::NTuple{N, CartesianIndex{2}}, ket::InfinitePEPS, bra::InfinitePEPS, env::CTMRGEnv
) where {N}
static_inds = Val.(inds)
return _contract_densitymatrix(static_inds, ket, bra, env)
end
function reduced_densitymatrix(
inds::NTuple{N, CartesianIndex{2}}, ket::InfinitePEPO, bra::InfinitePEPO, env::CTMRGEnv
) where {N}
size(ket) == size(bra) || throw(DimensionMismatch("incompatible bra and ket dimensions"))
size(ket, 3) == 1 || throw(DimensionMismatch("only single-layer densitymatrices are supported"))
static_inds = Val.(inds)
return _contract_densitymatrix(static_inds, ket, bra, env)
end
function reduced_densitymatrix(
inds::NTuple{N, Tuple{Int, Int}}, ket::InfinitePEPS, bra::InfinitePEPS, env::CTMRGEnv
) where {N}
return reduced_densitymatrix(CartesianIndex.(inds), ket, bra, env)
end
function reduced_densitymatrix(
inds::NTuple{N, Tuple{Int, Int}}, ket::InfinitePEPO, bra::InfinitePEPO, env::CTMRGEnv
) where {N}
return reduced_densitymatrix(CartesianIndex.(inds), ket, bra, env)
end
function reduced_densitymatrix(inds, ket::InfinitePEPS, env::CTMRGEnv)
return reduced_densitymatrix(inds, ket, ket, env)
end
# Special case 1x1 density matrix:
# Keep contraction order but try to optimize intermediate permutations:
# EE_SWA is largest object so keep largest legs to the front there
function reduced_densitymatrix(
inds::Tuple{CartesianIndex{2}}, ket::InfinitePEPS, bra::InfinitePEPS, env::CTMRGEnv
)
row, col = Tuple(inds[1])
# Unpack variables and absorb corners
A = ket[mod1(row, end), mod1(col, end)]
Ā = bra[mod1(row, end), mod1(col, end)]
E_north =
env.edges[NORTH, mod1(row - 1, end), mod1(col, end)] *
twistdual(env.corners[NORTHEAST, mod1(row - 1, end), mod1(col + 1, end)], 1)
E_east =
env.edges[EAST, mod1(row, end), mod1(col + 1, end)] *
twistdual(env.corners[SOUTHEAST, mod1(row + 1, end), mod1(col + 1, end)], 1)
E_south =
env.edges[SOUTH, mod1(row + 1, end), mod1(col, end)] *
twistdual(env.corners[SOUTHWEST, mod1(row + 1, end), mod1(col - 1, end)], 1)
E_west =
env.edges[WEST, mod1(row, end), mod1(col - 1, end)] *
twistdual(env.corners[NORTHWEST, mod1(row - 1, end), mod1(col - 1, end)], 1)
@tensor EE_SW[χSE χNW DSb DWb; DSt DWt] :=
E_south[χSE DSt DSb; χSW] * E_west[χSW DWt DWb; χNW]
@tensor EE_SWA[χSE χNW DNt DEt; dt DSb DWb] :=
EE_SW[χSE χNW DSb DWb; DSt DWt] * A[dt; DNt DEt DSt DWt]
@tensor EE_NE[DNb DEb; χSE χNW DNt DEt] :=
E_north[χNW DNt DNb; χNE] * E_east[χNE DEt DEb; χSE]
@tensor EEAEE[dt; DNb DEb DSb DWb] :=
EE_NE[DNb DEb; χSE χNW DNt DEt] * EE_SWA[χSE χNW DNt DEt; dt DSb DWb]
@tensor ρ[dt; db] := EEAEE[dt; DNb DEb DSb DWb] * conj(Ā[db; DNb DEb DSb DWb])
return ρ / str(ρ)
end
function reduced_densitymatrix(
inds::NTuple{2, CartesianIndex{2}}, ket::InfinitePEPS, bra::InfinitePEPS, env::CTMRGEnv
)
if inds[2] - inds[1] == CartesianIndex(1, 0)
return reduced_densitymatrix2x1(inds[1], ket, bra, env)
elseif inds[2] - inds[1] == CartesianIndex(0, 1)
return reduced_densitymatrix1x2(inds[1], ket, bra, env)
else
static_inds = Val.(inds)
return _contract_densitymatrix(static_inds, ket, bra, env)
end
end
# Special case 2x1 density matrix:
# Keep contraction order but try to optimize intermediate permutations:
function reduced_densitymatrix2x1(
ind::CartesianIndex, ket::InfinitePEPS, bra::InfinitePEPS, env::CTMRGEnv
)
row, col = Tuple(ind)
# Unpack variables and absorb corners
A_north = ket[mod1(row, end), mod1(col, end)]
Ā_north = bra[mod1(row, end), mod1(col, end)]
A_south = ket[mod1(row + 1, end), mod1(col, end)]
Ā_south = bra[mod1(row + 1, end), mod1(col, end)]
E_north =
env.edges[NORTH, mod1(row - 1, end), mod1(col, end)] *
twistdual(env.corners[NORTHEAST, mod1(row - 1, end), mod1(col + 1, end)], 1)
E_northeast = env.edges[EAST, mod1(row, end), mod1(col + 1, end)]
E_southeast =
env.edges[EAST, mod1(row + 1, end), mod1(col + 1, end)] *
twistdual(env.corners[SOUTHEAST, mod1(row + 2, end), mod1(col + 1, end)], 1)
E_south =
env.edges[SOUTH, mod1(row + 2, end), mod1(col, end)] *
twistdual(env.corners[SOUTHWEST, mod1(row + 2, end), mod1(col - 1, end)], 1)
E_southwest = env.edges[WEST, mod1(row + 1, end), mod1(col - 1, end)]
E_northwest =
env.edges[WEST, mod1(row, end), mod1(col - 1, end)] *
twistdual(env.corners[NORTHWEST, mod1(row - 1, end), mod1(col - 1, end)], 1)
@tensor EE_NW[χW χNE DNWt DNt; DNWb DNb] :=
E_northwest[χW DNWt DNWb; χNW] * E_north[χNW DNt DNb; χNE]
@tensor EEA_NW[χW DMb dNb χNE DNEb; DNWt DNt] :=
EE_NW[χW χNE DNWt DNt; DNWb DNb] * conj(Ā_north[dNb; DNb DNEb DMb DNWb])
@tensor EEAA_NW[χW DMb dNb dNt DMt; χNE DNEt DNEb] :=
EEA_NW[χW DMb dNb χNE DNEb; DNWt DNt] * A_north[dNt; DNt DNEt DMt DNWt]
@tensor EEEAA_N[dNt dNb; χW DMt DMb χE] :=
EEAA_NW[χW DMb dNb dNt DMt; χNE DNEt DNEb] * E_northeast[χNE DNEt DNEb; χE]
@tensor EE_SE[χE χSW DSEt DSt; DSEb DSb] :=
E_southeast[χE DSEt DSEb; χSE] * E_south[χSE DSt DSb; χSW]
@tensor EEA_SE[χE DMb dSb χSW DSWb; DSEt DSt] :=
EE_SE[χE χSW DSEt DSt; DSEb DSb] * conj(Ā_south[dSb; DMb DSEb DSb DSWb])
@tensor EEAA_SE[χE DMb dSb dSt DMt; χSW DSWt DSWb] :=
EEA_SE[χE DMb dSb χSW DSWb; DSEt DSt] * A_south[dSt; DMt DSEt DSt DSWt]
@tensor EEEAA_S[χW DMt DMb χE; dSt dSb] :=
EEAA_SE[χE DMb dSb dSt DMt; χSW DSWt DSWb] * E_southwest[χSW DSWt DSWb; χW]
@tensor ρ[dNt dSt; dNb dSb] :=
EEEAA_N[dNt dNb; χW DMt DMb χE] * EEEAA_S[χW DMt DMb χE; dSt dSb]
return ρ / str(ρ)
end
function reduced_densitymatrix1x2(
ind::CartesianIndex, ket::InfinitePEPS, bra::InfinitePEPS, env::CTMRGEnv
)
row, col = Tuple(ind)
# Unpack variables and absorb corners
A_west = ket[mod1(row, end), mod1(col, end)]
Ā_west = bra[mod1(row, end), mod1(col, end)]
A_east = ket[mod1(row, end), mod1(col + 1, end)]
Ā_east = bra[mod1(row, end), mod1(col + 1, end)]
E_northwest = env.edges[NORTH, mod1(row - 1, end), mod1(col, end)]
E_northeast =
env.edges[NORTH, mod1(row - 1, end), mod1(col + 1, end)] *
twistdual(env.corners[NORTHEAST, mod1(row - 1, end), mod1(col + 2, end)], 1)
E_east =
env.edges[EAST, mod1(row, end), mod1(col + 2, end)] *
twistdual(env.corners[SOUTHEAST, mod1(row + 1, end), mod1(col + 2, end)], 1)
E_southeast = env.edges[SOUTH, mod1(row + 1, end), mod1(col + 1, end)]
E_southwest =
env.edges[SOUTH, mod1(row + 1, end), mod1(col, end)] *
twistdual(env.corners[SOUTHWEST, mod1(row + 1, end), mod1(col - 1, end)], 1)
E_west =
env.edges[WEST, mod1(row, end), mod1(col - 1, end)] *
twistdual(env.corners[NORTHWEST, mod1(row - 1, end), mod1(col - 1, end)], 1)
@tensor EE_SW[χS χNW DSWt DWt; DSWb DWb] :=
E_southwest[χS DSWt DSWb; χSW] * E_west[χSW DWt DWb; χNW]
@tensor EEA_SW[χS DMb dWb χNW DNWb; DSWt DWt] :=
EE_SW[χS χNW DSWt DWt; DSWb DWb] * conj(Ā_west[dWb; DNWb DMb DSWb DWb])
@tensor EEAA_SW[χS DMb dWb dWt DMt; χNW DNWt DNWb] :=
EEA_SW[χS DMb dWb χNW DNWb; DSWt DWt] * A_west[dWt; DNWt DMt DSWt DWt]
@tensor EEEAA_W[dWt dWb; χS DMt DMb χN] :=
EEAA_SW[χS DMb dWb dWt DMt; χNW DNWt DNWb] * E_northwest[χNW DNWt DNWb; χN]
@tensor EE_NE[χN χSE DNEt DEt; DNEb DEb] :=
E_northeast[χN DNEt DNEb; χNE] * E_east[χNE DEt DEb; χSE]
@tensor EEA_NE[χN DMb dEb χSE DSEb; DNEt DEt] :=
EE_NE[χN χSE DNEt DEt; DNEb DEb] * conj(Ā_east[dEb; DNEb DEb DSEb DMb])
@tensor EEAA_NE[χN DMb dEb dEt DMt; χSE DSEt DSEb] :=
EEA_NE[χN DMb dEb χSE DSEb; DNEt DEt] * A_east[dEt; DNEt DEt DSEt DMt]
@tensor EEEAA_E[χS DMt DMb χN; dEt dEb] :=
EEAA_NE[χN DMb dEb dEt DMt; χSE DSEt DSEb] * E_southeast[χSE DSEt DSEb; χS]
@tensor ρ[dWt dEt; dWb dEb] :=
EEEAA_W[dWt dWb; χS DMt DMb χN] * EEEAA_E[χS DMt DMb χN; dEt dEb]
return ρ / str(ρ)
end
@generated function _contract_densitymatrix(
inds::NTuple{N, Val}, ket::InfinitePEPS, bra::InfinitePEPS, env::CTMRGEnv
) where {N}
cartesian_inds = collect(CartesianIndex{2}, map(x -> x.parameters[1], inds.parameters)) # weird hack to extract information from Val
allunique(cartesian_inds) ||
throw(ArgumentError("Indices should not overlap: $cartesian_inds."))
rowrange = getindex.(cartesian_inds, 1)
colrange = getindex.(cartesian_inds, 2)
corner_NW, corner_NE, corner_SE, corner_SW = _contract_corner_expr(rowrange, colrange)
edges_N, edges_E, edges_S, edges_W = _contract_edge_expr(rowrange, colrange)
result = tensorexpr(
:ρ,
ntuple(i -> physicallabel(:O, :ket, i), N),
ntuple(i -> physicallabel(:O, :bra, i), N),
)
bra, ket = _contract_state_expr(rowrange, colrange, cartesian_inds)
multiplication_ex = Expr(
:call, :*,
corner_NW, corner_NE, corner_SE, corner_SW,
edges_N..., edges_E..., edges_S..., edges_W...,
ket..., map(x -> Expr(:call, :conj, x), bra)...,
)
multex = :(@autoopt @tensor $result := $multiplication_ex)
return quote
$(macroexpand(@__MODULE__, multex))
return ρ / str(ρ)
end
end
@generated function _contract_densitymatrix(
inds::NTuple{N, Val}, ket::InfinitePEPO, bra::InfinitePEPO, env::CTMRGEnv
) where {N}
cartesian_inds = collect(CartesianIndex{2}, map(x -> x.parameters[1], inds.parameters)) # weird hack to extract information from Val
allunique(cartesian_inds) ||
throw(ArgumentError("Indices should not overlap: $cartesian_inds."))
rowrange = getindex.(cartesian_inds, 1)
colrange = getindex.(cartesian_inds, 2)
corner_NW, corner_NE, corner_SE, corner_SW = _contract_corner_expr(rowrange, colrange)
edges_N, edges_E, edges_S, edges_W = _contract_edge_expr(rowrange, colrange)
result = tensorexpr(
:ρ,
ntuple(i -> physicallabel(:O, :ket, i), N),
ntuple(i -> physicallabel(:O, :bra, i), N),
)
bra, ket = _contract_pepo_state_expr(rowrange, colrange, cartesian_inds)
multiplication_ex = Expr(
:call, :*,
corner_NW, corner_NE, corner_SE, corner_SW,
edges_N..., edges_E..., edges_S..., edges_W...,
ket..., map(x -> Expr(:call, :conj, x), bra)...,
)
multex = :(@autoopt @tensor contractcheck = true $result := $multiplication_ex)
return quote
$(macroexpand(@__MODULE__, multex))
return ρ / str(ρ)
end
end