Skip to content

Commit 763bf37

Browse files
committed
clean up
1 parent ce0996a commit 763bf37

File tree

11 files changed

+64
-194
lines changed

11 files changed

+64
-194
lines changed

project/createmap.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ function dump_mapping_to_julia(filename, patterns)
5757
end
5858

5959
dump_mapping_to_julia(joinpath(@__DIR__, "..", "src", "extracting_results.jl"),
60-
(Cross{false}(), Cross{true}(), TShape{true}(), TShape{false}(),
60+
(Cross{false}(), Cross{true}(),
6161
Turn(), WTurn(), Branch(), BranchFix(), TrivialTurn(), TCon(), BranchFixB()))

project/vizgadget.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ end
6565

6666
function pattern2tikz(folder::String)
6767
for (p, sub) in ((Cross{true}(), "crosscon"), (Cross{false}(), "cross"),
68-
(TShape{true}(), "tshapecon"), (TShape{false}(), "tshape"), (Turn(), "turn"))
68+
(Turn(), "turn"))
6969
open(joinpath(folder, sub*"-udg.tex"), "w") do f
7070
write(f, viz_gadget(p))
7171
end

project/vizugrid.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using UnitDiskMapping
22

33
function print_mapback(folder::String)
44
for (p, sub) in ((Cross{true}(), "crosscon"), (Cross{false}(), "cross"),
5-
(TShape{true}(), "tshapecon"), (TShape{false}(), "tshape"), (Turn(), "turn"))
5+
(Turn(), "turn"))
66
println()
77
println(typeof(p))
88
for (bc, bcs) in allbcs(p)

src/UnitDiskMapping.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module UnitDiskMapping
44

55
using Graphs
66

7-
export UGrid, apply_crossing_gadgets!, apply_gadget!, embed_graph, unitdisk_graph
7+
export UGrid, apply_crossing_gadgets!, apply_gadget!, embed_graph
88
export unapply_gadgets!, unmatch
9-
export Pattern, TShape, Corner, Turn, Cross, source_graph, mapped_graph, TruncatedTurn
9+
export Pattern, Corner, Turn, Cross, source_graph, mapped_graph, TruncatedTurn
1010
export mapped_entry_to_compact, source_entry_to_configs, map_config_back
1111

1212
include("utils.jl")

src/extracting_results.jl

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function mapped_entry_to_compact(::Cross{false})
55
end
66

77
function source_entry_to_configs(::Cross{false})
8-
return Dict(Pair{Int64, Vector{BitVector}}[5 => [[1, 0, 1, 0, 0, 0, 1, 0, 1], [1, 0, 0, 1, 0, 0, 1, 0, 1]], 12 => [[0, 0, 1, 0, 1, 0, 1, 0, 1], [0, 1, 0, 0, 1, 0, 1, 0, 1]], 8 => [[0, 0, 1, 0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 0, 1, 0], [0, 1, 0, 0, 1, 0, 0, 1, 0]], 1 => [[1, 0, 1, 0, 0, 0, 1, 0, 0], [1, 0, 0, 1, 0, 0, 1, 0, 0], [1, 0, 1, 0, 0, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 0, 1, 0]], 0 => [[0, 1, 0, 1, 0, 0, 1, 0, 0], [0, 1, 0, 1, 0, 0, 0, 1, 0]], 6 => [[0, 1, 0, 1, 0, 1, 0, 0, 1]], 11 => [[1, 0, 1, 0, 1, 1, 0, 1, 0]], 9 => [[1, 0, 1, 0, 1, 0, 1, 0, 0], [1, 0, 1, 0, 1, 0, 0, 1, 0]], 14 => [[0, 0, 1, 0, 1, 1, 0, 0, 1], [0, 1, 0, 0, 1, 1, 0, 0, 1]], 3 => [[1, 0, 1, 0, 0, 1, 0, 1, 0], [1, 0, 0, 1, 0, 1, 0, 1, 0]], 7 => [[1, 0, 1, 0, 0, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1, 0, 0, 1]], 4 => [[0, 1, 0, 1, 0, 0, 1, 0, 1]], 13 => [[1, 0, 1, 0, 1, 0, 1, 0, 1]], 15 => [[1, 0, 1, 0, 1, 1, 0, 0, 1]], 2 => [[0, 1, 0, 1, 0, 1, 0, 1, 0]], 10 => [[0, 0, 1, 0, 1, 1, 0, 1, 0], [0, 1, 0, 0, 1, 1, 0, 1, 0]]])
8+
return Dict(Pair{Int64, Vector{BitVector}}[5 => [[1, 0, 0, 1, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 1, 0, 1]], 12 => [[0, 1, 0, 0, 1, 0, 1, 0, 1], [0, 0, 1, 0, 1, 0, 1, 0, 1]], 8 => [[0, 1, 0, 0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 1, 0, 0, 1, 0], [0, 1, 0, 0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0, 1, 0, 0]], 1 => [[1, 0, 0, 1, 0, 0, 0, 1, 0], [1, 0, 1, 0, 0, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 1, 0, 0], [1, 0, 1, 0, 0, 0, 1, 0, 0]], 0 => [[0, 1, 0, 1, 0, 0, 0, 1, 0], [0, 1, 0, 1, 0, 0, 1, 0, 0]], 6 => [[0, 1, 0, 1, 0, 1, 0, 0, 1]], 11 => [[1, 0, 1, 0, 1, 1, 0, 1, 0]], 9 => [[1, 0, 1, 0, 1, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0, 1, 0, 0]], 14 => [[0, 1, 0, 0, 1, 1, 0, 0, 1], [0, 0, 1, 0, 1, 1, 0, 0, 1]], 3 => [[1, 0, 0, 1, 0, 1, 0, 1, 0], [1, 0, 1, 0, 0, 1, 0, 1, 0]], 7 => [[1, 0, 0, 1, 0, 1, 0, 0, 1], [1, 0, 1, 0, 0, 1, 0, 0, 1]], 4 => [[0, 1, 0, 1, 0, 0, 1, 0, 1]], 13 => [[1, 0, 1, 0, 1, 0, 1, 0, 1]], 15 => [[1, 0, 1, 0, 1, 1, 0, 0, 1]], 2 => [[0, 1, 0, 1, 0, 1, 0, 1, 0]], 10 => [[0, 1, 0, 0, 1, 1, 0, 1, 0], [0, 0, 1, 0, 1, 1, 0, 1, 0]]])
99
end
1010

