Skip to content

Commit

Permalink
Merge 0a70581 into 4205f3a
Browse files Browse the repository at this point in the history
  • Loading branch information
lpawela committed Jan 15, 2021
2 parents 4205f3a + 0a70581 commit eb0d970
Show file tree
Hide file tree
Showing 27 changed files with 975 additions and 347 deletions.
9 changes: 8 additions & 1 deletion Project.toml
Expand Up @@ -7,22 +7,29 @@ version = "0.0.1"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Infiltrator = "5903a43b-9cc3-4c30-8d17-598619ec4e9b"
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
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"
TensorOperations = "6aa20fa7-93e2-5fca-9bc0-fbd0db3c71a2"

[compat]
MetaGraphs = "0.6"
CSV = "0.8"
CUDA = "2.3"
DocStringExtensions = "0.8"
LightGraphs = "1.3"
LowRankApprox = "0.4"
MetaGraphs = "0.6"
Requires = "1.1"
StatsBase = "0.33"
TensorCast = "0.3"
TensorOperations = "3.0.1"
julia = "1.5"
Expand Down
Empty file added SpinGlassPEPS.jl/Project.toml
Empty file.
13 changes: 6 additions & 7 deletions benchmarks/low_energy_approx_chimera.jl
Expand Up @@ -14,7 +14,7 @@ using Test
import SpinGlassPEPS: solve, solve_mps, M2graph, energy, binary2spins, ising_graph

disable_logging(LogLevel(0))

δH = 0.9
# this is axiliary function for npz write

function vecvec2matrix(v::Vector{Vector{Int}})
Expand All @@ -28,7 +28,7 @@ end

s = ArgParseSettings("description")
@add_arg_table! s begin
"--file", "-f"
"--file", "-i"
arg_type = String
help = "the file name"
"--size", "-s"
Expand Down Expand Up @@ -72,7 +72,7 @@ problem_size = parse_args(s)["size"]
χ = parse_args(s)["chi"]
si = parse_args(s)["size"]

ig = ising_graph(fi, si, 1, 1)
ig = ising_graph(fi, si, 1)

n_sols = parse_args(s)["n_sols"]
node_size = (parse_args(s)["node_size1"], parse_args(s)["node_size2"])
Expand All @@ -88,9 +88,8 @@ energy_ref = energy(ground_spins, ig)

spectrum_cutoff = parse_args(s)["spectrum_cutoff"]

ses = collect(spectrum_cutoff:-10:1)
step = 9
step = 100
ses = collect(spectrum_cutoff:-10:40)
step = 10
n_s = collect(n_sols:-step:1)

delta_e = ones(length(ses), length(n_s))
Expand All @@ -102,7 +101,7 @@ function proceed()
i = 1
for s in ses

@time spins, _ = solve(ig, n_sol; β=β, χ = χ, threshold = 1e-8, node_size = node_size, spectrum_cutoff = s)
@time spins, _ = solve(ig, n_sol; β=β, χ = χ, threshold = 1e-8, node_size = node_size, spectrum_cutoff = s, δH=δH)

en = minimum([energy(s, ig) for s in spins])

Expand Down
Expand Up @@ -31,6 +31,7 @@ println("examples = ", examples)


β = 3.
δH = 0.

number_of_states = 10

Expand Down Expand Up @@ -71,7 +72,7 @@ for k in 1:examples


number = number_of_states + more_states_for_peps
@time spins, objective = solve(g, number ; β = T(β), threshold = 0.)
@time spins, objective = solve(g, number ; β = T(β), threshold = 0., δH = δH)

for i in 1:number_of_states

Expand All @@ -87,7 +88,7 @@ for k in 1:examples
print("approx peps ")

number = number_of_states + more_states_for_peps
@time spins_approx, objective_approx = solve(g, number; β = T(β), χ = χ, threshold = 1e-12)
@time spins_approx, objective_approx = solve(g, number; β = T(β), χ = χ, threshold = 1e-12, δH = δH)

for i in 1:number_of_states

Expand All @@ -102,7 +103,7 @@ for k in 1:examples

print("peps larger T")
number = number_of_states + more_states_for_peps
@time spins_larger_nodes, objective_larger_nodes = solve(g, number; node_size = (2,2), β = T(β), χ = χ, threshold = 1e-12)
@time spins_larger_nodes, objective_larger_nodes = solve(g, number; node_size = (2,2), β = T(β), χ = χ, threshold = 1e-12, δH = δH)

for i in 1:number_of_states

Expand All @@ -117,7 +118,7 @@ for k in 1:examples

print("peps larger T, limited spectrum")
number = number_of_states + more_states_for_peps
@time spins_spec, objective_spec = solve(g, number; node_size = (2,2), β = T(β), χ = χ, threshold = 1e-12, spectrum_cutoff = 15)
@time spins_spec, objective_spec = solve(g, number; node_size = (2,2), β = T(β), χ = χ, threshold = 1e-12, spectrum_cutoff = 15, δH = δH)

