Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gnojownik fix #28

Merged
merged 4 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/mps_implementation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function add_MPO!(mpo::AbstractMPO{T}, vec_edges::VE, g::MetaGraph, β::T) where
for e in vec_edges
j = dst(e)
# minus for convention 1/2 since each is taken doubled in the product
J = -1/2*props(g, e)[:J]
J = 1/2*props(g, e)[:J]
# minus for probability
mpo[j] = Ctensor(T, -J*β, d, j==l)
end
Expand All @@ -81,7 +81,7 @@ function add_phase!(mps::AbstractMPS{T}, g::MetaGraph, β::T) where T<: Real
for i in 1:length(mps)
internal_e = props(g, i)[:energy]
for j in 1:length(internal_e)
mps[i][:,j,:] = mps[i][:,j,:]*exp(-β/2*internal_e[j])
mps[i][:,j,:] = mps[i][:,j,:]*exp(β/2*internal_e[j])
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/notation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ function M_of_interaction(g::EE, g1::EE, ig::MetaGraph)
σ_cluster = ind2spin(i, no_spins)
for j in 1:2^subset_size
σ = ind2spin(j, subset_size)
@inbounds energy[j,i] = -sum(J.*σ.*σ_cluster[spin_subset])
@inbounds energy[j,i] = sum(J.*σ.*σ_cluster[spin_subset])
end
end
energy
Expand Down
60 changes: 30 additions & 30 deletions src/spectrum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct Spectrum
states::Array{Vector{<:Number}}
end

struct MPSControl
struct MPSControl
max_bond::Int
var_ϵ::Number
max_sweeps::Int
Expand All @@ -19,7 +19,7 @@ end
function solve(ψ::MPS, keep::Int)
@assert keep > 0 "Number of states has to be > 0"
T = eltype(ψ)

keep_extra = keep
pCut = prob = 0.
k = 1
Expand All @@ -38,7 +38,7 @@ function solve(ψ::MPS, keep::Int)
LL = zeros(T, b, b, k, d)
config = zeros(Int, i, k, d)

for j ∈ 1:k
for j ∈ 1:k
L = left_env[:, :, j]

for σ ∈ local_basis(d)
Expand All @@ -47,19 +47,19 @@ function solve(ψ::MPS, keep::Int)
LL[:, :, j, m] = M[:, m, :]' * (L * M[:, m, :])
pdo[j, m] = tr(LL[:, :, j, m])
config[:, j, m] = vcat(states[:, j]..., σ)
end
end
end

perm = collect(1: k * d)
k = min(k * d, keep_extra)

if k >= keep_extra
partialsortperm!(perm, vec(pdo), 1:k, rev=true)
partialsortperm!(perm, vec(pdo), 1:k, rev=true)
prob = vec(pdo)[perm]
pCut < last(prob) ? pCut = last(prob) : ()
end

@cast A[α, β, (l, d)] |= LL[α, β, l, d]
@cast A[α, β, (l, d)] |= LL[α, β, l, d]
left_env = A[:, :, perm]

@cast B[α, (l, d)] |= config[α, l, d]
Expand All @@ -72,7 +72,7 @@ end
function solve_new(ψ::MPS, keep::Int)
@assert keep > 0 "Number of states has to be > 0"
T = eltype(ψ)

keep_extra = keep
lpCut = -1000
k = 1
Expand All @@ -92,7 +92,7 @@ function solve_new(ψ::MPS, keep::Int)
LL = zeros(T, b, k, d)
config = zeros(Int, i, k, d)

for j ∈ 1:k
for j ∈ 1:k
L = left_env[:, j]

for σ ∈ local_basis(d)
Expand All @@ -111,9 +111,9 @@ function solve_new(ψ::MPS, keep::Int)

