From 1fa2831fdbbcffa69a60f2d9b8bd99e695b5430f Mon Sep 17 00:00:00 2001 From: Bartlomiej Gardas Date: Fri, 20 Nov 2020 20:52:05 +0100 Subject: [PATCH 1/8] extending ising graph --- src/ising.jl | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/ising.jl b/src/ising.jl index b5e78806..daf81322 100644 --- a/src/ising.jl +++ b/src/ising.jl @@ -4,6 +4,7 @@ export GibbsControl export brute_force export brute_force_lazy +const State = Union{Vector, NTuple} struct GibbsControl β::Number β_schedule::Vector{<:Number} @@ -17,7 +18,7 @@ function brute_force_lazy(ig::MetaGraph, k::Int=1) collect.(states)[perm], energies[perm] end -function brute_force(ig::MetaGraph, k::Int=1) +function _brute_force(ig::MetaGraph, k::Int=1) L = nv(ig) states = ising.(digits.(0:2^L-1, base=2, pad=L)) energies = energy.(states, Ref(ig)) @@ -25,11 +26,25 @@ function brute_force(ig::MetaGraph, k::Int=1) states[perm], energies[perm] end -function gibbs_tensor(ig::MetaGraph, opts::GibbsControl) - L = nv(ig) - β = opts.β - states = product(fill([-1, 1], L)...) - ρ = exp.(-β .* energy.(states, Ref(ig))) + +""" +$(TYPEDSIGNATURES) + +Calculates Gibbs state of a classical Ising Hamiltonian + +# Details + +Calculates all matrix elements of \$\\rho\$ (probabilities) +```math +\$\\bra{\\σ}\\rho\\ket{\\eta}\$ +``` +for all possible configurations \$\\σ\$. +""" +function gibbs_tensor(ig::MetaGraph) + β = get_prop(ig, :β) + rank = get_prop(ig, :rank) + + ρ = exp.(-β .* energy.(all_states(rank), Ref(ig))) ρ ./ sum(ρ) end @@ -37,9 +52,9 @@ end """ Calculate the Ising energy as E = -sum_ s_i * J_ij * s_j - sum_j h_i * s_j. """ -function energy(σ::Union{Vector, NTuple}, ig::MetaGraph) +function energy(σ::State, ig::MetaGraph) - energy = 0 + energy = 0. # quadratic for edge ∈ edges(ig) i, j = src(edge), dst(edge) @@ -92,6 +107,7 @@ function ising_graph(instance::String, L::Int, β::Number=1) # store extra information set_prop!(ig, :β, β) + set_prop!(ig, :rank, fill(d, L)) ig end From ad5acb740db0d269f04ae295af5794289fa49b23 Mon Sep 17 00:00:00 2001 From: Bartlomiej Gardas Date: Fri, 20 Nov 2020 20:56:57 +0100 Subject: [PATCH 2/8] start using doc --- .DS_Store | Bin 0 -> 6148 bytes docs/make.jl | 40 ++++++++++++++++++++++++++++++++++ docs/src/index.md | 7 ++++++ docs/src/lib/SpinGlassPEPS.md | 14 ++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 .DS_Store create mode 100644 docs/make.jl create mode 100644 docs/src/index.md create mode 100644 docs/src/lib/SpinGlassPEPS.md diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6733c9c72f9ef4ac73e3b260314ebb72b46f84a8 GIT binary patch literal 6148 zcmeHK%Wl&^6upxK>I5RPKx#Kg-mr_RG@%F-?*K z5`}5jYJFp+a^=$H4a=%owR-J^d&FmM=4I2g>m|=b?ZC^9d|iIbd9*5f{*!P#8#b=* z^EmUuc$`S*Fc^dK{Am~meAeaDI7sCUM0trQDI$m{oF)_%JU?J7^cTPH1-~ji!n5sB50U$bHlZLNsU$R5pHUglmCz{9!U#m4bAOdg&7i;& z`mFO%HFjPK=%rv?Ij;vWag72-feWjE*dJ^pfsV$dLb-JylUD#>1mY@DWTQmS2 zjZ1}SfidL@RIW^4F_^wNFy)SZNAs5ol{+yR8RO{4Oy5wLzIZTO(up|=O>Pu03gi_i zt7=1>|NTGT|MO0!W)v_A{8tLFO3&?eF(rMro|&9DYi*=MBw>QLR484L>El=y#8JG3 bBn^GGC;%OeOND5GnGXRegUO5n|5SmW!KJt` literal 0 HcmV?d00001 diff --git a/docs/make.jl b/docs/make.jl new file mode 100644 index 00000000..5d28e3d0 --- /dev/null +++ b/docs/make.jl @@ -0,0 +1,40 @@ +using Documenter, SpinGlassPEPS + +format = Documenter.HTML( + edit_link = "master", + prettyurls = !("local" in ARGS), + mathengine = mathengine = MathJax3(Dict( + :tex=>Dict( + "inlineMath"=>[ ["\$","\$"], ["\\(","\\)"] ], + "processEscapes"=>true, + "macros"=>Dict( + "bra"=> ["{\\langle #1|}",1], + "ket"=> ["{| #1\\rangle}",1], + "ketbra"=> ["{\\left| #1 \\rangle \\langle #2 \\right|}",2], + "braket"=> ["{\\langle #1|#2\\rangle}",2], + "Tr"=> ["\\mathrm{Tr}",0], + "tr"=> ["\\Tr",0], + "ee"=> ["\\mathrm{e}"], + "ii"=> ["\\mathrm{i}"], + "dd"=> ["\\mathrm{d}"], + "1"=> ["{\\mathbb{1}}"] + ) + ) + )) +) + +makedocs( + clean = true, + format = format, + sitename = "SpinGlassPEPS.jl", + authors = "Krzysztof Domino, Bartłomiej Gardas, Konrad Jałowiecki, Łukasz Pawela, Marek Rams", + pages = [ + "Home" => "index.md", + "Library" => "lib/SpinGlassPEPS.md" + ] +) + +deploydocs( + repo = "github.com/iitis/SpinGlassPEPS.jl.git", + devbranch = "lp/docs-example" +) \ No newline at end of file diff --git a/docs/src/index.md b/docs/src/index.md new file mode 100644 index 00000000..bf15528c --- /dev/null +++ b/docs/src/index.md @@ -0,0 +1,7 @@ +```@meta +Author = "Krzysztof Domino, Bartłomiej Gardas, Konrad Jałowiecki, Łukasz Pawela, Marek Rams" +``` + +# Home + +A Julia package for numerical computation in quantum information theory. \ No newline at end of file diff --git a/docs/src/lib/SpinGlassPEPS.md b/docs/src/lib/SpinGlassPEPS.md new file mode 100644 index 00000000..8801d769 --- /dev/null +++ b/docs/src/lib/SpinGlassPEPS.md @@ -0,0 +1,14 @@ +# Documentation + + +## Index + +A list of all documentation sorted by module. + +```@index +``` + + +```@autodocs +Modules = [SpinGlassPEPS] +``` \ No newline at end of file From 12425661f5f013b06b444a8e4b45c02859e6bad1 Mon Sep 17 00:00:00 2001 From: Bartlomiej Gardas Date: Sun, 22 Nov 2020 14:12:21 +0100 Subject: [PATCH 3/8] add reading from Dict --- src/ising.jl | 78 ++++++++++++++++++++++++++++++++++-------------- src/search.jl | 2 +- src/utils.jl | 2 -- test/ising.jl | 75 +++++++++++++++++++++++++++++++++------------- test/runtests.jl | 8 ++--- 5 files changed, 114 insertions(+), 51 deletions(-) diff --git a/src/ising.jl b/src/ising.jl index daf81322..f9d25707 100644 --- a/src/ising.jl +++ b/src/ising.jl @@ -5,22 +5,31 @@ export brute_force export brute_force_lazy const State = Union{Vector, NTuple} -struct GibbsControl - β::Number - β_schedule::Vector{<:Number} -end -function brute_force_lazy(ig::MetaGraph, k::Int=1) - L = nv(ig) - states = product(fill([-1, 1], L)...) +""" +$(TYPEDSIGNATURES) + +Return the low energy spectrum + +# Details + +Calculates \$k\$ lowest energy states +together with the coresponding energies +of a classical Ising Hamiltonian +""" + +function brute_force(ig::MetaGraph, k::Int=1) + states = all_states(get_prop(ig, :rank)) energies = vec(energy.(states, Ref(ig))) perm = partialsortperm(energies, 1:k) collect.(states)[perm], energies[perm] end +_ising(σ::State) = 2 .* σ .- 1 + function _brute_force(ig::MetaGraph, k::Int=1) L = nv(ig) - states = ising.(digits.(0:2^L-1, base=2, pad=L)) + states = _ising.(digits.(0:2^L-1, base=2, pad=L)) energies = energy.(states, Ref(ig)) perm = partialsortperm(energies, 1:k) states[perm], energies[perm] @@ -34,27 +43,31 @@ Calculates Gibbs state of a classical Ising Hamiltonian # Details -Calculates all matrix elements of \$\\rho\$ (probabilities) +Calculates matrix elements (probabilities) of \$\\rho\$ ```math -\$\\bra{\\σ}\\rho\\ket{\\eta}\$ +\$\\bra{\\σ}\\rho\\ket{\\sigma}\$ ``` for all possible configurations \$\\σ\$. """ function gibbs_tensor(ig::MetaGraph) β = get_prop(ig, :β) rank = get_prop(ig, :rank) - ρ = exp.(-β .* energy.(all_states(rank), Ref(ig))) ρ ./ sum(ρ) end """ -Calculate the Ising energy as E = -sum_ s_i * J_ij * s_j - sum_j h_i * s_j. +$(TYPEDSIGNATURES) + +Calculate the Ising energy +```math +E = -\\sum_ s_i J_{ij} * s_j - \\sum_j h_i s_j. +``` """ function energy(σ::State, ig::MetaGraph) + energy::Float64 = 0 - energy = 0. # quadratic for edge ∈ edges(ig) i, j = src(edge), dst(edge) @@ -71,19 +84,28 @@ function energy(σ::State, ig::MetaGraph) end """ -Create a graph that represents the Ising Hamiltonian. +$(TYPEDSIGNATURES) + +Create the Ising spin glass model. + +# Details + +Store extra information """ -function ising_graph(instance::String, L::Int, β::Number=1) +function ising_graph(instance::Union{String, Dict}, L::Int, β::Number=1) # load the Ising instance - ising = CSV.File(instance, types=[Int, Int, Float64], comment = "#") - ig = MetaGraph(L, 0.0) + if typeof(instance) == String + ising = CSV.File(instance, types=[Int, Int, Float64], comment = "#") + else + ising = [ (first(c), last(c), J) for (c, J) ∈ instance ] + end + ig = MetaGraph(L, 0.0) set_prop!(ig, :description, "The Ising model.") # setup the model (J_ij, h_i) - for row ∈ ising - i, j, v = row + for (i, j, v) ∈ ising if i == j set_prop!(ig, i, :h, v) || error("Node $i missing!") else @@ -99,19 +121,29 @@ function ising_graph(instance::String, L::Int, β::Number=1) end end - # state and corresponding energy + # state (random by default) and corresponding energy state = 2(rand(L) .< 0.5) .- 1 set_prop!(ig, :state, state) set_prop!(ig, :energy, energy(state, ig)) || error("Unable to calculate the Ising energy!") - # store extra information + # store extra information set_prop!(ig, :β, β) - set_prop!(ig, :rank, fill(d, L)) - + set_prop!(ig, :rank, fill(2, L)) + ig end +""" +$(TYPEDSIGNATURES) + +Calculate unique neighbors of node \$i\$ + +# Details + +This is equivalent of taking the upper +diagonal of the adjacency matrix +""" function unique_neighbors(ig::MetaGraph, i::Int) nbrs = neighbors(ig::MetaGraph, i::Int) filter(j -> j > i, nbrs) diff --git a/src/search.jl b/src/search.jl index 0eebbf12..d6ecb855 100644 --- a/src/search.jl +++ b/src/search.jl @@ -108,7 +108,7 @@ end _holes(nbrs::Vector) = setdiff(first(nbrs) : last(nbrs), nbrs) -function MPS(ig::MetaGraph, mps::MPSControl, gibbs::GibbsControl) +function MPS(ig::MetaGraph) L = nv(ig) Dcut = mps.max_bond diff --git a/src/utils.jl b/src/utils.jl index 5ade3d28..1e6f4445 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -2,8 +2,6 @@ export idx, ising, proj export HadamardMPS, rq export all_states, local_basis -ising(σ::Union{Vector, NTuple}) = 2 .* σ .- 1 - idx(σ::Int) = (σ == -1) ? 1 : σ + 1 _σ(idx::Int) = (idx == 1) ? -1 : idx - 1 diff --git a/test/ising.jl b/test/ising.jl index 4416a9e3..aedc8eb0 100644 --- a/test/ising.jl +++ b/test/ising.jl @@ -1,6 +1,7 @@ using MetaGraphs using LightGraphs using GraphPlot +using CSV @testset "Ising" begin @@ -37,45 +38,77 @@ using GraphPlot end end - @test B+B' == A + @test B + B' == A gplot(ig, nodelabel=1:N) - @testset "Naive brute force" begin + @testset "Naive brute force for +/-1" begin k = 2^N states, energies = brute_force(ig, k) - display(states[1:5]) + s = 5 + display(states[1:s]) println(" ") - display(energies[1:5]) + display(energies[1:s]) println(" ") @test energies ≈ energy.(states, Ref(ig)) - states_lazy, energies_lazy = brute_force_lazy(ig, k) + _states, _energies = SpinGlassPEPS._brute_force(ig, k) - @test energies_lazy ≈ energies - @test states_lazy == states + @test _energies ≈ energies + @test _states == states - if k == 2^N - - β = rand(Float64) - opts = GibbsControl(β, [β]) + set_prop!(ig, :β, rand(Float64)) - ρ = gibbs_tensor(ig, opts) - @test size(ρ) == Tuple(fill(2, N)) + ρ = gibbs_tensor(ig) - r = exp.(-β .* energies) - R = r ./ sum(r) + @test size(ρ) == Tuple(fill(2, N)) - @test sum(R) ≈ 1 - @test sum(ρ) ≈ 1 + β = get_prop(ig, :β) - @test maximum(R) ≈ maximum(ρ) - @test minimum(R) ≈ minimum(ρ) + r = exp.(-β .* energies) + R = r ./ sum(r) - @test [ρ[idx.(σ)...] for σ ∈ states] ≈ R - end + @test sum(R) ≈ 1 + @test sum(ρ) ≈ 1 + + @test [ ρ[idx.(σ)...] for σ ∈ states ] ≈ R + end + + @testset "Naive brute force for general spins" begin + L = 4 + instance = "$(@__DIR__)/instances/$(L)_001.txt" + + ig = ising_graph(instance, L) + + set_prop!(ig, :rank, (3,2,5,4)) + rank = get_prop(ig, :rank) + + all = prod(rank) + states, energies = brute_force(ig, all) + + β = get_prop(ig, :β) + ρ = exp.(-β .* energies) + + ϱ = ρ ./ sum(ρ) + ϱ̃ = gibbs_tensor(ig) + + @test [ ϱ̃[idx.(σ)...] for σ ∈ states ] ≈ ϱ end + + @testset "Reading from Dict" begin + instance_dict = Dict() + ising = CSV.File(instance, types=[Int, Int, Float64], comment = "#") + + for (i, j, v) ∈ ising + push!(instance_dict, (i, j) => v) + end + + ig = ising_graph(instance, N) + ig_dict = ising_graph(instance_dict, N) + + @test gibbs_tensor(ig) ≈ gibbs_tensor(ig_dict) + end end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index d4fa65a4..6b143d21 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -22,11 +22,11 @@ if CUDA.functional() && CUDA.has_cutensor() && false end push!(my_tests, - "base.jl", - "contractions.jl", - "compressions.jl", +# "base.jl", +# "contractions.jl", +# "compressions.jl", "ising.jl", - "search.jl" +# "search.jl" ) for my_test in my_tests From 710bee464637851d14be5b82285f45e19c3d0a2a Mon Sep 17 00:00:00 2001 From: Bartlomiej Gardas Date: Tue, 24 Nov 2020 12:25:02 +0100 Subject: [PATCH 4/8] add support for arbitrary rank tensors --- src/ising.jl | 6 ++--- src/search.jl | 51 ++++++++++++++++++++-------------------- src/utils.jl | 9 +++++-- test/runtests.jl | 4 ++-- test/search.jl | 61 ++++++++++++++++++++++-------------------------- 5 files changed, 65 insertions(+), 66 deletions(-) diff --git a/src/ising.jl b/src/ising.jl index f9d25707..67d65fcf 100644 --- a/src/ising.jl +++ b/src/ising.jl @@ -1,8 +1,6 @@ export ising_graph, energy -export gibbs_tensor -export GibbsControl -export brute_force -export brute_force_lazy +export gibbs_tensor, brute_force +export State const State = Union{Vector, NTuple} diff --git a/src/search.jl b/src/search.jl index d6ecb855..5dcd7712 100644 --- a/src/search.jl +++ b/src/search.jl @@ -6,6 +6,7 @@ struct MPSControl max_bond::Int var_ϵ::Number max_sweeps::Int + β::Vector end # ρ needs to be in the right canonical form @@ -72,57 +73,57 @@ function _apply_bias!(ψ::AbstractMPS, ig::MetaGraph, dβ::Number, i::Int) ψ[i] = M end -function _apply_exponent!(ψ::AbstractMPS, ig::MetaGraph, dβ::Number, i::Int, j::Int) +function _apply_exponent!(ψ::AbstractMPS, ig::MetaGraph, dβ::Number, i::Int, j::Int, last::Int) M = ψ[j] - - d = size(M, 2) - basis = local_basis(d) - + D = I(ψ, i) + J = get_prop(ig, i, j, :J) - C = [exp(0.5 * dβ * k * J * l) for k ∈ basis, l ∈ basis] - D = I(d) + C = [ exp(0.5 * dβ * k * J * l) for k ∈ local_basis(ψ, i), l ∈ local_basis(ψ, j) ] - if j == length(ψ) - @cast M̃[(x, a), σ, b] := C[σ, x] * M[a, σ, b] + if j == last + @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 function _apply_projector!(ψ::AbstractMPS, i::Int) M = ψ[i] - D = I(size(M, 2)) + D = I(ψ, i) @cast M̃[a, σ, (y, b)] := D[σ, y] * M[a, σ, b] ψ[i] = M̃ end -function _apply_nothing!(ψ::AbstractMPS, i::Int) - M = ψ[i] - D = I(size(M, 2)) +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] - ψ[i] = M̃ + ψ[l] = M̃ end _holes(nbrs::Vector) = setdiff(first(nbrs) : last(nbrs), nbrs) -function MPS(ig::MetaGraph) + +function MPS(ig::MetaGraph, control::MPSControl) L = nv(ig) - Dcut = mps.max_bond - tol = mps.var_ϵ - max_sweeps = mps.max_sweeps + Dcut = control.max_bond + tol = control.var_ϵ + max_sweeps = control.max_sweeps + schedule = control.β @info "Set control parameters for MPS" Dcut tol max_sweeps - β = gibbs.β - schedule = gibbs.β_schedule + β = get_prop(ig, :β) + rank = get_prop(ig, :rank) @assert β ≈ sum(schedule) "Incorrect β schedule." @info "Preparing Hadamard state as MPS" - ρ = HadamardMPS(L) + ρ = HadamardMPS(rank) is_right = true @info "Sweeping through β and σ" schedule @@ -135,11 +136,11 @@ function MPS(ig::MetaGraph) _apply_projector!(ρ, i) for j ∈ nbrs - _apply_exponent!(ρ, ig, dβ, i, j) + _apply_exponent!(ρ, ig, dβ, i, j, last(nbrs)) end for l ∈ _holes(nbrs) - _apply_nothing!(χ, l) + _apply_nothing!(χ, l, i) end end diff --git a/src/utils.jl b/src/utils.jl index 1e6f4445..432c933c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -5,7 +5,10 @@ export all_states, local_basis idx(σ::Int) = (σ == -1) ? 1 : σ + 1 _σ(idx::Int) = (idx == 1) ? -1 : idx - 1 +LinearAlgebra.I(ψ::AbstractMPS, i::Int) = I(size(ψ[i], 2)) + local_basis(d::Int) = union(-1, 1:d-1) +local_basis(ψ::AbstractMPS, i::Int) = local_basis(size(ψ[i], 2)) function proj(state, dims::T) where {T <: Union{Vector, NTuple}} P = Matrix{Float64}[] @@ -23,9 +26,11 @@ function all_states(rank::T) where T <: Union{Vector, NTuple} end function HadamardMPS(rank::T) where T <: Union{Vector, NTuple} - MPS(fill(1, r) ./ sqrt(r) for r ∈ rank) + vec = [ fill(1, r) ./ sqrt(r) for r ∈ rank ] + MPS(vec) end -HadamardMPS(L::Int) = MPS(fill([1., 1.] / sqrt(2), L)) + +HadamardMPS(L::Int) = MPS(fill(2, L)) function LinearAlgebra.qr(M::AbstractMatrix, Dcut::Int, args...) fact = pqrfact(M, rank=Dcut, args...) diff --git a/test/runtests.jl b/test/runtests.jl index 6b143d21..5f505fc7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -25,8 +25,8 @@ push!(my_tests, # "base.jl", # "contractions.jl", # "compressions.jl", - "ising.jl", -# "search.jl" +# "ising.jl", + "search.jl" ) for my_test in my_tests diff --git a/test/search.jl b/test/search.jl index a393df59..68dd4e8f 100644 --- a/test/search.jl +++ b/test/search.jl @@ -7,32 +7,29 @@ N = L^2 instance = "$(@__DIR__)/instances/$(N)_001.txt" ig = ising_graph(instance, N) - -ϵ = 1E-14 - -Dcut = N^2 + 1 -var_tol = 1E-8 -max_sweeps = 4 - -β = 1 -dβ = 0.25 -β_schedule = [β] #[dβ for _ ∈ 1:4] - -gibbs_param = GibbsControl(β, β_schedule) -mps_param = MPSControl(Dcut, var_tol, max_sweeps) - -ϱ = gibbs_tensor(ig, gibbs_param) +set_prop!(ig, :β, rand(Float64)) +r = (3, 2, 5, 4) +set_prop!(ig, :rank, r) + +ϵ = 1E-10 +D = prod(r) + 1 +var_ϵ = 1E-8 +sweeps = 4 +schedule = [get_prop(ig, :β)] +control = MPSControl(D, var_ϵ, sweeps, schedule) + +states = all_states(get_prop(ig, :rank)) +ϱ = gibbs_tensor(ig) @test sum(ϱ) ≈ 1 - @testset "Verifying gate operations" begin - χ = HadamardMPS(N) + β = get_prop(ig, :β) + rank = get_prop(ig, :rank) - d = 2 - rank = fill(d, N) - states = all_states(rank) - T = ones(rank...) ./ d^N + χ = HadamardMPS(rank) + T = ones(rank...) ./ prod(rank) + show(χ) @test sum(T) ≈ 1 for i ∈ 1:N @@ -49,7 +46,7 @@ mps_param = MPSControl(Dcut, var_tol, max_sweeps) SpinGlassPEPS._apply_projector!(χ, i) for j ∈ nbrs - SpinGlassPEPS._apply_exponent!(χ, ig, β, i, j) + SpinGlassPEPS._apply_exponent!(χ, ig, β, i, j, last(nbrs)) J = get_prop(ig, i, j, :J) for σ ∈ states @@ -57,10 +54,8 @@ mps_param = MPSControl(Dcut, var_tol, max_sweeps) end end - for l ∈ i+1:maximum(nbrs) - if l ∉ nbrs - SpinGlassPEPS._apply_nothing!(χ, l) - end + for l ∈ SpinGlassPEPS._holes(nbrs) + SpinGlassPEPS._apply_nothing!(χ, l, i) end end @@ -73,17 +68,17 @@ mps_param = MPSControl(Dcut, var_tol, max_sweeps) @test T ./ sum(T) ≈ ϱ end + @testset "MPS from gates" begin @testset "Exact Gibbs pure state (MPS)" begin - d = 2 L = nv(ig) - dims = fill(d, L) + β = get_prop(ig, :β) + rank = get_prop(ig, :rank) - @info "Generating Gibbs state - |ρ>" d L dims β ϵ + @info "Generating Gibbs state - |ρ>" d L rank β ϵ - states = all_states(dims) - ψ = ones(dims...) + ψ = ones(rank...) for σ ∈ states for i ∈ 1:L @@ -121,7 +116,7 @@ end @info "Verifying MPS from gates" - Gψ = MPS(ig, mps_param, gibbs_param) + Gψ = MPS(ig, control) @test_nowarn is_right_normalized(Gψ) @test bond_dimension(Gψ) > 1 @@ -134,7 +129,7 @@ end for σ ∈ states p = dot(rψ, σ) - r = dot(rψ, proj(σ, dims), rψ) + r = dot(rψ, proj(σ, rank), rψ) @test p ≈ r @test ϱ[idx.(σ)...] ≈ p From 1c90babdf5ad123929480c315bd91829e3b0eb6f Mon Sep 17 00:00:00 2001 From: Bartlomiej Gardas Date: Tue, 24 Nov 2020 12:37:39 +0100 Subject: [PATCH 5/8] slightly modyfing base --- src/base.jl | 11 +++++++++++ test/base.jl | 21 +++++++++++++++++++++ test/runtests.jl | 4 ++-- test/search.jl | 2 +- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/base.jl b/src/base.jl index 703b4143..ddc488c1 100644 --- a/src/base.jl +++ b/src/base.jl @@ -146,6 +146,17 @@ function MPS(O::MPO) ψ end +function Base.randn(::Type{MPS{T}}, D::Int, rank::Union{Vector, NTuple}) where {T} + L = length(rank) + ψ = MPS(T, L) + ψ[1] = randn(T, 1, rank[1], D) + for i ∈ 2:(L-1) + ψ[i] = randn(T, D, rank[i], D) + end + ψ[end] = randn(T, D, rank[end], 1) + ψ +end + function Base.randn(::Type{MPS{T}}, L::Int, D::Int, d::Int) where {T} ψ = MPS(T, L) ψ[1] = randn(T, 1, d, D) diff --git a/test/base.jl b/test/base.jl index e178e843..f644632c 100644 --- a/test/base.jl +++ b/test/base.jl @@ -23,6 +23,27 @@ T = ComplexF64 @test ϕ ≈ ψ show(ψ) + + dims = (3, 2, 5, 4) + @info "Veryfing ψ of arbitrary rank" dims + + ψ = randn(MPS{T}, D, dims) + @test verify_bonds(ψ) == nothing + + @test ψ == ψ + @test ψ ≈ ψ + + @test length(ψ) == length(dims) + @test size(ψ) == (length(dims), ) + @test eltype(ψ) == ComplexF64 + @test rank(ψ) == dims + @test bond_dimension(ψ) ≈ D + + ϕ = copy(ψ) + @test ϕ == ψ + @test ϕ ≈ ψ + + show(ψ) end @testset "Random MPO" begin diff --git a/test/runtests.jl b/test/runtests.jl index 5f505fc7..e3f4e8d1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -22,11 +22,11 @@ if CUDA.functional() && CUDA.has_cutensor() && false end push!(my_tests, -# "base.jl", + "base.jl", # "contractions.jl", # "compressions.jl", # "ising.jl", - "search.jl" +# "search.jl" ) for my_test in my_tests diff --git a/test/search.jl b/test/search.jl index 68dd4e8f..79a41c1b 100644 --- a/test/search.jl +++ b/test/search.jl @@ -76,7 +76,7 @@ end β = get_prop(ig, :β) rank = get_prop(ig, :rank) - @info "Generating Gibbs state - |ρ>" d L rank β ϵ + @info "Generating Gibbs state - |ρ>" L rank β ϵ ψ = ones(rank...) From 07f7527d09f1af8a6b5d67d614600dc5179ba21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pawela?= Date: Tue, 24 Nov 2020 18:36:02 +0100 Subject: [PATCH 6/8] enable all tests --- test/runtests.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index e3f4e8d1..7f639a72 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -13,7 +13,7 @@ function reshape_row(A::AbstractArray{T}, dims::Tuple) where {T <: Number} end my_tests = [] -if CUDA.functional() && CUDA.has_cutensor() && false +if CUDA.functional() && CUDA.has_cutensor() push!(my_tests, "cuda/base.jl", "cuda/contractions.jl", @@ -23,10 +23,10 @@ end push!(my_tests, "base.jl", -# "contractions.jl", -# "compressions.jl", -# "ising.jl", -# "search.jl" + "contractions.jl", + "compressions.jl", + "ising.jl", + "search.jl" ) for my_test in my_tests From a7c58c0efa93a01374b5ef3fdc0f30bb04a75a2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pawela?= Date: Tue, 24 Nov 2020 18:40:19 +0100 Subject: [PATCH 7/8] minor stylastic change --- src/ising.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ising.jl b/src/ising.jl index 67d65fcf..63fc806d 100644 --- a/src/ising.jl +++ b/src/ising.jl @@ -96,7 +96,7 @@ function ising_graph(instance::Union{String, Dict}, L::Int, β::Number=1) if typeof(instance) == String ising = CSV.File(instance, types=[Int, Int, Float64], comment = "#") else - ising = [ (first(c), last(c), J) for (c, J) ∈ instance ] + ising = [ (i, j, J) for ((i, j), J) ∈ instance ] end ig = MetaGraph(L, 0.0) From 97ad0f7c981163be38037b61f5fe54a50ac0466a Mon Sep 17 00:00:00 2001 From: Bartlomiej Gardas Date: Tue, 24 Nov 2020 19:06:16 +0100 Subject: [PATCH 8/8] set \beta=1 to test search via mps --- test/runtests.jl | 8 ++++---- test/search.jl | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index e3f4e8d1..d4fa65a4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -23,10 +23,10 @@ end push!(my_tests, "base.jl", -# "contractions.jl", -# "compressions.jl", -# "ising.jl", -# "search.jl" + "contractions.jl", + "compressions.jl", + "ising.jl", + "search.jl" ) for my_test in my_tests diff --git a/test/search.jl b/test/search.jl index 79a41c1b..c31c1afe 100644 --- a/test/search.jl +++ b/test/search.jl @@ -6,12 +6,13 @@ L = 2 N = L^2 instance = "$(@__DIR__)/instances/$(N)_001.txt" + ig = ising_graph(instance, N) -set_prop!(ig, :β, rand(Float64)) +set_prop!(ig, :β, 1.)#rand(Float64)) r = (3, 2, 5, 4) set_prop!(ig, :rank, r) -ϵ = 1E-10 +ϵ = 1E-8 D = prod(r) + 1 var_ϵ = 1E-8 sweeps = 4