Skip to content

Commit 83cf3e6

Browse files
lkdvosleburgel
andauthored
Refactor derivatives and add JordanMPOTensor (#270)
* make derivatives it's own folder * Rewrite single-site hamiltonian * Add benchmark script * gitignore dev/ * Start twosite derivative * start playing with JordanMPOTensor * Contraction fixes * JordanMPO fixes * Start simplified implementations * Add support to `force_planar` for testing * Fix derivative * remove stray contractcheck * Start supporting finite systems * First attempt at twosite version * Reorganize derivatives * Improve jordanmpotensor * various fixes * make warnings toggleable * signature fixes * small fixes * Fix periodic boundary * Make SVD algorithm accessible and trscheme mandatory * small improvement hamiltonian derivative * Fix bad merge * Add required kwarg in test * Fix `nonzero_length` * restrict `ham_derivatives` types * Reorganize derivatives * remove BlockTensorKit code duplication * implement fallback case that should never happen * Add space assertions in constructor * implement some mpotensor kernels * `copy(::JordanMPOTensor)` * Go all in and replace all MPOHamiltonian objects * Bump version * Remove unused files * Formatter * move `ac_proj` and friends * consistent naming for Projection_derivatives * Apply suggestions from code review Co-authored-by: Lander Burgelman <39218680+leburgel@users.noreply.github.com> * move some definitons around * standardize ham derivative property names * Add `JordanMPOTensor` space checks * No default `trscheme` * Remove obsolete code * Remove obsolete to do --------- Co-authored-by: Lander Burgelman <39218680+leburgel@users.noreply.github.com>
1 parent 9fe20ae commit 83cf3e6

27 files changed

Lines changed: 1747 additions & 494 deletions

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MPSKit"
22
uuid = "bb1c41ca-d63c-52ed-829e-0820dda26502"
33
authors = "Lukas Devos, Maarten Van Damme and contributors"
4-
version = "0.12.6"
4+
version = "0.13.0-DEV"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
@@ -25,7 +25,7 @@ VectorInterface = "409d34a3-91d5-4945-b6ec-7529ddf182d8"
2525
[compat]
2626
Accessors = "0.1"
2727
Aqua = "0.8.9"
28-
BlockTensorKit = "0.1.4"
28+
BlockTensorKit = "0.1.6"
2929
Combinatorics = "1"
3030
Compat = "3.47, 4.10"
3131
DocStringExtensions = "0.9.3"

src/MPSKit.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ using Compat: @compat
6060
using TensorKit
6161
using TensorKit: BraidingTensor
6262
using BlockTensorKit
63+
using BlockTensorKit: TensorMapSumSpace
6364
using TensorOperations
6465
using KrylovKit
6566
using KrylovKit: KrylovAlgorithm
@@ -75,7 +76,7 @@ using DocStringExtensions
7576
using LinearAlgebra: diag, Diagonal
7677
using LinearAlgebra: LinearAlgebra
7778
using Random
78-
using Base: @kwdef
79+
using Base: @kwdef, @propagate_inbounds
7980
using LoggingExtras
8081
using OhMyThreads
8182

@@ -112,6 +113,7 @@ include("states/ortho.jl")
112113

113114
include("operators/abstractmpo.jl")
114115
include("operators/mpo.jl")
116+
include("operators/jordanmpotensor.jl")
115117
include("operators/mpohamiltonian.jl") # the mpohamiltonian objects
116118
include("operators/multilinempo.jl")
117119
include("operators/projection.jl")
@@ -131,7 +133,10 @@ include("environments/multiple_envs.jl")
131133
include("environments/lazylincocache.jl")
132134

133135
include("algorithms/fixedpoint.jl")
134-
include("algorithms/derivatives.jl")
136+
include("algorithms/derivatives/derivatives.jl")
137+
include("algorithms/derivatives/mpo_derivatives.jl")
138+
include("algorithms/derivatives/hamiltonian_derivatives.jl")
139+
include("algorithms/derivatives/projection_derivatives.jl")
135140
include("algorithms/expval.jl")
136141
include("algorithms/toolbox.jl")
137142
include("algorithms/grassmann.jl")

src/algorithms/approximate/idmrg.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
7272
for col in 1:size(ψ, 2)
7373
for row in 1:size(ψ, 1)
7474
AC2′ = ac2_proj(row, col, ψ, toapprox, envs)
75-
al, c, ar, = tsvd!(AC2′; trunc=alg.trscheme, alg=TensorKit.SVD())
75+
al, c, ar, = tsvd!(AC2′; trunc=alg.trscheme, alg=alg.alg_svd)
7676
normalize!(c)
7777

7878
ψ.AL[row + 1, col] = al
@@ -90,10 +90,8 @@ function approximate!(ψ::MultilineMPS, toapprox::Tuple{<:MultilineMPO,<:Multili
9090
for row in 1:size(ψ, 1)
9191
# TODO: also write this as ac2_proj?
9292
AC2 = ϕ.AL[row, col] * _transpose_tail.AC[row, col + 1])
93-
AC2′ = ∂AC2(AC2, O[row, col], O[row, col + 1],
94-
leftenv(envs[row], col, ψ[row]),
95-
rightenv(envs[row], col, ψ[row]))
96-
al, c, ar, = tsvd!(AC2′; trunc=alg.trscheme, alg=TensorKit.SVD())
93+
AC2′ = ∂∂AC2(row, col, ψ, O, envs) * AC2
94+
al, c, ar, = tsvd!(AC2′; trunc=alg.trscheme, alg=alg.alg_svd)
9795
normalize!(c)
9896

9997
ψ.AL[row + 1, col] = al

src/algorithms/changebonds/optimalexpand.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ dominant contributions of a two-site updated MPS tensor, orthogonal to the origi
99
1010
$(TYPEDFIELDS)
1111
"""
12-
@kwdef struct OptimalExpand <: Algorithm
12+
@kwdef struct OptimalExpand{S} <: Algorithm
13+
"algorithm used for the singular value decomposition"
14+
alg_svd::S = Defaults.alg_svd()
15+
1316
"algorithm used for truncating the expanded space"
14-
trscheme::TruncationScheme = truncdim(1)
17+
trscheme::TruncationScheme
1518
end
1619

1720
function changebonds::InfiniteMPS, H::InfiniteMPOHamiltonian, alg::OptimalExpand,
@@ -28,7 +31,7 @@ function changebonds(ψ::InfiniteMPS, H::InfiniteMPOHamiltonian, alg::OptimalExp
2831
VL = leftnull.AL[i])
2932
VR = rightnull!(_transpose_tail.AR[i + 1]))
3033
intermediate = adjoint(VL) * AC2 * adjoint(VR)
31-
U, _, V, = tsvd!(intermediate; trunc=alg.trscheme, alg=SVD())
34+
U, _, V, = tsvd!(intermediate; trunc=alg.trscheme, alg=alg.alg_svd)
3235

3336
AL′[i] = VL * U
3437
AR′[i + 1] = V * VR
@@ -54,7 +57,7 @@ function changebonds(ψ::MultilineMPS, H, alg::OptimalExpand, envs=environments(
5457
VL = leftnull.AL[i, j])
5558
VR = rightnull!(_transpose_tail.AR[i, j + 1]))
5659
intermediate = adjoint(VL) * AC2 * adjoint(VR)
57-
U, _, V, = tsvd!(intermediate; trunc=alg.trscheme, alg=SVD())
60+
U, _, V, = tsvd!(intermediate; trunc=alg.trscheme, alg=alg.alg_svd)
5861

5962
AL′[i, j] = VL * U
6063
AR′[i, j + 1] = V * VR
@@ -85,7 +88,7 @@ function changebonds!(ψ::AbstractFiniteMPS, H, alg::OptimalExpand, envs=environ
8588

8689
#Use this nullspaces and SVD decomposition to determine the optimal expansion space
8790
intermediate = adjoint(NL) * AC2 * adjoint(NR)
88-
_, _, V, = tsvd!(intermediate; trunc=alg.trscheme, alg=SVD())
91+
_, _, V, = tsvd!(intermediate; trunc=alg.trscheme, alg=alg.alg_svd)
8992

9093
ar_re = V * NR
9194
ar_le = zerovector!(similar(ar_re, codomain.AC[i]) space(V, 1)))

src/algorithms/changebonds/randexpand.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ two-site MPS tensor, which is made orthogonal to the existing state.
99
1010
$(TYPEDFIELDS)
1111
"""
12-
@kwdef struct RandExpand <: Algorithm
12+
@kwdef struct RandExpand{S} <: Algorithm
13+
"algorithm used for the singular value decomposition"
14+
alg_svd::S = Defaults.alg_svd()
15+
1316
"algorithm used for [truncation](@extref TensorKit.tsvd] the expanded space"
14-
trscheme::TruncationScheme = truncdim(1)
17+
trscheme::TruncationScheme
1518
end
1619

1720
function changebonds::InfiniteMPS, alg::RandExpand)
@@ -26,7 +29,7 @@ function changebonds(ψ::InfiniteMPS, alg::RandExpand)
2629
VL = leftnull.AL[i])
2730
VR = rightnull!(_transpose_tail.AR[i + 1]))
2831
intermediate = adjoint(VL) * AC2 * adjoint(VR)
29-
U, _, V, = tsvd!(intermediate; trunc=alg.trscheme, alg=SVD())
32+
U, _, V, = tsvd!(intermediate; trunc=alg.trscheme, alg=alg.alg_svd)
3033

