Skip to content

Commit ecbac58

Browse files
committed
Format algorithms
1 parent bac4fbd commit ecbac58

40 files changed

+882
-753
lines changed

src/algorithms/ED.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ equivalent to dense eigenvectors.
3030
of all the physical spaces in the system.
3131
3232
"""
33-
function exact_diagonalization(H::FiniteMPOHamiltonian;
34-
sector=one(sectortype(H)),
35-
num::Int=1, which::Symbol=:SR,
36-
alg=Defaults.alg_eigsolve(; dynamic_tols=false))
33+
function exact_diagonalization(
34+
H::FiniteMPOHamiltonian;
35+
sector = one(sectortype(H)), num::Int = 1, which::Symbol = :SR,
36+
alg = Defaults.alg_eigsolve(; dynamic_tols = false)
37+
)
3738
L = length(H)
3839
@assert L > 1 "FiniteMPOHamiltonian must have length > 1"
3940
middle_site = (L >> 1) + 1
@@ -42,7 +43,7 @@ function exact_diagonalization(H::FiniteMPOHamiltonian;
4243
TA = tensormaptype(spacetype(H), 2, 1, T)
4344

4445
# fuse from left to right
45-
ALs = Vector{Union{Missing,TA}}(missing, L)
46+
ALs = Vector{Union{Missing, TA}}(missing, L)
4647
left = oneunit(spacetype(H))
4748
for i in 1:(middle_site - 1)
4849
P = physicalspace(H, i)
@@ -51,7 +52,7 @@ function exact_diagonalization(H::FiniteMPOHamiltonian;
5152
end
5253

5354
# fuse from right to left
54-
ARs = Vector{Union{Missing,TA}}(missing, L)
55+
ARs = Vector{Union{Missing, TA}}(missing, L)
5556
right = spacetype(H)(sector => 1)
5657
for i in reverse((middle_site + 1):L)
5758
P = physicalspace(H, i)
@@ -60,12 +61,12 @@ function exact_diagonalization(H::FiniteMPOHamiltonian;
6061
end
6162

6263
# center
63-
ACs = Vector{Union{Missing,TA}}(missing, L)
64+
ACs = Vector{Union{Missing, TA}}(missing, L)
6465
P = physicalspace(H, middle_site)
6566
ACs[middle_site] = rand!(similar(ALs[1], left P right))
6667

6768
TB = tensormaptype(spacetype(H), 1, 1, T)
68-
Cs = Vector{Union{Missing,TB}}(missing, L + 1)
69+
Cs = Vector{Union{Missing, TB}}(missing, L + 1)
6970
state = FiniteMPS(ALs, ARs, ACs, Cs)
7071
envs = environments(state, H)
7172

src/algorithms/approximate/approximate.jl

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,41 @@ of an MPS `ψ₀`.
2727
approximate, approximate!
2828

2929
# implementation in terms of Multiline
30-
function approximate(ψ::InfiniteMPS,
31-
toapprox::Tuple{<:InfiniteMPO,<:InfiniteMPS},
32-
algorithm,
33-
envs=environments(ψ, toapprox))
30+
function approximate(
31+
ψ::InfiniteMPS, toapprox::Tuple{<:InfiniteMPO, <:InfiniteMPS}, algorithm,
32+
envs = environments(ψ, toapprox)
33+
)
3434
envs′ = Multiline([envs])
35-
multi, envs = approximate(convert(MultilineMPS, ψ),
36-
(convert(MultilineMPO, toapprox[1]),
37-
convert(MultilineMPS, toapprox[2])), algorithm, envs′)
35+
multi, envs = approximate(
36+
convert(MultilineMPS, ψ),
37+
(convert(MultilineMPO, toapprox[1]), convert(MultilineMPS, toapprox[2])),
38+
algorithm,
39+
envs′
40+
)
3841
ψ = convert(InfiniteMPS, multi)
3942
return ψ, envs
4043
end
4144

4245
# dispatch to in-place method
43-
function approximate(ψ, toapprox, alg::Union{DMRG,DMRG2,IDMRG,IDMRG2},
44-
envs=environments(ψ, toapprox))
46+
function approximate(
47+
ψ, toapprox, alg::Union{DMRG, DMRG2, IDMRG, IDMRG2},
48+
envs = environments(ψ, toapprox)
49+
)
4550
return approximate!(copy(ψ), toapprox, alg, envs)
4651
end
4752

4853
# disambiguate
49-
function approximate(ψ::InfiniteMPS,
50-
toapprox::Tuple{<:InfiniteMPO,<:InfiniteMPS},
51-
algorithm::Union{IDMRG,IDMRG2},
52-
envs=environments(ψ, toapprox))
54+
function approximate(
55+
ψ::InfiniteMPS, toapprox::Tuple{<:InfiniteMPO, <:InfiniteMPS},
56+
algorithm::Union{IDMRG, IDMRG2}, envs = environments(ψ, toapprox)
57+
)
5358
envs′ = Multiline([envs])
54-
multi, envs = approximate(convert(MultilineMPS, ψ),
55-
(convert(MultilineMPO, toapprox[1]),
56-
convert(MultilineMPS, toapprox[2])), algorithm, envs′)
59+
multi, envs = approximate(
60+
convert(MultilineMPS, ψ),
61+
(convert(MultilineMPO, toapprox[1]), convert(MultilineMPS, toapprox[2])),
62+
algorithm,
63+
envs′
64+
)
5765
ψ = convert(InfiniteMPS, multi)
5866
return ψ, envs
5967
end

src/algorithms/approximate/fvomps.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
function approximate!::AbstractFiniteMPS, Oϕ, alg::DMRG2,
2-
envs=environments(ψ, Oϕ))
1+
function approximate!::AbstractFiniteMPS, Oϕ, alg::DMRG2, envs = environments(ψ, Oϕ))
32
ϵ::Float64 = 2 * alg.tol
43
log = IterLog("DMRG2")
54

@@ -9,7 +8,7 @@ function approximate!(ψ::AbstractFiniteMPS, Oϕ, alg::DMRG2,
98
ϵ = 0.0
109
for pos in [1:(length(ψ) - 1); (length(ψ) - 2):-1:1]
1110
AC2′ = AC2_projection(pos, ψ, Oϕ, envs)
12-
al, c, ar, = tsvd!(AC2′; trunc=alg.trscheme)
11+
al, c, ar, = tsvd!(AC2′; trunc = alg.trscheme)
1312

1413
AC2 = ψ.AC[pos] * _transpose_tail.AR[pos + 1])
1514
ϵ = max(ϵ, norm(al * c * ar - AC2) / norm(AC2))
@@ -19,7 +18,7 @@ function approximate!(ψ::AbstractFiniteMPS, Oϕ, alg::DMRG2,
1918
end
2019

2120
# finalize
22-
ψ, envs = alg.finalize(iter, ψ, Oϕ, envs)::Tuple{typeof(ψ),typeof(envs)}
21+
ψ, envs = alg.finalize(iter, ψ, Oϕ, envs)::Tuple{typeof(ψ), typeof(envs)}
2322

2423
if ϵ < alg.tol
2524
@infov 2 logfinish!(log, iter, ϵ)
@@ -36,7 +35,7 @@ function approximate!(ψ::AbstractFiniteMPS, Oϕ, alg::DMRG2,
3635
return ψ, envs, ϵ
3736
end
3837

39-
function approximate!::AbstractFiniteMPS, Oϕ, alg::DMRG, envs=environments(ψ, Oϕ))
38+
function approximate!::AbstractFiniteMPS, Oϕ, alg::DMRG, envs = environments(ψ, Oϕ))
4039
ϵ::Float64 = 2 * alg.tol
4140
log = IterLog("DMRG")
4241

@@ -53,7 +52,7 @@ function approximate!(ψ::AbstractFiniteMPS, Oϕ, alg::DMRG, envs=environments(
5352
end
5453

5554
# finalize
56-
ψ, envs = alg.finalize(iter, ψ, Oϕ, envs)::Tuple{typeof(ψ),typeof(envs)}
55+
ψ, envs = alg.finalize(iter, ψ, Oϕ, envs)::Tuple{typeof(ψ), typeof(envs)}
5756

5857
if ϵ < alg.tol
5958
@infov 2 logfinish!(log, iter, ϵ)

src/algorithms/approximate/idmrg.jl

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
function approximate!::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:MultilineMPS},
2-
alg::IDMRG, envs=environments(ψ, toapprox))
1+
function approximate!(
2+
ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO, <:MultilineMPS}, alg::IDMRG,
3+
envs = environments(ψ, toapprox)
4+
)
35
log = IterLog("IDMRG")
46
ϵ::Float64 = 2 * alg.tol
57
local iter
@@ -12,8 +14,9 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
1214
# left to right sweep
1315
for col in 1:size(ψ, 2)
1416
for row in 1:size(ψ, 1)
15-
ψ.AC[row + 1, col] = AC_projection(CartesianIndex(row, col), ψ,
16-
toapprox, envs)
17+
ψ.AC[row + 1, col] = AC_projection(
18+
CartesianIndex(row, col), ψ, toapprox, envs
19+
)
1720
normalize!.AC[row + 1, col])
1821
ψ.AL[row + 1, col], ψ.C[row + 1, col] = leftorth!.AC[row + 1, col])
1922
end
@@ -23,11 +26,11 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
2326
# right to left sweep
2427
for col in reverse(1:size(ψ, 2))
2528
for row in 1:size(ψ, 1)
26-
ψ.AC[row + 1, col] = AC_projection(CartesianIndex(row, col),
27-
ψ, toapprox, envs)
29+
ψ.AC[row + 1, col] = AC_projection(
30+
CartesianIndex(row, col), ψ, toapprox, envs
31+
)
2832
normalize!.AC[row + 1, col])
29-
ψ.C[row + 1, col - 1], temp = rightorth!(_transpose_tail.AC[row + 1,
30-
col]))
33+
ψ.C[row + 1, col - 1], temp = rightorth!(_transpose_tail.AC[row + 1, col]))
3134
ψ.AR[row + 1, col] = _transpose_front(temp)
3235
end
3336
transfer_rightenv!(envs, ψ, toapprox, col - 1)
@@ -57,8 +60,10 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
5760
return ψ, envs, ϵ
5861
end
5962

60-
function approximate!::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:MultilineMPS},
61-
alg::IDMRG2, envs=environments(ψ, toapprox))
63+
function approximate!(
64+
ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO, <:MultilineMPS},
65+
alg::IDMRG2, envs = environments(ψ, toapprox)
66+
)
6267
size(ψ, 2) < 2 && throw(ArgumentError("unit cell should be >= 2"))
6368
ϵ::Float64 = 2 * alg.tol
6469
log = IterLog("IDMRG2")
@@ -73,9 +78,11 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
7378
# sweep from left to right
7479
for site in 1:size(ψ, 2)
7580
for row in 1:size(ψ, 1)
76-
AC2′ = AC2_projection(CartesianIndex(row, site), ψ, toapprox, envs;
77-
kind=:ACAR)
78-
al, c, ar, = tsvd!(AC2′; trunc=alg.trscheme, alg=alg.alg_svd)
81+
AC2′ = AC2_projection(
82+
CartesianIndex(row, site), ψ, toapprox, envs;
83+
kind = :ACAR
84+
)
85+
al, c, ar, = tsvd!(AC2′; trunc = alg.trscheme, alg = alg.alg_svd)
7986
normalize!(c)
8087

8188
ψ.AL[row + 1, site] = al
@@ -93,9 +100,11 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
93100
# sweep from right to left
94101
for site in reverse(0:(size(ψ, 2) - 1))
95102
for row in 1:size(ψ, 1)
96-
AC2′ = AC2_projection(CartesianIndex(row, site), ψ, toapprox, envs;
97-
kind=:ALAC)
98-
al, c, ar, = tsvd!(AC2′; trunc=alg.trscheme, alg=alg.alg_svd)
103+
AC2′ = AC2_projection(
104+
CartesianIndex(row, site), ψ, toapprox, envs;
105+
kind = :ALAC
106+
)
107+
al, c, ar, = tsvd!(AC2′; trunc = alg.trscheme, alg = alg.alg_svd)
99108
normalize!(c)
100109

101110
ψ.AL[row + 1, site] = al

src/algorithms/approximate/vomps.jl

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
Base.@deprecate(approximate::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:MultilineMPS},
2-
alg::VUMPS, envs...; kwargs...),
3-
approximate(ψ, toapprox,
4-
VOMPS(; alg.tol, alg.maxiter, alg.finalize,
5-
alg.verbosity, alg.alg_gauge, alg.alg_environments),
6-
envs...; kwargs...))
7-
8-
function approximate(mps::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:MultilineMPS},
9-
alg::VOMPS, envs=environments(mps, toapprox))
1+
Base.@deprecate(
2+
approximate(
3+
ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO, <:MultilineMPS}, alg::VUMPS, envs...;
4+
kwargs...
5+
),
6+
approximate(
7+
ψ, toapprox,
8+
VOMPS(; alg.tol, alg.maxiter, alg.finalize, alg.verbosity, alg.alg_gauge, alg.alg_environments),
9+
envs...; kwargs...
10+
)
11+
)
12+
13+
function approximate(
14+
mps::MultilineMPS, toapprox::Tuple{<:MultilineMPO, <:MultilineMPS}, alg::VOMPS,
15+
envs = environments(mps, toapprox)
16+
)
1017
log = IterLog("VOMPS")
1118
iter = 0
1219
ϵ = calc_galerkin(mps, toapprox..., envs)
@@ -38,7 +45,7 @@ end
3845

3946
# need to specialize a bunch of functions because different arguments are passed with tuples
4047
# TODO: can we avoid this?
41-
function Base.iterate(it::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any,<:Tuple})
48+
function Base.iterate(it::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any, <:Tuple})
4249
ACs = localupdate_step!(it, state)
4350
mps = gauge_step!(it, state, ACs)
4451
envs = envs_step!(it, state, mps)
@@ -55,28 +62,32 @@ function Base.iterate(it::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any,<:Tu
5562
return (mps, envs, ϵ), it.state
5663
end
5764

58-
function localupdate_step!(::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any,<:Tuple},
59-
::SerialScheduler)
65+
function localupdate_step!(
66+
::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any, <:Tuple}, ::SerialScheduler
67+
)
6068
alg_orth = QRpos()
6169

6270
ACs = similar(state.mps.AC)
6371
dst_ACs = state.mps isa Multiline ? eachcol(ACs) : ACs
6472

6573
foreach(eachsite(state.mps)) do site
66-
AC = circshift([AC_projection(CartesianIndex(row, site), state.mps, state.operator,
67-
state.envs)
68-
for row in 1:size(state.mps, 1)], 1)
69-
C = circshift([C_projection(CartesianIndex(row, site), state.mps, state.operator,
70-
state.envs)
71-
for row in 1:size(state.mps, 1)], 1)
72-
dst_ACs[site] = regauge!(AC, C; alg=alg_orth)
74+
AC = map(1:size(state.mps, 1)) do row
75+
AC_projection(CartesianIndex(row, site), state.mps, state.operator, state.envs)
76+
end
77+
circshift!(AC, 1)
78+
C = map(1:size(state.mps, 1)) do row
79+
C_projection(CartesianIndex(row, site), state.mps, state.operator, state.envs)
80+
end
81+
circshift!(C, 1)
82+
dst_ACs[site] = regauge!(AC, C; alg = alg_orth)
7383
return nothing
7484
end
7585

7686
return ACs
7787
end
78-
function localupdate_step!(::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any,<:Tuple},
79-
scheduler)
88+
function localupdate_step!(
89+
::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any, <:Tuple}, scheduler
90+
)
8091
alg_orth = QRpos()
8192

8293
ACs = similar(state.mps.AC)
@@ -86,24 +97,26 @@ function localupdate_step!(::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any,<
8697
local AC, C
8798
@sync begin
8899
Threads.@spawn begin
89-
AC = circshift([AC_projection(CartesianIndex(row, site), state.mps,
90-
state.operator, state.envs)
91-
for row in 1:size(state.mps, 1)], 1)
100+
AC = map(1:size(state.mps, 1)) do row
101+
AC_projection(CartesianIndex(row, site), state.mps, state.operator, state.envs)
102+
end
103+
circshift!(AC, 1)
92104
end
93105
Threads.@spawn begin
94-
C = circshift([C_projection(CartesianIndex(row, site), state.mps,
95-
state.operator, state.envs)
96-
for row in 1:size(state.mps, 1)], 1)
106+
C = map(1:size(state.mps, 1)) do row
107+
C_projection(CartesianIndex(row, site), state.mps, state.operator, state.envs)
108+
end
109+
circshift!(C, 1)
97110
end
98111
end
99-
dst_ACs[site] = regauge!(AC, C; alg=alg_orth)
112+
dst_ACs[site] = regauge!(AC, C; alg = alg_orth)
100113
return nothing
101114
end
102115

103116
return ACs
104117
end
105118

106-
function envs_step!(it::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any,<:Tuple}, mps)
119+
function envs_step!(it::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any, <:Tuple}, mps)
107120
alg_environments = updatetol(it.alg_environments, state.iter, state.ϵ)
108121
return recalculate!(state.envs, mps, state.operator...; alg_environments.tol)
109122
end

src/algorithms/changebonds/changebonds.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ function changebonds end
1010
function changebonds! end
1111

1212
# write in terms of MultilineMPS
13-
function changebonds::InfiniteMPS, operator::InfiniteMPO, alg,
14-
envs=environments(ψ, operator))
15-
ψ′, envs′ = changebonds(convert(MultilineMPS, ψ), convert(MultilineMPO, operator), alg,
16-
Multiline([envs]))
13+
function changebonds(
14+
ψ::InfiniteMPS, operator::InfiniteMPO, alg, envs = environments(ψ, operator)
15+
)
16+
ψ′, envs′ = changebonds(
17+
convert(MultilineMPS, ψ), convert(MultilineMPO, operator), alg, Multiline([envs])
18+
)
1719
return convert(InfiniteMPS, ψ′), envs
1820
end
1921

0 commit comments

Comments
 (0)