if k > keep_extra
k = keep_extra
partialsortperm!(perm, vec(lpdo), 1:k, rev=true)
partialsortperm!(perm, vec(lpdo), 1:k, rev=true)
lprob = vec(lpdo)[perm]
lpCut < last(lprob) ? lpCut = last(lprob) : ()
lpCut < last(lprob) ? lpCut = last(lprob) : ()
end

lprob = vec(lpdo)[perm]
Expand All @@ -124,7 +124,7 @@ function solve_new(ψ::MPS, keep::Int)
end
states = states'
states, lprob, lpCut
end
end

function _apply_bias!(ψ::AbstractMPS, ig::MetaGraph, dβ::Number, i::Int)
M = ψ[i]
Expand All @@ -133,22 +133,22 @@ function _apply_bias!(ψ::AbstractMPS, ig::MetaGraph, dβ::Number, i::Int)
h = get_prop(ig, i, :h)
v = [exp(-0.5 * dβ * h * σ) for σ ∈ local_basis(d)]

@cast M[x, σ, y] = M[x, σ, y] * v[σ]
@cast M[x, σ, y] = M[x, σ, y] * v[σ]
ψ[i] = M
end

function _apply_exponent!(ψ::AbstractMPS, ig::MetaGraph, dβ::Number, i::Int, j::Int, last::Int)
M = ψ[j]
D = I(ψ, i)
J = get_prop(ig, i, j, :J)

J = get_prop(ig, i, j, :J)
C = [ exp(-0.5 * dβ * k * J * l) for k ∈ local_basis(ψ, i), l ∈ local_basis(ψ, j) ]

if j == last
@cast M̃[(x, a), σ, b] := C[x, σ] * M[a, σ, b]
@cast M̃[(x, a), σ, b] := C[x, σ] * M[a, σ, b]
else
@cast M̃[(x, a), σ, (y, b)] := C[x, σ] * D[x, y] * M[a, σ, b]
end
@cast M̃[(x, a), σ, (y, b)] := C[x, σ] * D[x, y] * M[a, σ, b]
end

ψ[j] = M̃
end
Expand All @@ -161,12 +161,12 @@ function _apply_projector!(ψ::AbstractMPS, i::Int)
ψ[i] = M̃
end

function _apply_nothing!(ψ::AbstractMPS, l::Int, i::Int)
M = ψ[l]
function _apply_nothing!(ψ::AbstractMPS, l::Int, i::Int)
M = ψ[l]
D = I(ψ, i)

@cast M̃[(x, a), σ, (y, b)] := D[x, y] * M[a, σ, b]
ψ[l] = M̃
@cast M̃[(x, a), σ, (y, b)] := D[x, y] * M[a, σ, b]
ψ[l] = M̃
end

_holes(nbrs::Vector) = setdiff(first(nbrs) : last(nbrs), nbrs)
Expand All @@ -192,25 +192,25 @@ function MPS(ig::MetaGraph, control::MPSControl)

@info "Sweeping through β and σ" schedule
for dβ ∈ schedule, i ∈ 1:L
_apply_bias!(ρ, ig, dβ, i)
_apply_bias!(ρ, ig, dβ, i)
is_right = false

nbrs = unique_neighbors(ig, i)
if !isempty(nbrs)
_apply_projector!(ρ, i)

for j ∈ nbrs
_apply_exponent!(ρ, ig, dβ, i, j, last(nbrs))
for j ∈ nbrs
_apply_exponent!(ρ, ig, dβ, i, j, last(nbrs))
end

for l ∈ _holes(nbrs)
for l ∈ _holes(nbrs)
_apply_nothing!(ρ, l, i)
end
end

if bond_dimension(ρ) > Dcut
@info "Compresing MPS" bond_dimension(ρ), Dcut
ρ = compress(ρ, Dcut, tol, max_sweeps)
ρ = compress(ρ, Dcut, tol, max_sweeps)
is_right = true
end
end
Expand All @@ -230,22 +230,22 @@ Return the low energy spectrum

# Details

