Skip to content

Commit

Permalink
Fix double computation of environments in ctmrg_gradient
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrehmer committed Mar 7, 2024
1 parent 3e190cb commit 1c52d33
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/algorithms/peps_opt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ my_inner(_, η₁, η₂) = real(dot(η₁, η₂))
# Function returning energy and CTMRG gradient for each optimization step
function ctmrg_gradient(x, H, alg::PEPSOptimize)
peps, env = x
env′ = leading_boundary(peps, alg.boundary_alg, env)
alg.reuse_env && @diffset env = env′
E = costfun(peps, env′, H)
∂E∂A = gradient-> _ctmrgcostfun(ψ, env′, H, alg), peps)[1]
E, g = withgradient-> _ctmrgcostfun!(ψ, env, H, alg), peps)
∂E∂A = g[1] # Extract PEPS derivative from gradient tuple
if !(typeof(∂E∂A) <: InfinitePEPS) # NaiveAD returns NamedTuple as gradient instead of InfinitePEPS
∂E∂A = InfinitePEPS(∂E∂A.A)
end
Expand All @@ -58,13 +56,15 @@ function ctmrg_gradient(x, H, alg::PEPSOptimize)
end

# Helper function wrapping CTMRG run and cost function with custom adjoint
function _ctmrgcostfun(peps, env::CTMRGEnv, H, alg::PEPSOptimize)
costfun(peps, leading_boundary(peps, alg.boundary_alg, env), H)
function _ctmrgcostfun!(peps, env::CTMRGEnv, H, alg::PEPSOptimize)
env′ = leading_boundary(peps, alg.boundary_alg, env)
alg.reuse_env && @diffset env = env′
return costfun(peps, env′, H)
end

# Energy gradient backwards rule (does not apply to NaiveAD gradient mode)
function ChainRulesCore.rrule(
::typeof(_ctmrgcostfun), peps, env::CTMRGEnv, H, alg::PEPSOptimize{G}
::typeof(_ctmrgcostfun!), peps, env::CTMRGEnv, H, alg::PEPSOptimize{G}
) where {G<:Union{GeomSum,ManualIter,LinSolve}}
env = leading_boundary(peps, alg.boundary_alg, env)
E, Egrad = withgradient(costfun, peps, env, H)
Expand Down

0 comments on commit 1c52d33

Please sign in to comment.