Skip to content

Commit 4b8af2c

Browse files
format
1 parent 4051a85 commit 4b8af2c

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

src/algorithms/timestep/clusterexpansion.jl

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ function make_time_mpo(H::MPOHamiltonian, dt::Number, alg::ClusterExpansion)
2828
b = _make_b(H, O, n, τ)
2929
Onew, info = lssolve(apply_function, b; verbosity=5)
3030

31-
(info.converged) != 1 && @warn "Did not converge when constucting the $n cluster"
31+
(info.converged) != 1 &&
32+
@warn "Did not converge when constucting the $n cluster"
3233

3334
if isodd(n) # linear problem
3435
# assign the new level to O
3536
O[jnl, 1, 1, jnl] = Onew
3637
elseif iseven(n) # linear problem + svd
3738
U, S, V = tsvd(Onew, (1, 2, 4), (3, 5, 6))
3839
# assign the new levels to O
39-
O[jnl-1, 1, 1, jnl] = permute(U * sqrt(S), ((1, 2), (3, 4)))
40-
O[jnl, 1, 1, jnl-1] = permute(sqrt(S) * V, ((1, 2), (3, 4)))
40+
O[jnl - 1, 1, 1, jnl] = permute(U * sqrt(S), ((1, 2), (3, 4)))
41+
O[jnl, 1, 1, jnl - 1] = permute(sqrt(S) * V, ((1, 2), (3, 4)))
4142
end
4243
end
4344
end
@@ -46,14 +47,16 @@ function make_time_mpo(H::MPOHamiltonian, dt::Number, alg::ClusterExpansion)
4647
return MPO(PeriodicVector([O]))
4748
end
4849

49-
5050
function _make_Hamterms(H::MPOHamiltonian, n::Int, τ::Number)
51-
return add_util_leg(convert(TensorMap, scale(DenseMPO(open_boundary_conditions(H, n)), τ)))
51+
return add_util_leg(convert(TensorMap,
52+
scale(DenseMPO(open_boundary_conditions(H, n)), τ)))
5253
end
5354

5455
function _make_b(H::MPOHamiltonian, O::SparseBlockTensorMap, n::Int, τ::Number)
5556
# be - bo
56-
be = permute(exp(permute(_make_Hamterms(H, n, τ), Tuple(1:n+1), Tuple(vcat([2n + 2,], collect(n+2:2n+1))))), Tuple(1:n+1), Tuple(vcat(collect(n+3:2n+2), [n + 2,])))
57+
be = permute(exp(permute(_make_Hamterms(H, n, τ), Tuple(1:(n + 1)),
58+
Tuple(vcat([2n + 2], collect((n + 2):(2n + 1)))))),
59+
Tuple(1:(n + 1)), Tuple(vcat(collect((n + 3):(2n + 2)), [n + 2])))
5760
bo = _fully_contract_O(O, n)
5861
return be - bo
5962
end
@@ -69,7 +72,8 @@ function _fully_contract_O(O::SparseBlockTensorMap, n::Int)
6972
O_inds = [[i, -(i + 1), -(i + 1 + n), i + 1] for i in 1:n]
7073

7174
# Contract and permute to right structure
72-
return permute(ncon([Pl, fill(O, n)..., Pr], [[-1, 1], O_inds..., [n + 1, -(2n + 2)]]), Tuple(1:n+1), Tuple(n+2:2n+2))
75+
return permute(ncon([Pl, fill(O, n)..., Pr], [[-1, 1], O_inds..., [n + 1, -(2n + 2)]]),
76+
Tuple(1:(n + 1)), Tuple((n + 2):(2n + 2)))
7377
end
7478

