Skip to content

Fixing changebonds, some inconsistency remaining#415

Open
LHerviou wants to merge 5 commits intoQuantumKitHub:mainfrom
LHerviou:fix_changebonds
Open

Fixing changebonds, some inconsistency remaining#415
LHerviou wants to merge 5 commits intoQuantumKitHub:mainfrom
LHerviou:fix_changebonds

Conversation

@LHerviou
Copy link
Copy Markdown

Following discussion with @lkdvos

Here is a small fix for some minor bugs I noticed in changebonds: for RandExpand or SvdCut, the returned environments were not updated after the update of the mps. Schematically, we had
newmps, oldenv = changebonds(oldmps, H, alg = ..., oldenv)
which made any groundstate algorithm failed.
Now the environments are updated such that
newmps, newenv = changebonds(oldmps, H, alg = ..., oldenv)
without touching oldmps or oldenv

Before accepting the update: there are some inconsistencies in the behavior of changebonds.
In particular, for OptimalExpand, env are directly modified in place, which makes oldenv not work with the old mps. Is it intended behavior for memory optimization? If so, I can reproduce a similar behavior for others. It should in any case be advertised.

I modified also slightly the converter between InfiniteMPOHamiltonian and InfiniteMPO which was only used by OptimalExpand. There is some unsafe looking environment update in it, though.

The test pass for FiniteMPS, though I did not investigate it as much. In particular, the environment update in OptimalExpand is hidden in the computation of AC2.

Finally, I added a constructor of the type FiniteMPS(Type, Space, Space) as the syntax works for infiniteMPS and not Finite, which was annoying me.

For reproduction: here is a script that fails before the PR, and pass afterwards:

## Infinite chain, InfiniteMPOHamiltonian
L = 2
mps = InfiniteMPS(ComplexF64, repeat([ComplexSpace(2)], L), repeat([ComplexSpace(20)], L) )
H = transverse_field_ising(ComplexF64, Trivial, InfiniteChain(L))
env = environments(mps, H)

for alg in [OptimalExpand, RandExpand, SvdCut, VUMPSSvdCut]
  env = environments(mps, H)
  newmps, newenv = changebonds(mps, H, alg(trscheme = truncrank(10)), env)    
  try
    newmps, newenv, = find_groundstate(newmps, H, VUMPS(; maxiter=1, verbosity=1, tol= 1e-6), newenv)
  catch e
    error("Fail in Infinite $alg")
    #println(e)
  end
end

## Infinite chain, InfiniteMPO
L = 2
mps = InfiniteMPS(ComplexF64, repeat([ComplexSpace(2)], L), repeat([ComplexSpace(20)], L) )
H = InfiniteMPO(transverse_field_ising(ComplexF64, Trivial, InfiniteChain(L)))
env = environments(mps, H)

for alg in [OptimalExpand, RandExpand, SvdCut, VUMPSSvdCut]
  env = environments(mps, H)
  newmps, newenv = changebonds(mps, H, alg(trscheme = truncrank(10)), env)    
  try
    newmps, newenv, = find_groundstate(newmps, H, VUMPS(; maxiter=1, verbosity=1, tol= 1e-6), newenv)
  catch e
    error("Fail in Infinite $alg")
    #println(e)
  end
end


##Finite Chain
L = 10
mps = FiniteMPS(ComplexF64, repeat([ComplexSpace(2)], L), repeat([ComplexSpace(20)], L-1) )
H = transverse_field_ising(ComplexF64, Trivial, FiniteChain(L))
env = environments(mps, H)


for alg in [OptimalExpand, RandExpand, SvdCut]
  env = environments(mps, H)
  newmps, newenv = changebonds(mps, H, alg(trscheme = truncrank(10)), env)    
  try
    newmps, newenv, = find_groundstate(newmps, H, DMRG(; maxiter=1, verbosity=1, tol= 1e-6), newenv)
  catch e
    error("Fail in Finite $alg")
    #println(e)
  end
end```

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 17, 2026

Your PR no longer requires formatting changes. Thank you for your contribution!

Comment thread src/algorithms/changebonds/optimalexpand.jl
Comment thread src/algorithms/changebonds/optimalexpand.jl Outdated
@lkdvos
Copy link
Copy Markdown
Member

lkdvos commented Apr 17, 2026

As a general comment, I feel like we are not super consistent about the role of environments throughout the entire package to be honest. In general they are somewhat treated as an in-place thing, so I think I would be okay with all functions counting as "destroying the input/taking ownership of that", which would then indeed mean that the environments are no longer compatible with the old state, unless you did an explicit copy. However, this is probably something I should better document 😁

@LHerviou
Copy link
Copy Markdown
Author

LHerviou commented Apr 20, 2026

If you want, I can go through the different changebonds functions to systematize it.
Just let me know whether you want to:

  • always modify the environments in place
  • never modify the environments

At least for infiniteMPS, we can make stuff consistent.
I will also update the doc accordingly

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 30, 2026

Codecov Report

❌ Patch coverage is 0% with 23 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/algorithms/changebonds/randexpand.jl 0.00% 7 Missing ⚠️
src/algorithms/changebonds/svdcut.jl 0.00% 7 Missing ⚠️
src/algorithms/changebonds/optimalexpand.jl 0.00% 5 Missing ⚠️
src/algorithms/changebonds/vumpssvd.jl 0.00% 2 Missing ⚠️
src/states/finitemps.jl 0.00% 2 Missing ⚠️
Files with missing lines Coverage Δ
src/algorithms/changebonds/changebonds.jl 0.00% <ø> (-100.00%) ⬇️
src/algorithms/changebonds/vumpssvd.jl 0.00% <0.00%> (-100.00%) ⬇️
src/states/finitemps.jl 73.48% <0.00%> (-21.24%) ⬇️
src/algorithms/changebonds/optimalexpand.jl 0.00% <0.00%> (-100.00%) ⬇️
src/algorithms/changebonds/randexpand.jl 0.00% <0.00%> (-100.00%) ⬇️
src/algorithms/changebonds/svdcut.jl 40.54% <0.00%> (-53.67%) ⬇️

... and 74 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@lkdvos lkdvos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some minor comments, but I definitely like that this is more principled with the inplace vs non-inplace functions.

[Zauner-Stauber et al. Phys. Rev. B 97 (2018)](@cite zauner-stauber2018), by selecting the
dominant contributions of a two-site updated MPS tensor, orthogonal to the original ψ.

changedbonds! is only defined for FiniteMPS, and modify its environment.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
changedbonds! is only defined for FiniteMPS, and modify its environment.
!!! note
`changebonds!` is only defined for `FiniteMPS`, and modifies its environment.

distributed weights for each state in the two-site space and truncating that.

The environments are not used here.
changebonds! modify both the provided state and environments.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
changebonds! modify both the provided state and environments.
`changebonds!` modifies both the provided state and environments.


newψ = _expand(ψ, AL′, AR′)
recalculate!(envs, newψ, H)
envs = environments(newψ, H)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely following this change. Is it not possible to overwrite the old environments?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I think I see now what you intended: is it that the non-bang (!) functions shouldn't modify their inputs?
I can see that being a good choice, I think it is mostly that the envs are always kind of treated as a special thing that is modified everywhere.
I'm happy either way, I think you have a better feeling of what is convenient/efficient.


newψ = _expand(ψ, AL′, AR′)
recalculate!(envs, newψ, H)
envs = environments(newψ, H) # recalculate!(envs, newψ, H)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here


An algorithm that uses a two-site update step to change the bond dimension of a state.

changedbonds! is not defined.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
changedbonds! is not defined.
!!! note
`changebonds!` is not defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants