Skip to content

Commit

Permalink
Fix tests some more
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrehmer committed Jul 11, 2024
1 parent eaa8506 commit bd77412
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 44 deletions.
6 changes: 4 additions & 2 deletions src/algorithms/ctmrg_gauge_fix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,14 @@ function check_elementwise_convergence(
ΔC = envfinal.corners .- envfix.corners
ΔCmax = norm(ΔC, Inf)
ΔCmean = norm(ΔC)
@debug "maxᵢⱼ|Cⁿ⁺¹ - Cⁿ|ᵢⱼ = $ΔCmax mean |Cⁿ⁺¹ - Cⁿ|ᵢⱼ = $ΔCmean"
# @debug "maxᵢⱼ|Cⁿ⁺¹ - Cⁿ|ᵢⱼ = $ΔCmax mean |Cⁿ⁺¹ - Cⁿ|ᵢⱼ = $ΔCmean"
println("maxᵢⱼ|Cⁿ⁺¹ - Cⁿ|ᵢⱼ = $ΔCmax mean |Cⁿ⁺¹ - Cⁿ|ᵢⱼ = $ΔCmean")

ΔT = envfinal.edges .- envfix.edges
ΔTmax = norm(ΔT, Inf)
ΔTmean = norm(ΔT)
@debug "maxᵢⱼ|Tⁿ⁺¹ - Tⁿ|ᵢⱼ = $ΔTmax mean |Tⁿ⁺¹ - Tⁿ|ᵢⱼ = $ΔTmean"
# @debug "maxᵢⱼ|Tⁿ⁺¹ - Tⁿ|ᵢⱼ = $ΔTmax mean |Tⁿ⁺¹ - Tⁿ|ᵢⱼ = $ΔTmean"
println("maxᵢⱼ|Tⁿ⁺¹ - Tⁿ|ᵢⱼ = $ΔTmax mean |Tⁿ⁺¹ - Tⁿ|ᵢⱼ = $ΔTmean")

# Check differences for all tensors in unit cell to debug properly
for (dir, r, c) in Iterators.product(axes(envfinal.edges)...)
Expand Down
2 changes: 1 addition & 1 deletion src/utility/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function ChainRulesCore.rrule(
alg::SVDAdjoint{F,R,B};
trunc::TruncationScheme=notrunc(),
p::Real=2,
) where {F<:Union{IterSVD,FixedSVD},R,B}
) where {F<:Union{IterSVD,FixedSVD},R<:Union{GMRES,BiCGStab,Arnoldi},B}
U, S, V, ϵ = PEPSKit.tsvd(t, alg; trunc, p)

function tsvd!_itersvd_pullback((ΔU, ΔS, ΔV, Δϵ))
Expand Down
1 change: 1 addition & 0 deletions test/ctmrg/fixediter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ unitcells = [(1, 1), (3, 4)]
@test check_elementwise_convergence(env_conv1, env_fixedsvd)
end

# TODO: Why doesn't FixedIter work with IterSVD?
##
# ctm_alg = CTMRG(;
# tol=1e-12,
Expand Down
12 changes: 8 additions & 4 deletions test/ctmrg/gaugefix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ using PEPSKit: ctmrg_iter, gauge_fix, check_elementwise_convergence

scalartypes = [Float64, ComplexF64]
unitcells = [(1, 1), (2, 2), (3, 2)]
# scalartypes = [ComplexF64]
# unitcells = [(2, 2)]
χ = 6

function _make_symmetric(psi)
Expand Down Expand Up @@ -43,16 +45,16 @@ end
psi = _make_symmetric(psi)

Random.seed!(987654321) # Seed RNG to make random environment consistent
# psi = InfinitePEPS(physical_space, peps_space; unitcell)
ctm = CTMRGEnv(psi; Venv=ctm_space)

