Skip to content

Commit

Permalink
Merge pull request #8 from psrenergy/fix
Browse files Browse the repository at this point in the history
Fix data access
  • Loading branch information
pedromxavier authored Aug 23, 2022
2 parents d5209a7 + 0c56ba1 commit c782b3c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/abstract/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ QUBOTools.quadratic_size(model::AbstractQUBOModel) = length(QUBOTools.quadratic_

QUBOTools.qubo(model::AbstractQUBOModel{<:BoolDomain}) = QUBOTools.qubo(Dict, Float64, model)

function QUBOTools.qubo(::AbstractQUBOModel{<:SpinDomain})
QUBOTools.codec_error(
"""
Can't generate normal qubo form from ising model.
Consider converting your model with `convert`"""
)
end

function QUBOTools.qubo(::Type{<:Dict}, T::Type, model::AbstractQUBOModel{<:BoolDomain})
x = QUBOTools.variable_map(model)
Q = Dict{Tuple{Int,Int},T}()
Expand Down Expand Up @@ -54,6 +62,14 @@ end

QUBOTools.ising(model::AbstractQUBOModel{<:SpinDomain}) = QUBOTools.ising(Dict, model)

function QUBOTools.ising(::AbstractQUBOModel{<:BoolDomain})
QUBOTools.codec_error(
"""
Can't generate normal ising form from boolean model.
Consider converting your model with `convert`"""
)
end

function QUBOTools.ising(::Type{<:Dict}, model::AbstractQUBOModel{<:SpinDomain})
s = QUBOTools.variable_map(model)
h = QUBOTools.linear_terms(model)
Expand Down
19 changes: 17 additions & 2 deletions src/backend/data.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
QUBOTools.backend(model::StandardQUBOModel) = model

QUBOTools.offset(model::StandardQUBOModel) = model.offset
QUBOTools.scale(model::StandardQUBOModel) = model.scale
function QUBOTools.offset(model::StandardQUBOModel{<:Any, <:Any, T, <:Any}) where {T}
if isnothing(model.offset)
return zero(T)
else
return model.offset
end
end

function QUBOTools.scale(model::StandardQUBOModel{<:Any, <:Any, T, <:Any}) where {T}
if isnothing(model.scale)
return one(T)
else
return model.scale
end
end

QUBOTools.id(model::StandardQUBOModel) = model.id
QUBOTools.version(model::StandardQUBOModel) = model.version
QUBOTools.description(model::StandardQUBOModel) = model.description
QUBOTools.metadata(model::StandardQUBOModel) = model.metadata
QUBOTools.sampleset(model::StandardQUBOModel) = model.sampleset

QUBOTools.linear_terms(model::StandardQUBOModel) = model.linear_terms
QUBOTools.quadratic_terms(model::StandardQUBOModel) = model.quadratic_terms

Expand Down

0 comments on commit c782b3c

Please sign in to comment.