Skip to content

Commit

Permalink
Add Boost functions (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo authored Jun 5, 2018
1 parent 163386b commit b021145
Show file tree
Hide file tree
Showing 19 changed files with 707 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/Blocks/Composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,4 @@ include("ChainBlock.jl")
include("KronBlock.jl")
include("Control.jl")
include("Roller.jl")
include("Repeated.jl")
42 changes: 42 additions & 0 deletions src/Blocks/Repeated.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export RepeatedBlock

mutable struct RepeatedBlock{N, T, GT<:MatrixBlock} <: CompositeBlock{N, T}
block::GT
lines::Vector{Int}

function RepeatedBlock{N}(block::GT) where {N, M, T, GT <: MatrixBlock{M, T}}
new{N, T, GT}(block, Vector{Int}(1:N))
end

function RepeatedBlock{N}(block::GT, lines::Vector{Int}) where {N, M, T, GT <: MatrixBlock{M, T}}
new{N, T, GT}(block, lines)
end
end

start(c::RepeatedBlock) = start(c.lines)

function next(c::RepeatedBlock, st)
line, st = next(c.lines, st)
(line, c.block), st
end

done(c::RepeatedBlock, st) = done(c.lines, st)
length(c::RepeatedBlock) = length(c.lines)
eachindex(c::RepeatedBlock) = eachindex(c.lines)
getindex(c::RepeatedBlock, index) = c.block
blocks(rb::RepeatedBlock) = [rb.block]

copy(x::RepeatedBlock) = RepeatedBlock

dispatch!(f::Function, rb::RepeatedBlock, params...) = dispatch!(f, rb.block, params...)

function hash(rb::RepeatedBlock, h::UInt)
hashkey = hash(objectid(rb), h)
hashkey = hash(rb.block, hashkey)
hashkey = hash(rb.lines, hashkey)
hashkey
end

function ==(lhs::RepeatedBlock{N, T, GT}, rhs::RepeatedBlock{N, T, GT}) where {N, T, GT}
(lhs.block == rhs.block) && (lhs.lines == rhs.lines)
end
24 changes: 24 additions & 0 deletions src/Boost/Boost.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Boost

using ..Registers
using ..Blocks
using ..Intrinsics
using ..LuxurySparse

import ..Blocks: mat, apply!

export general_controlled_gates
export xgate, ygate, zgate
export cxgate, cygate, czgate
export controlled_U1

export xapply!, yapply!, zapply!
export cxapply!, cyapply!, czapply!

include("gates.jl")
include("applys.jl")

include("Control.jl")
include("Repeated.jl")

end
3 changes: 3 additions & 0 deletions src/Boost/Control.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function mat(ctrl::ControlBlock{BT, N, T}) where {N, T, BT <: MatrixBlock{1, T}}
controlled_U1(N, mat(ctrl.block), ctrl.ctrl_qubits, ctrl.addr)
end
44 changes: 44 additions & 0 deletions src/Boost/Repeated.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
GATES = [:X, :Y, :Z]

for (G, MATFUNC) in zip(GATES, [:xgate, :ygate, :zgate])
GGate = Symbol(G, :Gate)
@eval function mat(rb::RepeatedBlock{N, MT, GT}) where {N, MT, GT <: $GGate}
$MATFUNC(MT, N, rb.lines)
end
end

function apply!(r::AbstractRegister{1}, rb::RepeatedBlock{N, T, <:XGate}) where {N, T}
if nremains(r) == 0
xapply!(vec(state(r)), rb.lines)
else
xapply!(state(r), rb.lines)
end
end

function apply!(r::AbstractRegister{1}, rb::RepeatedBlock{N, T, <:YGate}) where {N, T}
if nremains(r) == 0
yapply!(vec(state(r)), rb.lines)
else
yapply!(state(r), rb.lines)
end
end

function apply!(r::AbstractRegister{1}, rb::RepeatedBlock{N, T, <:ZGate}) where {N, T}
if nremains(r) == 0
zapply!(vec(state(r)), rb.lines)
else
zapply!(state(r), rb.lines)
end
end

function apply!(r::AbstractRegister, rb::RepeatedBlock{N, T, <:XGate}) where {N, T}
xapply!(state(r), rb.lines)
end

function apply!(r::AbstractRegister, rb::RepeatedBlock{N, T, <:YGate}) where {N, T}
yapply!(state(r), rb.lines)
end

function apply!(r::AbstractRegister, rb::RepeatedBlock{N, T, <:ZGate}) where {N, T}
zapply!(state(r), rb.lines)
end
271 changes: 271 additions & 0 deletions src/Boost/applys.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
function xapply!(state::Matrix{T}, bits::Ints) where T
if length(bits) == 0
return state
end
mask = bmask(bits...)
M, N = size(state)
do_mask = bmask(bits[1])
@simd for b = basis(state)
local temp::T
local i_::Int
if testany(b, do_mask)
i = b+1
i_ = flip(b, mask) + 1
@inbounds for c = 1:N
temp = state[i, c]
state[i, c] = state[i_, c]
state[i_, c] = temp
end
end
end
state
end

function xapply!(state::Vector{T}, bits::Ints) where T
if length(bits) == 0
return state
end
mask = bmask(bits...)
do_mask = bmask(bits[1])
@simd for b = basis(state)
local temp::T
local i_::Int
@inbounds if testany(b, do_mask)
i = b+1
i_ = flip(b, mask) + 1
temp = state[i]
state[i] = state[i_]
state[i_] = temp
end
end
state
end

function yapply!(state::Vector{T}, bits::Ints) where T
if length(bits) == 0
return state
end
mask = bmask(bits...)
do_mask = bmask(bits[1])
factor = T(-im^length(bits))

@inbounds @simd for b = basis(state)
local temp::T
local i_::Int
if testany(b, do_mask)
i = b+1
i_ = flip(b, mask) + 1
temp = state[i]
if count_ones(b&mask)%2 == 1
state[i] = -state[i_]*factor
state[i_] = temp*factor
else
state[i] = state[i_]*factor
state[i_] = -temp*factor
end
end
end
state
end

function yapply!(state::Matrix{T}, bits::Ints) where T
if length(bits) == 0
return state
end
mask = bmask(bits...)
do_mask = bmask(bits[1])
factor = T(-im^length(bits))
M, N = size(state)

@simd for b = basis(state)
local temp::T
local factor_::T
local i_::Int
if testany(b, do_mask)
i = b+1
i_ = flip(b, mask) + 1
if count_ones(b&mask)%2 == 1
factor_ = -factor
else
factor_ = factor
end
@simd for c = 1:N
temp = state[i, c]
@inbounds state[i, c] = state[i_, c]*factor_
@inbounds state[i_, c] = -temp*factor_
end
end
end
state
end

function zapply!(state::Vector{T}, bits::Ints) where T
mask = bmask(bits...)
@simd for b in basis(state)
if count_ones(b&mask)%2==1
@inbounds state[b+1] *= -1
end
end
state
end
function zapply!(state::Matrix{T}, bits::Ints) where T
mask = bmask(bits...)
M, N = size(state)
for b in basis(state)
if count_ones(b&mask)%2==1
@simd for j = 1:N
@inbounds state[b+1, j] *= -1
end
end
end
state
end

function zapply!(state::Vector{T}, bit::Int) where T
mask = bmask(bit)
@simd for b in basis(state)
if testany(b, mask)
@inbounds state[b+1] *= -1
end
end
state
end
function zapply!(state::Matrix{T}, bit::Int) where T
mask = bmask(bit)
M, N = size(state)
for b in basis(state)
if testany(b, mask)
@simd for j = 1:N
@inbounds state[b+1, j] *= -1
end
end
end
state
end

function cxapply!(state::Matrix{T}, b1::Ints, b2::Ints) where T
do_mask = bmask(b1..., b2[1])
mask2 = bmask(b2...)
M, N = size(state)

@simd for b = basis(state)
local temp::T
local i_::Int
if testall(b, do_mask)
i = b+1
i_ = flip(b, mask2) + 1
@inbounds for c = 1:N
temp = state[i, c]
state[i, c] = state[i_, c]
state[i_, c] = temp
end
end
end
state
end

function cxapply!(state::Vector{T}, b1::Ints, b2::Ints) where T
do_mask = bmask(b1..., b2[1])
mask2 = bmask(b2...)

@simd for b = basis(state)
local temp::T
local i_::Int
@inbounds if testall(b, do_mask)
i = b+1
i_ = flip(b, mask2) + 1
temp = state[i]
state[i] = state[i_]
state[i_] = temp
end
end
state
end

function czapply!(state::Vector{T}, b1::Ints, b2::Ints) where T
mask2 = bmask(b2)
step = 1<<(b1-1)
step_2 = 1<<b1
for j = step:step_2:length(state)-1
@simd for i = j+1:j+step
if testall(i-1, mask2)
@inbounds state[i] *= -1
end
end
end
state
end

function czapply!(state::Matrix{T}, b1::Ints, b2::Ints) where T
mask2 = bmask(b2)
M, N = size(state)
step = 1<<(b1-1)
step_2 = 1<<b1
for j = step:step_2:M-1
@simd for i = j+1:j+step
if testall(i-1, mask2)
@simd for k = 1:N
@inbounds state[i, k] *= -1
end
end
end
end
state
end

function cyapply!(state::Vector{T}, b1::Ints, b2::Ints) where T
mask2 = bmask(b2)
mask = bmask(b1, b2)

step = 1<<(b1-1)
step_2 = 1<<b1
for j = step:step_2:length(state)-1
local temp::T
local i_::Int
@simd for b = j:j+step-1
@inbounds if testall(b, mask2)
i = b+1
i_ = flip(b, mask2) + 1
if testall(b, mask2)
factor = T(im)
else
factor = T(-im)
end
temp = state[i]
state[i] = state[i_]*factor
state[i_] = -temp*factor
end
end
end
state
end

function cyapply!(state::Matrix{T}, b1::Ints, b2::Ints) where T
mask2 = bmask(b2)
mask = bmask(b1, b2)
M, N = size(state)

step = 1<<(b1-1)
step_2 = 1<<b1
for j = step:step_2:M-1
local temp::T
local i_::Int
@simd for b = j:j+step-1
if testall(b, mask2)
i = b+1
i_ = flip(b, mask2) + 1
if testall(b, mask2)
factor = T(im)
else
factor = T(-im)
end
@inbounds @simd for c = 1:N
temp = state[i, c]
state[i, c] = state[i_, c]*factor
state[i_, c] = -temp*factor
end
end
end
end
state
end
Loading

0 comments on commit b021145

Please sign in to comment.