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

Commit

Permalink
Merge 58e6b47 into eb4def4
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed Sep 26, 2020
2 parents eb4def4 + 58e6b47 commit edfa850
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 92 deletions.
100 changes: 23 additions & 77 deletions src/instruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,6 @@ function YaoBase.instruct!(
)
end

function YaoBase.instruct!(
state::AbstractVecOrMat{T1},
U1::AbstractMatrix{T2},
loc::Int,
) where {T1,T2}
@warn "Element Type Mismatch: register $(T1), operator $(T2). Converting operator to match, this may cause performance issue"
return instruct!(state, copyto!(similar(U1, T1), U1), loc)
end

function YaoBase.instruct!(
state::AbstractVecOrMat{T1},
U1::SDPermMatrix{T2},
loc::Int,
) where {T1,T2}
@warn "Element Type Mismatch: register $(T1), operator $(T2). Converting operator to match, this may cause performance issue"
return instruct!(state, copyto!(similar(U1, T1), U1), loc)
end

function YaoBase.instruct!(
state::AbstractVecOrMat{T1},
U1::SDDiagonal{T2},
loc::Int,
) where {T1,T2}
@warn "Element Type Mismatch: register $(T1), operator $(T2). Converting operator to match, this may cause performance issue"
return instruct!(state, copyto!(similar(U1, T1), U1), loc)
end

