|
| 1 | +module ProblemReductionsExt |
| 2 | + |
| 3 | +using UnitDiskMapping, UnitDiskMapping.Graphs |
| 4 | +import ProblemReductions: reduceto, target_problem, extract_multiple_solutions |
| 5 | +import ProblemReductions |
| 6 | + |
| 7 | +function _to_gridgraph(g::UnitDiskMapping.GridGraph) |
| 8 | + return ProblemReductions.GridGraph(getfield.(g.nodes, :loc), g.radius) |
| 9 | +end |
| 10 | +function _extract_weights(g::UnitDiskMapping.GridGraph{<:WeightedNode}) |
| 11 | + getfield.(g.nodes, :weight) |
| 12 | +end |
| 13 | + |
| 14 | +###### unweighted reduction |
| 15 | +struct IndependentSetToKSG{NT, VT} <: ProblemReductions.AbstractReductionResult |
| 16 | + mapres::MappingResult{NT} |
| 17 | + weights::VT |
| 18 | +end |
| 19 | +function ProblemReductions.reduceto(::Type{ProblemReductions.IndependentSet{ProblemReductions.GridGraph{2}, Int, ProblemReductions.UnitWeight}}, problem::ProblemReductions.IndependentSet{GT, Int, ProblemReductions.UnitWeight} where GT<:SimpleGraph) |
| 20 | + return IndependentSetToKSG(map_graph(UnWeighted(), problem.graph), problem.weights) |
| 21 | +end |
| 22 | + |
| 23 | +function ProblemReductions.target_problem(res::IndependentSetToKSG{<:UnWeightedNode}) |
| 24 | + return ProblemReductions.IndependentSet(_to_gridgraph(res.mapres.grid_graph)) |
| 25 | +end |
| 26 | +function ProblemReductions.extract_solution(res::IndependentSetToKSG, sol) |
| 27 | + return map_config_back(res.mapres, sol) |
| 28 | +end |
| 29 | + |
| 30 | +###### Weighted reduction |
| 31 | +# TODO: rescale the weights to avoid errors |
| 32 | +function ProblemReductions.reduceto(::Type{ProblemReductions.IndependentSet{ProblemReductions.GridGraph{2}, Float64, Vector{Float64}}}, problem::ProblemReductions.IndependentSet{GT} where GT<:SimpleGraph) |
| 33 | + return IndependentSetToKSG(map_graph(Weighted(), problem.graph), problem.weights) |
| 34 | +end |
| 35 | +function ProblemReductions.target_problem(res::IndependentSetToKSG{<:WeightedNode}) |
| 36 | + graph = _to_gridgraph(res.mapres.grid_graph) |
| 37 | + weights = UnitDiskMapping.map_weights(res.mapres, res.weights) |
| 38 | + return ProblemReductions.IndependentSet(graph, weights) |
| 39 | +end |
| 40 | + |
| 41 | +###### Factoring ###### |
| 42 | +struct FactoringToIndependentSet{NT} <: ProblemReductions.AbstractReductionResult |
| 43 | + mapres::FactoringResult{NT} |
| 44 | + raw_graph::ProblemReductions.GridGraph{2} |
| 45 | + raw_weight::Vector{Int} |
| 46 | + vmap::Vector{Int} |
| 47 | + problem::ProblemReductions.IndependentSet{ProblemReductions.GridGraph{2}, Int, Vector{Int}} |
| 48 | +end |
| 49 | +function ProblemReductions.reduceto(::Type{ProblemReductions.IndependentSet{ProblemReductions.GridGraph{2}, Int, Vector{Int}}}, problem::ProblemReductions.Factoring) |
| 50 | + mres = map_factoring(problem.m, problem.n) |
| 51 | + g = _to_gridgraph(mres.grid_graph) |
| 52 | + ws = getfield.(mres.grid_graph.nodes, :weight) |
| 53 | + mg, vmap = UnitDiskMapping.set_target(g, [mres.pins_zeros..., mres.pins_output...], problem.input << length(mres.pins_zeros)) |
| 54 | + return FactoringToIndependentSet(mres, g, ws, vmap, ProblemReductions.IndependentSet(mg, ws[vmap])) |
| 55 | +end |
| 56 | + |
| 57 | +function ProblemReductions.target_problem(res::FactoringToIndependentSet) |
| 58 | + return res.problem |
| 59 | +end |
| 60 | + |
| 61 | +function ProblemReductions.extract_solution(res::FactoringToIndependentSet, sol) |
| 62 | + cfg = zeros(Int, nv(res.raw_graph)) |
| 63 | + cfg[res.vmap] .= sol |
| 64 | + i1, i2 = map_config_back(res.mapres, cfg) |
| 65 | + return vcat([i1>>(k-1) & 1 for k=1:length(res.mapres.pins_input1)], [i2>>(k-1) & 1 for k=1:length(res.mapres.pins_input2)]) |
| 66 | +end |
| 67 | + |
| 68 | +###### Spinglass problem to MIS on KSG ###### |
| 69 | +# NOTE: I am not sure about the correctness of this reduction. If you encounter a bug, please question this function! |
| 70 | +struct SpinGlassToIndependentSet{NT} <: ProblemReductions.AbstractReductionResult |
| 71 | + mapres::QUBOResult{NT} |
| 72 | +end |
| 73 | +function ProblemReductions.reduceto(::Type{ProblemReductions.IndependentSet{ProblemReductions.GridGraph{2}, Float64, Vector{Float64}}}, problem::ProblemReductions.SpinGlass{<:SimpleGraph}) |
| 74 | + n = length(problem.h) |
| 75 | + M = similar(problem.h, n, n) |
| 76 | + for (e, j) in zip(edges(problem.graph), problem.J) |
| 77 | + M[e.src, e.dst] = M[e.dst, e.src] = j |
| 78 | + end |
| 79 | + return SpinGlassToIndependentSet(map_qubo(M, -problem.h)) |
| 80 | +end |
| 81 | + |
| 82 | +function ProblemReductions.target_problem(res::SpinGlassToIndependentSet) |
| 83 | + grid = _to_gridgraph(res.mapres.grid_graph) |
| 84 | + ws = getfield.(res.mapres.grid_graph.nodes, :weight) |
| 85 | + return ProblemReductions.IndependentSet(grid, ws) |
| 86 | +end |
| 87 | + |
| 88 | +function ProblemReductions.extract_solution(res::SpinGlassToIndependentSet, sol) |
| 89 | + res = map_config_back(res.mapres, sol) |
| 90 | + return 1 .- 2 .* Int.(res) |
| 91 | +end |
| 92 | + |
| 93 | +###### Spinglass problem on grid to MIS on KSG ###### |
| 94 | +# NOTE: the restricted layout is not implemented, since it is not often used |
| 95 | +struct SquareSpinGlassToIndependentSet{NT} <: ProblemReductions.AbstractReductionResult |
| 96 | + mapres::SquareQUBOResult{NT} |
| 97 | +end |
| 98 | +function ProblemReductions.reduceto(::Type{ProblemReductions.IndependentSet{ProblemReductions.GridGraph{2}, Float64, Vector{Float64}}}, problem::ProblemReductions.SpinGlass{ProblemReductions.GridGraph{2}}) |
| 99 | + g = problem.graph |
| 100 | + @assert 1 <= g.radius < sqrt(2) "Only support nearest neighbor interaction" |
| 101 | + coupling = [(g.locations[e.src]..., g.locations[e.dst]..., J) for (e, J) in zip(edges(g), problem.J)] |
| 102 | + onsite = [(i, j, -h) for ((i, j), h) in zip(g.locations, problem.h)] |
| 103 | + # a vector coupling of `(i, j, i', j', J)`, s.t. (i', j') == (i, j+1) or (i', j') = (i+1, j). |
| 104 | + # a vector of onsite term `(i, j, h)`. |
| 105 | + return SquareSpinGlassToIndependentSet(map_qubo_square(coupling, onsite)) |
| 106 | +end |
| 107 | + |
| 108 | +function ProblemReductions.target_problem(res::SquareSpinGlassToIndependentSet) |
| 109 | + grid = _to_gridgraph(res.mapres.grid_graph) |
| 110 | + ws = getfield.(res.mapres.grid_graph.nodes, :weight) |
| 111 | + return ProblemReductions.IndependentSet(grid, ws) |
| 112 | +end |
| 113 | + |
| 114 | +function ProblemReductions.extract_solution(res::SquareSpinGlassToIndependentSet, sol) |
| 115 | + res = map_config_back(res.mapres, sol) |
| 116 | + return 1 .- 2 .* Int.(res) |
| 117 | +end |
| 118 | +end |
0 commit comments