verbosity = 1
alg = CTMRG(;
tol=1e-10, miniter=4, maxiter=400, verbosity, trscheme=truncdim(dim(ctm_space))
tol=1e-10, miniter=4, maxiter=5000, verbosity, trscheme=FixedSpaceTruncation()
)
alg_fixed = @set alg.projector_alg.trscheme = FixedSpaceTruncation()

ctm = leading_boundary(ctm, psi, alg)
ctm2, = ctmrg_iter(psi, ctm, alg_fixed)
ctm2, = ctmrg_iter(psi, ctm, alg)
ctm_fixed, = gauge_fix(ctm, ctm2)
@test PEPSKit.check_elementwise_convergence(ctm, ctm_fixed; atol=1e-4)
end
Expand All @@ -74,7 +76,9 @@ end
alg = CTMRG(;
tol=1e-10, miniter=4, maxiter=400, verbosity, trscheme=truncdim(dim(ctm_space))
)
alg_fixed = @set alg.projector_alg.trscheme = FixedSpaceTruncation()
alg_fixed = CTMRG(;
verbosity, svd_alg=alg.projector_alg.svd_alg, trscheme=FixedSpaceTruncation()
)

ctm = leading_boundary(ctm, psi, alg)
ctm2, = ctmrg_iter(psi, ctm, alg_fixed)
Expand Down
13 changes: 10 additions & 3 deletions test/ctmrg/gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@ models = [square_lattice_heisenberg(), square_lattice_pwave()]
names = ["Heisenberg", "p-wave superconductor"]
Random.seed!(42039482030)
tol = 1e-8
boundary_alg = CTMRG(; tol=tol, miniter=4, maxiter=100, verbosity=0, ctmrgscheme=:AllSides)
boundary_alg = CTMRG(;
tol=tol,
miniter=4,
maxiter=100,
verbosity=0,
ctmrgscheme=:AllSides,
svd_alg=SVDAdjoint(; fwd_alg=TensorKit.SVD(), rrule_alg=GMRES(; tol=1e-10)),
)
gradmodes = [
nothing,
GeomSum(; tol, iterscheme=:FixedIter),
GeomSum(; tol, iterscheme=:DiffGauge),
ManualIter(; tol, iterscheme=:FixedIter),
ManualIter(; tol, iterscheme=:DiffGauge),
LinSolve(; alg=KrylovKit.GMRES(; tol=tol, maxiter=10), iterscheme=:FixedIter),
LinSolve(; alg=KrylovKit.GMRES(; tol=tol, maxiter=10), iterscheme=:DiffGauge),
LinSolver(; solver=KrylovKit.GMRES(; tol=tol, maxiter=10), iterscheme=:FixedIter),
LinSolver(; solver=KrylovKit.GMRES(; tol=tol, maxiter=10), iterscheme=:DiffGauge),
]
steps = -0.01:0.005:0.01

Expand Down
69 changes: 36 additions & 33 deletions test/ctmrg/leftmoves_allsides.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,44 @@ using PEPSKit
χenv = 16
ctm_alg_leftmoves = CTMRG(; tol=1e-10, verbosity=1, ctmrgscheme=:LeftMoves)
ctm_alg_allsides = CTMRG(; tol=1e-10, verbosity=1, ctmrgscheme=:AllSides)
unitcells = [(1, 1), (3, 4)]

# compute environments
Random.seed!(32350283290358)
psi = InfinitePEPS(2, χbond)
env_leftmoves = leading_boundary(
CTMRGEnv(psi; Venv=ComplexSpace(χenv)), psi, ctm_alg_leftmoves
)
env_allsides = leading_boundary(
CTMRGEnv(psi; Venv=ComplexSpace(χenv)), psi, ctm_alg_allsides
)
@testset "$(unitcell) unit cell" for unitcell in unitcells
# compute environments
Random.seed!(32350283290358)
psi = InfinitePEPS(2, χbond; unitcell)
env_leftmoves = leading_boundary(
CTMRGEnv(psi; Venv=ComplexSpace(χenv)), psi, ctm_alg_leftmoves
)
env_allsides = leading_boundary(
CTMRGEnv(psi; Venv=ComplexSpace(χenv)), psi, ctm_alg_allsides
)

