From 0c48fd0dd6ff1d782b381db6ebebe91a0c8168e8 Mon Sep 17 00:00:00 2001 From: pedromxavier Date: Thu, 23 Nov 2023 11:29:31 -0500 Subject: [PATCH] Integrate new QUBOTools interface --- CondaPkg.toml | 2 +- Project.toml | 16 ++++++++++------ src/PySA.jl | 39 +++++++++++++++++---------------------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/CondaPkg.toml b/CondaPkg.toml index fa10dd7..18e8332 100644 --- a/CondaPkg.toml +++ b/CondaPkg.toml @@ -5,4 +5,4 @@ python = ">=3.8,<3.11" [pip.deps] numpy = ">=1.20.0" -pysa = "@ git+https://github.com/nasa/pysa" \ No newline at end of file +pysa = "@ git+https://github.com/nasa/pysa" diff --git a/Project.toml b/Project.toml index 1d56b1e..2e1bb44 100644 --- a/Project.toml +++ b/Project.toml @@ -4,11 +4,15 @@ authors = ["pedromxavier "] version = "0.2.0" [deps] -LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" -PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d" -QUBODrivers = "a3f166f7-2cd3-47b6-9e1e-6fbfe0449eb0" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee" +PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d" +QUBODrivers = "a3f166f7-2cd3-47b6-9e1e-6fbfe0449eb0" +QUBOTools = "60eb5b62-0a39-4ddc-84c5-97d2adff9319" [compat] -PythonCall = "0.9.15" -QUBODrivers = "0.3" -julia = "1.9" +MathOptInterface = "1" +PythonCall = "0.9.15" +QUBODrivers = "0.3" +QUBOTools = "0.9" +julia = "1.9" diff --git a/src/PySA.jl b/src/PySA.jl index 1d7ee6e..cdd2637 100644 --- a/src/PySA.jl +++ b/src/PySA.jl @@ -2,16 +2,13 @@ module PySA using PythonCall using LinearAlgebra -using QUBODrivers: - QUBODrivers, - QUBOTools, - MOI, - Sample, - SampleSet, - ising - -const np = PythonCall.pynew() -const pysa = PythonCall.pynew() + +import QUBODrivers +import QUBOTools +import MathOptInterface as MOI + +const np = PythonCall.pynew() +const pysa = PythonCall.pynew() const pysa_sa = PythonCall.pynew() function __init__() @@ -21,10 +18,8 @@ function __init__() end QUBODrivers.@setup Optimizer begin - name = "PySA" - sense = :min - domain = :spin - version = v"0.1.0" # pysa version + name = "PySA" + version = v"0.1.0" # pysa version attributes = begin NumberOfSweeps["n_sweeps"]::Integer = 32 NumberOfReplicas["n_replicas"]::Integer = 3 @@ -52,7 +47,7 @@ function _float_type(::Type{T})::String where {T<:AbstractFloat} end function QUBODrivers.sample(sampler::Optimizer{T}) where {T} - h, J, α, β = ising(sampler, Matrix) + n, h, J, α, β = QUBOTools.ising(sampler, :dense; sense = :min) # Since PySA adopts the s = 1 - 2x instead of the s = 2x - 1 # convention, the sign of 'h' has to be inverted, as well as @@ -81,18 +76,18 @@ function QUBODrivers.sample(sampler::Optimizer{T}) where {T} verbose = !MOI.get(sampler, MOI.Silent()), ) - samples = Vector{Sample{T,Int}}(undef, num_replicas) + samples = Vector{QUBOTools.Sample{T,Int}}(undef, num_replicas) for Ψ in result.value["states"].values for i = 1:num_replicas # NOTE: Python is 0-indexed - # NOTE: sign has to be inverted as mentioned above + # NOTE: sign has to be inverted, as mentioned before ψ = -round.(Int, pyconvert.(T, Ψ[i-1])) - # Compute value instead of retrieving it, to avoid - # precision errors - λ = QUBOTools.value(h, J, ψ, α, β) - samples[i] = Sample{T}(ψ, λ) + # Compute value instead of retrieving it, to avoid precision errors + λ = QUBOTools.value(ψ, h, J, α, β) + + samples[i] = QUBOTools.Sample{T,Int}(ψ, λ) end end @@ -103,7 +98,7 @@ function QUBODrivers.sample(sampler::Optimizer{T}) where {T} ), ) - return SampleSet{T}(samples, metadata) + return QUBOTools.SampleSet{T}(samples, metadata; domain = :spin, sense = :min) end end # module PySA