From bd774122e52d6bcc09a9bedf786836306d62d6b5 Mon Sep 17 00:00:00 2001 From: Paul Brehmer Date: Thu, 11 Jul 2024 16:37:00 +0200 Subject: [PATCH] Fix tests some more --- src/algorithms/ctmrg_gauge_fix.jl | 6 ++- src/utility/svd.jl | 2 +- test/ctmrg/fixediter.jl | 1 + test/ctmrg/gaugefix.jl | 12 ++++-- test/ctmrg/gradients.jl | 13 ++++-- test/ctmrg/leftmoves_allsides.jl | 69 ++++++++++++++++--------------- test/ctmrg/unitcell.jl | 2 +- 7 files changed, 61 insertions(+), 44 deletions(-) diff --git a/src/algorithms/ctmrg_gauge_fix.jl b/src/algorithms/ctmrg_gauge_fix.jl index 640d2ff0..fdf3dd91 100644 --- a/src/algorithms/ctmrg_gauge_fix.jl +++ b/src/algorithms/ctmrg_gauge_fix.jl @@ -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)...) diff --git a/src/utility/svd.jl b/src/utility/svd.jl index a848eadc..fac11254 100644 --- a/src/utility/svd.jl +++ b/src/utility/svd.jl @@ -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, Δϵ)) diff --git a/test/ctmrg/fixediter.jl b/test/ctmrg/fixediter.jl index 3e8a634c..b5d894cc 100644 --- a/test/ctmrg/fixediter.jl +++ b/test/ctmrg/fixediter.jl @@ -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, diff --git a/test/ctmrg/gaugefix.jl b/test/ctmrg/gaugefix.jl index 9e477e21..10a5e17e 100644 --- a/test/ctmrg/gaugefix.jl +++ b/test/ctmrg/gaugefix.jl @@ -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) @@ -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 @@ -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) diff --git a/test/ctmrg/gradients.jl b/test/ctmrg/gradients.jl index 3e3a7070..283a1493 100644 --- a/test/ctmrg/gradients.jl +++ b/test/ctmrg/gradients.jl @@ -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 diff --git a/test/ctmrg/leftmoves_allsides.jl b/test/ctmrg/leftmoves_allsides.jl index e697a44b..50ab2926 100644 --- a/test/ctmrg/leftmoves_allsides.jl +++ b/test/ctmrg/leftmoves_allsides.jl @@ -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 diff --git a/test/ctmrg/unitcell.jl b/test/ctmrg/unitcell.jl index 9d8cad8a..69b4d10b 100644 --- a/test/ctmrg/unitcell.jl +++ b/test/ctmrg/unitcell.jl @@ -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