Skip to content

[v2] Preserve hidden-leg partitions in DMRG diag, matvec, and mixed_svd - #797

Open
rpinho wants to merge 1 commit into
tenpy:v2_mainfrom
rpinho:fix-763-idmrg-hidden-leg
Open

rpinho wants to merge 1 commit into
tenpy:v2_mainfrom
rpinho:fix-763-idmrg-hidden-leg

Conversation

@rpinho

@rpinho rpinho commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Summary

On v2_main, running iDMRG or finite DMRG on a unit cell with a non-trivial total charge (a HiddenLegTensor carrying !MPS_total_charge) fails in DMRGEngine.diag and breaks MPS gauging across three interacting places:

  1. full_diag_effH / DMRGEngine.diag (tenpy/algorithms/dmrg.py): At a charged boundary bond (e.g., bond L-1 in iDMRG), theta_guess carries the hidden leg in its codomain, while full_diag_effH returns the eigenvector with the hidden leg in its domain (or squeezes a trivial hidden leg even under keep_sector=True). lasy.inner(theta_guess, theta) in DMRGEngine.diag then raises a leg-partition mismatch. Under diag_method='ED_all', the returned eigenvector can also switch charge sectors or hidden-leg placement, which likewise breaks lasy.inner.
  2. TwoSiteDMRGEngine.mixed_svd & _permute_theta_for_svd (tenpy/algorithms/dmrg.py, tenpy/networks/mps.py): mixed_svd calls lasy.planar_permute_legs(theta, codomain=['vL', 'p0'], domain=['vR', 'p1']), which lets the hidden leg migrate off the final site during sweeps (breaking is_gauged / total_charge_leg, even though get_total_charge() still fuses per-site charges). The existing _permute_theta_for_svd helper in mps.py also slices theta.labels directly (LegLabels is not sliceable, raising TypeError).
  3. OneSiteH.matvec, TwoSiteH.matvec, ZeroSiteH.matvec (tenpy/algorithms/mps_common.py): All three pass codomain=labels, domain=[], flattening a domain hidden leg into the codomain so Lanczos fails with ValueError: Different number of legs on charged finite chains and charged iDMRG (start_env=0).

Who hits this: TeNPy v2 users running finite or infinite DMRG on any MPS whose unit cell carries a non-trivial conserved charge (SpinChain with conserve='Sz'/'parity', FermionChain with conserve='N'/'parity', or any HiddenLegTensor state, #763).

Changes

  • tenpy/algorithms/dmrg.py:
    • In full_diag_effH: preserve theta_guess's hidden-leg dual when keep_sector=False, avoid squeezing a trivial hidden leg when keep_sector=True on a HiddenLegTensor, and restore (theta_guess.codomain_labels, theta_guess.domain_labels) via lasy.planar_permute_legs when keep_sector=True.
    • In DMRGEngine.diag: for diag_method='ED_all', align same-sector HiddenLegTensor placements before lasy.inner and treat sector changes as zero overlap (ov_change = 1.0).
    • In TwoSiteDMRGEngine.mixed_svd: when not self.combine, use _permute_theta_for_svd(theta, charge_leg_right) with charge_leg_right = self.psi.finite or self.psi._to_valid_site_index(i0) != self.psi.L - 1 so the hidden leg stays pinned on the final site across sweeps.
  • tenpy/networks/mps.py:
    • Fix _permute_theta_for_svd to convert theta.labels via theta.labels.as_list() before slicing.
  • tenpy/algorithms/mps_common.py:
    • Restore (theta.codomain_labels, theta.domain_labels) instead of flattening all labels into codomain in OneSiteH.matvec, TwoSiteH.matvec, and ZeroSiteH.matvec.
  • tests/test_dmrg.py & tests/test_infinite_dmrg.py:
    • Add regression tests covering full_diag_effH (ED_block across all hidden-leg layouts and ED_all sector switches), _permute_theta_for_svd (including odd-parity and complex cases), charged finite DMRG sweeps (L=3, 4), charged iDMRG (SpinChain Sz/parity, staggered FermionChain N/parity), and narrow the test_free_fermions_infinite_dmrg[N] xfail expectation to the separate #762/#765 failures.

Scope & Coordination

Verification

  • full_diag_effH: 36/36 ED_block hidden-leg partitions preserved; ED_all handles Sector([4]) -> Sector([2]) across all 3 placements (ov_change = 1.0, residual 5.56e-17); all 72 aligned signed eigen-residuals < 1.6e-15.
  • Charged finite L=3 and L=4 sweeps keep the hidden leg on the final site across all 150 and 200 updates, pass test_sanity() (norm error <= 1.38e-15), match conserve=None energies to <= 4.62e-14 and local Sz to <= 2.67e-15, and match an independent 4-site exact Hamiltonian ground-state energy (-0.9571067811865476 vs -0.9571067811865477).
  • Charged iDMRG: polarized spin gives E/site = 0.25; staggered FermionChain (conserve='N' and 'parity') agrees with an independent 4096-point Bloch band sum to 2.11e-9 and Jordan-Wigner SpinChain to < 3e-14.
  • Targeted pytest suite: 121 passed, 6 skipped, 4 xfailed (up from 87 passed on v2_main, 34 new cases), and all 4 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 reproducers, exact matrix/Bloch controls, eigen-residuals, and test/lint suites before this PR.

Fixes #763

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

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.

1 participant