Skip to content

Commit

Permalink
refactor search and ising
Browse files Browse the repository at this point in the history
  • Loading branch information
lpawela committed Oct 28, 2020
1 parent bbd3465 commit 59d5cfe
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 75 deletions.
1 change: 1 addition & 0 deletions src/SpinGlassPEPS.jl
Expand Up @@ -6,6 +6,7 @@ module SpinGlassPEPS
using LightGraphs
using MetaGraphs
using CSV
const product = Iterators.product

include("base.jl")
include("compressions.jl")
Expand Down
14 changes: 0 additions & 14 deletions src/base.jl
@@ -1,19 +1,6 @@
using Base
export bond_dimension
export MPS_control, Gibbs_control
export _verify_bonds

struct MPS_control
max_bond::Int
var_ϵ::Number
var_max_sweeps::Int
end

struct Gibbs_control
β::Number
β_schedule::Vector{<:Number}
end

for (T, N) in ((:MPO, 4), (:MPS, 3))
AT = Symbol(:Abstract, T)
@eval begin
Expand Down Expand Up @@ -117,7 +104,6 @@ function _verify_bonds(ψ::AbstractMPS)
for i 1:L-1
@assert size(ψ[i], 3) == size(ψ[i+1], 1) "Incorrect link between $i and $(i+1)."
end
return true
end

function Base.show(::IO, ψ::AbstractMPS)
Expand Down
48 changes: 27 additions & 21 deletions src/ising.jl
@@ -1,24 +1,30 @@
export ising_graph, energy
export Gibbs_tensor
export gibbs_tensor
export GibbsControl

function Gibbs_tensor(ig::MetaGraph, opts::Gibbs_control)
struct GibbsControl
β::Number
β_schedule::Vector{<:Number}
end

function gibbs_tensor(ig::MetaGraph, opts::GibbsControl)
L = nv(ig)
β = opts.β

all_states = Iterators.product([[-1, 1] for _ 1:L]...)
rank = [2 for i 1:L]

r = [exp(-β * energy(ig, collect(σ))) for σ all_states]
ρ = reshape(r, rank...)
all_states = product(fill([-1, 1], L)...)
rank = fill(2, L)

Z = sum(ρ)
return ρ / Z
r = exp.(-β * energy.(all_states, Ref(ig)))
# r = [exp(-β * energy(ig, collect(σ))) for σ ∈ all_states]
ρ = reshape(r, rank...)
ρ / sum(ρ)
end

function energy(ig::MetaGraph, σ::Vector{<:Number})
"""
Calculate the Ising energy as E = -sum_<i,j> s_i * J_ij * s_j - sum_j h_i * s_j.
"""

"""
Calculate the Ising energy as E = -sum_<i,j> s_i * J_ij * s_j - sum_j h_i * s_j.
"""
function energy::Union{Vector, NTuple}, ig::MetaGraph)

energy = 0
# quadratic
Expand All @@ -33,17 +39,17 @@ function energy(ig::MetaGraph, σ::Vector{<:Number})
h = get_prop(ig, i, :h)
energy += h * σ[i]
end
return -energy
-energy
end

"""
Create a graph that represents the Ising Hamiltonian.
"""
function ising_graph(instance::String, L::Int, β::Number=1)
"""
Create a graph that represents the Ising Hamiltonian.
"""

# load the Ising instance
ising = CSV.File(instance, types=[Int, Int, Float64])
ig = MetaGraph(L, 0.0)
ig = MetaGraph(L, 0.0)

set_prop!(ig, :description, "The Ising model.")

Expand All @@ -59,13 +65,13 @@ function ising_graph(instance::String, L::Int, β::Number=1)
end

# state and corresponding energy
state = [rand() < 0.5 ? -1 : 1 for _ 1:L]
state = 2(rand(L) .< 0.5) .- 1

set_prop!(ig, :state, state)
set_prop!(ig, :energy, energy(ig, state)) || error("Unable to calculate the Ising energy!")
set_prop!(ig, :energy, energy(state, ig)) || error("Unable to calculate the Ising energy!")

# store extra information
set_prop!(ig, , β)

return ig
ig
end
57 changes: 30 additions & 27 deletions src/search.jl
@@ -1,12 +1,18 @@
export MPS_from_gates, unique_neighbors
export MPSControl

struct MPSControl
max_bond::Int
ϵ::Number
max_sweeps::Int
end

function unique_neighbors(ig::MetaGraph, i::Int)
nbrs = neighbors(ig::MetaGraph, i::Int)
return filter(j -> j > i, nbrs)
filter(j -> j > i, nbrs)
end

function _apply_bias!::MPS, ig::MetaGraph, dβ::T, i::Int) where {T <: Number}
function _apply_bias!::AbstractMPS, ig::MetaGraph, dβ::Number, i::Int)
M = ψ[i]
if has_prop(ig, i, :h)
h = get_prop(ig, i, :h)
Expand All @@ -16,7 +22,7 @@ function _apply_bias!(ψ::MPS, ig::MetaGraph, dβ::T, i::Int) where {T <: Number
ψ[i] = M
end

function _apply_exponent!::MPS, ig::MetaGraph, dβ::T, i::Int, j::Int) where {T <: Number}
function _apply_exponent!::AbstractMPS, ig::MetaGraph, dβ::Number, i::Int, j::Int)
δ = I(2)
M = ψ[j]

