Skip to content

Instantly share code, notes, and snippets.

@wangleiphy
Last active July 19, 2020 07:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangleiphy/ef1f616f26ab37ef7fd3d329f2a5be0e to your computer and use it in GitHub Desktop.
Save wangleiphy/ef1f616f26ab37ef7fd3d329f2a5be0e to your computer and use it in GitHub Desktop.
spin glass ground state energy and entropy
using Yao, LinearAlgebra
# define tropical numbers with the following property.
# x ⊕ y := max(x ,y)
# x ⊗ y := x + y
struct Tropical{T} <: Number
n::T
c::T
end
Tropical(x::Real) = Tropical(x,one(x))
Base.:*(a::Tropical, b::Tropical) = Tropical(a.n + b.n, a.c * b.c)
function Base.:+(a::Tropical, b::Tropical)
n = max(a.n, b.n)
if a.n > b.n
c = a.c
elseif a.n == b.n
c = a.c + b.c
else
c = b.c
end
Tropical(n, c)
end
Base.zero(::Type{Tropical{T}}) where T<:Integer = Tropical(T(-999999), T(1))
Base.zero(::Type{Tropical{T}}) where T<:AbstractFloat = Tropical(typemin(T), T(1))
# define the "spinglass gates" that will be used in our "quantum" simulation.
# * `Gh` is the magnetic field term.
# * `G2` is the gate on parallel bond.
# * `G4` is the vertical two qubit diagonal "gate".
Gh(h::Real) = matblock(Diagonal(Tropical.([h, -h])))
G2(Jij::Real) = matblock(Tropical.([Jij -Jij; -Jij Jij]))
G4(Jij::Real) = matblock(Diagonal(Tropical.([Jij, -Jij, -Jij, Jij])))
function square_solve(Lx::Int, Ly::Int, Js::AbstractVector{T}, hs::AbstractVector{T}; usecuda=false) where T
Js, hs = copy(Js), copy(hs)
reg = ArrayReg(Tropical.(zeros(T, 1<<Ly)))
if usecuda
reg = cu(reg)
end
for i=1:Lx
println("Layer $i/$Lx")
i!=1 && for j=1:Ly
reg |> put(Ly, j=>G2(Js |> popfirst!))
end
for j=1:Ly
reg |> put(Ly, j=>Gh(hs |> popfirst!))
end
for j=1:Ly-1
reg |> put(Ly, (j,j+1)=>G4(Js |> popfirst!))
end
end
sum(state(reg))
end
L = 10
Js = rand([-1,1], 2*L*(L-1))
hs = zeros(Int, L^2)
res = square_solve(L, L, Js, hs)
@show res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment