Skip to content

Commit

Permalink
make problemreductions an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed Nov 27, 2024
1 parent 6db3c3c commit c1a8c68
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
8 changes: 7 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ version = "0.4.0"
[deps]
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
LuxorGraphPlot = "1f49bdf2-22a7-4bc4-978b-948dc219fbbc"

[weakdeps]
ProblemReductions = "899c297d-f7d2-4ebf-8815-a35996def416"

[extensions]
ProblemReductionsExt = "ProblemReductions"

[compat]
Graphs = "1.6"
LuxorGraphPlot = "0.5"
ProblemReductions = "0.1.1"
julia = "1"

[extras]
Expand All @@ -20,4 +26,4 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Random", "GenericTensorNetworks", "LinearAlgebra"]
test = ["Test", "Random", "GenericTensorNetworks", "LinearAlgebra", "ProblemReductions"]
48 changes: 30 additions & 18 deletions src/reduceto.jl → ext/ProblemReductionsExt.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
struct IndependentSetToKSG{NT, VT} <: ProblemReductions.AbstractReductionResult
mapres::MappingResult{NT}
weights::VT
end
module ProblemReductionsExt

using UnitDiskMapping, UnitDiskMapping.Graphs
import ProblemReductions: reduceto, target_problem, extract_multiple_solutions
import ProblemReductions

function _to_gridgraph(g::UnitDiskMapping.GridGraph)
return ProblemReductions.GridGraph(getfield.(g.nodes, :loc), g.radius)
end
Expand All @@ -10,6 +12,10 @@ function _extract_weights(g::UnitDiskMapping.GridGraph{<:WeightedNode})
end

