Skip to content

[WIP] Runic formatter #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/algorithms/ED.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ equivalent to dense eigenvectors.
of all the physical spaces in the system.

"""
function exact_diagonalization(H::FiniteMPOHamiltonian;
sector=one(sectortype(H)),
num::Int=1, which::Symbol=:SR,
alg=Defaults.alg_eigsolve(; dynamic_tols=false))
function exact_diagonalization(
H::FiniteMPOHamiltonian;
sector = one(sectortype(H)), num::Int = 1, which::Symbol = :SR,
alg = Defaults.alg_eigsolve(; dynamic_tols = false)
)
L = length(H)
@assert L > 1 "FiniteMPOHamiltonian must have length > 1"
middle_site = (L >> 1) + 1
Expand All @@ -42,7 +43,7 @@ function exact_diagonalization(H::FiniteMPOHamiltonian;
TA = tensormaptype(spacetype(H), 2, 1, T)

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

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

# center
ACs = Vector{Union{Missing,TA}}(missing, L)
ACs = Vector{Union{Missing, TA}}(missing, L)
P = physicalspace(H, middle_site)
ACs[middle_site] = rand!(similar(ALs[1], left ⊗ P ← right))

TB = tensormaptype(spacetype(H), 1, 1, T)
Cs = Vector{Union{Missing,TB}}(missing, L + 1)
Cs = Vector{Union{Missing, TB}}(missing, L + 1)
state = FiniteMPS(ALs, ARs, ACs, Cs)
envs = environments(state, H)

Expand Down
40 changes: 24 additions & 16 deletions src/algorithms/approximate/approximate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,41 @@ of an MPS `ψ₀`.
approximate, approximate!

# implementation in terms of Multiline
function approximate(ψ::InfiniteMPS,
toapprox::Tuple{<:InfiniteMPO,<:InfiniteMPS},
algorithm,
envs=environments(ψ, toapprox))
function approximate(
ψ::InfiniteMPS, toapprox::Tuple{<:InfiniteMPO, <:InfiniteMPS}, algorithm,
envs = environments(ψ, toapprox)
)
envs′ = Multiline([envs])
multi, envs = approximate(convert(MultilineMPS, ψ),
(convert(MultilineMPO, toapprox[1]),
convert(MultilineMPS, toapprox[2])), algorithm, envs′)
multi, envs = approximate(
convert(MultilineMPS, ψ),
(convert(MultilineMPO, toapprox[1]), convert(MultilineMPS, toapprox[2])),
algorithm,
envs′
)
ψ = convert(InfiniteMPS, multi)
return ψ, envs
end

# dispatch to in-place method
function approximate(ψ, toapprox, alg::Union{DMRG,DMRG2,IDMRG,IDMRG2},
envs=environments(ψ, toapprox))
function approximate(
ψ, toapprox, alg::Union{DMRG, DMRG2, IDMRG, IDMRG2},
envs = environments(ψ, toapprox)
)
return approximate!(copy(ψ), toapprox, alg, envs)
end

# disambiguate
function approximate(ψ::InfiniteMPS,
toapprox::Tuple{<:InfiniteMPO,<:InfiniteMPS},
algorithm::Union{IDMRG,IDMRG2},
envs=environments(ψ, toapprox))
function approximate(
ψ::InfiniteMPS, toapprox::Tuple{<:InfiniteMPO, <:InfiniteMPS},
algorithm::Union{IDMRG, IDMRG2}, envs = environments(ψ, toapprox)
)
envs′ = Multiline([envs])
multi, envs = approximate(convert(MultilineMPS, ψ),
(convert(MultilineMPO, toapprox[1]),
convert(MultilineMPS, toapprox[2])), algorithm, envs′)
multi, envs = approximate(
convert(MultilineMPS, ψ),
(convert(MultilineMPO, toapprox[1]), convert(MultilineMPS, toapprox[2])),
algorithm,
envs′
)
ψ = convert(InfiniteMPS, multi)
return ψ, envs
end
11 changes: 5 additions & 6 deletions src/algorithms/approximate/fvomps.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function approximate!(ψ::AbstractFiniteMPS, Oϕ, alg::DMRG2,
envs=environments(ψ, Oϕ))
function approximate!(ψ::AbstractFiniteMPS, Oϕ, alg::DMRG2, envs = environments(ψ, Oϕ))
ϵ::Float64 = 2 * alg.tol
log = IterLog("DMRG2")

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

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

# finalize
ψ, envs = alg.finalize(iter, ψ, Oϕ, envs)::Tuple{typeof(ψ),typeof(envs)}
ψ, envs = alg.finalize(iter, ψ, Oϕ, envs)::Tuple{typeof(ψ), typeof(envs)}

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

function approximate!(ψ::AbstractFiniteMPS, Oϕ, alg::DMRG, envs=environments(ψ, Oϕ))
function approximate!(ψ::AbstractFiniteMPS, Oϕ, alg::DMRG, envs = environments(ψ, Oϕ))
ϵ::Float64 = 2 * alg.tol
log = IterLog("DMRG")

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

# finalize
ψ, envs = alg.finalize(iter, ψ, Oϕ, envs)::Tuple{typeof(ψ),typeof(envs)}
ψ, envs = alg.finalize(iter, ψ, Oϕ, envs)::Tuple{typeof(ψ), typeof(envs)}

if ϵ < alg.tol
@infov 2 logfinish!(log, iter, ϵ)
Expand Down
41 changes: 25 additions & 16 deletions src/algorithms/approximate/idmrg.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:MultilineMPS},
alg::IDMRG, envs=environments(ψ, toapprox))
function approximate!(
ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO, <:MultilineMPS}, alg::IDMRG,
envs = environments(ψ, toapprox)
)
log = IterLog("IDMRG")
ϵ::Float64 = 2 * alg.tol
local iter
Expand All @@ -12,8 +14,9 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
# left to right sweep
for col in 1:size(ψ, 2)
for row in 1:size(ψ, 1)
ψ.AC[row + 1, col] = AC_projection(CartesianIndex(row, col), ψ,
toapprox, envs)
ψ.AC[row + 1, col] = AC_projection(
CartesianIndex(row, col), ψ, toapprox, envs
)
normalize!(ψ.AC[row + 1, col])
ψ.AL[row + 1, col], ψ.C[row + 1, col] = leftorth!(ψ.AC[row + 1, col])
end
Expand All @@ -23,11 +26,11 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
# right to left sweep
for col in reverse(1:size(ψ, 2))
for row in 1:size(ψ, 1)
ψ.AC[row + 1, col] = AC_projection(CartesianIndex(row, col),
ψ, toapprox, envs)
ψ.AC[row + 1, col] = AC_projection(
CartesianIndex(row, col), ψ, toapprox, envs
)
normalize!(ψ.AC[row + 1, col])
ψ.C[row + 1, col - 1], temp = rightorth!(_transpose_tail(ψ.AC[row + 1,
col]))
ψ.C[row + 1, col - 1], temp = rightorth!(_transpose_tail(ψ.AC[row + 1, col]))
ψ.AR[row + 1, col] = _transpose_front(temp)
end
transfer_rightenv!(envs, ψ, toapprox, col - 1)
Expand Down Expand Up @@ -57,8 +60,10 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
return ψ, envs, ϵ
end

function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:MultilineMPS},
alg::IDMRG2, envs=environments(ψ, toapprox))
function approximate!(
ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO, <:MultilineMPS},
alg::IDMRG2, envs = environments(ψ, toapprox)
)
size(ψ, 2) < 2 && throw(ArgumentError("unit cell should be >= 2"))
ϵ::Float64 = 2 * alg.tol
log = IterLog("IDMRG2")
Expand All @@ -73,9 +78,11 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
# sweep from left to right
for site in 1:size(ψ, 2)
for row in 1:size(ψ, 1)
AC2′ = AC2_projection(CartesianIndex(row, site), ψ, toapprox, envs;
kind=:ACAR)
al, c, ar, = tsvd!(AC2′; trunc=alg.trscheme, alg=alg.alg_svd)
AC2′ = AC2_projection(
CartesianIndex(row, site), ψ, toapprox, envs;
kind = :ACAR
)
al, c, ar, = tsvd!(AC2′; trunc = alg.trscheme, alg = alg.alg_svd)
normalize!(c)

ψ.AL[row + 1, site] = al
Expand All @@ -93,9 +100,11 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
# sweep from right to left
for site in reverse(0:(size(ψ, 2) - 1))
for row in 1:size(ψ, 1)
AC2′ = AC2_projection(CartesianIndex(row, site), ψ, toapprox, envs;
kind=:ALAC)
al, c, ar, = tsvd!(AC2′; trunc=alg.trscheme, alg=alg.alg_svd)
AC2′ = AC2_projection(
CartesianIndex(row, site), ψ, toapprox, envs;
kind = :ALAC
)
al, c, ar, = tsvd!(AC2′; trunc = alg.trscheme, alg = alg.alg_svd)
normalize!(c)

ψ.AL[row + 1, site] = al
Expand Down
71 changes: 42 additions & 29 deletions src/algorithms/approximate/vomps.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
Base.@deprecate(approximate(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:MultilineMPS},
alg::VUMPS, envs...; kwargs...),
approximate(ψ, toapprox,
VOMPS(; alg.tol, alg.maxiter, alg.finalize,
alg.verbosity, alg.alg_gauge, alg.alg_environments),
envs...; kwargs...))

function approximate(mps::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:MultilineMPS},
alg::VOMPS, envs=environments(mps, toapprox))
Base.@deprecate(
approximate(
ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO, <:MultilineMPS}, alg::VUMPS, envs...;
kwargs...
),
approximate(
ψ, toapprox,
VOMPS(; alg.tol, alg.maxiter, alg.finalize, alg.verbosity, alg.alg_gauge, alg.alg_environments),
envs...; kwargs...
)
)

function approximate(
mps::MultilineMPS, toapprox::Tuple{<:MultilineMPO, <:MultilineMPS}, alg::VOMPS,
envs = environments(mps, toapprox)
)
log = IterLog("VOMPS")
iter = 0
ϵ = calc_galerkin(mps, toapprox..., envs)
Expand Down Expand Up @@ -38,7 +45,7 @@ end

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

function localupdate_step!(::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any,<:Tuple},
::SerialScheduler)
function localupdate_step!(
::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any, <:Tuple}, ::SerialScheduler
)
alg_orth = QRpos()

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

foreach(eachsite(state.mps)) do site
AC = circshift([AC_projection(CartesianIndex(row, site), state.mps, state.operator,
state.envs)
for row in 1:size(state.mps, 1)], 1)
C = circshift([C_projection(CartesianIndex(row, site), state.mps, state.operator,
state.envs)
for row in 1:size(state.mps, 1)], 1)
dst_ACs[site] = regauge!(AC, C; alg=alg_orth)
AC = map(1:size(state.mps, 1)) do row
AC_projection(CartesianIndex(row, site), state.mps, state.operator, state.envs)
end
circshift!(AC, 1)
C = map(1:size(state.mps, 1)) do row
C_projection(CartesianIndex(row, site), state.mps, state.operator, state.envs)
end
circshift!(C, 1)
dst_ACs[site] = regauge!(AC, C; alg = alg_orth)
return nothing
end

return ACs
end
function localupdate_step!(::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any,<:Tuple},
scheduler)
function localupdate_step!(
::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any, <:Tuple}, scheduler
)
alg_orth = QRpos()

ACs = similar(state.mps.AC)
Expand All @@ -86,24 +97,26 @@ function localupdate_step!(::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any,<
local AC, C
@sync begin
Threads.@spawn begin
AC = circshift([AC_projection(CartesianIndex(row, site), state.mps,
state.operator, state.envs)
for row in 1:size(state.mps, 1)], 1)
AC = map(1:size(state.mps, 1)) do row
AC_projection(CartesianIndex(row, site), state.mps, state.operator, state.envs)
end
circshift!(AC, 1)
end
Threads.@spawn begin
C = circshift([C_projection(CartesianIndex(row, site), state.mps,
state.operator, state.envs)
for row in 1:size(state.mps, 1)], 1)
C = map(1:size(state.mps, 1)) do row
C_projection(CartesianIndex(row, site), state.mps, state.operator, state.envs)
end
circshift!(C, 1)
end
end
dst_ACs[site] = regauge!(AC, C; alg=alg_orth)
dst_ACs[site] = regauge!(AC, C; alg = alg_orth)
return nothing
end

return ACs
end

function envs_step!(it::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any,<:Tuple}, mps)
function envs_step!(it::IterativeSolver{<:VOMPS}, state::VOMPSState{<:Any, <:Tuple}, mps)
alg_environments = updatetol(it.alg_environments, state.iter, state.ϵ)
return recalculate!(state.envs, mps, state.operator...; alg_environments.tol)
end
10 changes: 6 additions & 4 deletions src/algorithms/changebonds/changebonds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ function changebonds end
function changebonds! end

# write in terms of MultilineMPS
function changebonds(ψ::InfiniteMPS, operator::InfiniteMPO, alg,
envs=environments(ψ, operator))
ψ′, envs′ = changebonds(convert(MultilineMPS, ψ), convert(MultilineMPO, operator), alg,
Multiline([envs]))
function changebonds(
ψ::InfiniteMPS, operator::InfiniteMPO, alg, envs = environments(ψ, operator)
)
ψ′, envs′ = changebonds(
convert(MultilineMPS, ψ), convert(MultilineMPO, operator), alg, Multiline([envs])
)
return convert(InfiniteMPS, ψ′), envs
end

Expand Down
Loading
Loading