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

rm matvec & furthur optimize performance #71

Merged
merged 6 commits into from
Sep 26, 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
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)
Roger-luo marked this conversation as resolved.
Show resolved Hide resolved
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