From 92dadc62e22f6bde5bc8071abffa7497fdbdee60 Mon Sep 17 00:00:00 2001 From: Pedro Maciel Xavier Date: Thu, 25 Aug 2022 17:45:13 -0300 Subject: [PATCH 1/5] Add interface tests --- src/abstract/data.jl | 2 ++ src/backend/data.jl | 2 ++ test/interface/interface.jl | 26 ++++++++++++++++++++++++++ test/runtests.jl | 2 ++ 4 files changed, 32 insertions(+) create mode 100644 test/interface/interface.jl diff --git a/src/abstract/data.jl b/src/abstract/data.jl index 70d2db5a..f095c499 100644 --- a/src/abstract/data.jl +++ b/src/abstract/data.jl @@ -8,6 +8,8 @@ QUBOTools.__isvalidbridge( kws... ) where {M<:AbstractQUBOModel} = false +QUBOTools.model_name(::X) where {X <: AbstractQUBOModel} = string(X) + QUBOTools.domain_name(model::AbstractQUBOModel{<:BoolDomain}) = "Bool" QUBOTools.domain_name(model::AbstractQUBOModel{<:SpinDomain}) = "Spin" diff --git a/src/backend/data.jl b/src/backend/data.jl index 3ce30c03..83c8416b 100644 --- a/src/backend/data.jl +++ b/src/backend/data.jl @@ -1,5 +1,7 @@ QUBOTools.backend(model::StandardQUBOModel) = model +Base.isvalid(::StandardQUBOModel) = true + function QUBOTools.offset(model::StandardQUBOModel{<:Any, <:Any, T, <:Any}) where {T} if isnothing(model.offset) return zero(T) diff --git a/test/interface/interface.jl b/test/interface/interface.jl new file mode 100644 index 00000000..1f2dca0a --- /dev/null +++ b/test/interface/interface.jl @@ -0,0 +1,26 @@ +struct Model{D} <: QUBOTools.AbstractQUBOModel{D} + backend +end + +QUBOTools.backend(m::Model) = m.backend +Base.isvalid(m::Model) = isvalid(QUBOTools.backend(m)) + +function test_interface() + V = Symbol + U = Int + T = Float64 + D = BoolDomain + + @testset "-*- Interface" verbose = true begin + model = Model{D}(StandardQUBOModel{V,U,T,D}()) + + @test QUBOTools.backend(model) isa StandardQUBOModel + @test isempty(model) + @test isvalid(model) + + @testset "Data access" begin + @test QUBOTools.scale(model) == one(T) + @test QUBOTools.offset(model) == zero(T) + end + end +end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 243054c9..eea06b3f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,11 +7,13 @@ include("library/error.jl") include("library/tools.jl") include("models/models.jl") include("bridges/bridges.jl") +include("interface/interface.jl") function test_main(path::String, n::Integer) @testset "~*~*~ QUBOTools.jl ~*~*~" verbose = true begin test_error() test_tools() + test_interface() test_models(path, n) test_bridges(path, n) end From ad29029c857bca0fe1a369df409617421907917a Mon Sep 17 00:00:00 2001 From: Pedro Maciel Xavier Date: Thu, 25 Aug 2022 18:39:49 -0300 Subject: [PATCH 2/5] Add query tests --- test/interface/interface.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/interface/interface.jl b/test/interface/interface.jl index 1f2dca0a..05d1c946 100644 --- a/test/interface/interface.jl +++ b/test/interface/interface.jl @@ -17,10 +17,15 @@ function test_interface() @test QUBOTools.backend(model) isa StandardQUBOModel @test isempty(model) @test isvalid(model) - + @testset "Data access" begin @test QUBOTools.scale(model) == one(T) @test QUBOTools.offset(model) == zero(T) end + + @testset "Queries" begin + @test QUBOTools.linear_size(model) == 0 + @test QUBOTools.quadratic_size(model) == 0 + end end end \ No newline at end of file From 5f942458d6d61adf3d853628474f4704e749d587 Mon Sep 17 00:00:00 2001 From: pedromxavier Date: Thu, 25 Aug 2022 22:14:35 -0300 Subject: [PATCH 3/5] Add comments --- src/QUBOTools.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/QUBOTools.jl b/src/QUBOTools.jl index 044e6133..b6a048d6 100644 --- a/src/QUBOTools.jl +++ b/src/QUBOTools.jl @@ -4,8 +4,10 @@ using Printf using JSON using JSONSchema +# ~*~ Variable Domains ~*~ # export BoolDomain, SpinDomain -export StandardQUBOModel + +# ~*~ Supported Model Formats ~*~ # export BQPJSON export HFS export MiniZinc @@ -32,8 +34,10 @@ include("fallback/fallback.jl") # ~*~ Standard backend implementation ~*~ # include("backend/backend.jl") +# ~*~ Model implementation ~*~ # include("models/models.jl") +# ~*~ Bridges between formats ~*~ # include("bridges/bridges.jl") end # module \ No newline at end of file From 2552892851b1670b18231faff41491f97b91ba8b Mon Sep 17 00:00:00 2001 From: pedromxavier Date: Thu, 25 Aug 2022 22:22:03 -0300 Subject: [PATCH 4/5] Add `varcmp` workaround --- src/QUBOTools.jl | 13 +++++++++++++ src/library/tools.jl | 2 +- src/models/bqpjson/io.jl | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/QUBOTools.jl b/src/QUBOTools.jl index b6a048d6..e9889180 100644 --- a/src/QUBOTools.jl +++ b/src/QUBOTools.jl @@ -4,6 +4,19 @@ using Printf using JSON using JSONSchema +# ~*~ Variable comparison ~*~ # +@doc raw""" + varcmp(x::V, y::V) where {V} + +This function exists to define an arbitrary ordering for a given type and was created to address [1]. +There is no predefined comparison between instances MOI's `VariableIndex` type. +[1] https://github.com/jump-dev/MathOptInterface.jl/issues/1985 +""" function varcmp end + +varcmp(x::V, y::V) where {V} = isless(x, y) + +const ≺ = varcmp # \prec[tab] + # ~*~ Variable Domains ~*~ # export BoolDomain, SpinDomain diff --git a/src/library/tools.jl b/src/library/tools.jl index 0623230a..fcdf6f17 100644 --- a/src/library/tools.jl +++ b/src/library/tools.jl @@ -134,7 +134,7 @@ function _normal_form(_linear_terms::Dict{V,T}, _quadratic_terms::Dict{Tuple{V,V else linear_terms[i] = q end - elseif i < j + elseif i ≺ j q += get(quadratic_terms, (i, j), zero(T)) if iszero(q) delete!(quadratic_terms, (i, j)) diff --git a/src/models/bqpjson/io.jl b/src/models/bqpjson/io.jl index d0cb57c9..2299232e 100644 --- a/src/models/bqpjson/io.jl +++ b/src/models/bqpjson/io.jl @@ -62,7 +62,7 @@ function Base.read(io::IO, M::Type{<:BQPJSON}) QUBOcodec_error("Unknown variable id '$i'") elseif j ∉ variable_set QUBOcodec_error("Unknown variable id '$j'") - elseif j < i + elseif j ≺ i i, j = j, i end From b766462e2af5232bfe8797cf71f3a747e7af8401 Mon Sep 17 00:00:00 2001 From: pedromxavier Date: Thu, 25 Aug 2022 22:30:14 -0300 Subject: [PATCH 5/5] Fix missing scope --- test/interface/interface.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/interface/interface.jl b/test/interface/interface.jl index 05d1c946..d5837217 100644 --- a/test/interface/interface.jl +++ b/test/interface/interface.jl @@ -12,9 +12,9 @@ function test_interface() D = BoolDomain @testset "-*- Interface" verbose = true begin - model = Model{D}(StandardQUBOModel{V,U,T,D}()) + model = Model{D}(QUBOTools.StandardQUBOModel{V,U,T,D}()) - @test QUBOTools.backend(model) isa StandardQUBOModel + @test QUBOTools.backend(model) isa QUBOTools.StandardQUBOModel @test isempty(model) @test isvalid(model)