-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase_utils.jl
More file actions
173 lines (144 loc) · 3.8 KB
/
Copy pathbase_utils.jl
File metadata and controls
173 lines (144 loc) · 3.8 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
# Author: V. Vitale
# Feb 2022
# Conctraction utilities
include("mps.jl")
include("mpo.jl")
## initial E and F matrices for the left and right vacuum states
function initial_L(W::Array)
sW=size(W)
L = ones(1,sW[1],1)#ones(sW[1],1,1)
return L
end
function initial_R(W::Array)
sW=size(W)
R = ones(1,sW[2],1)#ones(sW[2],1,1)
return R
end
function construct_R(A::MPS, W::MPO)
R = Dict()
R[A.N] = initial_R(W.data[A.N])
for i in A.N:-1:1
R[i-1] = contract_from_right(R[i], A.data[i], W.data[i])
end
return R
end
function construct_L(A::MPS, W::MPO)
L = Dict()
L[1] = initial_L(W.data[1])
return L
end
## tensor contraction from the left hand side
## +- +--X-
## | | |
## L' = L--W-
## | | |
## +- +--X-
function contract_from_left(E_L::Array,X::Array,W::Array)
@tensor temp_1[:] := E_L[1,-3,-4] * X[1,-2,-1]
@tensor temp_2[:] := temp_1[-1, -2, -5, 1 ]* conj( X[1, -4,-3] )
@tensor temp[:] := temp_2[ -1, 2, -3, 3, 1 ] * W[1,-2, 2, 3]
return temp
end
## tensor contraction from the right hand side
## -+ -Y--+
## | | |
## -R' = -W--R
## | | |
## -+ -Y--+
function contract_from_right(E_R::Array,Y::Array,W::Array)
@tensor temp_1[:] := Y[-1,-2,1] * E_R[ 1, -3,-4]
@tensor temp_2[:] := temp_1[ -1, -2, -5, 1] * conj( Y[-3,-4,1] )
@tensor temp[:] := temp_2[ -1, 1, -3, 2, 6] * W[ -2 6 1 2 ]
return temp
end
function average(psi::MPS,W::MPO)
sW=size(W.data[1])
L = ones(1,sW[1],1)#ones(sW[1],1,1)
right_normalize!(psi)
for i in 1:psi.N
@tensor L[:] := L[1,2,3]*psi.data[i][1,4,-1]*W.data[i][2,-2,4,5]*conj(psi.data[i][3,5,-3])
end
return L[1,1,1]
end
function contract(Q::MPO,W::MPO)
sW=size(W.data[1])
sQ=size(Q.data[1])
L = ones(sQ[1],sW[1])
for i in 1:Q.N
@tensor L[:] := L[1,2]*Q.data[i][1,-1,4,5]*W.data[i][2,-2,4,5]
end
return L[1,1]
end
function Normalize!(A::MPS)
norm = MPS_dot(A,A)
for i in 1:A.N
A.data[i] = A.data[i]/norm^(1/(2*A.N))
end
end
function MPS_dot(A::MPS,B::MPS)
E=ones(1,1)
for i in 1:A.N
@tensor temp[:] := E[-1,1]*A.data[i][1,-2,-3]
@tensor E[:] := temp[1,2,-2] * conj( B.data[i][1,2,-1] )
end
return E[1]
end
Base.:*(A::MPS,B::MPS)=MPS_dot(A,B)
function MPO_dot(W::MPO,Q::MPO)
if isempty(W.data) || isempty(Q.data)
@warn "Empty MPO."
return 0
end
temp=MPO()
temp.N=W.N
for i in 1:W.N
sW=size(W.data[i])
sQ=size(Q.data[i])
@tensor temp.data[i][:] := W.data[i][-1,-3,-5,1]*Q.data[i][-2,-4,1,-6]
temp.data[i]=reshape(temp.data[i],sW[1]*sQ[1],sW[2]*sQ[2],sW[3],sQ[4])
end
return temp
end
Base.:*(W::MPO,Q::MPO)=MPO_dot(W,Q)
function MPO_MPS_dot(W::MPO,Q::MPS)
if isempty(W.data)
@warn "Empty MPO."
#return 0
end
if isempty(Q.data)
@warn "Empty MPS."
#return 0
end
temp=MPS()
temp.N=W.N
for i in 1:W.N
sW=size(W.data[i])
sQ=size(Q.data[i])
@tensor temp.data[i][:] := W.data[i][-1,-4,-3,1]*Q.data[i][-2,1,-5]
temp.data[i]=reshape(temp.data[i],sW[1]*sQ[1],sW[3],sW[2]*sQ[3])
end
temp.b=Q.b
return temp
end
function MPS_MPO_dot(Q::MPS,W::MPO)
if isempty(W.data)
@warn "Empty MPO."
#return 0
end
if isempty(Q.data)
@warn "Empty MPS."
#return 0
end
temp=MPS()
temp.N=W.N
for i in 1:W.N
sW=size(W.data[i])
sQ=size(Q.data[i])
@tensor temp.data[i][:] := W.data[i][-1,-4,1,-3]*Q.data[i][-2,1,-5]
temp.data[i]=reshape(temp.data[i],sW[1]*sQ[1],sW[3],sW[2]*sQ[3])
end
temp.b=Q.b
return temp
end
Base.:*(W::MPO,A::MPS)=MPO_MPS_dot(W,A)
Base.:*(A::MPS,W::MPO)=MPS_MPO_dot(A,W)