3134
AL′[i] = VL * U
3235
AR′[i + 1] = V * VR
@@ -50,7 +53,7 @@ function changebonds!(ψ::AbstractFiniteMPS, alg::RandExpand)
5053

5154
#Use this nullspaces and SVD decomposition to determine the optimal expansion space
5255
intermediate = adjoint(NL) * AC2 * adjoint(NR)
53-
_, _, V, = tsvd!(intermediate; trunc=alg.trscheme, alg=SVD())
56+
_, _, V, = tsvd!(intermediate; trunc=alg.trscheme, alg=alg.alg_svd)
5457

5558
ar_re = V * NR
5659
ar_le = zerovector!(similar(ar_re, codomain.AC[i]) space(V, 1)))

src/algorithms/changebonds/svdcut.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ An algorithm that uses truncated SVD to change the bond dimension of a ψ.
77
88
$(TYPEDFIELDS)
99
"""
10-
@kwdef struct SvdCut <: Algorithm
10+
@kwdef struct SvdCut{S} <: Algorithm
11+
"algorithm used for the singular value decomposition"
12+
alg_svd::S = Defaults.alg_svd()
13+
1114
"algorithm used for [truncation][@extref TensorKit.tsvd] of the gauge tensors"
12-
trscheme::TruncationScheme = notrunc()
15+
trscheme::TruncationScheme
1316
end
1417

1518
function changebonds::AbstractFiniteMPS, alg::SvdCut; kwargs...)
1619
return changebonds!(copy(ψ), alg; kwargs...)
1720
end
1821
function changebonds!::AbstractFiniteMPS, alg::SvdCut; normalize::Bool=true)
1922
for i in (length(ψ) - 1):-1:1
20-
U, S, V, = tsvd.C[i]; trunc=alg.trscheme, alg=TensorKit.SVD())
23+
U, S, V, = tsvd.C[i]; trunc=alg.trscheme, alg=alg.alg_svd)
2124
AL′ = ψ.AL[i] * U
2225
ψ.AC[i] = (AL′, complex(S))
2326
AR′ = _transpose_front(V * _transpose_tail.AR[i + 1]))
@@ -41,7 +44,7 @@ function changebonds!(mpo::FiniteMPO, alg::SvdCut)
4144
O_left = transpose(mpo[1], ((3, 1, 2), (4,)))
4245
local O_right
4346
for i in 2:length(mpo)
44-
U, S, V, = tsvd!(O_left; trunc=alg.trscheme, alg=TensorKit.SVD())
47+
U, S, V, = tsvd!(O_left; trunc=alg.trscheme, alg=alg.alg_svd)
4548
@inbounds mpo[i - 1] = transpose(U, ((2, 3), (1, 4)))
4649
if i < length(mpo)
4750
@plansor O_left[-3 -1 -2; -4] := S[-1; 1] * V[1; 2] * mpo[i][2 -2; -3 -4]
@@ -52,7 +55,7 @@ function changebonds!(mpo::FiniteMPO, alg::SvdCut)
5255

5356
# right to left
5457
for i in (length(mpo) - 1):-1:1
55-
U, S, V, = tsvd!(O_right; trunc=alg.trscheme, alg=TensorKit.SVD())
58+
U, S, V, = tsvd!(O_right; trunc=alg.trscheme, alg=alg.alg_svd)
5659
@inbounds mpo[i + 1] = transpose(V, ((1, 4), (2, 3)))
5760
if i > 1
5861
@plansor O_right[-1; -3 -4 -2] := mpo[i][-1 -2; -3 2] * U[2; 1] * S[1; -4]
@@ -81,7 +84,7 @@ function changebonds(ψ::InfiniteMPS, alg::SvdCut)
8184
ncr = ψ.C[1]
8285

8386
for i in 1:length(ψ)
84-
U, ncr, = tsvd.C[i]; trunc=alg.trscheme, alg=TensorKit.SVD())
87+
U, ncr, = tsvd.C[i]; trunc=alg.trscheme, alg=alg.alg_svd)
8588
copied[i] = copied[i] * U
8689
copied[i + 1] = _transpose_front(U' * _transpose_tail(copied[i + 1]))
8790
end

src/algorithms/changebonds/vumpssvd.jl

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ An algorithm that uses a two-site update step to change the bond dimension of a
88
$(TYPEDFIELDS)
99
"""
1010
@kwdef struct VUMPSSvdCut <: Algorithm
11-
"tolerance for gauging algorithm"
12-
tol_gauge = Defaults.tolgauge
13-
"tolerance for the eigenvalue solver"
14-
tol_eigenval = Defaults.tol
11+
"algorithm used for gauging the `InfiniteMPS`"
12+
alg_gauge = Defaults.alg_gauge(; dynamic_tols=false)
13+
14+
"algorithm used for the eigenvalue solvers"
15+
alg_eigsolve = Defaults.alg_eigsolve(; dynamic_tols=false)
16+
17+
"algorithm used for the singular value decomposition"
18+
alg_svd = Defaults.alg_svd()
19+
1520
"algorithm used for [truncation][@extref TensorKit.tsvd] of the two-site update"
16-
trscheme::TruncationScheme = notrunc()
21+
trscheme::TruncationScheme
1722
end
1823

