diff --git a/src/operators/mpoham.jl b/src/operators/mpoham.jl index c25a7c0..cae1b63 100644 --- a/src/operators/mpoham.jl +++ b/src/operators/mpoham.jl @@ -82,10 +82,19 @@ function deduce_pspaces(opps::SumOfLocalOperators) end end - non_deduced = map(ismissing, pspaces) - any(non_deduced) && - error("cannot automatically deduce physical spaces at $(findall(non_deduced))") + not_missing = filter(!ismissing, pspaces) + 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(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(map(ismissing, pspaces)))") + 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