Skip to content

Commit

Permalink
corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomino committed Feb 2, 2021
1 parent 54bfdd4 commit 0aa40ba
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 132 deletions.
1 change: 0 additions & 1 deletion Project.toml
Expand Up @@ -14,7 +14,6 @@ LowRankApprox = "898213cb-b102-5a47-900c-97e73b919f73"
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
TensorCast = "02d47bb6-7ce6-556a-be16-bb1710789e2b"
Expand Down
13 changes: 3 additions & 10 deletions src/utils.jl
Expand Up @@ -2,17 +2,10 @@ export idx, ising, proj
export HadamardMPS, rq
export all_states, local_basis, enum, state_to_ind, rank_vec
export @state
export reshape_row

using Base.Cartesian
import Base.Prehashed

function reshape_row(A::AbstractArray{T}, dims::Tuple) where {T <: Number}
ord = reverse(1:length(dims))
A = reshape(A, reverse(dims))
permutedims(A, ord)
end

enum(vec) = Dict(v => i for (i, v) enumerate(vec))

idx::Int) === -1) ? 1 : σ + 1
Expand Down Expand Up @@ -59,7 +52,7 @@ local_basis(d::Int) = union(-1, 1:d-1)
local_basis::AbstractMPS, i::Int) = local_basis(size(ψ[i], 2))

function proj(state, dims::Union{Vector, NTuple})
P = Matrix{Float64}[]
P = Matrix{Float64}[]
for (σ, r) zip(state, dims)
v = zeros(r)
v[idx(σ)...] = 1.
Expand Down Expand Up @@ -133,7 +126,7 @@ function Base.LinearIndices(m::Int, n::Int, origin::Symbol=:NW)

if origin (:NW, :NE, :SE, :SW)
i_max, j_max = m, n
else
else
i_max, j_max = n, m
end

Expand Down Expand Up @@ -224,4 +217,4 @@ function rank_vec(ig::MetaGraph)
rank = get_prop(ig, :rank)
L = get_prop(ig, :L)
Int[get(rank, i, 1) for i=1:L]
end
end
54 changes: 17 additions & 37 deletions test/base.jl
Expand Up @@ -18,13 +18,13 @@ T = ComplexF64
@test rank(ψ) == Tuple(fill(d, 1:sites))
@test bond_dimension(ψ) D

ϕ = copy(ψ)
ϕ = copy(ψ)
@test ϕ == ψ
@test ϕ ψ

show(ψ)

dims = (3, 2, 5, 4)
dims = (3, 2, 5, 4)
@info "Veryfing ψ of arbitrary rank" dims

ψ = randn(MPS{T}, D, dims)
Expand All @@ -39,7 +39,7 @@ T = ComplexF64
@test rank(ψ) == dims
@test bond_dimension(ψ) D

ϕ = copy(ψ)
ϕ = copy(ψ)
@test ϕ == ψ
@test ϕ ψ

Expand All @@ -57,40 +57,20 @@ end
@test eltype(O) == ComplexF64

P = copy(O)
@test P == O
@test P O
@test P == O
@test P O
end

@testset "Reshaping (row-wise)" begin
vec = Vector(1:6)

A = reshape_row(vec, (2, 3))
B = [1 2 3; 4 5 6]

@test A B
end

@testset "Basic vector to tensor reshaping" begin
dims = (2, 3, 4, 5)
states = [randn(T, d) for d dims]
vec = kron(states...)

ψ = tensor(MPS(states))
ϕ = reshape_row(vec, dims)

@test ψ ϕ
end

@testset "MPS from tensor" begin
ϵ = 1E-14

dims = (2,3,4,3,5)
dims = (2,3,4,3,5)
sites = length(dims)
A = randn(T, dims)
A = randn(T, dims)

@test sqrt(sum(abs.(A) .^ 2)) norm(A)

@test ndims(A) == sites
@test ndims(A) == sites
@test size(A) == dims

ψ = MPS(A, :right)
Expand All @@ -106,18 +86,18 @@ end
@test rank(ψ) == size(AA)
@test norm(AA) 1
@test size(AA) == size(A)

vA = vec(A)
nA = norm(vA)
@test abs(1 - abs(dot(vec(AA), vA ./ nA))) < ϵ
@test abs(1 - abs(dot(vec(AA), vA ./ nA))) < ϵ
#@test AA ≈ A ./ norm(A) # this is true "module phase"

B = randn(T, dims...)
ϕ = MPS(B, :left)

@test norm(ϕ) 1
@test_nowarn verify_bonds(ϕ)
@test_nowarn verify_physical_dims(ϕ, dims)
@test_nowarn verify_physical_dims(ϕ, dims)
@test is_left_normalized(ϕ)
show(ϕ)

Expand All @@ -126,16 +106,16 @@ end
@test rank(ϕ) == size(BB)
@test norm(BB) 1
@test sqrt(sum(abs.(B) .^ 2)) norm(B)

vB = vec(B)
nB = norm(vB)
@test abs(1 - abs(dot(vec(BB), vB ./ nB))) < ϵ
@test abs(1 - abs(dot(vec(BB), vB ./ nB))) < ϵ
#@test BB ≈ B ./ norm(B) # this is true "module phase"

χ = MPS(A, :left)

@test norm(χ) 1
@test abs(1 - abs(dot(ψ, χ))) < ϵ
@test abs(1 - abs(dot(ψ, χ))) < ϵ
end

end
end
132 changes: 69 additions & 63 deletions test/test_helpers.jl
Expand Up @@ -52,27 +52,30 @@ function make_interactions_case1()
L = 9

