From 3b0727cb99f7a78b40f1ce0e61069b4dd41023ec Mon Sep 17 00:00:00 2001 From: lkdvos Date: Wed, 23 Sep 2026 11:43:58 -0400 Subject: [PATCH 1/2] Fix SpaceMismatch in canonicalize! for partially decoupled Hamiltonian sites left_canonicalize!/right_canonicalize! forced both virtual legs of the reconstructed A-block empty in the "fully truncated" (dim(R)==0) branch, but only the leg actually being decomposed collapses when a bond only partially decouples. Inherit the other leg's real space instead. Fixes #522 Co-Authored-By: Claude Sonnet 5 --- src/operators/ortho.jl | 4 ++-- test/hamiltonian/finite.jl | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/operators/ortho.jl b/src/operators/ortho.jl index de7d00200..9ea1f64a5 100644 --- a/src/operators/ortho.jl +++ b/src/operators/ortho.jl @@ -50,7 +50,7 @@ function left_canonicalize!( if dim(R) == 0 # fully truncated V = _rightunit ⊞ _rightunit - Q1 = typeof(WA)(undef, SumSpace{S}() ⊗ P ← P ⊗ SumSpace{S}()) + Q1 = typeof(WA)(undef, codomain(WA) ← P ⊗ SumSpace{S}()) Q2 = typeof(WC)(undef, P ← P ⊗ SumSpace{S}()) else scale!(Q, d) @@ -144,7 +144,7 @@ function right_canonicalize!( R, Q = right_orth!(tmp; alg) if dim(R) == 0 V = _rightunit ⊞ _rightunit - Q1 = typeof(WA)(undef, SumSpace{S}() ⊗ P ← P ⊗ SumSpace{S}()) + Q1 = typeof(WA)(undef, SumSpace{S}() ⊗ P ← domain(WA)) Q2 = typeof(WB)(undef, SumSpace{S}() ⊗ P ← P) else scale!(Q, d) diff --git a/test/hamiltonian/finite.jl b/test/hamiltonian/finite.jl index 950f855fc..e3c266249 100644 --- a/test/hamiltonian/finite.jl +++ b/test/hamiltonian/finite.jl @@ -109,6 +109,46 @@ using TensorKit: ℙ end end +@testset "Finite MPOHamiltonian canonicalization with decoupled sites" begin + # regression test for https://github.com/QuantumKitHub/MPSKit.jl/issues/522 + T = ComplexF64 + for V in (ℂ^2, U1Space(-1 => 1, 0 => 1, 1 => 1), Rep[SU₂](1 // 2 => 1)) + O₁ = randn(T, V, V) + O₁ += O₁' + O₂ = randn(T, V^2 ← V^2) + O₂ += O₂' + E = id(storagetype(O₁), domain(O₁)) + + # bulk block of the middle site is empty on the left but not on the right + H_right = FiniteMPOHamiltonian(fill(V, 3), (1,) => O₁, (2, 3) => O₂) + H_right_tm = O₁ ⊗ E ⊗ E + E ⊗ O₂ + H = MPSKit.right_canonicalize!(copy(H_right), 2) + @test convert(TensorMap, H) ≈ H_right_tm + + # bulk block of the middle site is empty on the right but not on the left + H_left = FiniteMPOHamiltonian(fill(V, 3), (1, 2) => O₂, (3,) => O₁) + H_left_tm = O₂ ⊗ E + E ⊗ E ⊗ O₁ + H = MPSKit.left_canonicalize!(copy(H_left), 2) + @test convert(TensorMap, H) ≈ H_left_tm + + L = 5 + H5 = FiniteMPOHamiltonian(fill(V, L), (1, 2) => O₂, (3,) => O₁, (4, 5) => O₂) + H5_tm = O₂ ⊗ E ⊗ E ⊗ E + E ⊗ E ⊗ O₁ ⊗ E ⊗ E + E ⊗ E ⊗ E ⊗ O₂ + H = copy(H5) + for i in L:-1:2 + MPSKit.right_canonicalize!(H, i) + end + @test convert(TensorMap, H) ≈ H5_tm + for i in 1:(L - 1) + MPSKit.left_canonicalize!(H, i) + end + @test convert(TensorMap, H) ≈ H5_tm + + H5_trunc = changebonds(H5, SvdCut(; trunc = truncrank(16))) + @test convert(TensorMap, H5_trunc) ≈ H5_tm + end +end + @testset "Finite MPOHamiltonian repeated indices" begin X = randn(ComplexF64, ℂ^2, ℂ^2) X += X' From f7e055efdf22bda7cfe7b1e9b7ef9475b727794d Mon Sep 17 00:00:00 2001 From: lkdvos Date: Thu, 24 Sep 2026 14:15:07 -0400 Subject: [PATCH 2/2] Bump BlockTensorKit compat to 0.3.19 BlockTensorKit 0.3.17/0.3.18 silently returned zero from sparse block-tensor contractions with 3+ legs, corrupting convert(TensorMap, ::FiniteMPOHamiltonian) and related densification paths for essentially any Hamiltonian. 0.3.19 fixes this upstream; tests (operators/mpohamiltonian, algorithms/changebonds) pass cleanly against it. Co-Authored-By: Claude Sonnet 5 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index f6c5866f0..740f72d0f 100644 --- a/Project.toml +++ b/Project.toml @@ -37,7 +37,7 @@ MPSKitAdaptExt = "Adapt" [compat] Accessors = "0.1" Adapt = "4" -BlockTensorKit = "0.3.16" +BlockTensorKit = "0.3.19" Compat = "3.47, 4.10" DocStringExtensions = "0.9.3" HalfIntegers = "1.6.0"