Skip to content
This repository has been archived by the owner on Dec 18, 2021. It is now read-only.

Commit

Permalink
rm matvec & furthur optimize performance (#71)
Browse files Browse the repository at this point in the history
* rm matvec & furthur optimize performance

* Update src/instruct.jl

Co-authored-by: Leo <cacate0129@gmail.com>

* Update src/instruct.jl

Co-authored-by: Leo <cacate0129@gmail.com>

* Update src/instruct.jl

Co-authored-by: Leo <cacate0129@gmail.com>

* add test

Co-authored-by: Leo <cacate0129@gmail.com>
  • Loading branch information
Roger-luo and GiggleLiu committed Sep 26, 2020
1 parent 517f68d commit 4946146
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
73 changes: 59 additions & 14 deletions src/instruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@
using YaoBase, BitBasis, LuxurySparse, StaticArrays
export instruct!

function YaoBase.instruct!(reg::ArrayReg, operator, args...; kwargs...)
instruct!(matvec(reg.state), operator, args...; kwargs...)
return reg
function YaoBase.instruct!(r::ArrayReg, op, locs::Tuple, control_locs::Tuple, control_bits::Tuple)
instruct!(r.state, op, locs, control_locs, control_bits)
end

function YaoBase.instruct!(r::ArrayReg, op, locs::Tuple)
instruct!(r.state, op, locs)
end

function YaoBase.instruct!(r::ArrayReg, op, locs::Tuple, control_locs::Tuple, control_bits::Tuple, theta::Number)
instruct!(r.state, op, locs, control_locs, control_bits, theta)
end

function YaoBase.instruct!(r::ArrayReg, op, locs::Tuple, theta::Number)
instruct!(r.state, op, locs, theta)
end

"""
Expand Down Expand Up @@ -326,19 +337,53 @@ for G in [:Rx, :Ry, :Rz, :CPHASE]
instruct!(state, m, locs, control_locs, control_bits)
return state
end

@eval function YaoBase.instruct!(
state::AbstractVecOrMat{T},
g::Val{$(QuoteNode(G))},
locs::NTuple{N, Int},
theta::Number
) where {T, N}
m = rot_mat(T, g, theta)
instruct!(state, m, locs)
return state
end
end # for

@inline function YaoBase.instruct!(
state::AbstractVecOrMat{T},
::Val{:Rx},
(loc, )::Tuple{Int},
theta::Number
) where {T, N}
b, a = sincos(theta / 2)
instruct_kernel(state, loc, 1 << (loc - 1), 1 << loc, a, -im*b, -im*b, a)
return state
end

function YaoBase.instruct!(
state::AbstractVecOrMat{T},
::Val{:Ry},
(loc, )::Tuple{Int},
theta::Number
) where {T, N}
b, a = sincos(theta / 2)
instruct_kernel(state, loc, 1 << (loc - 1), 1 << loc, a, -b, b, a)
return state
end

function YaoBase.instruct!(
state::AbstractVecOrMat{T},
::Val{:Rz},
(loc, )::Tuple{Int},
theta::Number
) where {T, N}
a = exp(-im * theta / 2)
instruct_kernel(state, loc, 1 << (loc - 1), 1 << loc, a, zero(T), zero(T), a')
return state
end

function YaoBase.instruct!(
state::AbstractVecOrMat{T},
::Val{:CPHASE},
locs::NTuple{N, Int},
theta::Number
) where {T, N}
m = rot_mat(T, Val(:CPHASE), theta)
instruct!(state, m, locs)
return state
end


# forward single gates
function YaoBase.instruct!(
state::AbstractVecOrMat{T},
Expand Down
8 changes: 8 additions & 0 deletions test/instruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,11 @@ end
@test instruct!(copy(st), Val(G), ()) == st
end
end

@testset "register insterface" begin
r = rand_state(5)
@test instruct!(copy(r), Val(:X), (2, )) instruct!(copy(r.state), Val(:X), (2, ))
@test instruct!(copy(r), Val(:X), (2, ), (3, ), (1, )) instruct!(copy(r.state), Val(:X), (2, ), (3, ), (1, ))
@test instruct!(copy(r), Val(:Rx), (2, ), 0.5) instruct!(copy(r.state), Val(:Rx), (2, ), 0.5)
@test instruct!(copy(r), Val(:Rx), (2, ), (3, ), (1, ), 0.5) instruct!(copy(r.state), Val(:Rx), (2, ), (3, ), (1, ), 0.5)
end

0 comments on commit 4946146

Please sign in to comment.