Skip to content

Commit 3243ec8

Browse files
committed
Format
1 parent 5f668b3 commit 3243ec8

3 files changed

Lines changed: 91 additions & 91 deletions

File tree

src/algorithms/bp/beliefpropagation.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
@kwdef struct BeliefPropagation
22
maxiter::Int = 10
3-
tol::Float64 = 1e-6
3+
tol::Float64 = 1.0e-6
44
verbosity::Int = 2
55
end
66

7-
function gauge_fix(psi::InfinitePEPS, alg::BeliefPropagation, env::BPEnv=BPEnv(psi))
7+
function gauge_fix(psi::InfinitePEPS, alg::BeliefPropagation, env::BPEnv = BPEnv(psi))
88
# Compute belief propagation fixed point solutions
99
env, err = bp_fixedpoint(env, InfiniteSquareNetwork(psi), alg)
1010

@@ -130,9 +130,9 @@ function tr_distance(A::BPEnv, B::BPEnv)
130130
end
131131
end
132132

133-
function trnorm(M::AbstractTensorMap, p::Real=1)
133+
function trnorm(M::AbstractTensorMap, p::Real = 1)
134134
return TensorKit._norm(svdvals(M), p, zero(real(scalartype(M))))
135135
end
136-
function trnorm!(M::AbstractTensorMap, p::Real=1)
136+
function trnorm!(M::AbstractTensorMap, p::Real = 1)
137137
return TensorKit._norm(svdvals!(M), p, zero(real(scalartype(M))))
138138
end
Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const PEPSMessage = AbstractTensorMap{<:Any,<:Any,1,1}
1+
const PEPSMessage = AbstractTensorMap{<:Any, <:Any, 1, 1}
22

33
# Belief Propagation Updates
44
# --------------------------
55
function contract_north_message(
6-
A::PEPSSandwich, M_west::PEPSMessage, M_north::PEPSMessage, M_east::PEPSMessage
7-
)
6+
A::PEPSSandwich, M_west::PEPSMessage, M_north::PEPSMessage, M_east::PEPSMessage
7+
)
88
return @autoopt @tensor begin
99
M_north′[DSt; DSb] :=
1010
ket(A)[d; DNt DEt DSt DWt] *
@@ -15,8 +15,8 @@ function contract_north_message(
1515
end
1616
end
1717
function contract_east_message(
18-
A::PEPSSandwich, M_north::PEPSMessage, M_east::PEPSMessage, M_south::PEPSMessage
19-
)
18+
A::PEPSSandwich, M_north::PEPSMessage, M_east::PEPSMessage, M_south::PEPSMessage
19+
)
2020
return @autoopt @tensor begin
2121
M_east′[DWt; DWb] :=
2222
ket(A)[d; DNt DEt DSt DWt] *
@@ -27,8 +27,8 @@ function contract_east_message(
2727
end
2828
end
2929
function contract_south_message(
30-
A::PEPSSandwich, M_east::PEPSMessage, M_south::PEPSMessage, M_west::PEPSMessage
31-
)
30+
A::PEPSSandwich, M_east::PEPSMessage, M_south::PEPSMessage, M_west::PEPSMessage
31+
)
3232
return @autoopt @tensor begin
3333
M_south′[DNt; DNb] :=
3434
ket(A)[d; DNt DEt DSt DWt] *
@@ -39,8 +39,8 @@ function contract_south_message(
3939
end
4040
end
4141
function contract_west_message(
42-
A::PEPSSandwich, M_south::PEPSMessage, M_west::PEPSMessage, M_north::PEPSMessage
43-
)
42+
A::PEPSSandwich, M_south::PEPSMessage, M_west::PEPSMessage, M_north::PEPSMessage
43+
)
4444
return @autoopt @tensor begin
4545
M_west′[DEt; DEb] :=
4646
ket(A)[d; DNt DEt DSt DWt] *
@@ -57,63 +57,63 @@ function MPSKit.expectation_value(peps::InfinitePEPS, O::LocalOperator, env::BPE
5757
checklattice(peps, O)
5858
term_vals = dtmap([O.terms...]) do (inds, operator) # OhMyThreads can't iterate over O.terms directly
5959
contract_local_operator(inds, operator, peps, peps, env) /
60-
contract_local_norm(inds, peps, peps, env)
60+
contract_local_norm(inds, peps, peps, env)
6161
end
6262
return sum(term_vals)
6363
end
6464

6565
function contract_local_operator(
66-
inds::NTuple{1,CartesianIndex{2}},
67-
O::AbstractTensorMap{<:Any,<:Any,1,1},
68-
ket::InfinitePEPS,
69-
bra::InfinitePEPS,
70-
env::BPEnv,
71-
)
66+
inds::NTuple{1, CartesianIndex{2}},
67+
O::AbstractTensorMap{<:Any, <:Any, 1, 1},
68+
ket::InfinitePEPS,
69+
bra::InfinitePEPS,
70+
env::BPEnv,
71+
)
7272
row, col = Tuple(only(inds))
7373
M_north = env.messages[NORTH, _prev(row, end), mod1(col, end)]
7474
M_east = env.messages[EAST, mod1(row, end), _next(col, end)]
7575
M_south = env.messages[SOUTH, _next(row, end), mod1(col, end)]
7676
M_west = env.messages[WEST, mod1(row, end), _prev(col, end)]
7777

