diff --git a/LICENSE.md b/LICENSE.md index b35570c..b561db5 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -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 diff --git a/src/QuCmp.jl b/src/QuCmp.jl index 5afc76d..1eb3d85 100644 --- a/src/QuCmp.jl +++ b/src/QuCmp.jl @@ -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 diff --git a/src/utils/LogicExpr.jl b/src/utils/LogicExpr.jl new file mode 100644 index 0000000..30c4fce --- /dev/null +++ b/src/utils/LogicExpr.jl @@ -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)<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 diff --git a/src/utils/math.jl b/src/utils/math.jl new file mode 100644 index 0000000..66a78dc --- /dev/null +++ b/src/utils/math.jl @@ -0,0 +1,5 @@ +include("matrix.jl") + +function normalize!(vec::AbstractVector) + return vec[:]=vec/norm(vec) +end diff --git a/src/utils/matrix.jl b/src/utils/matrix.jl new file mode 100644 index 0000000..673b917 --- /dev/null +++ b/src/utils/matrix.jl @@ -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