Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Fix variable access
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Aug 14, 2023
1 parent 5b3718f commit b64d114
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/DWaveNeal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,51 @@ end
DWaveNeal.Optimizer{T}()
D-Wave's Simulated Annealing Sampler for QUBO and Ising models.
""" Optimizer
"""
Optimizer

function sample(sampler::Optimizer{T}) where {T}
# Retrieve Ising Model
# Retrieve QUBO Model
Q, α, β = qubo(sampler, Dict)

# Retrieve Optimizer Attributes
# Retrieve Optimizer attributes
n = MOI.get(sampler, MOI.NumberOfVariables())

# Solver-specific attributes
params = Dict{Symbol,Any}(
param => MOI.get(sampler, MOI.RawOptimizerAttribute(string(param)))
for param in _DWAVE_NEAL_ATTR_LIST
)

n = MOI.get(sampler, MOI.NumberOfVariables())

# Call D-Wave Neal API
sampler = neal.SimulatedAnnealingSampler()
results = @timed sampler.sample_qubo(Q; params...)
var_map = pyconvert.(Int, results.value.variables)

# Format Samples
samples = Vector{Sample{T,Int}}()

for ϕ in results.value.samples()
# Complete state
ψ = [pyconvert(Int, get(ϕ, i, 0)) for i = 1:n]
λ = QUBOTools.value(Q, ψ, α, β)
s = Sample{T}(ψ, λ)
for (ϕ, λ, r) in results.value.record
# dwave-neal will not consider variables that are not present
# in the objective funcion, leading to holes with respect to
# the indices in the record table.
# Therefore, it is necessary to introduce an extra layer of
# indirection to account for the missing variables.
ψ = zeros(Int, n)

for (i, v) in enumerate(ϕ)
ψ[var_map[i]] = pyconvert(Int, v)
end

s = Sample{T,Int}(
# state:
ψ,
# energy:
α * (pyconvert(T, λ) + β),
# reads:
pyconvert(Int, r),
)

push!(samples, s)
end
Expand All @@ -90,7 +109,7 @@ function sample(sampler::Optimizer{T}) where {T}
),
)

return SampleSet{T}(samples, metadata)
return SampleSet{T,Int}(samples, metadata)
end

end # module

2 comments on commit b64d114

@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/89622

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.4.2 -m "<description of version>" b64d1146371a3d5d62a8cac277e7c8cf3bd2f198
git push origin v0.4.2

Please sign in to comment.