From 332fa43c529a3314ba57f45c6da8d8cace005ab3 Mon Sep 17 00:00:00 2001 From: lkdvos Date: Thu, 16 May 2024 13:05:33 +0200 Subject: [PATCH] Change argument order `leading_boundary` with `CTMRG` --- examples/heisenberg.jl | 2 +- src/algorithms/ctmrg.jl | 7 +++++-- src/algorithms/peps_opt.jl | 10 +++++----- test/boundarymps/vumps.jl | 9 ++++----- test/ctmrg/gaugefix.jl | 4 ++-- test/ctmrg/gradients.jl | 4 ++-- test/heisenberg.jl | 2 +- 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/examples/heisenberg.jl b/examples/heisenberg.jl index d88f82a8..a0f9367f 100644 --- a/examples/heisenberg.jl +++ b/examples/heisenberg.jl @@ -36,6 +36,6 @@ alg = PEPSOptimize(; # E/N = −0.6694421, which is a QMC estimate from https://arxiv.org/abs/1101.3281. # Of course there is a noticable bias for small χbond and χenv. ψ₀ = InfinitePEPS(2, χbond) -env₀ = leading_boundary(ψ₀, ctmalg, CTMRGEnv(ψ₀; Venv=ℂ^χenv)) +env₀ = leading_boundary(CTMRGEnv(ψ₀; Venv=ℂ^χenv), ψ₀, ctmalg) result = fixedpoint(ψ₀, H, alg, env₀) @show result.E diff --git a/src/algorithms/ctmrg.jl b/src/algorithms/ctmrg.jl index 4b5e13ad..ed743602 100644 --- a/src/algorithms/ctmrg.jl +++ b/src/algorithms/ctmrg.jl @@ -22,12 +22,15 @@ Regardless of the truncation scheme, the space can be kept fixed with `fixedspac end """ - MPSKit.leading_boundary(state, alg::CTMRG, [envinit]) + MPSKit.leading_boundary([envinit], state, alg::CTMRG) Contract `state` using CTMRG and return the CTM environment. Per default, a random initial environment is used. """ -function MPSKit.leading_boundary(state, alg::CTMRG, envinit=CTMRGEnv(state)) +function MPSKit.leading_boundary(state, alg::CTMRG) + return MPSKit.leading_boundary(CTMRGEnv(state), state, alg) +end +function MPSKit.leading_boundary(envinit, state, alg::CTMRG) normold = 1.0 CSold = map(x -> tsvd(x; alg=TensorKit.SVD())[2], envinit.corners) TSold = map(x -> tsvd(x; alg=TensorKit.SVD())[2], envinit.edges) diff --git a/src/algorithms/peps_opt.jl b/src/algorithms/peps_opt.jl index 15f8c8ab..b42d4fb2 100644 --- a/src/algorithms/peps_opt.jl +++ b/src/algorithms/peps_opt.jl @@ -68,9 +68,9 @@ function fixedpoint( E, g = withgradient(peps) do ψ envs´ = hook_pullback( leading_boundary, + envs, ψ, - alg.boundary_alg, - envs; + alg.boundary_alg; alg_rrule=alg.gradient_alg, ) ignore_derivatives() do @@ -107,11 +107,11 @@ function _rrule( gradmode::Union{GradMode,KrylovKit.LinearSolver}, ::RuleConfig, ::typeof(MPSKit.leading_boundary), + envinit, state, alg::CTMRG, - envinit, ) - envs = leading_boundary(state, alg, envinit) + envs = leading_boundary(envinit, state, alg) function leading_boundary_pullback(Δenvs′) Δenvs = unthunk(Δenvs′) @@ -127,7 +127,7 @@ function _rrule( ∂f∂x(x)::typeof(envs) = env_vjp(x)[2] ∂F∂envs = fpgrad(Δenvs, ∂f∂x, ∂f∂A, Δenvs, gradmode) - return NoTangent(), ∂F∂envs, NoTangent(), ZeroTangent() + return NoTangent(), ZeroTangent(), ∂F∂envs, NoTangent() end return envs, leading_boundary_pullback diff --git a/test/boundarymps/vumps.jl b/test/boundarymps/vumps.jl index 9943cac5..a0794410 100644 --- a/test/boundarymps/vumps.jl +++ b/test/boundarymps/vumps.jl @@ -6,17 +6,16 @@ using MPSKit using LinearAlgebra Random.seed!(29384293742893) -psi = InfinitePEPS(ComplexSpace(2), ComplexSpace(2)) +psi = InfinitePEPS(ComplexSpace(2), ComplexSpace(2)) T = PEPSKit.InfiniteTransferPEPS(psi, 1, 1) -mps = PEPSKit.initializeMPS(T, [ComplexSpace(20)]) +mps = PEPSKit.initializeMPS(T, [ComplexSpace(20)]) mps, envs, ϵ = leading_boundary(mps, T, VUMPS()) N = abs(sum(expectation_value(mps, T))) -ctm = leading_boundary( - psi, CTMRG(; verbosity=1, fixedspace=true), CTMRGEnv(psi; Venv=ComplexSpace(20)) -) +ctm = CTMRGEnv(psi; Venv=ComplexSpace(20)) +ctm = leading_boundary(ctm, psi, CTMRG(; verbosity=1, fixedspace=true)) N2 = abs(norm(psi, ctm)) @test N ≈ N2 atol = 1e-3 diff --git a/test/ctmrg/gaugefix.jl b/test/ctmrg/gaugefix.jl index b156f744..45bee5d5 100644 --- a/test/ctmrg/gaugefix.jl +++ b/test/ctmrg/gaugefix.jl @@ -50,7 +50,7 @@ end ) alg_fixed = CTMRG(; trscheme=truncdim(dim(ctm_space)), verbosity, fixedspace=true) - ctm = leading_boundary(psi, alg, ctm) + ctm = leading_boundary(ctm, psi, alg) ctm2, = ctmrg_iter(psi, ctm, alg_fixed) ctm_fixed = gauge_fix(ctm, ctm2) @test PEPSKit.check_elementwise_convergence(ctm, ctm_fixed; atol=1e-4) @@ -75,7 +75,7 @@ end ) alg_fixed = CTMRG(; trscheme=truncspace(ctm_space), verbosity, fixedspace=true) - ctm = leading_boundary(psi, alg, ctm) + ctm = leading_boundary(ctm, psi, alg) ctm2, = ctmrg_iter(psi, ctm, alg_fixed) ctm_fixed = gauge_fix(ctm, ctm2) @test PEPSKit.check_elementwise_convergence(ctm, ctm_fixed; atol=1e-4) diff --git a/test/ctmrg/gradients.jl b/test/ctmrg/gradients.jl index 4ebe3d46..81a9715d 100644 --- a/test/ctmrg/gradients.jl +++ b/test/ctmrg/gradients.jl @@ -38,13 +38,13 @@ gradmodes = [nothing, GeomSum(; tol), ManualIter(; tol), KrylovKit.GMRES(; tol)] Random.seed!(42039482038) dir = InfinitePEPS(2, χbond) psi = InfinitePEPS(2, χbond) - env = leading_boundary(psi, boundary_alg, CTMRGEnv(psi; Venv=ℂ^χenv)) + env = leading_boundary(CTMRGEnv(psi; Venv=ℂ^χenv), psi, boundary_alg) alphas, fs, dfs1, dfs2 = OptimKit.optimtest( (psi, env), dir; alpha=steps, retract=PEPSKit.my_retract, inner=PEPSKit.my_inner ) do (peps, envs) E, g = Zygote.withgradient(peps) do psi - envs2 = PEPSKit.hook_pullback(leading_boundary, psi, boundary_alg, envs; alg_rrule) + envs2 = PEPSKit.hook_pullback(leading_boundary, envs, psi, boundary_alg; alg_rrule) return costfun(psi, envs2, H) end return E, only(g) diff --git a/test/heisenberg.jl b/test/heisenberg.jl index d022bd24..a3b51c56 100644 --- a/test/heisenberg.jl +++ b/test/heisenberg.jl @@ -37,7 +37,7 @@ opt_alg = PEPSOptimize(; # initialize states Random.seed!(91283219347) psi_init = InfinitePEPS(2, χbond) -env_init = leading_boundary(psi_init, ctm_alg, CTMRGEnv(psi_init; Venv=ComplexSpace(χenv))) +env_init = leading_boundary(CTMRGEnv(psi_init; Venv=ComplexSpace(χenv)), psi_init, ctm_alg) # find fixedpoint result = fixedpoint(psi_init, H, opt_alg, env_init)