Skip to content

[v2] Use planar leg permutations and level-separated hidden-leg restoration in mps_common - #796

Open
rpinho wants to merge 1 commit into
tenpy:v2_mainfrom
rpinho:fix-762-fermionic-dmrg
Open

rpinho wants to merge 1 commit into
tenpy:v2_mainfrom
rpinho:fix-762-fermionic-dmrg

Conversation

@rpinho

@rpinho rpinho commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

On v2_main, finite and infinite fermionic DMRG fail with lasyten._core.SymmetryError: Need to specify bend_right! whenever Lanczos is used (eff_H.N >= 400 or diag_method='lanczos'), because TwoSiteH.matvec and the other effective-Hamiltonian / sweep helpers in tenpy/algorithms/mps_common.py still call non-planar lasy.permute_legs and lasy.tdot.

Who hits this: TeNPy v2 users running finite or infinite fermionic DMRG (such as FermionChain with conserve='parity' or conserve='N'), Lanczos excited-state DMRG (orthogonal_to), or fermionic VariationalCompression (#762).

While auditing fermionic signs on odd-parity finite chains (HiddenLegTensor with a domain hidden leg at the right boundary), a plain permute_legs -> planar_permute_legs swap alone flipped H|theta> to -H|theta> because lasyten's _arrange_hidden_around_public_contr bends domain hidden legs with BendRight{true} and braids them past public legs (level=-1). This PR avoids that sign flip and also preserves exact (codomain_labels, domain_labels) for both boundary and interleaved hidden legs.

Changes

  • tenpy/algorithms/mps_common.py:
    • Convert remaining non-planar lasy.permute_legs and lasy.tdot calls across Sweep._wrap_ortho_eff_H, OneSiteH (from_LP_W0_RP, matvec, combine_theta), TwoSiteH (matvec), ZeroSiteH (from_LP_RP, matvec), and VariationalCompression (update_local, update_new_psi) to planar operations (planar_permute_legs, planar_contraction, compose), and fix the reversed SVD domain order ['p1', 'vR'] -> ['vR', 'p1'] in VariationalCompression.update_local.
    • Add _hidden_legs_to_front_codomain and _restore_matvec_legs using matched inverse level-separated permutations (hidden legs on level=-1 bending left with bend_right=False, public legs on level=0 bending right with bend_right=True), keeping non-hidden tensors on plain lasy.planar_permute_legs while preserving exact fermionic signs and leg ordering for both contiguous and interleaved HiddenLegTensor layouts.
  • tests/test_infinite_dmrg.py:
    • Remove the strict=True #762 xfail on test_free_fermions_infinite_dmrg[parity] (now passes).
  • tests/test_dmrg.py & tests/test_mps_common.py:
    • Add regression tests for finite fermionic Lanczos DMRG (L=12 #762 reproducer, L=6 even/odd parity & N, Jordan-Wigner SpinChain control), Lanczos vs ED_block and orthogonal_to excited states (diag_method='lanczos'), fermionic OneSiteH/TwoSiteH/ZeroSiteH matvec vs lasy.compose(H.to_tensor(), theta), all 8 contiguous/interleaved hidden-leg layouts across widths 0, 1, 2, and fermionic VariationalCompression.

Scope Notes

  • combine=True (#694), SingleSiteDMRGEngine (#698), charged-unit-cell iDMRG test_free_fermions_infinite_dmrg[N] (#763, #765), and ProjectedLinearOperator.to_tensor for ED_block are separate upstream items and remain unchanged.

Verification

  • FermionChain(L=12, J=1.0, V=0.0, mu=0.0, conserve='parity') converges to -7.2962298105587635 (matching the exact cosine sum -7.296229810558755 and Jordan-Wigner SpinChain -7.296229810558754), and test_free_fermions_infinite_dmrg[parity] passes (8.4e-5 above -2/pi at chi=16).
  • All 21 named hidden-leg layouts (including interleaved hidden legs) preserve exact (codomain_labels, domain_labels) and signed -theta (max residual 7.97e-16), alongside 192/192 finite-cut comparisons and 24/24 interacting Hermiticity checks (max discrepancy 1.25e-16).
  • Targeted suite passes (57 passed, 59 skipped, 1 xfailed across tests/test_dmrg.py, tests/test_mps_common.py, tests/test_infinite_dmrg.py), broad suite passes (331 passed), and all CI linters (ruff check, ruff format --diff, flake8 --select RST, python ./tests/linting.py) pass.
  • Found and written with AI agents; a separate cross-family AI referee independently verified the fermionic sign behavior, exact energies, hidden-leg layouts, and test/lint suites before this PR.

Fixes #762

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

@Jakob-Unfried

Jakob-Unfried commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Before we look at the implementation details (I dont love the underscore helper functions), we should understand why this fix is needed and what it means for our mental model.

When we discussed about hidden legs, i think we convinced ourselves that the braid chirality with the hidden leg can be chosen arbitrarily, correct? and for fermions it doesnt matter either way.
what goes wrong here, if i understand correctly, is that we accidentally get a twist in the hidden legs, which is -1 if it has odd fermion parity.

the learning i think is that we need to be careful about bend_right when moving the hidden legs. and this highlights the need for extensive test with fermions where the hidden leg is odd parity, ie odd number of fermions per unit cell

@jhauschild @LudwigZw

@rpinho

rpinho commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Yes, that's right, as far as I know. The -1 would happen only when the hidden leg has odd parity: with FermionChain(L=6, N=3) the hidden leg in the domain is odd, and a plain planar_permute_legs is -H|theta>. In lasyten, _arrange_hidden_around_public_contr (src/tensors/planar.cpp) bends domain hidden legs with BendRight{true}, and the PR moves them with bend_right=False first.

The tests in this PR have odd-parity hidden legs (L=6 with N=3, and the OneSiteH, TwoSiteH and ZeroSiteH checks with even and odd N). For the underscore helpers, I can try to move it to lasyten instead if you want, and fix the bend in lasyten.

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