###### unweighted reduction
struct IndependentSetToKSG{NT, VT} <: ProblemReductions.AbstractReductionResult
mapres::MappingResult{NT}
weights::VT
end
function ProblemReductions.reduceto(::Type{ProblemReductions.IndependentSet{ProblemReductions.GridGraph{2}, Int, ProblemReductions.UnitWeight}}, problem::ProblemReductions.IndependentSet{GT, Int, ProblemReductions.UnitWeight} where GT<:SimpleGraph)
return IndependentSetToKSG(map_graph(UnWeighted(), problem.graph), problem.weights)
end
Expand Down Expand Up @@ -44,7 +50,7 @@ function ProblemReductions.reduceto(::Type{ProblemReductions.IndependentSet{Prob
mres = map_factoring(problem.m, problem.n)
g = _to_gridgraph(mres.grid_graph)
ws = getfield.(mres.grid_graph.nodes, :weight)
mg, vmap = set_target(g, [mres.pins_zeros..., mres.pins_output...], problem.input << length(mres.pins_zeros))
mg, vmap = UnitDiskMapping.set_target(g, [mres.pins_zeros..., mres.pins_output...], problem.input << length(mres.pins_zeros))
return FactoringToIndependentSet(mres, g, ws, vmap, ProblemReductions.IndependentSet(mg, ws[vmap]))
end

Expand All @@ -61,46 +67,52 @@ end

###### Spinglass problem to MIS on KSG ######
# NOTE: I am not sure about the correctness of this reduction. If you encounter a bug, please question this function!
struct SpinGlassToIndependentSet{NT} <: ProblemReductions.AbstractReductionResult
mapres::QUBOResult{NT}
end
function ProblemReductions.reduceto(::Type{ProblemReductions.IndependentSet{ProblemReductions.GridGraph{2}, T, Vector{T}}} where T, problem::ProblemReductions.SpinGlass{<:SimpleGraph})
n = length(problem.h)
M = similar(problem.h, n, n)
for (e, j) in zip(edges(problem.graph), problem.J)
M[e.src, e.dst] = M[e.dst, e.src] = j
end
return map_qubo(M, -problem.h)
return SpinGlassToIndependentSet(map_qubo(M, -problem.h))
end

function ProblemReductions.target_problem(res::QUBOResult)
grid = _to_gridgraph(res.grid_graph)
ws = getfield.(res.grid_graph.nodes, :weight)
function ProblemReductions.target_problem(res::SpinGlassToIndependentSet)
grid = _to_gridgraph(res.mapres.grid_graph)
ws = getfield.(res.mapres.grid_graph.nodes, :weight)
return ProblemReductions.IndependentSet(grid, ws)
end

function ProblemReductions.extract_solution(res::QUBOResult, sol)
res = map_config_back(res, sol)
function ProblemReductions.extract_solution(res::SpinGlassToIndependentSet, sol)
res = map_config_back(res.mapres, sol)
return 1 .- 2 .* Int.(res)
end

###### Spinglass problem on grid to MIS on KSG ######
# NOTE: the restricted layout is not implemented, since it is not often used
struct SquareSpinGlassToIndependentSet{NT} <: ProblemReductions.AbstractReductionResult
mapres::SquareQUBOResult{NT}
end
function ProblemReductions.reduceto(::Type{ProblemReductions.IndependentSet{ProblemReductions.GridGraph{2}, T, Vector{T}}} where T, problem::ProblemReductions.SpinGlass{ProblemReductions.GridGraph{2}})
g = problem.graph
@assert 1 <= g.radius < sqrt(2) "Only support nearest neighbor interaction"
coupling = [(g.locations[e.src]..., g.locations[e.dst]..., J) for (e, J) in zip(edges(g), problem.J)]
onsite = [(i, j, -h) for ((i, j), h) in zip(g.locations, problem.h)]
# a vector coupling of `(i, j, i', j', J)`, s.t. (i', j') == (i, j+1) or (i', j') = (i+1, j).
# a vector of onsite term `(i, j, h)`.
return map_qubo_square(coupling, onsite)
return SquareSpinGlassToIndependentSet(map_qubo_square(coupling, onsite))
end

function ProblemReductions.target_problem(res::SquareQUBOResult)
grid = _to_gridgraph(res.grid_graph)
ws = getfield.(res.grid_graph.nodes, :weight)
function ProblemReductions.target_problem(res::SquareSpinGlassToIndependentSet)
grid = _to_gridgraph(res.mapres.grid_graph)
ws = getfield.(res.mapres.grid_graph.nodes, :weight)
return ProblemReductions.IndependentSet(grid, ws)
end

function ProblemReductions.extract_solution(res::SquareQUBOResult, sol)
res = map_config_back(res, sol)
function ProblemReductions.extract_solution(res::SquareSpinGlassToIndependentSet, sol)
res = map_config_back(res.mapres, sol)
return 1 .- 2 .* Int.(res)
end

end
3 changes: 0 additions & 3 deletions src/UnitDiskMapping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module UnitDiskMapping
using Graphs
using LuxorGraphPlot
using LuxorGraphPlot.Luxor.Colors
import ProblemReductions: reduceto, target_problem, extract_multiple_solutions
import ProblemReductions

# Basic types
export UnWeighted, Weighted
Expand Down Expand Up @@ -55,7 +53,6 @@ include("mapping.jl")
include("weighted.jl")
include("simplifiers.jl")
include("extracting_results.jl")
include("reduceto.jl")
include("visualize.jl")

end
6 changes: 3 additions & 3 deletions src/dragondrop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ function post_process_grid(grid::Matrix{SimpleCell{T}}, h0, h1) where T
return gg, pins
end

struct QUBOResult{NT} <: ProblemReductions.AbstractReductionResult
struct QUBOResult{NT}
grid_graph::GridGraph{NT}
pins::Vector{Int}
mis_overhead::Int
Expand All @@ -353,13 +353,13 @@ function map_config_back(res::WMISResult, cfg)
return cfg[res.pins]
end

struct RestrictedQUBOResult{NT} <: ProblemReductions.AbstractReductionResult
struct RestrictedQUBOResult{NT}
grid_graph::GridGraph{NT}
end
function map_config_back(res::RestrictedQUBOResult, cfg)
end

struct SquareQUBOResult{NT} <: ProblemReductions.AbstractReductionResult
struct SquareQUBOResult{NT}
grid_graph::GridGraph{NT}
pins::Vector{Int}
mis_overhead::Float64
Expand Down

0 comments on commit c1a8c68

Please sign in to comment.