1924
function changebonds_1(state::InfiniteMPS, H, alg::VUMPSSvdCut,
@@ -31,11 +36,12 @@ function changebonds_1(state::InfiniteMPS, H, alg::VUMPSSvdCut,
3136

3237
# collapse back to 1 site
3338
if D2 != D1
34-
(nstate, nenvs) = changebonds(nstate, nH,
35-
SvdCut(; trscheme=truncspace(infimum(D1, D2))), nenvs)
39+
cut_alg = SvdCut(; alg.alg_svd, trscheme=truncspace(infimum(D1, D2)))
40+
nstate, nenvs = changebonds(nstate, nH, cut_alg, nenvs)
3641
end
3742

38-
collapsed = InfiniteMPS([nstate.AL[1]], nstate.C[1]; tol=alg.tol_gauge)
43+
collapsed = InfiniteMPS([nstate.AL[1]], nstate.C[1]; alg.alg_gauge.tol,
44+
alg.alg_gauge.maxiter)
3945
recalculate!(envs, collapsed, H, collapsed)
4046

4147
return collapsed, envs
@@ -47,17 +53,13 @@ function changebonds_n(state::InfiniteMPS, H, alg::VUMPSSvdCut, envs=environment
4753
@plansor AC2[-1 -2; -3 -4] := state.AC[loc][-1 -2; 1] * state.AR[loc + 1][1 -4; -3]
4854

4955
h_ac2 = ∂∂AC2(loc, state, H, envs)
50-
(vals, vecs, _) = eigsolve(h_ac2, AC2, 1, :SR; tol=alg.tol_eigenval,
51-
ishermitian=false)
52-
nAC2 = vecs[1]
56+
_, nAC2 = fixedpoint(h_ac2, AC2, :SR, alg.alg_eigsolve)
5357

5458
h_c = ∂∂C(loc + 1, state, H, envs)
55-
(vals, vecs, _) = eigsolve(h_c, state.C[loc + 1], 1, :SR; tol=alg.tol_eigenval,
56-
ishermitian=false)
57-
nC2 = vecs[1]
59+
_, nC2 = fixedpoint(h_c, state.C[loc + 1], :SR, alg.alg_eigsolve)
5860

5961
#svd ac2, get new AL1 and S,V ---> AC
60-
(AL1, S, V, eps) = tsvd(nAC2; trunc=alg.trscheme, alg=TensorKit.SVD())
62+
AL1, S, V, eps = tsvd!(nAC2; trunc=alg.trscheme, alg=alg.alg_svd)
6163
@plansor AC[-1 -2; -3] := S[-1; 1] * V[1; -3 -2]
6264
meps = max(eps, meps)
6365

@@ -72,16 +74,13 @@ function changebonds_n(state::InfiniteMPS, H, alg::VUMPSSvdCut, envs=environment
7274
copied = copy(state.AL)
7375
copied[loc] = AL1
7476
copied[loc + 1] = AL2
75-
state = InfiniteMPS(copied; tol=alg.tol_gauge)
77+
state = InfiniteMPS(copied; alg.alg_gauge.tol, alg.alg_gauge.maxiter)
7678
recalculate!(envs, state, H, state)
7779
end
7880
return state, envs
7981
end
8082

8183
function changebonds(state::InfiniteMPS, H, alg::VUMPSSvdCut, envs=environments(state, H))
82-
if (length(state) == 1)
83-
return changebonds_1(state, H, alg, envs)
84-
else
85-
return changebonds_n(state, H, alg, envs)
86-
end
84+
return length(state) == 1 ? changebonds_1(state, H, alg, envs) :
85+
changebonds_n(state, H, alg, envs)
8786
end

0 commit comments

Comments
 (0)