for i in 1:minimum([number_of_states, 60])
@test energy(spins_spec[i], g) energies_given[i]
Expand Down
12 changes: 8 additions & 4 deletions test/tests_on_chimera.jl → benchmarks/tests_on_chimera.jl
Expand Up @@ -24,10 +24,9 @@ function vecvec2matrix(v::Vector{Vector{Int}})
M
end


s = ArgParseSettings("description")
@add_arg_table! s begin
"--file", "-f"
"--file", "-i"
arg_type = String
help = "the file name"
"--size", "-s"
Expand Down Expand Up @@ -58,6 +57,10 @@ s = ArgParseSettings("description")
default = 1000
arg_type = Int
help = "size of the lower spectrum"
"--deltaH", "-d"
default = 0.1
arg_type = Float64
help = "merge parameter on merging dX, the threshold on ratios of objectives"
end

fi = parse_args(s)["file"]
Expand All @@ -70,15 +73,16 @@ problem_size = parse_args(s)["size"]
β = parse_args(s)["beta"]
χ = parse_args(s)["chi"]
si = parse_args(s)["size"]
δH = parse_args(s)["deltaH"]

ig = ising_graph(fi, si, 1, 1)
ig = ising_graph(fi, si, 1)

n_sols = parse_args(s)["n_sols"]
node_size = (parse_args(s)["node_size1"], parse_args(s)["node_size2"])
println(node_size)
spectrum_cutoff = parse_args(s)["spectrum_cutoff"]

@time spins, objective = solve(ig, n_sols; β=β, χ = χ, threshold = 1e-8, node_size = node_size, spectrum_cutoff = spectrum_cutoff)
@time spins, objective = solve(ig, n_sols; β=β, χ = χ, threshold = 1e-8, node_size = node_size, spectrum_cutoff = spectrum_cutoff, δH=δH)

energies = [energy(s, ig) for s in spins]
println("energies from peps")
Expand Down
194 changes: 142 additions & 52 deletions src/PEPS.jl
@@ -1,65 +1,155 @@
export PepsTensor

mutable struct PepsTensor
nbrs::Dict{String, Int}
left::AbstractArray
right::AbstractArray
up::AbstractArray
down::AbstractArray
loc::AbstractArray
tensor::AbstractArray

function PepsTensor(fg::MetaDiGraph, v::Int)
pc = new()
pc.nbrs = Dict()
pc.loc = get_prop(fg, v, :local_exp)

outgoing = outneighbors(fg, v)
incoming = inneighbors(fg, v)

for u outgoing
e = SimpleEdge(v, u)
if get_prop(fg, e, :orientation) == "horizontal"
pc.right = first(get_prop(fg, e, :decomposition))
push!(pc.nbrs, "h_out" => u)
else
pc.down = first(get_prop(fg, e, :decomposition))
push!(pc.nbrs, "v_out" => u)
end
end
export NetworkGraph, PepsNetwork
export generate_tensor, MPO

for u incoming
e = SimpleEdge(u, v)
if get_prop(fg, e, :orientation) == "horizontal"
pc.left = last(get_prop(fg, e, :decomposition))
push!(pc.nbrs, "h_in" => u)
else
pc.up = last(get_prop(fg, e, :decomposition))
push!(pc.nbrs, "v_in" => u)
end
end

# open boundary conditions
if !isdefined(pc, :left)
pc.left = ones(1, size(pc.right, 1))
mutable struct NetworkGraph
factor_graph::MetaDiGraph
nbrs::Dict
β::Number

function NetworkGraph(factor_graph::MetaDiGraph, nbrs::Dict, β::Number)
ng = new(factor_graph, nbrs, β)

count = 0
for v vertices(ng.factor_graph), w ng.nbrs[v]
if has_edge(ng.factor_graph, v, w) count += 1 end
end

if !isdefined(pc, :right)
pc.right = ones(size(pc.left, 2), 1)
mc = ne(ng.factor_graph)
if count < mc
error("Error: $(count) < $(mc)")
end
ng
end
end

function generate_tensor(ng::NetworkGraph, v::Int)
fg = ng.factor_graph
loc_exp = exp.(-ng.β .* get_prop(fg, v, :loc_en))

if !isdefined(pc, :up)
pc.up = ones(1, size(pc.down, 1))
dim = []
@cast tensor[_, i] := loc_exp[i]

for w ng.nbrs[v]
if has_edge(fg, w, v)
_, _, pv = get_prop(fg, w, v, :split)
pv = pv'
elseif has_edge(fg, v, w)
pv, _, _ = get_prop(fg, v, w, :split)
else
pv = ones(length(loc_exp), 1)
end