Expand All @@ -31,7 +37,7 @@ function _apply_exponent!(ψ::MPS, ig::MetaGraph, dβ::T, i::Int, j::Int) where
ψ[j] =
end

function _apply_projector!::MPS, i::Int)
function _apply_projector!::AbstractMPS, i::Int)
δ = I(2)
M = ψ[i]

Expand All @@ -43,7 +49,7 @@ function _apply_projector!(ψ::MPS, i::Int)
ψ[i] =
end

function _apply_nothing!::MPS, i::Int)
function _apply_nothing!::AbstractMPS, i::Int)
δ = I(2)
M = ψ[i]

Expand All @@ -57,13 +63,13 @@ function _apply_nothing!(ψ::MPS, i::Int)
ψ[i] =
end

function MPS_from_gates(ig::MetaGraph, mps::MPS_control, gibbs::Gibbs_control)
function MPS_from_gates(ig::MetaGraph, mps::MPSControl, gibbs::GibbsControl)
L = nv(ig)

# control for MPS
Dcut = mps.max_bond
var_tol = mps.var_ϵ
max_sweeps = mps.var_max_sweeps
tol = mps.ϵ
max_sweeps = mps.max_sweeps

# control for Gibbs state
β = gibbs.β
Expand All @@ -72,32 +78,29 @@ function MPS_from_gates(ig::MetaGraph, mps::MPS_control, gibbs::Gibbs_control)
@assert β sum(schedule) "Incorrect β schedule."

# prepare ~ Hadamard state as MPS
prod_state = [[1., 1.] for _ 1:nv(ig)]
prod_state = fill([1., 1.], nv(ig))
ρ = MPS(prod_state)

for schedule
# apply gates
for i 1:L
_apply_bias!(ρ, ig, dβ, i)
for schedule, i 1:L
_apply_bias!(ρ, ig, dβ, i)

nbrs = unique_neighbors(ig, i)
if !isempty(nbrs)
for j nbrs
_apply_exponent!(ρ, ig, dβ, i, j)
end
nbrs = unique_neighbors(ig, i)
if !isempty(nbrs)
for j nbrs
_apply_exponent!(ρ, ig, dβ, i, j)
end

_apply_projector!(ρ, i)
_apply_projector!(ρ, i)

for l setdiff(1:L, union(i, nbrs))
_apply_nothing!(ρ, l)
end
for l setdiff(1:L, union(i, nbrs))
_apply_nothing!(ρ, l)
end
end

# reduce bond dimension
if bond_dimension(ρ) > Dcut
ρ = compress(ρ, Dcut, var_tol, max_sweeps)
end
# reduce bond dimension
if bond_dimension(ρ) > Dcut
ρ = compress(ρ, Dcut, tol, max_sweeps)
end
end
return ρ
ρ
end
4 changes: 3 additions & 1 deletion test/base.jl
Expand Up @@ -7,7 +7,7 @@ T = ComplexF64

@testset "Random MPS" begin
ψ = randn(MPS{T}, sites, D, d)
@test _verify_bonds(ψ)
@test _verify_bonds(ψ) == nothing

@test ψ == ψ
@test ψ ψ
Expand Down Expand Up @@ -40,6 +40,8 @@ end
ϕ = MPS(prod_state)

show(ϕ)
end

@testset "Random MPO" begin
O = randn(MPO{T}, sites, D, d)

Expand Down
2 changes: 1 addition & 1 deletion test/cuda/compressions.jl
Expand Up @@ -89,7 +89,7 @@ end
dist1 = 2 - 2 * real(overlap)
dist2 = norm(Ψ)^2 + norm(Φ)^2 - 2 * real(overlap)

@test abs(dist1 - dist2) < 1e-14
@test abs(dist1 - dist2) < 1e-5

println("(Φ, Ψ) = ", overlap)
println("dist(Φ, Ψ)^2 = ", dist2)
Expand Down
13 changes: 6 additions & 7 deletions test/runtests.jl
Expand Up @@ -14,14 +14,13 @@ if CUDA.functional() && CUDA.has_cutensor()
)
end

my_tests = [
# "MPS.jl",
# "MPO.jl",
# "contractions.jl",
# "compressions.jl",
# "ising.jl"
push!(my_tests,
"base.jl",
"contractions.jl",
"compressions.jl",
"ising.jl",
"search.jl"
]
)

for my_test in my_tests
include(my_test)
Expand Down
8 changes: 4 additions & 4 deletions test/search.jl
Expand Up @@ -17,21 +17,21 @@ ig = ising_graph(instance, N)
= 0.25
β_schedule = [dβ for _ 1:4]

gibbs_param = Gibbs_control(β, β_schedule)
mps_param = MPS_control(Dcut, var_tol, max_sweeps)
gibbs_param = GibbsControl(β, β_schedule)
mps_param = MPSControl(Dcut, var_tol, max_sweeps)

@testset "Generate ρ" begin
ρ = MPS_from_gates(ig, mps_param, gibbs_param)

show(ρ)
@test _verify_bonds(ρ)
@test_nowarn _verify_bonds(ρ)

canonise!(ρ)
@test dot(ρ, ρ) 1
end

@testset "Generate Gibbs state exactly" begin
r = Gibbs_tensor(ig, gibbs_param)
r = gibbs_tensor(ig, gibbs_param)
@test sum(r) 1
end
end

0 comments on commit 59d5cfe

Please sign in to comment.