Skip to content

Commit

Permalink
Fix QUBOTools.varshow(::VI)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Oct 25, 2023
1 parent f6997ed commit d8cbd80
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
9 changes: 7 additions & 2 deletions ext/QUBOTools_MOI/num_reads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ struct NumberOfReads <: MOI.AbstractModelAttribute
end
end

QUBOTools.__moi_num_reads() = NumberOfReads

function MOI.is_set_by_optimize(::NumberOfReads)
return true

Check warning on line 15 in ext/QUBOTools_MOI/num_reads.jl

View check run for this annotation

Codecov / codecov/patch

ext/QUBOTools_MOI/num_reads.jl#L14-L15

Added lines #L14 - L15 were not covered by tests
end
Expand All @@ -28,3 +26,10 @@ function MOI.get(model::MOI.ModelLike, attr::NumberOfReads)
# will not account for solution multiplicity.
return 1

Check warning on line 27 in ext/QUBOTools_MOI/num_reads.jl

View check run for this annotation

Codecov / codecov/patch

ext/QUBOTools_MOI/num_reads.jl#L27

Added line #L27 was not covered by tests
end

# The function takes no arguments and returns the NumberOfReads type.
# Other packages will assign its return value to a constant, e.g.,
#
# const NumberOfReads = QUBOTools.__moi_num_reads()
#
QUBOTools.__moi_num_reads() = NumberOfReads
14 changes: 7 additions & 7 deletions ext/QUBOTools_MOI/qubo_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ mutable struct QUBOModel{T,C<:BinaryDomain} <: MOI.ModelLike
end
end

# The function takes no arguments and returns the QUBOModel type. Other
# packages will assign its return value to a constant, e.g.,
#
# const QUBOModel = QUBOTools.__moi_qubo_model()
#
QUBOTools.__moi_qubo_model() = QUBOModel

function QUBOModel{T}() where {T}
return QUBOModel{T,MOI.ZeroOne}()

Check warning on line 19 in ext/QUBOTools_MOI/qubo_model.jl

View check run for this annotation

Codecov / codecov/patch

ext/QUBOTools_MOI/qubo_model.jl#L18-L19

Added lines #L18 - L19 were not covered by tests
end
Expand Down Expand Up @@ -160,3 +153,10 @@ function MOI.get(::QUBOModel{T,C}, ::MOI.VariableName, vi::VI) where {T,C<:Binar
return "s[$(vi.value)]"

Check warning on line 153 in ext/QUBOTools_MOI/qubo_model.jl

View check run for this annotation

Codecov / codecov/patch

ext/QUBOTools_MOI/qubo_model.jl#L153

Added line #L153 was not covered by tests
end
end

# The function takes no arguments and returns the QUBOModel type.
# Other packages will assign its return value to a constant, e.g.,
#
# const QUBOModel = QUBOTools.__moi_qubo_model()
#
QUBOTools.__moi_qubo_model() = QUBOModel
16 changes: 8 additions & 8 deletions ext/QUBOTools_MOI/spin_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ The set ``\left\lbrace{}{\pm 1}\right\rbrace{}``.
"""
struct Spin <: MOI.AbstractScalarSet end

function MOIU._to_string(options::MOIU._PrintOptions, ::Spin)
return string(MOIU._to_string(options, ), " {±1}")
end

function MOIU._to_string(::MOIU._PrintOptions{MIME"text/latex"}, ::Spin)
return raw"\in \left\lbrace{}{\pm 1}\right\rbrace{}"

Check warning on line 13 in ext/QUBOTools_MOI/spin_set.jl

View check run for this annotation

Codecov / codecov/patch

ext/QUBOTools_MOI/spin_set.jl#L12-L13

Added lines #L12 - L13 were not covered by tests
end

# Since extensions do not allow new types to be defined and exported (only
# methods), we can still provide a type by first defining an empty function
# and then defining an "argless" method to retrive it, requiring that the
Expand All @@ -13,11 +21,3 @@ struct Spin <: MOI.AbstractScalarSet end
# const Spin = QUBOTools.__moi_spin_set()
#
QUBOTools.__moi_spin_set() = Spin

function MOIU._to_string(options::MOIU._PrintOptions, ::Spin)
return string(MOIU._to_string(options, ), " {±1}")
end

function MOIU._to_string(::MOIU._PrintOptions{MIME"text/latex"}, ::Spin)
return raw"\in \left\lbrace{}{\pm 1}\right\rbrace{}"
end
2 changes: 1 addition & 1 deletion ext/QUBOTools_MOI/variables.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This will fall back to the integer ordering method.
QUBOTools.varlt(u::VI, v::VI) = QUBOTools.varlt(u.value, v.value)

QUBOTools.varshow(io::IO, v::VI) = QUBOTools.varshow(io, v.value)
QUBOTools.varshow(v::VI) = QUBOTools.varshow(v.value)
23 changes: 15 additions & 8 deletions test/integration/ext/moi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ function test_moi_variables()
# Check if the Spin variable set is defined
@test QUBOTools.__moi_spin_set() === Spin

# Check if the variable ordering of variables is behaving accordingly
@test QUBOTools.varlt(VI(1), VI(1)) === false
@test QUBOTools.varlt(VI(1), VI(2)) === true
@test QUBOTools.varlt(VI(2), VI(1)) === false

# This specific ordering follows as 1, 2, 3, ..., -1, -2, -3, ...
@test QUBOTools.varlt(VI(1), VI(-1)) === true
@test QUBOTools.varlt(VI(-1), VI(1)) === false
@testset "→ varlt" begin
# Check if the variable ordering of variables is behaving accordingly
@test QUBOTools.varlt(VI(1), VI(1)) === false
@test QUBOTools.varlt(VI(1), VI(2)) === true
@test QUBOTools.varlt(VI(2), VI(1)) === false

# This specific ordering follows as 1, 2, 3, ..., -1, -2, -3, ...
@test QUBOTools.varlt(VI(1), VI(-1)) === true
@test QUBOTools.varlt(VI(-1), VI(1)) === false
end

@testset "→ varshow" begin
@test QUBOTools.varshow(VI(103)) == "x₁₀₃"
@test QUBOTools.varshow(VI(-13)) == "x₋₁₃"
end
end

return nothing
Expand Down

0 comments on commit d8cbd80

Please sign in to comment.