D = Dict{Tuple{Int64,Int64},Float64}()
push!(D, (1,1) => 1.0)
push!(D, (2,2) => -2.0)
push!(D, (3,3) => 4.0)
push!(D, (4,4) => 0.0)
push!(D, (5,5) => 1.5)
push!(D, (6,6) => 0.1)
push!(D, (7,7) => 0.7)
push!(D, (8,8) => -0.16)
push!(D, (9,9) => 0.66)
push!(D, (1, 2) => -1.0)
push!(D, (1, 4) => -3)
push!(D, (2, 3) => -3.0)
push!(D, (2, 5) => -2.0)
push!(D, (3,6) => 3.0)
push!(D, (4,5) => 1.0)
push!(D, (4,7) => -0.02)
push!(D, (5,6) => -0.5)
push!(D, (5,8) => 1.0)
push!(D, (6,9) => -1.04)
push!(D, (7,8) => 1.7)
push!(D, (8,9) => -0.1)
push!(
D,
(1,1) => 1.0,
(2,2) => -2.0,
(3,3) => 4.0,
(4,4) => 0.0,
(5,5) => 1.5,
(6,6) => 0.1,
(7,7) => 0.7,
(8,8) => -0.16,
(9,9) => 0.66,

(1, 2) => -1.0,
(1, 4) => -3,
(2, 3) => -3.0,
(2, 5) => -2.0,
(3,6) => 3.0,
(4,5) => 1.0,
(4,7) => -0.02,
(5,6) => -0.5,
(5,8) => 1.0,
(6,9) => -1.04,
(7,8) => 1.7,
(8,9) => -0.1)


ising_graph(D, L)#, ising_graph(D, L)
Expand All @@ -84,48 +87,51 @@ function make_interactions_case2()
f1 = 1
L = 16
D = Dict{Tuple{Int64,Int64},Float64}()
push!(D, (1, 1) => -2.8)
push!(D, (2, 2) => 2.7)
push!(D, (3, 3) => -2.6)
push!(D, (4, 4) => 2.5)
push!(D, (5, 5) => -2.4)
push!(D, (6, 6) => 2.3)
push!(D, (7, 7) => -2.2)
push!(D, (8, 8) => 2.1)
push!(D, (9, 9) => -2.0)
push!(D, (10, 10) => 1.9)
push!(D, (11, 11) => -1.8)
push!(D, (12, 12) => 1.7)
push!(D, (13, 13) => -1.6)
push!(D, (14, 14) => 1.5)
push!(D, (15, 15) => -1.4)
push!(D, (16, 16) => 1.3)

push!(D, (1, 2) => 0.3)
push!(D, (1, 5) => 0.2)
push!(D, (2, 3) => 0.255)
push!(D, (2, 6) => 0.21)
push!(D, (3, 4) => 0.222)
push!(D, (3, 7) => 0.213)

push!(D, (4, 8) => 0.2)
push!(D, (5, 6) => 0.15)
push!(D, (5, 9) => 0.211)
push!(D, (6, 7) => 0.2)
push!(D, (6, 10) => 0.15)
push!(D, (7, 8) => 0.11)
push!(D, (7, 11) => 0.35)
push!(D, (8, 12) => 0.19)
push!(D, (9, 10) => 0.222)
push!(D, (9, 13) => 0.15)
push!(D, (10, 11) => 0.28)
push!(D, (10, 14) => 0.21)
push!(D, (11, 12) => 0.19)
push!(D, (11, 15) => 0.18)
push!(D, (12, 16) => 0.27)
push!(D, (13, 14) => 0.32)
push!(D, (14, 15) => 0.19)
push!(D, (15, 16) => 0.21)
push!(
D,
(1, 1) => -2.8,
(2, 2) => 2.7,
(3, 3) => -2.6,
(4, 4) => 2.5,
(5, 5) => -2.4,
(6, 6) => 2.3,
(7, 7) => -2.2,
(8, 8) => 2.1,
(9, 9) => -2.0,
(10, 10) => 1.9,
(11, 11) => -1.8,
(12, 12) => 1.7,
(13, 13) => -1.6,
(14, 14) => 1.5,
(15, 15) => -1.4,
(16, 16) => 1.3,

(1, 2) => 0.3,
(1, 5) => 0.2,
(2, 3) => 0.255,
(2, 6) => 0.21,
(3, 4) => 0.222,
(3, 7) => 0.213,

(4, 8) => 0.2,
(5, 6) => 0.15,
(5, 9) => 0.211,
(6, 7) => 0.2,
(6, 10) => 0.15,
(7, 8) => 0.11,
(7, 11) => 0.35,
(8, 12) => 0.19,
(9, 10) => 0.222,
(9, 13) => 0.15,
(10, 11) => 0.28,
(10, 14) => 0.21,
(11, 12) => 0.19,
(11, 15) => 0.18,
(12, 16) => 0.27,
(13, 14) => 0.32,
(14, 15) => 0.19,
(15, 16) => 0.21
)

ising_graph(D, L)
end
21 changes: 0 additions & 21 deletions test/utils.jl
Expand Up @@ -4,24 +4,3 @@
x = rand(2, 2, d)
@test (@state x[1, 1, σ]) == x[1, 1, 20]
end

@testset "Reshape by rows" begin
A = [1 2; 3 4]
@test A[1,2] == 2
display(A)
B = reshape_row(A, (2,2))
display(B)
@test B[1,1] == 1
@test B[1,2] == 3
@test B[2,1] == 2
@test B[2,2] == 4

C = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
display(C)
D = reshape_row(C, (2,2,2,2))
display(D)
@test D[:, :, 1, 1] == [1 2 ; 3 4]
@test D[:, :, 1, 2] == [5 6 ; 7 8]
@test D[:, :, 2, 1] == [9 10 ; 11 12]
@test D[:, :, 2, 2] == [13 14 ; 15 16]
end

0 comments on commit 0aa40ba

Please sign in to comment.