1111

@@ -18,24 +18,6 @@ function source_entry_to_configs(::Cross{true})
1818
end
1919

2020

21-
function mapped_entry_to_compact(::TShape{true})
22-
return Dict([0 => 0, 4 => 4, 5 => 5, 6 => 2, 2 => 2, 7 => 7, 3 => 3, 1 => 1])
23-
end
24-
25-
function source_entry_to_configs(::TShape{true})
26-
return Dict(Pair{Int64, Vector{BitVector}}[0 => [[0, 0, 0, 1, 0], [0, 0, 1, 0, 0]], 4 => [[0, 0, 1, 0, 1]], 5 => [], 6 => [[0, 1, 0, 0, 1]], 2 => [[0, 1, 0, 1, 0]], 7 => [], 3 => [[1, 1, 0, 1, 0]], 1 => [[1, 0, 0, 1, 0], [1, 0, 1, 0, 0]]])
27-
end
28-
29-
30-
function mapped_entry_to_compact(::TShape{false})
31-
return Dict([0 => 0, 4 => 0, 5 => 1, 6 => 6, 2 => 0, 7 => 7, 3 => 1, 1 => 1])
32-
end
33-
34-
function source_entry_to_configs(::TShape{false})
35-
return Dict(Pair{Int64, Vector{BitVector}}[0 => [[0, 0, 1, 0, 1, 0]], 4 => [[0, 0, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1]], 5 => [[1, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 1]], 6 => [[0, 1, 0, 1, 0, 1]], 2 => [[0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0]], 7 => [[1, 1, 0, 1, 0, 1]], 3 => [[1, 1, 0, 1, 0, 0], [1, 1, 0, 0, 1, 0]], 1 => [[1, 0, 1, 0, 1, 0]]])
36-
end
37-
38-
3921
function mapped_entry_to_compact(::Turn)
4022
return Dict([0 => 0, 2 => 0, 3 => 3, 1 => 0])
4123
end
@@ -68,7 +50,7 @@ function mapped_entry_to_compact(::BranchFix)
6850
end
6951

7052
function source_entry_to_configs(::BranchFix)
71-
return Dict(Pair{Int64, Vector{BitVector}}[0 => [[0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 1, 0]], 2 => [[0, 1, 0, 1, 0, 1]], 3 => [[1, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 1]], 1 => [[1, 0, 1, 0, 1, 0]]])
53+
return Dict(Pair{Int64, Vector{BitVector}}[0 => [[0, 0, 1, 0, 1, 0], [0, 1, 0, 0, 1, 0], [0, 1, 0, 1, 0, 0]], 2 => [[0, 1, 0, 1, 0, 1]], 3 => [[1, 0, 1, 0, 0, 1], [1, 0, 0, 1, 0, 1]], 1 => [[1, 0, 1, 0, 1, 0]]])
7254
end
7355