function _prepare_instruct(
state,
U,
Expand Down Expand Up @@ -161,19 +134,13 @@ function _instruct!(
return state
end

YaoBase.instruct!(state::AbstractVecOrMat, U::IMatrix, locs::NTuple{N,Int}) where {N} =
state
YaoBase.instruct!(state::AbstractVecOrMat, U::IMatrix, locs::Int) = state
YaoBase.instruct!(state::AbstractVecOrMat, U::IMatrix, locs::NTuple{N,Int}) where {N} = state
YaoBase.instruct!(state::AbstractVecOrMat, U::IMatrix, locs::Tuple{Int}) = state

# one-qubit instruction
YaoBase.instruct!(state::AbstractVecOrMat, g::AbstractMatrix, locs::Tuple{Int}) =
instruct!(state, g, locs...)

function YaoBase.instruct!(
state::AbstractVecOrMat{T},
U1::AbstractMatrix{T},
loc::Int,
(loc, )::Tuple{Int},
) where {T}
a, c, b, d = U1
instruct_kernel(state, loc, 1 << (loc - 1), 1 << loc, a, b, c, d)
Expand All @@ -189,16 +156,10 @@ end
return state
end

YaoBase.instruct!(
state::AbstractVecOrMat{T},
g::SDPermMatrix{T},
locs::Tuple{Int},
) where {T} = instruct!(state, g, locs...)

function YaoBase.instruct!(
state::AbstractVecOrMat{T},
U1::SDPermMatrix{T},
loc::Int,
(loc, )::Tuple{Int},
) where {T}
U1.perm[1] == 1 && return instruct!(state, Diagonal(U1), loc)
mask = bmask(loc)
Expand All @@ -214,16 +175,10 @@ function YaoBase.instruct!(
return state
end

YaoBase.instruct!(
state::AbstractVecOrMat{T},
g::SDDiagonal{T},
locs::Tuple{Int},
) where {T} = instruct!(state, g, locs...)

function YaoBase.instruct!(
state::AbstractVecOrMat{T},
U1::SDDiagonal{T},
loc::Int,
(loc, )::Tuple{Int},
) where {T}
mask = bmask(loc)
a, d = U1.diag
Expand Down Expand Up @@ -323,24 +278,17 @@ for (G, FACTOR) in zip(
[:Z, :S, :T, :Sdag, :Tdag],
[:(-1), :(im), :($(exp(im * π / 4))), :(-im), :($(exp(-im * π / 4)))],
)
# forward single gate
@eval YaoBase.instruct!(
state::AbstractVecOrMat,
g::Val{$(QuoteNode(G))},
locs::Tuple{Int},
) = instruct!(state, g, locs...)

# no effect (to fix ambiguity)
@eval YaoBase.instruct!(st::AbstractVecOrMat, ::Val{$(QuoteNode(G))}, ::Tuple{}) = st

@eval function YaoBase.instruct!(
state::AbstractVecOrMat{T},
::Val{$(QuoteNode(G))},
locs::Int,
(loc, )::Tuple{Int},
) where {T}
mask = bmask(locs)
step = 1 << (locs - 1)
step_2 = 1 << locs
mask = bmask(loc)
step = 1 << (loc - 1)
step_2 = 1 << loc
@threads for j = 0:step_2:size(state, 1)-step
for i = j+step+1:j+step_2
mulrow!(state, i, $FACTOR)
Expand All @@ -350,20 +298,6 @@ for (G, FACTOR) in zip(
end
end

# multi-controlled gates
## controlled paulis
for G in [:X, :Y, :Z, :S, :T, :Sdag, :Tdag]
# forward single gates
@eval YaoBase.instruct!(
state::AbstractVecOrMat,
g::Val{$(QuoteNode(G))},
locs::Int,
control_locs::NTuple{N1,Int},
control_bits::NTuple{N2,Int},
) where {N1,N2} = instruct!(state, g, (locs,), control_locs, control_bits)
end


# Specialized
import YaoBase: rot_mat

Expand All @@ -382,18 +316,30 @@ for G in [:Rx, :Ry, :Rz, :CPHASE]
@eval function YaoBase.instruct!(
state::AbstractVecOrMat{T},
g::Val{$(QuoteNode(G))},
locs::Union{Int,NTuple{N3,Int}},
locs::NTuple{N3,Int},
control_locs::NTuple{N1,Int},
control_bits::NTuple{N2,Int},
theta::Number,
) where {T,N1,N2,N3}
m = rot_mat(T, g, theta)
instruct!(state, m, locs, control_locs, control_bits)
return state
end
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

# forward single gates
@eval function YaoBase.instruct!(
function YaoBase.instruct!(
state::AbstractVecOrMat{T},
g::Val,
locs::Union{Int,NTuple{N1,Int}},
Expand Down
26 changes: 13 additions & 13 deletions test/instruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ using YaoBase.Const
I2 = IMatrix(2)
M = kron(I2, U1, I2, I2) * ST

@test instruct!(copy(ST), U1, 3) M instruct!(reshape(copy(ST), :, 1), U1, 3)
@test instruct!(copy(ST), U1, (3, )) M instruct!(reshape(copy(ST), :, 1), U1, (3, ))

U2 = rand(ComplexF64, 4, 4)
M = kron(I2, U2, I2) * ST
@test instruct!(copy(ST), U2, (2, 3)) M

@test instruct!(copy(ST), kron(U1, U1), (3, 1))
instruct!(instruct!(copy(ST), U1, 3), U1, 1)
instruct!(instruct!(copy(ST), U1, (3, )), U1, (1, ))
@test instruct!(copy(REG), kron(U1, U1), (3, 1))
instruct!(instruct!(copy(REG), U1, 3), U1, 1)
instruct!(instruct!(copy(REG), U1, (3, )), U1, (1, ))
@test instruct!(transpose_storage(REG), kron(U1, U1), (3, 1))
instruct!(instruct!(copy(REG), U1, 3), U1, 1)
instruct!(instruct!(copy(REG), U1, (3, )), U1, (1, ))
@test instruct!(transpose_storage(REG), kron(U1, U1), (3, 1))
instruct!(instruct!(transpose_storage(REG), U1, 3), U1, 1)
instruct!(instruct!(transpose_storage(REG), U1, (3, )), U1, (1, ))

@test instruct!(reshape(copy(ST), :, 1), kron(U1, U1), (3, 1))
instruct!(instruct!(reshape(copy(ST), :, 1), U1, 3), U1, 1)
instruct!(instruct!(reshape(copy(ST), :, 1), U1, (3, )), U1, (1, ))

U2 = sprand(ComplexF64, 8, 8, 0.1)
ST = randn(ComplexF64, 1 << 5)
Expand Down Expand Up @@ -74,10 +74,10 @@ end
end

@testset "test controlled $G instructions" for (G, M) in zip((:X, :Y, :Z), (X, Y, Z))
@test linop2dense(s -> instruct!(s, Val(G), 4, (2, 1), (0, 1)), 4)
@test linop2dense(s -> instruct!(s, Val(G), (4, ), (2, 1), (0, 1)), 4)
general_controlled_gates(4, [P0, P1], [2, 1], [M], [4])

@test linop2dense(s -> instruct!(s, Val(G), 1, 2, 0), 2)
@test linop2dense(s -> instruct!(s, Val(G), (1, ), (2, ), (0, )), 2)
general_controlled_gates(2, [P0], [2], [M], [1])
end
end
Expand All @@ -87,12 +87,12 @@ end
Pm = pmrand(ComplexF64, 2)
Dv = Diagonal(randn(ComplexF64, 2))

@test instruct!(copy(ST), Pm, 3)
@test instruct!(copy(ST), Pm, (3, ))
kron(I2, Pm, I2, I2) * ST
instruct!(reshape(copy(ST), :, 1), Pm, 3)
@test instruct!(copy(ST), Dv, 3)
instruct!(reshape(copy(ST), :, 1), Pm, (3, ))
@test instruct!(copy(ST), Dv, (3, ))
kron(I2, Dv, I2, I2) * ST
instruct!(reshape(copy(ST), :, 1), Dv, 3)
instruct!(reshape(copy(ST), :, 1), Dv, (3, ))
end

@testset "swap instruction" begin
Expand Down Expand Up @@ -134,7 +134,7 @@ end

@testset "Yao.jl/#189" begin
st = rand(1 << 4)
@test instruct!(st, IMatrix{2,Float64}(), 1) == st
@test instruct!(st, IMatrix{2,Float64}(), (1, )) == st
end

@testset "test empty locs" begin
Expand Down
6 changes: 4 additions & 2 deletions test/utils.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LinearAlgebra, StaticArrays, LuxurySparse, SparseArrays
using Test
import YaoArrayRegister: swaprows!, swapcols!, mulrow!, mulcol!, u1rows!, unrows!

@testset "swaprows! & mulrow!" begin
Expand All @@ -25,9 +26,9 @@ end
su1 = SMatrix{2,2}(u1)
inds1 = [1, 3]
sinds1 = SVector{2}(inds1)
@test 0 == @allocated unrows!(v, sinds1, su1)

unrows!(v, sinds1, su1)
@test 0 == @allocated unrows!(v, sinds1, su1)
@test u1rows!(copy(v), inds1..., u1[1], u1[3], u1[2], u1[4])
unrows!(copy(v), inds1, u1)
@test unrows!(copy(v), inds1, u1) unrows!(copy(v), sinds1, su1)
Expand All @@ -52,7 +53,8 @@ end
dg = ComplexF64[1 0; 0 -1] |> staticize
inds = SVector{2}([1, 3])
unrows!(v, inds, dg)
@test 0 == @allocated unrows!(v, inds, dg)
# NOTE: some machines have a small allocation
@test 32 >= @allocated unrows!(v, inds, dg)
@test unrows!(copy(v), inds, dg) unrows!(copy(v), [1, 3], dg)
@test unrows!(copy(v), inds, IMatrix{1 << 2}()) == v
end
Expand Down

0 comments on commit edfa850

Please sign in to comment.