# compare norms
@test abs(norm(psi, env_leftmoves)) abs(norm(psi, env_allsides)) rtol = 1e-6
# compare norms
@test abs(norm(psi, env_leftmoves)) abs(norm(psi, env_allsides)) rtol = 1e-6

# compare singular values
CS_leftmoves = map(c -> tsvd(c; alg=TensorKit.SVD())[2], env_leftmoves.corners)
CS_allsides = map(c -> tsvd(c; alg=TensorKit.SVD())[2], env_allsides.corners)
ΔCS = maximum(zip(CS_leftmoves, CS_allsides)) do (c_lm, c_as)
smallest = infimum(MPSKit._firstspace(c_lm), MPSKit._firstspace(c_as))
e_old = isometry(MPSKit._firstspace(c_lm), smallest)
e_new = isometry(MPSKit._firstspace(c_as), smallest)
return norm(e_new' * c_as * e_new - e_old' * c_lm * e_old)
end
@test ΔCS < 1e-3
# compare singular values
CS_leftmoves = map(c -> tsvd(c; alg=TensorKit.SVD())[2], env_leftmoves.corners)
CS_allsides = map(c -> tsvd(c; alg=TensorKit.SVD())[2], env_allsides.corners)
ΔCS = maximum(zip(CS_leftmoves, CS_allsides)) do (c_lm, c_as)
smallest = infimum(MPSKit._firstspace(c_lm), MPSKit._firstspace(c_as))
e_old = isometry(MPSKit._firstspace(c_lm), smallest)
e_new = isometry(MPSKit._firstspace(c_as), smallest)
return norm(e_new' * c_as * e_new - e_old' * c_lm * e_old)
end
@test ΔCS < 1e-2

ΔTS = maximum(zip(TS_leftmoves, TS_allsides)) do (t_lm, t_as)
MPSKit._firstspace(t_lm) == MPSKit._firstspace(t_as) || return scalartype(t_lm)(Inf)
return norm(t_as - t_lm)
end
TS_leftmoves = map(t -> tsvd(t; alg=TensorKit.SVD())[2], env_leftmoves.edges)
TS_allsides = map(t -> tsvd(t; alg=TensorKit.SVD())[2], env_allsides.edges)
@test ΔTS < 1e-3
TS_leftmoves = map(t -> tsvd(t; alg=TensorKit.SVD())[2], env_leftmoves.edges)
TS_allsides = map(t -> tsvd(t; alg=TensorKit.SVD())[2], env_allsides.edges)
ΔTS = maximum(zip(TS_leftmoves, TS_allsides)) do (t_lm, t_as)
MPSKit._firstspace(t_lm) == MPSKit._firstspace(t_as) || return scalartype(t_lm)(Inf)
return norm(t_as - t_lm)
end
@test ΔTS < 1e-2

# compare Heisenberg energies
H = square_lattice_heisenberg()
E_leftmoves = costfun(psi, env_leftmoves, H)
E_allsides = costfun(psi, env_allsides, H)
@test E_leftmoves E_allsides rtol=1e-6
# compare Heisenberg energies
H = square_lattice_heisenberg(; unitcell)
E_leftmoves = costfun(psi, env_leftmoves, H)
E_allsides = costfun(psi, env_allsides, H)
@test E_leftmoves E_allsides rtol=1e-4
end
2 changes: 1 addition & 1 deletion test/ctmrg/unitcell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ end
env = CTMRGEnv(corners, edges)

# apply one CTMRG iteration with fixeds
ctm_alg = CTMRG(; fixedspace=true)
ctm_alg = CTMRG(; trscheme=FixedSpaceTruncation())
env′, = ctmrg_iter(peps, env, ctm_alg)

# compute random expecation value to test matching bonds
Expand Down

0 comments on commit bd77412

Please sign in to comment.