if !isdefined(pc, :down)
pc.down = ones(size(pc.up, 2), 1)
@cast tensor[(c, γ), σ] |= tensor[c, σ] * pv[σ, γ]
push!(dim, size(pv, 2))
end

reshape(tensor, dim..., :)
end

function generate_tensor(ng::NetworkGraph, v::Int, w::Int)
fg = ng.factor_graph
if has_edge(fg, w, v)
_, e, _ = get_prop(fg, w, v, :split)
tensor = exp.(-ng.β .* e')
elseif has_edge(fg, v, w)
_, e, _ = get_prop(fg, v, w, :split)
tensor = exp.(-ng.β .* e)
else
tensor = ones(1, 1)
end
tensor
end

mutable struct PepsNetwork
size::NTuple{2, Int}
map::Dict
network_graph::NetworkGraph
origin::Symbol
i_max::Int
j_max::Int

function PepsNetwork(m::Int, n::Int, fg::MetaDiGraph, β::Number, origin::Symbol)
pn = new((m, n))
pn.map, pn.i_max, pn.j_max = LinearIndices(m, n, origin)

nbrs = Dict()
for i 1:pn.i_max, j 1:pn.j_max
push!(nbrs,
pn.map[i, j] => (pn.map[i, j-1], pn.map[i-1, j],
pn.map[i, j+1], pn.map[i+1, j]))
end
pn.network_graph = NetworkGraph(fg, nbrs, β)
pn
end
end

generate_tensor(pn::PepsNetwork, m::NTuple{2,Int}) = generate_tensor(pn.network_graph, pn.map[m])
generate_tensor(pn::PepsNetwork, m::NTuple{2,Int}, n::NTuple{2,Int}) = generate_tensor(pn.network_graph, pn.map[m], pn.map[n])

function MPO(::Type{T}, Ψ::PEPSRow, σ::Vector{State}) where {T <: Number}
n = length(Ψ)
ϕ = MPO(T, n)
for i=1:n
k = σ[n]
A = Ψ[i]
@cast B[l, u, r, d] |= A[l, u, r, d, $k]

ϕ[i] = B
end
ϕ
end
MPO::PEPSRow, σ::Vector{State}) = MPO(Float64, ψ, σ)

function MPO(::Type{T}, Ψ::PEPSRow) where {T <: Number}
n = length(Ψ)
ϕ = MPO(T, n)
for i=1:n
A = Ψ[i]
@reduce B[l, u, r, d] |= sum(σ) A[l, u, r, d, σ]
ϕ[i] = B
end
ϕ
end
MPO::PEPSRow) = MPO(Float64, ψ)

@cast pc.tensor[l, r, u, d, σ] |= pc.loc[σ] * pc.left[l, σ] * pc.right[σ, r] * pc.up[u, σ] * pc.down[σ, d]
function PEPSRow(::Type{T}, peps::PepsNetwork, i::Int) where {T <: Number}
n = peps.j_max
ψ = PEPSRow(T, n)

pc
for j 1:n
ψ[j] = generate_tensor(peps, (i, j))
end

for j 1:n-1
ten = generate_tensor(peps, (i, j), (i, j+1))
A = ψ[j]
@tensor B[l, u, r, d, σ] := A[l, u, r̃, d, σ] * ten[r̃, r]
ψ[j] = B
end
ψ
end
PEPSRow(peps::PepsNetwork, i::Int) = PEPSRow(Float64, peps, i)

function MPO(::Type{T}, peps::PepsNetwork, i::Int, k::Int) where {T <: Number}
n = peps.j_max

ψ = MPO(T, n)
fg = peps.network_graph.factor_graph

for j 1:n
v, w = peps.map[i, j], peps.map[k, j]

Base.size(A::PepsTensor) = size(A.tensor)
if has_edge(fg, v, w)
_, en, _ = get_prop(fg, v, w, :split)
elseif has_edge(fg, w, v)
_, en, _ = get_prop(fg, w, v, :split)
else
en = ones(1, 1)
end

@cast A[u, _, d, _] := en[u, d]
ψ[j] = A
end
ψ
end
MPO(peps::PepsNetwork, i::Int, k::Int) = MPO(Float64, peps, i, k)
9 changes: 6 additions & 3 deletions src/SpinGlassPEPS.jl
Expand Up @@ -6,16 +6,19 @@ module SpinGlassPEPS
using LightGraphs
using MetaGraphs
using CSV
using Infiltrator
using Logging
using StatsBase

using DocStringExtensions
const product = Iterators.product

include("base.jl")
include("compressions.jl")
include("contractions.jl")
include("graphs/model.jl")
include("graphs/chimera.jl")
include("graphs/lattice.jl")
include("lattice.jl")
#include("graphs/chimera.jl")
#include("graphs/lattice.jl")
include("graph.jl")
include("ising.jl")
include("PEPS.jl")
Expand Down

0 comments on commit eb0d970

Please sign in to comment.