7579
function make_envs(O::SparseBlockTensorMap, n::Int)
@@ -82,30 +86,30 @@ function _make_envs(O::SparseBlockTensorMap, nt::Int)
8286
return O[1, 1, 1, 2], O[2, 1, 1, 1]
8387
else
8488
Tl, Tr = _make_envs(O, nt - 1)
85-
Ol = O[nt, 1, 1, nt+1]
86-
Or = O[nt+1, 1, 1, nt]
89+
Ol = O[nt, 1, 1, nt + 1]
90+
Or = O[nt + 1, 1, 1, nt]
8791
return _make_left_env(Tl, Ol), _make_right_env(Or, Tr)
8892
end
8993
end
9094

9195
@generated function _make_left_env(Tl::AbstractTensorMap, Ol::AbstractTensorMap)
9296
N₁ = numin(Tl)
9397
N = numind(Tl)
94-
t_out = tensorexpr(:new_e_l, -(1:N₁+1), -(N₁+2:N+2))
95-
t_left = tensorexpr(:Tl, -(1:N₁), ((-(N₁+2:N)...), 1))
98+
t_out = tensorexpr(:new_e_l, -(1:(N₁ + 1)), -((N₁ + 2):(N + 2)))
99+
t_left = tensorexpr(:Tl, -(1:N₁), ((-((N₁ + 2):N)...), 1))
96100
t_right = tensorexpr(:Ol, (1, -(N₁ + 1)), (-(N + 1), -(N + 2)))
97101
return macroexpand(@__MODULE__,
98-
:(return @tensor $t_out := $t_left * $t_right))
102+
:(return @tensor $t_out := $t_left * $t_right))
99103
end
100104

101105
@generated function _make_right_env(Or::AbstractTensorMap, Tr::AbstractTensorMap)
102106
N₁ = numin(Tr)
103107
N = numind(Tr)
104-
t_out = tensorexpr(:new_e_r, -(1:N₁+1), -(N₁+2:N+2))
108+
t_out = tensorexpr(:new_e_r, -(1:(N₁ + 1)), -((N₁ + 2):(N + 2)))
105109
t_left = tensorexpr(:Or, -(1:2), (-(N₁ + 2), 1))
106-
t_right = tensorexpr(:Tr, (1, (-(3:N₁+1)...)), -(N₁+3:N+2))
110+
t_right = tensorexpr(:Tr, (1, (-(3:(N₁ + 1))...)), -((N₁ + 3):(N + 2)))
107111
return macroexpand(@__MODULE__,
108-
:(return @tensor $t_out := $t_left * $t_right))
112+
:(return @tensor $t_out := $t_left * $t_right))
109113
end
110114

111115
function _make_apply_functions(O::SparseBlockTensorMap, n::Int)
@@ -115,34 +119,46 @@ function _make_apply_functions(O::SparseBlockTensorMap, n::Int)
115119
fun = if iseven(n)
116120
let
117121
function A(x::TensorMap, ::Val{false})
118-
Al_inds = vcat(-collect(1:nT+1), -collect(2nT+4:3nT+3), [1,])
122+
Al_inds = vcat(-collect(1:(nT + 1)), -collect((2nT + 4):(3nT + 3)), [1])
119123
x_inds = [1, -(nT + 2), -(nT + 3), -(3nT + 4), -(3nT + 5), 2]
120-
Ar_inds = vcat([2,], -collect(nT+4:2nT+3), -collect(3nT+6:4nT+6))
121-
return permute(ncon([Al, x, Ar], [Al_inds, x_inds, Ar_inds]), Tuple(1:2nT+3), Tuple((2nT+4):(4nT+6)))
124+
Ar_inds = vcat([2], -collect((nT + 4):(2nT + 3)),
125+
-collect((3nT + 6):(4nT + 6)))
126+
return permute(ncon([Al, x, Ar], [Al_inds, x_inds, Ar_inds]),
127+
Tuple(1:(2nT + 3)), Tuple((2nT + 4):(4nT + 6)))
122128
end
123129

