Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clone and merge candidates #31

Merged
merged 5 commits into from
Jul 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/transformations.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export smooth, forget, propagate_constants, deepcopy, condition, replace_node,
split, clone, merge, split_candidates, random_split, split_step, struct_learn
split, clone, merge, split_candidates, random_split, split_step, struct_learn,
clone_candidates

"""
Create an equivalent smooth circuit from the given circuit.
Expand Down Expand Up @@ -253,6 +254,43 @@ function split_candidates(circuit::Node)::Tuple{Vector{Tuple{Node, Node}}, Dict{
candidates, scope
end

function clone_candidates(circuit::Node)::Dict{Node, Vector{Node}}
candidates = Dict{Node, Vector{Node}}()
parents = Dict{Node, Vector{Node}}()

f_con(n) = begin
false
end
f_lit(n) = begin
false
end
f_a(n, cv) = begin
for c in children(n)
if !(GateType(c) isa ⋁Gate)
continue
end

if c in keys(parents)
push!(parents[c], n)
else
parents[c] = [n]
end
end
false
end
f_o(n, cv) = begin
if !(n in keys(parents))
parents[n] = []
end
true
end

foldup_aggregate(circuit, f_con, f_lit, f_a, f_o, Bool)

# Find candidates
candidates = filter(p->(length(last(p)) == 2), parents) # Set of AND gates shared by exactly 2 OR gates
candidates
end

"""
Randomly picking egde and variable from candidates
Expand Down
18 changes: 17 additions & 1 deletion test/transformations_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,20 @@ end
# TODO
# @test isstructdecomposable(c1)
# @test isdeterministic(c1)
end
end

@testset "Clone Candidates" begin
lit1 = compile(PlainLogicCircuit, Lit(1))
litn1 = compile(PlainLogicCircuit, - Lit(1))
lit2 = compile(PlainLogicCircuit, Lit(2))
litn2 = compile(PlainLogicCircuit, - Lit(2))
or = lit1 | litn1
and1 = lit2 & or
and2 = or & litn2
c1 = root = and1 | and2

cands = clone_candidates(c1)
candidates = [(k, v[1], v[2]) for (k,v) in cands]

@assert (or, and1, and2) in candidates
end