7456

src/gadgets.jl

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -161,52 +161,6 @@ end
161161
Base.size(::Cross{false}) = (4, 5)
162162
cross_location(::Cross{false}) = (2,3)
163163

164-
struct TShape{CON} <: CrossPattern end
165-
iscon(::TShape{CON}) where {CON} = CON
166-
# ⋅ ● ⋅ ⋅
167-
# ⋅ ● ⋅ ⋅
168-
# ● ● ⋅ ⋅
169-
# ⋅ ● ⋅ ⋅
170-
# ⋅ ● ⋅ ⋅
171-
function source_graph(::TShape{false})
172-
locs = [(3, 1), (1,2), (2,2), (3,2), (4,2), (5,2)]
173-
g = simplegraph([(2,3), (3,4), (4,5), (5,6)])
174-
return locs, g, [1,2,6]
175-
end
176-
177-
# ⋅ ● ⋅ ⋅
178-
# ⋅ ⋅ ● ⋅
179-
# ● ⋅ ● ⋅
180-
# ⋅ ⋅ ● ⋅
181-
# ⋅ ● ⋅ ⋅
182-
function mapped_graph(::TShape{false})
183-
locs = [(3, 1), (1,2), (2,3), (3,3), (4,3), (5,2)]
184-
locs, unitdisk_graph(locs, 1.5), [1, 2, 6]
185-
end
186-
Base.size(::TShape{false}) = (5, 4)
187-
cross_location(::TShape{false}) = (3,2)
188-
189-
#
190-
#
191-
# ◆ ●
192-
#
193-
function source_graph(::TShape{true})
194-
locs = [(3, 1), (1,2), (2,2), (3,2), (4,2)]
195-
g = simplegraph([(1,5), (2,3), (3,4), (4,5)])
196-
return locs, g, [1,2,5]
197-
end
198-
#
199-
#
200-
# ● ●
201-
#
202-
function mapped_graph(::TShape{true})
203-
locs = [(3, 1), (1,2), (2,3), (3,3), (4,2)]
204-
locs, unitdisk_graph(locs, 1.5), [1, 2, 5]
205-
end
206-
connect_locations(::TShape{true}) = [(3, 1), (4,2)]
207-
Base.size(::TShape{true}) = (4, 4)
208-
cross_location(::TShape{true}) = (3,2)
209-
210164
struct Turn <: CrossPattern end
211165
iscon(::Turn) = false
212166
# ⋅ ● ⋅ ⋅
@@ -450,7 +404,7 @@ end
450404
for T in [:Cross, :Turn, :WTurn, :Branch, :BranchFix, :BranchFixB]
451405
@eval mis_overhead(p::$T) = -1
452406
end
453-
for T in [:TrivialTurn, :TShape, :TCon]
407+
for T in [:TrivialTurn, :TCon]
454408
@eval mis_overhead(p::$T) = 0
455409
end
456410

src/mapping.jl

Lines changed: 18 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,6 @@ Base.:(==)(ug::UGrid, ug2::UGrid) = ug.lines == ug2.lines && ug.content == ug2.c
2929
padding(ug::UGrid) = ug.padding
3030
coordinates(ug::UGrid) = [ci.I for ci in findall(!iszero, ug.content)]
3131

32-
function plain_ugrid(n::Int; padding=2)
33-
@assert padding >= 2
34-
s = 4
35-
N = (n-1)*s+1+2*padding
36-
u = zeros(Int, N, N)
37-
for j=n-1:-1:0
38-
for i=0:n-1
39-
# two extra rows
40-
if 1<=i<=2
41-
u[i+1, s*j+1+padding] += 1
42-
end
43-
# others
44-
if i<=j
45-
u[max(2+padding, s*i-s+4+padding):2:s*i+2+padding, s*j+1+padding] .+= 1
46-
i!=0 && (u[s*i-s+3+padding:2:s*i+padding+1, s*j+1+padding] .+= 1)
47-
else
48-
u[s*j+3+padding, max(1+padding, s*i-s+1+padding):s*i+padding] .+= 1
49-
end
50-
end
51-
end
52-
return UGrid(collect(1:n), padding, u)
53-
end
54-
5532
function Graphs.SimpleGraph(ug::UGrid)
5633
if any(x->abs(x)>1, ug.content)
5734
error("This mapping is not done yet!")
@@ -72,16 +49,6 @@ function print_ugrid(io::IO, content::AbstractMatrix)
7249
end
7350
end
7451
Base.copy(ug::UGrid) = UGrid(ug.lines, ug.padding, copy(ug.content))
75-
function crossat(ug::UGrid, i, j)
76-
s = 4
77-
return (i-1)*s+3+ug.padding, (j-1)*s+1+ug.padding
78-
end
79-
function Graphs.add_edge!(ug::UGrid, i, j)
80-
I, J = crossat(ug, i, j)
81-
ug.content[I+1, J] *= -1
82-
ug.content[I, J-1] *= -1
83-
return ug
84-
end
8552