78-
@autoopt @tensor begin
78+
return @autoopt @tensor begin
7979
ket[mod1(row, end), mod1(col, end)][dt; DNt DEt DSt DWt] *
80-
conj(bra[mod1(row, end), mod1(col, end)][db; DNb DEb DSb DWb]) *
81-
O[db; dt] *
82-
M_north[DNt; DNb] *
83-
M_east[DEt; DEb] *
84-
M_south[DSt; DSb] *
85-
M_west[DWt; DWb]
80+
conj(bra[mod1(row, end), mod1(col, end)][db; DNb DEb DSb DWb]) *
81+
O[db; dt] *
82+
M_north[DNt; DNb] *
83+
M_east[DEt; DEb] *
84+
M_south[DSt; DSb] *
85+
M_west[DWt; DWb]
8686
end
8787
end
8888

8989
function contract_local_norm(
90-
inds::NTuple{1,CartesianIndex{2}}, ket::InfinitePEPS, bra::InfinitePEPS, env::BPEnv
91-
)
90+
inds::NTuple{1, CartesianIndex{2}}, ket::InfinitePEPS, bra::InfinitePEPS, env::BPEnv
91+
)
9292
row, col = Tuple(only(inds))
9393
M_north = env.messages[NORTH, _prev(row, end), mod1(col, end)]
9494
M_east = env.messages[EAST, mod1(row, end), _next(col, end)]
9595
M_south = env.messages[SOUTH, _next(row, end), mod1(col, end)]
9696
M_west = env.messages[WEST, mod1(row, end), _prev(col, end)]
9797

98-
@autoopt @tensor begin
98+
return @autoopt @tensor begin
9999
ket[mod1(row, end), mod1(col, end)][d; DNt DEt DSt DWt] *
100-
conj(bra[mod1(row, end), mod1(col, end)][d; DNb DEb DSb DWb]) *
101-
M_north[DNt; DNb] *
102-
M_east[DEt; DEb] *
103-
M_south[DSt; DSb] *
104-
M_west[DWt; DWb]
100+
conj(bra[mod1(row, end), mod1(col, end)][d; DNb DEb DSb DWb]) *
101+
M_north[DNt; DNb] *
102+
M_east[DEt; DEb] *
103+
M_south[DSt; DSb] *
104+
M_west[DWt; DWb]
105105
end
106106
end
107107

