diff --git a/src/Quantum/MatrixProductState.jl b/src/Quantum/MatrixProductState.jl index 23dd6eb32..d8f811067 100644 --- a/src/Quantum/MatrixProductState.jl +++ b/src/Quantum/MatrixProductState.jl @@ -48,6 +48,7 @@ function MatrixProductState{Open}(arrays; χ = nothing, order = (:l, :r, :p), me invpermute!(labels, permutator) alias = Dict([x => y for (x, y) in zip(invpermute!(original_order, permutator), labels)]) + push!(tn, Tensor(data, labels; alias = alias)) end @@ -151,8 +152,8 @@ function Base.rand(rng::Random.AbstractRNG, sampler::MPSSampler{Open,T}) where { χl = min(χ, p^(i - 1)) χr = min(χ, p^i) - # swap bond dims after mid - after_mid ? (χr, χl) : (χl, χr) + # swap bond dims after mid and handle midpoint for odd-length MPS + (isodd(n) && i == n ÷ 2 + 1) ? (χl, χl) : (after_mid ? (χr, χl) : (χl, χr)) end # fix for first site diff --git a/test/MatrixProductState_test.jl b/test/MatrixProductState_test.jl index fed2d32b8..38f24d026 100644 --- a/test/MatrixProductState_test.jl +++ b/test/MatrixProductState_test.jl @@ -55,6 +55,26 @@ MatrixProductState{Open}(arrays) isa TensorNetwork{MatrixProductState{Open}} end + @testset "rand" begin + function max_size(t) + labels = (get(t.meta[:alias], :l, :none), get(t.meta[:alias], :r, :none)) + return maximum(size(t, label) for label in labels if label != :none) + end + + # Test with χ > maximum possible χ for the given parameters + ψ = rand(MatrixProductState{Open}, 7, 2, 32) + + @test ψ isa TensorNetwork{MatrixProductState{Open}} + @test length(ψ) == 7 + @test maximum(max_size(t) for t in ψ.tensors) <= 32 + + # Test with χ < maximum possible χ for the given parameters + ψ = rand(MatrixProductState{Open}, 7, 2, 4) + @test ψ isa TensorNetwork{MatrixProductState{Open}} + @test length(ψ) == 7 + @test maximum(max_size(t) for t in ψ.tensors) <= 4 + end + @testset "Metadata" begin @testset "alias" begin arrays = [rand(2, 2), rand(2, 2, 2), rand(2, 2)]