Skip to content

Commit

Permalink
Fix (#49) and its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Sep 12, 2024
1 parent f478c25 commit 2ea48e2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 41 deletions.
10 changes: 8 additions & 2 deletions src/library/form/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ data(lf::DenseLinearForm) = lf.data
function linear_terms(lf::DenseLinearForm)
L = data(lf)

return (i => L[i] for i = eachindex(L) if !iszero(L[i]))
return (i => L[i] for i in eachindex(L) if !iszero(L[i]))
end

function linear_size(lf::DenseLinearForm)
Expand Down Expand Up @@ -124,7 +124,13 @@ function DenseForm{T}(
q = UpperTriangular{T,Matrix{T}}(zeros(T, n, n))

for i = 1:n
l[i] = L[i] + Q[i, i]
if QUBOTools.domain(domain) === BoolDomain
l[i] = L[i] + Q[i, i]
else # QUBOTools.domain(domain) === SpinDomain
l[i] = L[i]

β += Q[i, i]
end
end

for i = 1:n, j = (i+1):n
Expand Down
8 changes: 6 additions & 2 deletions src/library/form/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,13 @@ function DictForm{T}(
iszero(v) && continue

if i == j
l[i] = get(l, i, zero(T)) + v
if QUBOTools.domain(domain) === BoolDomain
l[i] = get(l, i, zero(T)) + v

iszero(l[i]) && delete!(l, i)
iszero(l[i]) && delete!(l, i)
else # QUBOTools.domain(domain) === SpinDomain
β += v
end
elseif i > j
q[(j, i)] = get(q, (j, i), zero(T)) + v

Expand Down
8 changes: 7 additions & 1 deletion src/library/form/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ function SparseForm{T}(
q = spzeros(T, n, n)

for i = 1:n
l[i] = L[i] + Q[i, i]
if QUBOTools.domain(domain) === BoolDomain
l[i] = L[i] + Q[i, i]
else # QUBOTools.domain(domain) === SpinDomain
l[i] = L[i]

β += Q[i, i]
end
end

for i = 1:n, j = (i+1):n
Expand Down
80 changes: 44 additions & 36 deletions test/unit/library/form.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,25 @@ function test_form_dict()
)
Φ = QUBOTools.DictForm{Float64}(3, L, Q, 1.0, -1.0; sense = :max, domain = :bool)

= Dict{Int,Float64}(1 => 6.25, 2 => 8.25, 3 => 8.0)
= Dict{Int,Float64}(1 => 6.50, 2 => 8.75, 3 => 8.75)
= Dict{Tuple{Int,Int},Float64}(
(1, 1) => 0.25,
(2, 2) => 0.50,
(3, 3) => 0.75,
(1, 2) => 1.00,
(2, 3) => 1.25,
)
Ψ̄ =
QUBOTools.DictForm{Float64}(3, h̄, J̄, 1.0, 22.75; sense = :min, domain = :spin)
Ψ̄ = QUBOTools.DictForm{Float64}(3, h̄, J̄, 1.0, 21.25; sense = :min, domain = :spin)

h = Dict{Int,Float64}(1 => -6.25, 2 => -8.25, 3 => -8.0)
h = Dict{Int,Float64}(1 => -6.50, 2 => -8.75, 3 => -8.75)
J = Dict{Tuple{Int,Int},Float64}(
(1, 1) => -0.25,
(2, 2) => -0.50,
(3, 3) => -0.75,
(1, 2) => -1.00,
(2, 3) => -1.25,
)
Ψ = QUBOTools.DictForm{Float64}(3, h, J, 1.0, -22.75; sense = :max, domain = :spin)
Ψ = QUBOTools.DictForm{Float64}(3, h, J, 1.0, -21.25; sense = :max, domain = :spin)

@testset "Constructor" begin
@test _compare_forms(
Expand Down Expand Up @@ -220,7 +219,7 @@ function test_form_sparse()
])
Φ = QUBOTools.SparseForm{Float64}(3, L, Q, 1.0, -1.0; sense = :max, domain = :bool)

= sparse([6.25, 8.25, 8.0])
= sparse([6.5, 8.75, 8.75])
= sparse([
0.25 1.00 0.00
0.00 0.50 1.25
Expand All @@ -231,12 +230,12 @@ function test_form_sparse()
h̄,
J̄,
1.0,
22.75;
21.25;
sense = :min,
domain = :spin,
)

h = sparse([-6.25, -8.25, -8.0])
h = sparse([-6.5, -8.75, -8.75])
J = sparse([
-0.25 -1.00 -0.00
-0.00 -0.50 -1.25
Expand All @@ -247,7 +246,7 @@ function test_form_sparse()
h,
J,
1.0,
-22.75;
-21.25;
sense = :max,
domain = :spin,
)
Expand Down Expand Up @@ -333,57 +332,66 @@ end

function test_form_dense()
@testset "⋅ Dense" begin
= [10.0, 11.0, 12.0]
= [
= ([10.0, 11.0, 12.0])
= ([
1.0 4.0 0.0
0.0 2.0 5.0
0.0 0.0 3.0
]
Φ̄ = QUBOTools.DenseForm{Float64}(3, L̄, Q̄, 1.0, 1.0; sense = :min, domain = :bool)
])
Φ̄ =
QUBOTools.DenseForm{Float64}(3, L̄, Q̄, 1.0, 1.0; sense = :min, domain = :bool)

L = [-10.0, -11.0, -12.0]
Q = [
L = ([-10.0, -11.0, -12.0])
Q = ([
-1.0 -4.0 -0.0
-0.0 -2.0 -5.0
-0.0 -0.0 -3.0
]
])
Φ = QUBOTools.DenseForm{Float64}(3, L, Q, 1.0, -1.0; sense = :max, domain = :bool)

= [6.25, 8.25, 8.0]
= [
= ([6.5, 8.75, 8.75])
= ([
0.25 1.00 0.00
0.00 0.50 1.25
0.00 0.00 0.75
]
])
Ψ̄ = QUBOTools.DenseForm{Float64}(
3,
h̄,
J̄,
1.0,
22.75;
21.25;
sense = :min,
domain = :spin,
)

h = [-6.25, -8.25, -8.0]
J = [
h = ([-6.5, -8.75, -8.75])
J = ([
-0.25 -1.00 -0.00
-0.00 -0.50 -1.25
-0.00 -0.00 -0.75
]
Ψ = QUBOTools.DenseForm{Float64}(3, h, J, 1.0, -22.75; sense = :max, domain = :spin)
])
Ψ = QUBOTools.DenseForm{Float64}(
3,
h,
J,
1.0,
-21.25;
sense = :max,
domain = :spin,
)

@testset "Constructor" begin
@test _compare_forms(
Φ̄,
QUBOTools.DenseForm{Float64}(
3,
[11.0, 13.0, 15.0],
[
([11.0, 13.0, 15.0]),
([
0.0 4.0 0.0
0.0 0.0 5.0
0.0 0.0 0.0
],
]),
1.0,
1.0;
sense = :min,
Expand All @@ -395,12 +403,12 @@ function test_form_dense()
Φ,
QUBOTools.DenseForm{Float64}(
3,
[-11.0, -13.0, -15.0],
[
([-11.0, -13.0, -15.0]),
([
-0.0 -4.0 -0.0
-0.0 -0.0 -5.0
-0.0 -0.0 -0.0
],
]),
1.0,
-1.0;
sense = :max,
Expand All @@ -412,12 +420,12 @@ function test_form_dense()
Ψ̄,
QUBOTools.DenseForm{Float64}(
3,
[6.50, 8.75, 8.75],
[
([6.50, 8.75, 8.75]),
([
0.00 1.00 0.00
0.00 0.00 1.25
0.00 0.00 0.00
],
]),
1.0,
22.75;
sense = :min,
Expand All @@ -429,12 +437,12 @@ function test_form_dense()
Ψ,
QUBOTools.DenseForm{Float64}(
3,
[-6.50, -8.75, -8.75],
[
([-6.50, -8.75, -8.75]),
([
-0.00 -1.00 -0.00
-0.00 -0.00 -1.25
-0.00 -0.00 -0.00
],
]),
1.0,
-22.75;
sense = :max,
Expand Down

0 comments on commit 2ea48e2

Please sign in to comment.