124130
function A(b::TensorMap, ::Val{true})
125-
Al_inds = vcat(collect(3:nT+2), [-1, 1], collect(nT+3:2nT+2))
126-
b_inds = vcat([1,], collect(nT+3:2nT+2), [-2, -3], collect(3nT+3:4nT+2), collect(3:nT+2), [-4, -5], collect(2nT+3:3nT+2), [2,])
127-
Ar_inds = vcat(collect(2nT+3:3nT+2), [2, -6], collect(3nT+3:4nT+2))
128-
return permute(ncon([adjoint(Al), b, adjoint(Ar)], [Al_inds, b_inds, Ar_inds]), (1, 2, 3), (4, 5, 6))
131+
Al_inds = vcat(collect(3:(nT + 2)), [-1, 1], collect((nT + 3):(2nT + 2)))
132+
b_inds = vcat([1], collect((nT + 3):(2nT + 2)), [-2, -3],
133+
collect((3nT + 3):(4nT + 2)), collect(3:(nT + 2)), [-4, -5],
134+
collect((2nT + 3):(3nT + 2)), [2])
135+
Ar_inds = vcat(collect((2nT + 3):(3nT + 2)), [2, -6],
136+
collect((3nT + 3):(4nT + 2)))
137+
return permute(ncon([adjoint(Al), b, adjoint(Ar)],
138+
[Al_inds, b_inds, Ar_inds]), (1, 2, 3), (4, 5, 6))
129139
end
130140
end
131141
elseif isodd(n)
132142
let
133143
function A(x::TensorMap, ::Val{false})
134-
Al_inds = vcat(-collect(1:nT+1), -collect(2nT+3:3nT+2), [1,])
144+
Al_inds = vcat(-collect(1:(nT + 1)), -collect((2nT + 3):(3nT + 2)), [1])
135145
x_inds = [1, -(nT + 2), -(3nT + 3), 2]
136-
Ar_inds = vcat([2,], -collect(nT+3:2nT+2), -collect(3nT+4:4nT+4))
137-
return permute(ncon([Al, x, Ar], [Al_inds, x_inds, Ar_inds]), Tuple(1:2nT+2), Tuple((2nT+3):(4nT+4)))
146+
Ar_inds = vcat([2], -collect((nT + 3):(2nT + 2)),
147+
-collect((3nT + 4):(4nT + 4)))
148+
return permute(ncon([Al, x, Ar], [Al_inds, x_inds, Ar_inds]),
149+
Tuple(1:(2nT + 2)), Tuple((2nT + 3):(4nT + 4)))
138150
end
139151
function A(b::TensorMap, ::Val{true})
140-
Al_inds = vcat(collect(3:nT+2), [-1, 1], collect(nT+3:2nT+2))
141-
b_inds = vcat([1,], collect(nT+3:2nT+2), [-2,], collect(3nT+3:4nT+2), collect(3:nT+2), [-3,], collect(2nT+3:3nT+2), [2,])
142-
Ar_inds = vcat(collect(2nT+3:3nT+2), [2, -4], collect(3nT+3:4nT+2))
143-
return permute(ncon([adjoint(Al), b, adjoint(Ar)], [Al_inds, b_inds, Ar_inds]), (1, 2), (3, 4))
152+
Al_inds = vcat(collect(3:(nT + 2)), [-1, 1], collect((nT + 3):(2nT + 2)))
153+
b_inds = vcat([1], collect((nT + 3):(2nT + 2)), [-2],
154+
collect((3nT + 3):(4nT + 2)), collect(3:(nT + 2)), [-3],
155+
collect((2nT + 3):(3nT + 2)), [2])
156+
Ar_inds = vcat(collect((2nT + 3):(3nT + 2)), [2, -4],
157+
collect((3nT + 3):(4nT + 2)))
158+
return permute(ncon([adjoint(Al), b, adjoint(Ar)],
159+
[Al_inds, b_inds, Ar_inds]), (1, 2), (3, 4))
144160
end
145161
end
146162
end
147163
return fun
148-
end
164+
end

0 commit comments

Comments
 (0)