Skip to content
This repository was archived by the owner on Jul 31, 2020. It is now read-only.
Open
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
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The QuCmp.jl package is licensed under the MIT "Expat" License:

> Copyright (c) 2016: Roger-Luo.
> Copyright (c) 2016: Contributors to QuCmp.jl.
>
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
Expand Down
10 changes: 10 additions & 0 deletions src/QuCmp.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
module QuCmp

<<<<<<< HEAD
abstract QuComput

include("const.jl")
include("utils/LogicExpr.jl")
include("utils/math.jl")
include("Adiabatic/Adiabatic.jl")

=======
# package code goes here
>>>>>>> ad742772d3e82260d55f64ba87c31ee32eb94575

end # module
208 changes: 208 additions & 0 deletions src/utils/LogicExpr.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
###################################################################
#
# This file generates SAT's Instance in classical method for tests
# in quantum computing.
# Ref: arxiv:quant-ph/0007071v1
#
###################################################################



abstract AbstractBits
abstract AbstractClause{N}

immutable Bits <: AbstractBits
value::UInt32
end

function call(b::Bits,n::Integer)
@assert n>0
return (b.value>>(n-1))&1
end

"""
TruthTable Example:

|bits value | truth value|
|-----------|------------|
| 000 | 0 |
| 001 | 1 |
| 010 | 0 |
| 011 | 1 |
| 100 | 0 |
| 101 | 1 |
| 110 | 0 |
| 111 | 0 |

TruthTable(0b00101010)
"""
immutable TruthTable
value::UInt32
end

function call(truthtable::TruthTable,index::Integer)
return (truthtable.value>>index)&1
end

immutable Clause{N} <: AbstractClause{N}
table::TruthTable
ids::AbstractVector{Integer}

function Clause(table::TruthTable,ids::AbstractVector{Integer})
@assert length(ids)==N
sort!(ids)
new(table,ids)
end
end

Clause(table::TruthTable,ids::Integer...) = Clause{length(ids)}(table,[ids...])
Clause(num::Integer,table::TruthTable,ids::Integer...) = Clause{num}(table,[ids...])
Clause(num::Integer,table::Integer,ids::Integer...) = Clause{num}(TruthTable(table),[ids...])

function call{N}(clause::Clause{N},assign::Integer)
return clause.table(assign)
end

################################################################################
#
# Clauses in exact cover problem
#
################################################################################

type ECClause{N} <: AbstractClause{N}
ids::Vector{Int}

function ECClause(ids::Vector{Int})
@assert length(ids) == N
sort!(ids)
new(ids)
end
end

ECClause(ids::Vector{Int}) = ECClause{length(ids)}(ids)
ECClause(ids::Integer...) = ECClause{length(ids)}([ids...])

function call{N}(c::ECClause{N},assign::Integer)
res = 0
for i = 1:N
res += assign&1
assign = assign>>1
end
return Int(res==1)
end

function save{N}(io::IO,clause::ECClause{N})
write(io,clause.ids[1])
for id in clause.ids[2:end]
write(io,"\t")
write(io,id)
end
write(io,"\n")
end

"""
Instance is a collection of M clauses:

~~~TeX
C_1 Λ C_2 Λ... C_M
~~~

construct an instance:

- `num` is the number of bits
- `clauses` is the collection of clause
"""
type Instance{M,N}
c::AbstractVector{AbstractClause{N}}
end

Instance{N}(num::Integer,clauses::AbstractVector{ECClause{N}}) = Instance{num,N}(clauses)
Instance{N}(num::Integer,clause::ECClause{N},clauses::ECClause{N}...) = Instance(num,[clause,clauses...])
Instance{N}(num::Integer,clauses::AbstractVector{Clause{N}}) = Instance{num,N}(clauses)
Instance{N}(num::Integer,clause::Clause{N},clauses::Clause{N}...) = Instance(num,[clause,clauses...])

function call{M,N}(clauses::Instance{M,N},assign::Bits)
res = 1
for clause in clauses.c
assignment = 0
digit = 0
for id in clause.ids
assignment += assign(id)<<digit
digit += 1
end
res &= clause(assignment)
end
return res
end

function show{M,N}(io::IO,ins::Instance{M,N})
for clause in ins.c[1:end-1]
print(io,clause.ids)
print(io,"Λ")
end
print(io,ins.c[end].ids)
print(io,"\n")
end

show{M,N}(ins::Instance{M,N}) = show(STDOUT,ins)

function save{M,N}(io::IO,ins::Instance{M,N})
for clause in ins.c
save(io,clause)
end
end


function AssignNum{M,N}(instance::Instance{M,N})
count = 0
for i = 0:2^M-1
if instance(Bits(i)) == 1
count+=1
end
end
return count
end

import Base: push!

function push!{M,N}(instance::Instance{M,N},clause::ECClause{N})
push!(instance.c,clause)
end

################################################################################
#
# generate instance with only one assignment satisfied
#
################################################################################

function engine(n::Integer,N::Integer,maxtry=1000)
list = collect(1:n)
ids = (list|>shuffle)[1:N]

clauses = [ECClause(ids)]
ins = Instance(n,clauses)

for i = 1:maxtry
pre_ids = ids
ids = (list|>shuffle)[1:N]
if sort!(ids) != sort!(pre_ids)
push!(ins,ECClause(ids))
end

if AssignNum(ins)<2
return ins
end
end

return nothing
end

function generate(n::Integer,maxiter=1000;maxbits=16,N=3)
for i=1:maxiter
t = engine(n,N)
t === nothing || return t
end
warn("may not have an assignment\n")
end

export AbstractBits,AbstractClause,Bits,Clause,ECClause,TruthTable,save,show,generate,call,Instance
5 changes: 5 additions & 0 deletions src/utils/math.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include("matrix.jl")

function normalize!(vec::AbstractVector)
return vec[:]=vec/norm(vec)
end
15 changes: 15 additions & 0 deletions src/utils/matrix.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#trotter expansion
function trotter(A::AbstractMatrix,B::AbstractMatrix,P::Int64)
return (expm(full(A/(2*P)))*expm(full(B/P))*expm(full(A/(2*P))))^P
end

function (⊗)(A::AbstractMatrix,B::AbstractMatrix)
@assert size(A)[1]==size(A)[2]
@assert size(B)[1]==size(B)[2]

return kron(A,B)
end

function (⊕)(A::AbstractMatrix,B::AbstractMatrix)
return full(blkdiag(sparse(A),sparse(B)))
end