Skip to content

Commit

Permalink
Merge pull request #32 from psrenergy/px/docs
Browse files Browse the repository at this point in the history
Enhance Interface + Fix issues
  • Loading branch information
pedromxavier authored Dec 16, 2022
2 parents 00d9b00 + f04891a commit 9db1299
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = [
"joaquimg <[email protected]>",
"bernalde <[email protected]>"
]
version = "0.5.2"
version = "0.5.3"

[deps]
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Expand Down
37 changes: 15 additions & 22 deletions src/analysis/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@
title --> "Solution Summary"
xlabel --> "Energy"
ylabel --> "Frequency"
fill --> 0
legend --> nothing
seriestype := :bar

# n = length(ω)
# a = ω[1].value
# b = ω[n].value
# m = 1000
# σ = 0.08 * (b - a)
# t = range(a, b, m)
# z = zeros(T, m)

# for i = 1:n
# μ = ω[i].value
# y = ω[i].reads

x = value.(ω)
y = reads.(ω)
n = length(y)
z = zeros(Int, n)
λ = nothing

# for j = 1:m
# x = t[j]
# z[j] += y * exp(-((x - μ) / σ)^2)
# end
# end
for i = 1:n
if !isnothing(λ) && λ x[i]
z[i] = y[i-1]
y[i] = y[i] + z[i]
end

# return (t, z)
λ = x[i]
end

x = value.(ω)
y = reads.(ω)
seriestype := :bar
fillrange := z

return (x, y)
end
4 changes: 2 additions & 2 deletions src/formats/bqpjson/printer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ function write_model(io::IO, model::AbstractModel{D}, ::BQPJSON{D}) where {D<:Va
assignment = Dict{String,Any}[
Dict{String,Any}(
"id" => i,
"value" => state(s, j)
) for (i, j) in variable_map(model)
"value" => state(s, i),
) for i in values(variable_map(model))
]

for _ = 1:reads(s)
Expand Down
4 changes: 2 additions & 2 deletions src/formats/qubist/printer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ function write_model(io::IO, model::AbstractModel{D}, fmt::Qubist{D}) where {D}
_print_header(io, data, fmt)

for (i, h) in linear_terms(model)
println(io, "$(variable_inv(model, i)) $(variable_inv(model, i)) $(h)")
println(io, "$(i) $(i) $(h)")
end

for ((i, j), J) in quadratic_terms(model)
println(io, "$(variable_inv(model, i)) $(variable_inv(model, j)) $(J)")
println(io, "$(i) $(j) $(J)")
end

return nothing
Expand Down
6 changes: 3 additions & 3 deletions src/formats/qubo/printer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ function write_model(io::IO, model::AbstractModel{D}, fmt::QUBO{D}) where {D}

!isnothing(fmt.comment) && println(io, "$(fmt.comment) linear terms")

for (i, l) in explicit_linear_terms(model)
println(io, "$(variable_inv(model, i)) $(variable_inv(model, i)) $(l)")
for (i, l) in linear_terms(model)
println(io, "$(i) $(i) $(l)")
end

!isnothing(fmt.comment) && println(io, "$(fmt.comment) quadratic terms")

for ((i, j), q) in quadratic_terms(model)
println(io, "$(variable_inv(model, i)) $(variable_inv(model, j)) $(q)")
println(io, "$(i) $(j) $(q)")
end

return nothing
Expand Down
8 changes: 7 additions & 1 deletion src/interface/fallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This file contains fallback implementations by calling the model's backend.
This allows for external models to define a QUBOTools-based backend and profit from these queries.
"""

frontend(model) = backend(model)

# ~*~ Data access ~*~ #
model_name(model) = model_name(backend(model))
domain(model) = domain(backend(model))
Expand Down Expand Up @@ -42,4 +44,8 @@ quadratic_size(model) = quadratic_size(backend(model))
density(model) = density(backend(model))
linear_density(model) = linear_density(backend(model))
quadratic_density(model) = quadratic_density(backend(model))
adjacency(model, args...) = adjacency(backend(model), args...)
adjacency(model, args...) = adjacency(backend(model), args...)

# ~*~ File I/O ~*~ #
write_model(io, model, args...) = write_model(io, backend(model), args...)
read_model!(io, model, args...) = read_model!(io, backend(model), args...)
16 changes: 15 additions & 1 deletion src/interface/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ function ising(Q::SparseMatrixCSC{T}, α::T = one(T), β::T = zero(T)) where {T}
end

swap_sense(::Nothing) = nothing
swap_sense(s::Sense) = s === Min ? Max : Min
swap_sense(s::Sense) = s === Min ? Max : Min
swap_sense(target::Symbol, x::Any) = swap_sense(Sense(target), x)
swap_sense(source::Symbol, target::Symbol, x::Any) = swap_sense(Sense(source), Sense(target), x)

Expand All @@ -356,4 +356,18 @@ end

function swap_sense(Q::Dict{Tuple{Int,Int},T}) where {T}
return Dict{Tuple{Int,Int},T}(ij => -c for (ij, c) in Q)
end

function format(
source_sense::Sense,
source_domain::VariableDomain,
target_sense::Sense,
target_domain::VariableDomain,
x::Any,
)
return swap_sense(
source_sense,
target_sense,
swap_domain(source_domain, target_domain, x),
)
end
16 changes: 15 additions & 1 deletion src/interface/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ Returns a list containing all available QUBO file formats.
Given a file path, tries to infer the type associated to a QUBO model format.
""" function infer_format end

@doc raw"""
frontend(model)::AbstractModel
frontend(model::AbstractModel)::AbstractModel
Retrieves the model's backend.
Implementing this function allows one to profit from fallback implementations of the other methods.
""" function frontend end

@doc raw"""
backend(model)::AbstractModel
backend(model::AbstractModel)::AbstractModel
Expand Down Expand Up @@ -427,5 +435,11 @@ If a second parameter, an integer, is present, then the set of neighbors of that

@doc raw"""
format(data::Vector{Sample{T,U}}) where {T,U}
format(
source_sense::Sense,
source_domain::VariableDomain,
target_sense::Sense,
target_domain::VariableDomain,
x::Any
)
""" function format end
17 changes: 9 additions & 8 deletions src/model/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ function Base.copy(model::Model{D,V,T,U}) where {D,V,T,U}
copy(model.quadratic_terms),
copy(model.variable_map),
copy(model.variable_inv);
scale = model.scale,
offset = model.offset,
sense = model.sense,
id = model.id,
version = model.version,
description = model.description,
metadata = deepcopy(model.metadata),
sampleset = model.sampleset,
scale = scale(model),
offset = offset(model),
sense = sense(model),
id = id(model),
version = version(model),
description = description(model),
metadata = deepcopy(metadata(model)),
sampleset = copy(sampleset(model)),
)
end

Expand Down Expand Up @@ -191,6 +191,7 @@ function swap_domain(::X, ::Y, model::Model{X,V,T,U}) where {X,Y,V,T,U}
copy(variable_inv(model));
scale = α,
offset = β,
sense = sense(model),
id = id(model),
version = version(model),
description = description(model),
Expand Down

2 comments on commit 9db1299

@pedromxavier
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/74268

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.3 -m "<description of version>" 9db12992db7903502c1242446fed3311611fe4e3
git push origin v0.5.3

Please sign in to comment.