108108
function contract_local_operator(
109-
inds::NTuple{2,CartesianIndex{2}},
110-
O::AbstractTensorMap{<:Any,<:Any,2,2},
111-
ket::InfinitePEPS,
112-
bra::InfinitePEPS,
113-
env::BPEnv,
114-
)
109+
inds::NTuple{2, CartesianIndex{2}},
110+
O::AbstractTensorMap{<:Any, <:Any, 2, 2},
111+
ket::InfinitePEPS,
112+
bra::InfinitePEPS,
113+
env::BPEnv,
114+
)
115115
ind_relative = inds[2] - inds[1]
116-
if ind_relative == CartesianIndex(1, 0)
116+
return if ind_relative == CartesianIndex(1, 0)
117117
contract_vertical_operator(inds[1], O, ket, bra, env)
118118
elseif ind_relative == CartesianIndex(0, 1)
119119
contract_horizontal_operator(inds[1], O, ket, bra, env)
@@ -123,12 +123,12 @@ function contract_local_operator(
123123
end
124124

125125
function contract_vertical_operator(
126-
coord::CartesianIndex{2},
127-
O::AbstractTensorMap{<:Any,<:Any,2,2},
128-
ket::InfinitePEPS,
129-
bra::InfinitePEPS,
130-
env::BPEnv,
131-
)
126+
coord::CartesianIndex{2},
127+
O::AbstractTensorMap{<:Any, <:Any, 2, 2},
128+
ket::InfinitePEPS,
129+
bra::InfinitePEPS,
130+
env::BPEnv,
131+
)
132132
row, col = Tuple(coord)
133133
M_north = env.messages[NORTH, _prev(row, end), mod1(col, end)]
134134
M_northeast = env.messages[EAST, mod1(row, end), _next(col, end)]
@@ -151,12 +151,12 @@ function contract_vertical_operator(
151151
end
152152

153153
function contract_horizontal_operator(
154-
coord::CartesianIndex{2},
155-
O::AbstractTensorMap{<:Any,<:Any,2,2},
156-
ket::InfinitePEPS,
157-
bra::InfinitePEPS,
158-
env::BPEnv,
159-
)
154+
coord::CartesianIndex{2},
155+
O::AbstractTensorMap{<:Any, <:Any, 2, 2},
156+
ket::InfinitePEPS,
157+
bra::InfinitePEPS,
158+
env::BPEnv,
159+
)
160160
row, col = Tuple(coord)
161161
M_west = env.messages[WEST, mod1(row, end), _prev(col, end)]
162162
M_northwest = env.messages[NORTH, _prev(row, end), mod1(col, end)]
@@ -171,24 +171,24 @@ function contract_horizontal_operator(
171171

172172
return @autoopt @tensor begin
173173
A_west[dWt; DNWt DMt DSWt DWt] *
174-
A_east[dEt; DNEt DEt DSEt DMt] *
175-
conj(Ā_west[dWb; DNWb DMb DSWb DWb]) *
176-
conj(Ā_east[dEb; DNEb DEb DSEb DMb]) *
177-
M_west[DWt; DWb] *
178-
M_northwest[DNWt; DNWb] *
179-
M_northeast[DNEt; DNEb] *
180-
M_east[DEt; DEb] *
181-
M_southeast[DSEt; DSEb] *
182-
M_southwest[DSWt; DSWb] *
183-
O[dWb dEb; dWt dEt]
174+
A_east[dEt; DNEt DEt DSEt DMt] *
175+
conj(Ā_west[dWb; DNWb DMb DSWb DWb]) *
176+
conj(Ā_east[dEb; DNEb DEb DSEb DMb]) *
177+
M_west[DWt; DWb] *
178+
M_northwest[DNWt; DNWb] *
179+
M_northeast[DNEt; DNEb] *
180+
M_east[DEt; DEb] *
181+
M_southeast[DSEt; DSEb] *
182+
M_southwest[DSWt; DSWb] *
183+
O[dWb dEb; dWt dEt]
184184
end
185185
end
186186

187187
function contract_local_norm(
188-
inds::NTuple{2,CartesianIndex{2}}, ket::InfinitePEPS, bra::InfinitePEPS, env::BPEnv
189-
)
188+
inds::NTuple{2, CartesianIndex{2}}, ket::InfinitePEPS, bra::InfinitePEPS, env::BPEnv
189+
)
190190
ind_relative = inds[2] - inds[1]
191-
if ind_relative == CartesianIndex(1, 0)
191+
return if ind_relative == CartesianIndex(1, 0)
192192
contract_vertical_norm(inds[1], ket, bra, env)
193193
elseif ind_relative == CartesianIndex(0, 1)
194194
contract_horizontal_norm(inds[1], ket, bra, env)
@@ -198,8 +198,8 @@ function contract_local_norm(
198198
end
199199

200200
function contract_vertical_norm(
201-
coord::CartesianIndex{2}, ket::InfinitePEPS, bra::InfinitePEPS, env::BPEnv
202-
)
201+
coord::CartesianIndex{2}, ket::InfinitePEPS, bra::InfinitePEPS, env::BPEnv
202+
)
203203
row, col = Tuple(coord)
204204
M_north = env.messages[NORTH, _prev(row, end), mod1(col, end)]
205205
M_northeast = env.messages[EAST, mod1(row, end), _next(col, end)]
@@ -221,8 +221,8 @@ function contract_vertical_norm(
221221
end
222222

223223
function contract_horizontal_norm(
224-
coord::CartesianIndex{2}, ket::InfinitePEPS, bra::InfinitePEPS, env::BPEnv
225-
)
224+
coord::CartesianIndex{2}, ket::InfinitePEPS, bra::InfinitePEPS, env::BPEnv
225+
)
226226
row, col = Tuple(coord)
227227

