From 0ac258f202fb508e2b2b851321336e5e183b378e Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Wed, 9 Oct 2024 15:10:48 +0200 Subject: [PATCH 01/17] Format code --- test/Ansatz_test.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/Ansatz_test.jl b/test/Ansatz_test.jl index 4c9f64a0b..7c9218851 100644 --- a/test/Ansatz_test.jl +++ b/test/Ansatz_test.jl @@ -1,2 +1 @@ -@testset "Ansatz" begin -end \ No newline at end of file +@testset "Ansatz" begin end From b3489113c9b3f658b9e0930ff62571cd502916ca Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Wed, 9 Oct 2024 15:11:23 +0200 Subject: [PATCH 02/17] Implement MPS identity initialization --- src/Ansatz/MPS.jl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index 51851a793..e385e406a 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -62,6 +62,30 @@ function MPS(arrays::Vector{<:AbstractArray}; order=defaultorder(MPS)) return MPS(ansatz, NonCanonical()) end +MPS(arraysdims::NTuple{N,<:Vector{Int}}; order=defaultorder(MPS)) where {N} = MPS(collect(arraysdims); order=order) +MPS(arraysdims::Vector{<:Tuple{Vararg{Int}}}; order=defaultorder(MPS)) = MPS(collect.(arraysdims); order=order) +function MPS(arraysdims::NTuple{N,Tuple{Vararg{Int}}}; order=defaultorder(MPS)) where {N} + return MPS(collect(collect.(arraysdims)); order=order) +end +function MPS(arraysdims::Vector{<:Vector{Int}}; order=defaultorder(MPS)) + @assert length(arraysdims[1]) == 2 "First array must have 2 dimensions" + @assert all(==(3) ∘ length, arraysdims[2:(end - 1)]) "All arrays must have 3 dimensions" + @assert length(arraysdims[end]) == 2 "Last array must have 2 dimensions" + issetequal(order, defaultorder(MPS)) || + throw(ArgumentError("order must be a permutation of $(String.(defaultorder(MPS)))")) + + return MPS( + map(arraysdims) do arrdims + mindim = minimum(arrdims) + arr = zeros(ComplexF64, arrdims...) + deltas = ntuple(x -> ntuple(_ -> x, length(arrdims)), mindim) + broadcast(delta -> arr[delta...] = 1.0, deltas) + arr + end; + order=order, + ) +end + function Base.convert(::Type{MPS}, tn::Product) @assert socket(tn) == State() From c3800a885a1ee73945686d58edd2075a07112492 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Wed, 9 Oct 2024 15:11:42 +0200 Subject: [PATCH 03/17] Add tests for all dispatches of MPS identity init --- test/MPS_test.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/MPS_test.jl b/test/MPS_test.jl index 43b665bb8..a9e8d5183 100644 --- a/test/MPS_test.jl +++ b/test/MPS_test.jl @@ -26,6 +26,28 @@ @test inds(ψ; at=site"3", dir=:left) == inds(ψ; at=site"2", dir=:right) !== nothing @test all(i -> size(ψ, inds(ψ; at=Site(i))) == 2, 1:nsites(ψ)) + dispatch_arraysdims = [ + ([2, 4], [5, 4, 3], [2, 3]), + [(2, 4), (5, 4, 3), (2, 3)], + ((2, 4), (5, 4, 3), (2, 3)), + [[2, 4], [5, 4, 3], [2, 3]] + ] + for arraysdims in dispatch_arraysdims + ψ = MPS(arraysdims) # Default order (:o, :l, :r) + @test size(tensors(ψ; at=site"1")) == (2, 4) + @test size(tensors(ψ; at=site"2")) == (5, 4, 3) + @test size(tensors(ψ; at=site"3")) == (2, 3) + t1 = tensors(ψ; at=site"1") + @test t1[1, 1] == t1[2, 2] == 1 + @test sum(t1) == 2 + t2 = tensors(ψ; at=site"2") + @test t2[1, 1, 1] == t2[2, 2, 2] == t2[3, 3, 3] == 1 + @test sum(t2) == 3 + t3 = tensors(ψ; at=site"3") + @test t3[1, 1] == t3[2, 2] == 1 + @test sum(t3) == 2 + end + @testset "Site" begin ψ = MPS([rand(2, 2), rand(2, 2, 2), rand(2, 2)]) From d18f89463847c15128c4c71651759b157e8255f7 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Wed, 9 Oct 2024 15:19:22 +0200 Subject: [PATCH 04/17] Format julia code --- test/MPS_test.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/MPS_test.jl b/test/MPS_test.jl index a9e8d5183..4896356eb 100644 --- a/test/MPS_test.jl +++ b/test/MPS_test.jl @@ -30,7 +30,7 @@ ([2, 4], [5, 4, 3], [2, 3]), [(2, 4), (5, 4, 3), (2, 3)], ((2, 4), (5, 4, 3), (2, 3)), - [[2, 4], [5, 4, 3], [2, 3]] + [[2, 4], [5, 4, 3], [2, 3]], ] for arraysdims in dispatch_arraysdims ψ = MPS(arraysdims) # Default order (:o, :l, :r) From 347b7bf95d45f471811e577fcd822e29f9e1913a Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Wed, 9 Oct 2024 18:43:48 +0200 Subject: [PATCH 05/17] Rename function header & add docstring --- src/Ansatz/MPS.jl | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index e385e406a..8ee50ec7f 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -62,12 +62,17 @@ function MPS(arrays::Vector{<:AbstractArray}; order=defaultorder(MPS)) return MPS(ansatz, NonCanonical()) end -MPS(arraysdims::NTuple{N,<:Vector{Int}}; order=defaultorder(MPS)) where {N} = MPS(collect(arraysdims); order=order) -MPS(arraysdims::Vector{<:Tuple{Vararg{Int}}}; order=defaultorder(MPS)) = MPS(collect.(arraysdims); order=order) -function MPS(arraysdims::NTuple{N,Tuple{Vararg{Int}}}; order=defaultorder(MPS)) where {N} - return MPS(collect(collect.(arraysdims)); order=order) -end -function MPS(arraysdims::Vector{<:Vector{Int}}; order=defaultorder(MPS)) +""" + Base.identity(::Type{MPS}, arraysdims; order=defaultorder(MPS)) + +Returns an [`MPS`](@ref) whose tensors are initialized to the identity with the specified +dimensions from `arraysdims`. + +# Keyword Arguments + + - `order` Tensors' indices order. Default: output - left - right (:o, :l, :r). +""" +function Base.identity(::Type{MPS}, arraysdims; order=defaultorder(MPS)) @assert length(arraysdims[1]) == 2 "First array must have 2 dimensions" @assert all(==(3) ∘ length, arraysdims[2:(end - 1)]) "All arrays must have 3 dimensions" @assert length(arraysdims[end]) == 2 "Last array must have 2 dimensions" From a5b07de589d87a7a1bebbd311aa15885e316e35a Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Wed, 9 Oct 2024 18:44:09 +0200 Subject: [PATCH 06/17] Fix test set for identity MPS --- test/MPS_test.jl | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/MPS_test.jl b/test/MPS_test.jl index 4896356eb..4cc017404 100644 --- a/test/MPS_test.jl +++ b/test/MPS_test.jl @@ -25,24 +25,23 @@ @test inds(ψ; at=site"2", dir=:left) == inds(ψ; at=site"1", dir=:right) !== nothing @test inds(ψ; at=site"3", dir=:left) == inds(ψ; at=site"2", dir=:right) !== nothing @test all(i -> size(ψ, inds(ψ; at=Site(i))) == 2, 1:nsites(ψ)) + + @testset "Base.identity" begin + arraysdims = [(2, 4), (5, 4, 3), (2, 3)] + ψ = identity(MPS, arraysdims) # Default order (:o, :l, :r) + + @test size(tensors(ψ; at=site"1")) == arraysdims[1] + @test size(tensors(ψ; at=site"2")) == arraysdims[2] + @test size(tensors(ψ; at=site"3")) == arraysdims[3] - dispatch_arraysdims = [ - ([2, 4], [5, 4, 3], [2, 3]), - [(2, 4), (5, 4, 3), (2, 3)], - ((2, 4), (5, 4, 3), (2, 3)), - [[2, 4], [5, 4, 3], [2, 3]], - ] - for arraysdims in dispatch_arraysdims - ψ = MPS(arraysdims) # Default order (:o, :l, :r) - @test size(tensors(ψ; at=site"1")) == (2, 4) - @test size(tensors(ψ; at=site"2")) == (5, 4, 3) - @test size(tensors(ψ; at=site"3")) == (2, 3) t1 = tensors(ψ; at=site"1") @test t1[1, 1] == t1[2, 2] == 1 @test sum(t1) == 2 + t2 = tensors(ψ; at=site"2") @test t2[1, 1, 1] == t2[2, 2, 2] == t2[3, 3, 3] == 1 @test sum(t2) == 3 + t3 = tensors(ψ; at=site"3") @test t3[1, 1] == t3[2, 2] == 1 @test sum(t3) == 2 From 36a43f439b52d03aac31575b360bae1f200b0d71 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Wed, 9 Oct 2024 18:46:26 +0200 Subject: [PATCH 07/17] Format code --- src/Ansatz/MPS.jl | 2 +- test/MPS_test.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index 8ee50ec7f..521ecee4b 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -65,7 +65,7 @@ end """ Base.identity(::Type{MPS}, arraysdims; order=defaultorder(MPS)) -Returns an [`MPS`](@ref) whose tensors are initialized to the identity with the specified +Returns an [`MPS`](@ref) whose tensors are initialized to the identity with the specified dimensions from `arraysdims`. # Keyword Arguments diff --git a/test/MPS_test.jl b/test/MPS_test.jl index 4cc017404..dd8682533 100644 --- a/test/MPS_test.jl +++ b/test/MPS_test.jl @@ -25,7 +25,7 @@ @test inds(ψ; at=site"2", dir=:left) == inds(ψ; at=site"1", dir=:right) !== nothing @test inds(ψ; at=site"3", dir=:left) == inds(ψ; at=site"2", dir=:right) !== nothing @test all(i -> size(ψ, inds(ψ; at=Site(i))) == 2, 1:nsites(ψ)) - + @testset "Base.identity" begin arraysdims = [(2, 4), (5, 4, 3), (2, 3)] ψ = identity(MPS, arraysdims) # Default order (:o, :l, :r) From 0112833e1e1f6e8aa87667c314187c5c587ce3c2 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Thu, 10 Oct 2024 18:52:24 +0200 Subject: [PATCH 08/17] Rewrite MPS identity init function to nsites instead of arrays' dimensions --- src/Ansatz/MPS.jl | 20 ++++++++++++------ test/MPS_test.jl | 52 +++++++++++++++++++++++++++++++---------------- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index 521ecee4b..aadc8251e 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -72,18 +72,26 @@ dimensions from `arraysdims`. - `order` Tensors' indices order. Default: output - left - right (:o, :l, :r). """ -function Base.identity(::Type{MPS}, arraysdims; order=defaultorder(MPS)) - @assert length(arraysdims[1]) == 2 "First array must have 2 dimensions" - @assert all(==(3) ∘ length, arraysdims[2:(end - 1)]) "All arrays must have 3 dimensions" - @assert length(arraysdims[end]) == 2 "Last array must have 2 dimensions" +function Base.identity(::Type{MPS}, n; physdim=2, maxdim=physdim^(n ÷ 2), order=defaultorder(MPS)) issetequal(order, defaultorder(MPS)) || throw(ArgumentError("order must be a permutation of $(String.(defaultorder(MPS)))")) + # Create bond dimensions until the middle of the MPS considering maxdim + virtualdims = physdim .^ collect(1:n ÷ 2) + virtualdims = ifelse.((virtualdims .> maxdim), maxdim, virtualdims) + # Complete the bond dimensions of the other half of the MPS + virtualdims = vcat(virtualdims, reverse(n % 2 == 1 ? virtualdims : virtualdims[1:end-1])) + + # Create each site dimensions in default order (:o, :l, :r) + arraysdims = [[physdim, virtualdims[1]]] + append!(arraysdims, [[physdim, virtualdims[i], virtualdims[i+1]] for i in 1:(length(virtualdims)-1)]) + push!(arraysdims, [physdim, virtualdims[end]]) + + # Create the MPS with copy-tensors according to the tensors dimensions return MPS( map(arraysdims) do arrdims - mindim = minimum(arrdims) arr = zeros(ComplexF64, arrdims...) - deltas = ntuple(x -> ntuple(_ -> x, length(arrdims)), mindim) + deltas = [fill(i, length(arrdims)) for i in 1:physdim] broadcast(delta -> arr[delta...] = 1.0, deltas) arr end; diff --git a/test/MPS_test.jl b/test/MPS_test.jl index dd8682533..c74a7acbc 100644 --- a/test/MPS_test.jl +++ b/test/MPS_test.jl @@ -27,24 +27,42 @@ @test all(i -> size(ψ, inds(ψ; at=Site(i))) == 2, 1:nsites(ψ)) @testset "Base.identity" begin - arraysdims = [(2, 4), (5, 4, 3), (2, 3)] - ψ = identity(MPS, arraysdims) # Default order (:o, :l, :r) - - @test size(tensors(ψ; at=site"1")) == arraysdims[1] - @test size(tensors(ψ; at=site"2")) == arraysdims[2] - @test size(tensors(ψ; at=site"3")) == arraysdims[3] - - t1 = tensors(ψ; at=site"1") - @test t1[1, 1] == t1[2, 2] == 1 - @test sum(t1) == 2 - - t2 = tensors(ψ; at=site"2") - @test t2[1, 1, 1] == t2[2, 2, 2] == t2[3, 3, 3] == 1 - @test sum(t2) == 3 + nsites_cases = [6, 7, 6, 7] + physdim_cases = [3, 2, 3, 2] + maxdim_cases = [nothing, nothing, 9, 4] # nothing means default + expected_tensorsizes_cases = [ + [(3,3), (3,3,9), (3,9,27), (3,27,9), (3,9,3), (3,3)], + [(2,2), (2,2,4), (2,4,8), (2,8,8), (2,8,4), (2,4,2), (2,2)], + [(3,3), (3,3,9), (3,9,9), (3,9,9), (3,9,3), (3,3)], + [(2,2), (2,2,4), (2,4,4), (2,4,4), (2,4,4), (2,4,2), (2,2)] + ] + + for (nsites, physdim, expected_tensorsizes, maxdim) in zip(nsites_cases, physdim_cases, expected_tensorsizes_cases, maxdim_cases) + ψ = isnothing(maxdim) ? identity(MPS, nsites; physdim=physdim) : identity(MPS, nsites; physdim=physdim, maxdim=maxdim) + # Test the tensor dimensions + obtained_tensorsizes = size.(tensors(ψ)) + @test obtained_tensorsizes == expected_tensorsizes + + # Test whether all tensors are the identity + alltns = tensors(ψ) + # - Test extreme tensors (2D) equal identity + diagonal_2D = [fill(i, 2) for i in 1:physdim] + @test all(delta -> alltns[1][delta...] == 1, diagonal_2D) + @test sum(alltns[1]) == physdim + @test all(delta -> alltns[end][delta...] == 1, diagonal_2D) + @test sum(alltns[end]) == physdim + # - Test middle tensors (3D) equal identity + diagonal_3D = [fill(i, 3) for i in 1:physdim] + @test all(tns -> all(delta -> tns[delta...] == 1, diagonal_3D), alltns[2:end-1]) + @test all(tns -> sum(tns) == physdim, alltns[2:end-1]) + + # Test whether the contraction gives the identity + contracted_ψ = contract(ψ) + diagonal_nsitesD = [fill(i, nsites) for i in 1:physdim] + @test all(delta -> contracted_ψ[delta...] == 1, diagonal_nsitesD) + @test sum(contracted_ψ) == physdim + end - t3 = tensors(ψ; at=site"3") - @test t3[1, 1] == t3[2, 2] == 1 - @test sum(t3) == 2 end @testset "Site" begin From 36ad5c27ebc4adadd0e83cb31de45c597fee0b09 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Thu, 10 Oct 2024 18:53:41 +0200 Subject: [PATCH 09/17] Format julia code --- src/Ansatz/MPS.jl | 6 +++--- test/MPS_test.jl | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index aadc8251e..f278fe6f8 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -77,14 +77,14 @@ function Base.identity(::Type{MPS}, n; physdim=2, maxdim=physdim^(n ÷ 2), order throw(ArgumentError("order must be a permutation of $(String.(defaultorder(MPS)))")) # Create bond dimensions until the middle of the MPS considering maxdim - virtualdims = physdim .^ collect(1:n ÷ 2) + virtualdims = physdim .^ collect(1:(n ÷ 2)) virtualdims = ifelse.((virtualdims .> maxdim), maxdim, virtualdims) # Complete the bond dimensions of the other half of the MPS - virtualdims = vcat(virtualdims, reverse(n % 2 == 1 ? virtualdims : virtualdims[1:end-1])) + virtualdims = vcat(virtualdims, reverse(n % 2 == 1 ? virtualdims : virtualdims[1:(end - 1)])) # Create each site dimensions in default order (:o, :l, :r) arraysdims = [[physdim, virtualdims[1]]] - append!(arraysdims, [[physdim, virtualdims[i], virtualdims[i+1]] for i in 1:(length(virtualdims)-1)]) + append!(arraysdims, [[physdim, virtualdims[i], virtualdims[i + 1]] for i in 1:(length(virtualdims) - 1)]) push!(arraysdims, [physdim, virtualdims[end]]) # Create the MPS with copy-tensors according to the tensors dimensions diff --git a/test/MPS_test.jl b/test/MPS_test.jl index c74a7acbc..54f6aed8b 100644 --- a/test/MPS_test.jl +++ b/test/MPS_test.jl @@ -31,14 +31,19 @@ physdim_cases = [3, 2, 3, 2] maxdim_cases = [nothing, nothing, 9, 4] # nothing means default expected_tensorsizes_cases = [ - [(3,3), (3,3,9), (3,9,27), (3,27,9), (3,9,3), (3,3)], - [(2,2), (2,2,4), (2,4,8), (2,8,8), (2,8,4), (2,4,2), (2,2)], - [(3,3), (3,3,9), (3,9,9), (3,9,9), (3,9,3), (3,3)], - [(2,2), (2,2,4), (2,4,4), (2,4,4), (2,4,4), (2,4,2), (2,2)] + [(3, 3), (3, 3, 9), (3, 9, 27), (3, 27, 9), (3, 9, 3), (3, 3)], + [(2, 2), (2, 2, 4), (2, 4, 8), (2, 8, 8), (2, 8, 4), (2, 4, 2), (2, 2)], + [(3, 3), (3, 3, 9), (3, 9, 9), (3, 9, 9), (3, 9, 3), (3, 3)], + [(2, 2), (2, 2, 4), (2, 4, 4), (2, 4, 4), (2, 4, 4), (2, 4, 2), (2, 2)], ] - for (nsites, physdim, expected_tensorsizes, maxdim) in zip(nsites_cases, physdim_cases, expected_tensorsizes_cases, maxdim_cases) - ψ = isnothing(maxdim) ? identity(MPS, nsites; physdim=physdim) : identity(MPS, nsites; physdim=physdim, maxdim=maxdim) + for (nsites, physdim, expected_tensorsizes, maxdim) in + zip(nsites_cases, physdim_cases, expected_tensorsizes_cases, maxdim_cases) + ψ = if isnothing(maxdim) + identity(MPS, nsites; physdim=physdim) + else + identity(MPS, nsites; physdim=physdim, maxdim=maxdim) + end # Test the tensor dimensions obtained_tensorsizes = size.(tensors(ψ)) @test obtained_tensorsizes == expected_tensorsizes @@ -53,8 +58,8 @@ @test sum(alltns[end]) == physdim # - Test middle tensors (3D) equal identity diagonal_3D = [fill(i, 3) for i in 1:physdim] - @test all(tns -> all(delta -> tns[delta...] == 1, diagonal_3D), alltns[2:end-1]) - @test all(tns -> sum(tns) == physdim, alltns[2:end-1]) + @test all(tns -> all(delta -> tns[delta...] == 1, diagonal_3D), alltns[2:(end - 1)]) + @test all(tns -> sum(tns) == physdim, alltns[2:(end - 1)]) # Test whether the contraction gives the identity contracted_ψ = contract(ψ) @@ -62,7 +67,6 @@ @test all(delta -> contracted_ψ[delta...] == 1, diagonal_nsitesD) @test sum(contracted_ψ) == physdim end - end @testset "Site" begin From 50d5b3c4bfcdcb1fee87a1b30144bce63fa498b7 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Thu, 10 Oct 2024 19:11:35 +0200 Subject: [PATCH 10/17] Update docstring of identity --- src/Ansatz/MPS.jl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index f278fe6f8..9ecb91a16 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -63,16 +63,18 @@ function MPS(arrays::Vector{<:AbstractArray}; order=defaultorder(MPS)) end """ - Base.identity(::Type{MPS}, arraysdims; order=defaultorder(MPS)) + Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ 2), order=MPS.defaultorder(MPS)) -Returns an [`MPS`](@ref) whose tensors are initialized to the identity with the specified -dimensions from `arraysdims`. +Returns an [`MPS`](@ref) of `n` sites whose tensors are initialized to the identity attending to the +physical dimension `physdim` and the maximum dimension `maxdim`. # Keyword Arguments - - `order` Tensors' indices order. Default: output - left - right (:o, :l, :r). + - `physdim` The physical or output dimension of each site. Default is 2 + - `maxdim` The maximum bond dimension. Default is `physdim^(n ÷ 2)`. + - `order` Tensors' indices order. Default: output - left - right `(:o, :l, :r)`. """ -function Base.identity(::Type{MPS}, n; physdim=2, maxdim=physdim^(n ÷ 2), order=defaultorder(MPS)) +function Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ 2), order=defaultorder(MPS)) issetequal(order, defaultorder(MPS)) || throw(ArgumentError("order must be a permutation of $(String.(defaultorder(MPS)))")) From b7917c9c4b08dd3947631fa5f2ce840dbfb8c0fb Mon Sep 17 00:00:00 2001 From: Todorbsc <145352308+Todorbsc@users.noreply.github.com> Date: Fri, 11 Oct 2024 13:02:52 +0200 Subject: [PATCH 11/17] Clean code in test (suggested by Jofre) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jofre Vallès Muns <61060572+jofrevalles@users.noreply.github.com> --- test/MPS_test.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/MPS_test.jl b/test/MPS_test.jl index 54f6aed8b..adc70e263 100644 --- a/test/MPS_test.jl +++ b/test/MPS_test.jl @@ -44,19 +44,22 @@ else identity(MPS, nsites; physdim=physdim, maxdim=maxdim) end + # Test the tensor dimensions obtained_tensorsizes = size.(tensors(ψ)) @test obtained_tensorsizes == expected_tensorsizes # Test whether all tensors are the identity alltns = tensors(ψ) + # - Test extreme tensors (2D) equal identity diagonal_2D = [fill(i, 2) for i in 1:physdim] @test all(delta -> alltns[1][delta...] == 1, diagonal_2D) @test sum(alltns[1]) == physdim @test all(delta -> alltns[end][delta...] == 1, diagonal_2D) @test sum(alltns[end]) == physdim - # - Test middle tensors (3D) equal identity + + # - Test bulk tensors (3D) equal identity diagonal_3D = [fill(i, 3) for i in 1:physdim] @test all(tns -> all(delta -> tns[delta...] == 1, diagonal_3D), alltns[2:(end - 1)]) @test all(tns -> sum(tns) == physdim, alltns[2:(end - 1)]) From 984ba14ad6f005d170e355478eedd3ce897e7920 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Fri, 11 Oct 2024 13:09:25 +0200 Subject: [PATCH 12/17] Format julia code --- test/MPS_test.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/MPS_test.jl b/test/MPS_test.jl index adc70e263..9ac2f7350 100644 --- a/test/MPS_test.jl +++ b/test/MPS_test.jl @@ -44,21 +44,21 @@ else identity(MPS, nsites; physdim=physdim, maxdim=maxdim) end - + # Test the tensor dimensions obtained_tensorsizes = size.(tensors(ψ)) @test obtained_tensorsizes == expected_tensorsizes # Test whether all tensors are the identity alltns = tensors(ψ) - + # - Test extreme tensors (2D) equal identity diagonal_2D = [fill(i, 2) for i in 1:physdim] @test all(delta -> alltns[1][delta...] == 1, diagonal_2D) @test sum(alltns[1]) == physdim @test all(delta -> alltns[end][delta...] == 1, diagonal_2D) @test sum(alltns[end]) == physdim - + # - Test bulk tensors (3D) equal identity diagonal_3D = [fill(i, 3) for i in 1:physdim] @test all(tns -> all(delta -> tns[delta...] == 1, diagonal_3D), alltns[2:(end - 1)]) From b582cca439f47aed3c0d26d52a6e73be19eccbf0 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Mon, 14 Oct 2024 13:44:42 +0200 Subject: [PATCH 13/17] Refactor virtualdims in identity (suggested by Sergio) --- src/Ansatz/MPS.jl | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index 9ecb91a16..cb36529f4 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -66,7 +66,7 @@ end Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ 2), order=MPS.defaultorder(MPS)) Returns an [`MPS`](@ref) of `n` sites whose tensors are initialized to the identity attending to the -physical dimension `physdim` and the maximum dimension `maxdim`. +physical dimension `physdim` and the maximum bond dimension `maxdim`. # Keyword Arguments @@ -79,10 +79,9 @@ function Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ throw(ArgumentError("order must be a permutation of $(String.(defaultorder(MPS)))")) # Create bond dimensions until the middle of the MPS considering maxdim - virtualdims = physdim .^ collect(1:(n ÷ 2)) - virtualdims = ifelse.((virtualdims .> maxdim), maxdim, virtualdims) + virtualdims = min.(maxdim, physdim .^ (1:(n ÷ 2))) # Complete the bond dimensions of the other half of the MPS - virtualdims = vcat(virtualdims, reverse(n % 2 == 1 ? virtualdims : virtualdims[1:(end - 1)])) + virtualdims = vcat(virtualdims, virtualdims[(isodd(n) ? end : end - 1):-1:1]) # Create each site dimensions in default order (:o, :l, :r) arraysdims = [[physdim, virtualdims[1]]] From e4e719ce247fad43c3203a73ca4d7b121dafac61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= <15837247+mofeing@users.noreply.github.com> Date: Mon, 14 Oct 2024 14:14:03 +0200 Subject: [PATCH 14/17] Update src/Ansatz/MPS.jl --- src/Ansatz/MPS.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index cb36529f4..aaf32e572 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -80,6 +80,7 @@ function Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ # Create bond dimensions until the middle of the MPS considering maxdim virtualdims = min.(maxdim, physdim .^ (1:(n ÷ 2))) + # Complete the bond dimensions of the other half of the MPS virtualdims = vcat(virtualdims, virtualdims[(isodd(n) ? end : end - 1):-1:1]) From 3d32a5c9a116e36154a0616406dffd39d1f1341c Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Mon, 14 Oct 2024 14:39:37 +0200 Subject: [PATCH 15/17] Restrict to default order in identity MPS --- src/Ansatz/MPS.jl | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index aaf32e572..c19d7eaf6 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -63,21 +63,18 @@ function MPS(arrays::Vector{<:AbstractArray}; order=defaultorder(MPS)) end """ - Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ 2), order=MPS.defaultorder(MPS)) + Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ 2)) -Returns an [`MPS`](@ref) of `n` sites whose tensors are initialized to the identity attending to the -physical dimension `physdim` and the maximum bond dimension `maxdim`. +Returns an [`MPS`](@ref) of `n` sites whose tensors are initialized to the identity +in default order `(:o, :l, :r)` attending to the physical dimension `physdim` and +the maximum bond dimension `maxdim`. # Keyword Arguments - `physdim` The physical or output dimension of each site. Default is 2 - `maxdim` The maximum bond dimension. Default is `physdim^(n ÷ 2)`. - - `order` Tensors' indices order. Default: output - left - right `(:o, :l, :r)`. """ -function Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ 2), order=defaultorder(MPS)) - issetequal(order, defaultorder(MPS)) || - throw(ArgumentError("order must be a permutation of $(String.(defaultorder(MPS)))")) - +function Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ 2)) # Create bond dimensions until the middle of the MPS considering maxdim virtualdims = min.(maxdim, physdim .^ (1:(n ÷ 2))) @@ -97,7 +94,7 @@ function Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ broadcast(delta -> arr[delta...] = 1.0, deltas) arr end; - order=order, + order=defaultorder(MPS), ) end From 31f5482031ccc44347048f4f6692dea77596a67f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20S=C3=A1nchez=20Ram=C3=ADrez?= <15837247+mofeing@users.noreply.github.com> Date: Mon, 14 Oct 2024 19:59:01 +0200 Subject: [PATCH 16/17] Update src/Ansatz/MPS.jl --- src/Ansatz/MPS.jl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index c19d7eaf6..16e6dd470 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -65,14 +65,12 @@ end """ Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ 2)) -Returns an [`MPS`](@ref) of `n` sites whose tensors are initialized to the identity -in default order `(:o, :l, :r)` attending to the physical dimension `physdim` and -the maximum bond dimension `maxdim`. +Returns an [`MPS`](@ref) of `n` sites whose tensors are initialized to COPY-tensors. # Keyword Arguments - - `physdim` The physical or output dimension of each site. Default is 2 - - `maxdim` The maximum bond dimension. Default is `physdim^(n ÷ 2)`. + - `physdim` The physical or output dimension of each site. Defaults to 2. + - `maxdim` The maximum bond dimension. Defaults to `physdim^(n ÷ 2)`. """ function Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ 2)) # Create bond dimensions until the middle of the MPS considering maxdim From 5e2ac42b88812f68d762d509127dc98744c3d4c9 Mon Sep 17 00:00:00 2001 From: Todorbsc Date: Mon, 14 Oct 2024 20:52:24 +0200 Subject: [PATCH 17/17] Remove order parameter in identity --- src/Ansatz/MPS.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Ansatz/MPS.jl b/src/Ansatz/MPS.jl index 16e6dd470..28f83f7f6 100644 --- a/src/Ansatz/MPS.jl +++ b/src/Ansatz/MPS.jl @@ -91,8 +91,7 @@ function Base.identity(::Type{MPS}, n::Integer; physdim=2, maxdim=physdim^(n ÷ deltas = [fill(i, length(arrdims)) for i in 1:physdim] broadcast(delta -> arr[delta...] = 1.0, deltas) arr - end; - order=defaultorder(MPS), + end, ) end