8653
function showitem(io, x)
8754
if x == 1
@@ -103,7 +70,7 @@ end
10370
# 1. check if the resulting graph is a unit-disk
10471
# 2. other simplification rules
10572
function apply_crossing_gadgets!(ug::UGrid, ruleset=(
106-
Cross{false}(), Cross{true}(), TShape{false}(), TShape{true}(),
73+
Cross{false}(),
10774
Turn(), WTurn(), Branch(), BranchFix(), TCon(), TrivialTurn(),
10875
RotatedGadget(TCon(), 1), ReflectedGadget(Cross{true}(), "y"),
10976
ReflectedGadget(TrivialTurn(), "y"), BranchFixB(),
@@ -115,7 +82,7 @@ function apply_crossing_gadgets!(ug::UGrid, ruleset=(
11582
for i=1:n
11683
matched = false
11784
for pattern in ruleset
118-
x, y = crossat2(ug, i, j) .- cross_location(pattern) .+ (1,1)
85+
x, y = crossat(ug, i, j) .- cross_location(pattern) .+ (1,1)
11986
if match(pattern, ug.content, x, y)
12087
apply_gadget!(pattern, ug.content, x, y)
12188
push!(tape, (pattern, x, y))
@@ -142,17 +109,6 @@ function unapply_gadgets!(ug::UGrid, tape, configurations)
142109
return ug, cfgs
143110
end
144111

145-
function unitdisk_graph(locs::AbstractVector, unit::Real)
146-
n = length(locs)
147-
g = SimpleGraph(n)
148-
for i=1:n, j=i+1:n
149-
if sum(abs2, locs[i] .- locs[j]) < unit ^ 2
150-
add_edge!(g, i, j)
151-
end
152-
end
153-
return g
154-
end
155-
156112
# returns a vector of configurations
157113
function _map_config_back(s::Pattern, config)
158114
d1 = mapped_entry_to_compact(s)
@@ -205,66 +161,6 @@ function map_config_copyback!(ug::UGrid, c::AbstractMatrix)
205161
end
206162
return res
207163
end
208-
function map_config_copyback!(n::Int, c::AbstractMatrix, padding::Int)
209-
store = copy(c)
210-
s = 4
211-
res = zeros(Int, n)
212-
for j=1:n
213-
for i=1:(n-1)*s + 3
214-
J = (j-1)*s + 1 + padding
215-
if i > (j-1)*s+4
216-
J += i-(j-1)*s-4
217-
I = (j-1)*s + 3 + padding
218-
# bits belong to horizontal lines
219-
if i%s != 0 || (safe_get(c, I, J-1) == 0 && safe_get(c, I, J+1) == 0)
220-
if store[I, J] != 0
221-
res[j] += 1
222-
store[I, J] -= 1
223-
end
224-
end
225-
else
226-
I = i-1 + padding
227-
# bits belong to vertical lines
228-
if i%s != 0 || (safe_get(c, I-1, J) == 0 && safe_get(c, I+1, J) == 0)
229-
if store[I, J] != 0
230-
res[j] += 1
231-
store[I, J] -= 1
232-
end
233-
end
234-
end
235-
end
236-
end
237-
return map(res) do x
238-
if x == 2*(n-1)+1
239-
false
240-
elseif x == 2*(n-1) + 2
241-
true
242-
else
243-
error("mapping back fail! got $x (overhead = $((n-1)*2))")
244-
end
245-
end
246-
end
247-
248-
export is_independent_set
249-
function is_independent_set(g::SimpleGraph, config)
250-
for e in edges(g)
251-
if config[e.src] == config[e.dst] == 1
252-
return false
253-
end
254-
end
255-
return true
256-
end
257-
258-
function embed_graph(g::SimpleGraph)
259-
ug = plain_ugrid(nv(g))
260-
for e in edges(g)
261-
add_edge!(ug, e.src, e.dst)
262-
end
263-
return ug
264-
end
265-
266-
267-
##################### reordered mapping ###################
268164

269165
function remove_order(g::AbstractGraph, vertex_order::AbstractVector{Int})
270166
addremove = [Int[] for _=1:nv(g)]
@@ -355,20 +251,31 @@ function ugrid(g::SimpleGraph, vertex_order::AbstractVector{Int}; padding=2, nro
355251
return UGrid(copylines, padding, u)
356252
end
357253

358-
function crossat2(ug::UGrid, v, w)
254+
function crossat(ug::UGrid, v, w)
359255
i, j = findfirst(x->x.vertex==v, ug.lines), findfirst(x->x.vertex==w, ug.lines)
360256
i, j = minmax(i, j)
361257
hslot = ug.lines[i].hslot
362258
s = 4
363259
return (hslot-1)*s+2+ug.padding, (j-1)*s+1+ug.padding
364260
end
365261

366-
export embed_graph2
367-
function embed_graph2(g::SimpleGraph; vertex_order_optimizer=Greedy())
368-
L = pathwidth(g, vertex_order_optimizer)
262+
"""
263+
embed_graph(g::SimpleGraph; vertex_order=Greedy())
264+
265+
Embed graph `g` into a unit disk grid. The `vertex_order` can be a vector or one of the following inputs
266+
267+
* `Greedy()` fast but non-optimal.
268+
* `Branching()` slow but optimal.
269+
"""
270+
function embed_graph(g::SimpleGraph; vertex_order=Greedy())
271+
if vertex_order isa AbstractVector
272+
L = PathDecomposition.Layout(g, collect(vertex_order))
273+
else
274+
L = pathwidth(g, vertex_order)
275+
end
369276
ug = ugrid(g, L.vertices; padding=2, nrow=L.vsep+1)
370277
for e in edges(g)
371-
I, J = crossat2(ug, e.src, e.dst)
278+
I, J = crossat(ug, e.src, e.dst)
372279
@assert ug.content[I, J-1] == 1
373280
ug.content[I, J-1] *= -1
374281
if ug.content[I-1, J] == 1

src/utils.jl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export is_independent_set, unitdisk_graph
2+
13
function simplegraph(edgelist::AbstractVector{Tuple{Int,Int}})
24
nv = maximum(x->max(x...), edgelist)
35
g = SimpleGraph(nv)
@@ -28,4 +30,25 @@ function reflectdiag(loc)
2830
end
2931
function reflectoffdiag(loc)
3032
loc[2], loc[1]
31-
end
33+
end
34+
35+
function unitdisk_graph(locs::AbstractVector, unit::Real)
36+
n = length(locs)
37+
g = SimpleGraph(n)
38+
for i=1:n, j=i+1:n
39+
if sum(abs2, locs[i] .- locs[j]) < unit ^ 2
40+
add_edge!(g, i, j)
41+
end
42+
end
43+
return g
44+
end
45+
46+
function is_independent_set(g::SimpleGraph, config)
47+
for e in edges(g)
48+
if config[e.src] == config[e.dst] == 1
49+
return false
50+
end
51+
end
52+
return true
53+
end
54+

test/extracting_results.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ using UnitDiskMapping, Test
22
using GraphTensorNetworks
33

44
@testset "map results back" begin
5-
for s in (Cross{false}(), Cross{true}(), TShape{true}(), TShape{false}(), Turn())
5+
for s in (Cross{false}(), Cross{true}(), Turn(), TCon(),
6+
WTurn(), Branch(), BranchFix(), TrivialTurn(),
7+
RotatedGadget(TCon(), 1), ReflectedGadget(Cross{true}(), "y"),
8+
ReflectedGadget(TrivialTurn(), "y"), BranchFixB(),
9+
ReflectedGadget(RotatedGadget(TCon(), 1), "y"),)
610
_, g0, pins0 = source_graph(s)
711
locs, g, pins = mapped_graph(s)
812
d1 = mapped_entry_to_compact(s)

test/gadgets.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using Graphs
2121
end
2222
return true, x
2323
end
24-
for s in (Cross{false}(), Cross{true}(), TShape{true}(), TShape{false}(), Turn(),
24+
for s in (Cross{false}(), Cross{true}(), Turn(),
2525
TrivialTurn(), Branch(), TCon(), BranchFixB(), BranchFix(), WTurn(), RotatedGadget(TCon(), 1),
2626
ReflectedGadget(TCon(), "x")
2727
)

0 commit comments

Comments
 (0)