From b6e6f1da8fd80ad3c67af0d7d518ff5a53adcda6 Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 14 Apr 2025 14:35:39 +0200 Subject: [PATCH 1/2] relax `deduce_pspaces` --- src/operators/mpoham.jl | 13 +++++++++++-- test/mpoham.jl | 12 +++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/operators/mpoham.jl b/src/operators/mpoham.jl index c25a7c0..857792a 100644 --- a/src/operators/mpoham.jl +++ b/src/operators/mpoham.jl @@ -83,9 +83,18 @@ function deduce_pspaces(opps::SumOfLocalOperators) end non_deduced = map(ismissing, pspaces) - any(non_deduced) && - error("cannot automatically deduce physical spaces at $(findall(non_deduced))") + if any(non_deduced) # Some spaces were not defined / not able to be deduced + if allequal(filter(!ismissing, pspaces)) # all non-missing spaces are equal + # fill in the missing spaces with the unique non-missing space + uniquespace = first(filter(!ismissing, pspaces)) + for i in eachindex(pspaces) + pspaces[i] = uniquespace + end + else # Not all non-missing spaces are equal + error("cannot automatically deduce physical spaces at $(findall(non_deduced))") + end + end return collect(S, pspaces) end diff --git a/test/mpoham.jl b/test/mpoham.jl index 4251f6a..bb4884e 100644 --- a/test/mpoham.jl +++ b/test/mpoham.jl @@ -1,4 +1,4 @@ -using MPSKitModels +using MPSKitModels, MPSKit, TensorKit lattice = InfiniteChain(1) H1 = @mpoham begin @@ -20,3 +20,13 @@ end H = @mpoham sum(ZZ{i,j} for (i, j) in nearest_neighbours(lattice)) @test length(H) == length(lattice) end + +@testset "deduce_pspaces" begin + # Not fully defining the pspaces should still work + lattice = FiniteChain(5) + H = @mpoham S_zz(){lattice[2],lattice[3]} + + @test unique(MPSKit.physicalspace(H))[1] == ComplexSpace(2) + + @test_throws Exception @mpoham σˣ(){lattice[1]} + σˣ(; spin=3 // 2){lattice[2]} +end From 984194a2e5d9bbc5baebf91f10ec6daad7e3c470 Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 14 Apr 2025 15:07:30 +0200 Subject: [PATCH 2/2] avoid recomputations --- src/operators/mpoham.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/operators/mpoham.jl b/src/operators/mpoham.jl index 857792a..cae1b63 100644 --- a/src/operators/mpoham.jl +++ b/src/operators/mpoham.jl @@ -82,17 +82,17 @@ function deduce_pspaces(opps::SumOfLocalOperators) end end - non_deduced = map(ismissing, pspaces) + not_missing = filter(!ismissing, pspaces) - if any(non_deduced) # Some spaces were not defined / not able to be deduced - if allequal(filter(!ismissing, pspaces)) # all non-missing spaces are equal + if length(not_missing) != length(pspaces) # Some spaces were not defined / not able to be deduced + if allequal(not_missing) # all non-missing spaces are equal # fill in the missing spaces with the unique non-missing space - uniquespace = first(filter(!ismissing, pspaces)) + uniquespace = first(not_missing) for i in eachindex(pspaces) pspaces[i] = uniquespace end else # Not all non-missing spaces are equal - error("cannot automatically deduce physical spaces at $(findall(non_deduced))") + error("cannot automatically deduce physical spaces at $(findall(map(ismissing, pspaces)))") end end return collect(S, pspaces)