228228
M_west = env.messages[WEST, mod1(row, end), _prev(col, end)]
@@ -239,14 +239,14 @@ function contract_horizontal_norm(
239239

240240
return @autoopt @tensor begin
241241
A_west[dW; DNWt DMt DSWt DWt] *
242-
A_east[dE; DNEt DEt DSEt DMt] *
243-
conj(Ā_west[dW; DNWb DMb DSWb DWb]) *
244-
conj(Ā_east[dE; DNEb DEb DSEb DMb]) *
245-
M_west[DWt; DWb] *
246-
M_northwest[DNWt; DNWb] *
247-
M_northeast[DNEt; DNEb] *
248-
M_east[DEt; DEb] *
249-
M_southeast[DSEt; DSEb] *
250-
M_southwest[DSWt; DSWb]
242+
A_east[dE; DNEt DEt DSEt DMt] *
243+
conj(Ā_west[dW; DNWb DMb DSWb DWb]) *
244+
conj(Ā_east[dE; DNEb DEb DSEb DMb]) *
245+
M_west[DWt; DWb] *
246+
M_northwest[DNWt; DNWb] *
247+
M_northeast[DNEt; DNEb] *
248+
M_east[DEt; DEb] *
249+
M_southeast[DSEt; DSEb] *
250+
M_southwest[DSWt; DSWb]
251251
end
252252
end

src/environments/bp_environments.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
struct BPEnv{T}
22
"4 x rows x cols array of message tensors, where the first dimension specifies the spatial direction"
3-
messages::Array{T,3}
3+
messages::Array{T, 3}
44
end
55

6-
function _message_tensor(f, ::Type{T}, pspaces::P) where {T,P<:ProductSpaceLike}
6+
function _message_tensor(f, ::Type{T}, pspaces::P) where {T, P <: ProductSpaceLike}
77
Vp = _to_space(pspaces)
88
V = permute(Vp one(Vp), (ntuple(identity, length(pspaces) - 1), (length(pspaces),)))
99
return f(T, V)
1010
end
1111

12-
function BPEnv(Ds_north::A, Ds_east::A) where {A<:AbstractMatrix{<:ProductSpaceLike}}
12+
function BPEnv(Ds_north::A, Ds_east::A) where {A <: AbstractMatrix{<:ProductSpaceLike}}
1313
return BPEnv(randn, ComplexF64, N, Ds_north, Ds_east)
1414
end
15-
function BPEnv(f, T, Ds_north::A, Ds_east::A) where {A<:AbstractMatrix{<:ProductSpaceLike}}
15+
function BPEnv(f, T, Ds_north::A, Ds_east::A) where {A <: AbstractMatrix{<:ProductSpaceLike}}
1616
# no recursive broadcasting?
1717
Ds_south = _elementwise_dual.(circshift(Ds_north, (-1, 0)))
1818
Ds_west = _elementwise_dual.(circshift(Ds_east, (0, 1)))
@@ -40,13 +40,13 @@ function BPEnv(f, T, Ds_north::A, Ds_east::A) where {A<:AbstractMatrix{<:Product
4040
end
4141

4242
function BPEnv(
43-
D_north::P, D_east::P; unitcell::Tuple{Int,Int}=(1, 1)
44-
) where {P<:ProductSpaceLike}
43+
D_north::P, D_east::P; unitcell::Tuple{Int, Int} = (1, 1)
44+
) where {P <: ProductSpaceLike}
4545
return BPEnv(randn, ComplexF64, fill(D_north, unitcell), fill(D_east, unitcell))
4646
end
4747
function BPEnv(
48-
f, T, D_north::P, D_east::P; unitcell::Tuple{Int,Int}=(1, 1)
49-
) where {P<:ProductSpaceLike}
48+
f, T, D_north::P, D_east::P; unitcell::Tuple{Int, Int} = (1, 1)
49+
) where {P <: ProductSpaceLike}
5050
return BPEnv(f, T, N, fill(D_north, unitcell), fill(D_east, unitcell))
5151
end
5252

@@ -61,10 +61,10 @@ function BPEnv(f, T, network::InfiniteSquareNetwork)
6161
return BPEnv(f, T, Ds_north, Ds_east)
6262
end
6363

64-
function BPEnv(state::Union{InfinitePartitionFunction,InfinitePEPS}, args...)
64+
function BPEnv(state::Union{InfinitePartitionFunction, InfinitePEPS}, args...)
6565
return BPEnv(InfiniteSquareNetwork(state), args...)
6666
end
67-
function BPEnv(f, T, state::Union{InfinitePartitionFunction,InfinitePEPS}, args...)
67+
function BPEnv(f, T, state::Union{InfinitePartitionFunction, InfinitePEPS}, args...)
6868
return BPEnv(f, T, InfiniteSquareNetwork(state), args...)
6969
end
7070

@@ -73,7 +73,7 @@ end
7373

7474
VI.scalartype(::Type{BPEnv{T}}) where {T} = scalartype(T)
7575

76-
VI.zerovector(env::BPEnv, ::Type{S}) where {S<:Number} = BPEnv(zerovector.(env.messages, S))
76+
VI.zerovector(env::BPEnv, ::Type{S}) where {S <: Number} = BPEnv(zerovector.(env.messages, S))
7777
VI.zerovector!(env::BPEnv) = (zerovector!.(env.messages); env)
7878
VI.zerovector!!(env::BPEnv) = zerovector!(env)
7979

0 commit comments

Comments
 (0)