Skip to content

Commit

Permalink
start rewriting the node struct to make available a larger tensor
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomino committed Oct 16, 2020
1 parent 799a943 commit 9403d9b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
23 changes: 16 additions & 7 deletions src/notation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ function get_connection_if_exists(i::Int, j::Int, k::Int, grid::Matrix{Int})
end
end

function read_connecting_pairs(grid::Matrix{Int}, i::Int)
a = findall(x->x==i, grid)[1]
j = a[1]
k = a[2]
function enviroment(i::Int,j::Int,k::Int,grid)
left = get_connection_if_exists(i, j, k-1, grid)
right = get_connection_if_exists(i, j, k+1, grid)
up = get_connection_if_exists(i, j-1, k, grid)
down = get_connection_if_exists(i, j+1, k, grid)
return left, right, up, down
end

function read_connecting_pairs(grid::Matrix{Int}, i::Int)
a = findall(x->x==i, grid)[1]
enviroment(i,a[1],a[2], grid)
end

struct Bond_with_other_node
node::Int
spins1::Vector{Int}
Expand All @@ -44,7 +46,7 @@ function matrix2qubo_vec(M::Matrix{T}) where T <:AbstractFloat
push!(q_vec, Qubo_el((i,j), M[i,j]))
end
end
q_vec
q_vec
end

"""
Expand All @@ -63,15 +65,23 @@ struct Node_of_grid
down::Vector{Vector{Int}}
all_connections::Vector{Vector{Int}}
bonds::Vector{Bond_with_other_node}
#construction from the grid
function(::Type{Node_of_grid})(i::Int, grid::Matrix{Int})
s = size(grid)
intra_struct = Vector{Int}[]

left, right, up, down = read_connecting_pairs(grid, i)
all_connections = [left..., right..., up..., down...]
#all_connections = [el[2] for el in all_connections]
new(i, [i], intra_struct, left, right, up, down, all_connections)
end
#construction from matrix of matrices (a grid of many nodes)
function(::Type{Node_of_grid})(j::Int, k::Int, grid::Array{Array{Int64,N} where N,2})
intra_struct = grid[j,k]
spin_inds = vec(intra_struct)
# TODO ......
1
end
# construction from qubo directly, It will not check if qubo fits grid
function(::Type{Node_of_grid})(i::Int, qubos::Vector{Qubo_el{T}}) where T <: AbstractFloat
x = Vector{Int}[]
all_connections = Vector{Int}[]
Expand All @@ -83,7 +93,6 @@ struct Node_of_grid
push!(all_connections, [q.ind[2], q.ind[1]])
end
end

new(i, [i], x, x, x, x, x, all_connections)
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ include("compression.jl")
include("peps_no_types.jl")
include("mps_implementation.jl")

#include("tests/notation_tests.jl")
#include("tests/compression_tests.jl")
#include("tests/peps_tests.jl")
include("tests/notation_tests.jl")
include("tests/compression_tests.jl")
include("tests/peps_tests.jl")
include("tests/mps_tests.jl")
12 changes: 6 additions & 6 deletions src/tests/mps_tests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using TensorOperations
if false


@testset "axiliary" begin
b = scalar_prod_with_itself([ones(1,2,2), ones(2,1,2)])
Expand Down Expand Up @@ -284,11 +284,11 @@ end
println(s[3])
println(s[4])
end
end


@testset "mps full graph real train problem" begin

# TODO this needs by be specified, why 2v
# TODO this needs by be specified, why 2v not v

function M2qubo(Q::Matrix{T}) where T <: AbstractFloat
J = (Q - diagm(diag(Q)))/4
Expand Down Expand Up @@ -366,7 +366,7 @@ end
ns = [Node_of_grid(i, q_vec) for i in 1:get_system_size(q_vec)]
@time spins_mps, objective_mps = solve_mps(q_vec, ns, 10; β=β, β_step=β_step, χ=χ, threshold = 1.e-12)

#println(spins_mps)
println("first 10 states")
for spin in spins_mps
#println(v2energy(J, spin))
binary = [Int(i > 0) for i in spin]
Expand All @@ -378,11 +378,11 @@ end
if file == "tests/data/QUBO8qbits"
println("testing ground")
binary = [Int(i > 0) for i in spins_mps[1]]
println(binary == [0,1,0,0,1,0,0,0])
@test binary == [0,1,0,0,1,0,0,0]
elseif file == "tests/data/QUBO6qbits"
println("testing ground")
binary = [Int(i > 0) for i in spins_mps[1]]
println(binary == [0,1,0,1,0,0])
@test binary == [0,1,0,1,0,0]
end

end
15 changes: 13 additions & 2 deletions src/tests/notation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@

ns = [Node_of_grid(i, grid1) for i in 1:maximum(grid1)]
@test get_system_size(ns) == 12


grid = Array{Array{Int}}(undef, (2,2))
grid[1,1] = [1 2;5 6]
grid[1,2] = [3 4; 7 8]
grid[2,1] = [9 10;13 14]
grid[2,2] = [11 12;15 16]
grid = Array{Array{Int}}(grid)

n = Node_of_grid(1,1, grid)
end


Expand All @@ -78,10 +88,11 @@ end
n = Node_of_grid(9, qubo)
@test n.all_connections == [[9, 6], [9, 8]]

println(get_system_size(qubo) == 9)
@test get_system_size(qubo) == 9

M = rand(10,10)
matrix2qubo_vec(M)
m = matrix2qubo_vec(M)


end

Expand Down

0 comments on commit 9403d9b

Please sign in to comment.