Calculates \$k\$ lowest energy states
together with the coresponding energies
Calculates \$k\$ lowest energy states
together with the coresponding energies
of a classical Ising Hamiltonian
"""
function brute_force(ig::MetaGraph; num_states::Int=1)
cl = Cluster(ig, 0, enum(vertices(ig)), edges(ig))
brute_force(cl, num_states=num_states)
end
end


function brute_force(cl::Cluster; num_states::Int=1)
σ = collect.(all_states(cl.rank))
energies = energy.(σ, Ref(cl))
perm = partialsortperm(vec(energies), 1:num_states)
Spectrum(energies[perm], σ[perm])
end
end

function full_spectrum(cl::Cluster)
σ = collect.(all_states(cl.rank))
Expand Down
8 changes: 4 additions & 4 deletions test/mps_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ end

@test length(mps1) == 3
# this is B type tensor, only internal energy (± h/2)
@test mps1[1][1,:,:] ≈ [exp(-1/2) 0.0; 0.0 exp(1/2)]
@test mps1[1][1,:,:] ≈ [exp(1/2) 0.0; 0.0 exp(-1/2)]
# type C tensor input from internale enegy and interaction
#±(h/2 + J) -- J is twice due to the symmetry of M
@test mps1[2][1,:,:] ≈ [exp(1/2) 0.0; exp(-1/2) 0.0]
@test mps1[2][2,:,:] ≈ [0. exp(-1)*exp(-1/2); 0. exp(1)*exp(1/2)]
@test mps1[3][:,:,1] ≈ [exp(1/2) exp(-1/2); exp(-1)*exp(-1/2) exp(1)*exp(1/2)]
@test mps1[2][1,:,:] ≈ [exp(-1/2) 0.0; exp(1/2) 0.0]
@test mps1[2][2,:,:] ≈ [0. exp(1)*exp(1/2); 0. exp(-1)*exp(-1/2)]
@test mps1[3][:,:,1] ≈ [exp(-1/2) exp(1/2); exp(1)*exp(1/2) exp(-1)*exp(-1/2)]

####### compute probability ######

Expand Down
8 changes: 4 additions & 4 deletions test/notation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ end
ig = M2graph(M)

g1 = graph4peps(ig, (1,1))
@test props(g1, 1,2)[:M] == [-2.0 2.0; 2.0 -2.0]
@test props(g1, 2,4)[:M] == [-2.0 2.0; 2.0 -2.0]
@test props(g1, 1,2)[:M] == [2.0 -2.0; -2.0 2.0]
@test props(g1, 2,4)[:M] == [2.0 -2.0; -2.0 2.0]
@test props(g1, 1)[:energy] == [-1., 1.]
@test props(g1, 2)[:energy] == [-1., 1.]
@test props(g1, 1,2)[:inds] == [1]
Expand All @@ -154,7 +154,7 @@ end

M = props(g1, 1,2)[:M]
@test size(M) == (4,16)
@test M[:,1] == [-4.0, 0.0, 0.0, 4.0]
@test M[:,1] == [4.0, 0.0, 0.0, -4.0]
e = [-4.0, 2.0, 2.0, 0.0, 2.0, 0.0, 8.0, -2.0, 2.0, 8.0, 0.0, -2.0, 0.0, -2.0, -2.0, -12.0]
@test props(g1, 1)[:energy] == -e
@test props(g1, 1,2)[:inds] == [3, 4]
Expand All @@ -181,7 +181,7 @@ end
@test get_Js(v2, v1, ig) == [2.0, 2.0]
M = M_of_interaction(v2, v1, ig)
@test size(M) == (4, 16)
@test M[:,1] == [-4.0, 0.0, 0.0, 4.0]
@test M[:,1] == [4.0, 0.0, 0.0, -4.0]
end

@testset "operations on spins" begin
Expand Down
8 changes: 4 additions & 4 deletions test/peps_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SpinGlassPEPS: compute_single_tensor, conditional_probabs, get_parameters
import SpinGlassPEPS: make_lower_mps
import SpinGlassPEPS: set_spin_from_letf, spin_index_from_left, spin_indices_from_above
import SpinGlassPEPS: energy, solve

if true
@testset "PEPS - axiliary functions" begin

@testset "partial solution type" begin
Expand Down Expand Up @@ -141,7 +141,7 @@ Mq[8,9] = Mq[9,8] = -0.05

@testset "whole peps tensor" begin

g = M2graph(Mq)
g = M2graph(Mq, -1)
gg = graph4peps(g, (1,1))


Expand Down Expand Up @@ -175,7 +175,7 @@ end
@test mps[2] == 2*ones(2,2,2)

β = 3.
g = M2graph(Mq)
g = M2graph(Mq, -1)
gg = graph4peps(g, (1,1))

M = form_peps(gg, β)
Expand Down Expand Up @@ -226,7 +226,7 @@ end
@test objective ≈ [p1, p2]

end

end

@testset "test an exemple instance" begin

Expand Down
25 changes: 12 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ if CUDA.functional() && CUDA.has_cutensor() && false
end

push!(my_tests,
#"base.jl",
#"contractions.jl",
#"compressions.jl",
#"ising.jl",
#"spectrum.jl",
#"graph.jl",
"PEPS.jl"
#"notation_tests.jl",
# do NOT pass:
#"peps_tests.jl",
#"mps_tests.jl",
#"tests_full_graph.jl",
#"tests_on_data.jl"
"base.jl",
"contractions.jl",
"compressions.jl",
"ising.jl",
"spectrum.jl",
"graph.jl",
"PEPS.jl",
"notation_tests.jl",
"peps_tests.jl",
"mps_tests.jl",
"tests_full_graph.jl",
"tests_on_data.jl"
)

for my_test in my_tests
Expand Down
26 changes: 14 additions & 12 deletions test/test_helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,43 @@ function make_interactions_case1()
L = 9

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

ising_graph(D, L, 1, -1)
ising_graph(D, L, 1, 1)
end


function make_interactions_case2()
f = 1
f1 = 1
L = 16
D1 = Dict((1, 1) => -2.8, (1, 2) => 0.3, (1, 5) => 0.2, (2, 2) => 2.7, (2, 3) => 0.255, (2, 6) => 0.21, (3, 3) => -2.6)
D12 = Dict((3, 4) => 0.222, (3, 7) => 0.213, (4, 4) => 2.5, (4, 8) => 0.2)
D2 = Dict((5, 5) => -2.4, (5, 6) => 0.15, (5, 9) => 0.211, (6, 6) => 2.3, (6, 7) => 0.2)
D22 = Dict((6, 10) => 0.15, (7, 7) => -2.2, (7, 8) => 0.11, (7, 11) => 0.35, (8, 8) => 2.1, (8, 12) => 0.19)
D3 = Dict((9, 9) => -2., (9, 10) => 0.222, (9, 13) => 0.15, (10, 10) => 1.9, (10, 11) => 0.28)
D3 = Dict((9, 9) => -2.0, (9, 10) => 0.222, (9, 13) => 0.15, (10, 10) => 1.9, (10, 11) => 0.28)
D32 = Dict((10, 14) => 0.21, (11, 11) => -1.8, (11, 12) => 0.19, (11, 15) => 0.18, (12, 12) => 1.7, (12, 16) => 0.27)
D4 = Dict((13, 13) => -1.6, (13, 14) => 0.32, (14, 14) => 1.5, (14, 15) => 0.19, (15, 15) => -1.4, (15, 16) => 0.21, (16, 16) => 1.3)
D = merge!(+, D1, D12, D2, D22, D3, D32, D4)
ising_graph(D, L, 1, -1)
ising_graph(D, L, 1, 1)
end
Loading