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

Commit

Permalink
Format .jl files
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Sep 27, 2020
1 parent 477c861 commit 2e9b404
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 45 deletions.
68 changes: 41 additions & 27 deletions src/instruct.jl
Expand Up @@ -9,15 +9,28 @@
using YaoBase, BitBasis, LuxurySparse, StaticArrays
export instruct!

function YaoBase.instruct!(r::ArrayReg, op, locs::Tuple, control_locs::Tuple, control_bits::Tuple)
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)
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

Expand Down Expand Up @@ -146,13 +159,14 @@ 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::NTuple{N,Int}) where {N} =
state
YaoBase.instruct!(state::AbstractVecOrMat, U::IMatrix, locs::Tuple{Int}) = state

function YaoBase.instruct!(
state::AbstractVecOrMat{T},
U1::AbstractMatrix{T},
(loc, )::Tuple{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 @@ -171,9 +185,9 @@ end
function YaoBase.instruct!(
state::AbstractVecOrMat{T},
U1::SDPermMatrix{T},
(loc, )::Tuple{Int},
(loc,)::Tuple{Int},
) where {T}
U1.perm[1] == 1 && return instruct!(state, Diagonal(U1), (loc, ))
U1.perm[1] == 1 && return instruct!(state, Diagonal(U1), (loc,))
mask = bmask(loc)
b, c = U1.vals
step = 1 << (loc - 1)
Expand All @@ -190,7 +204,7 @@ end
function YaoBase.instruct!(
state::AbstractVecOrMat{T},
U1::SDDiagonal{T},
(loc, )::Tuple{Int},
(loc,)::Tuple{Int},
) where {T}
mask = bmask(loc)
a, d = U1.diag
Expand Down Expand Up @@ -296,7 +310,7 @@ for (G, FACTOR) in zip(
@eval function YaoBase.instruct!(
state::AbstractVecOrMat{T},
::Val{$(QuoteNode(G))},
(loc, )::Tuple{Int},
(loc,)::Tuple{Int},
) where {T}
mask = bmask(loc)
step = 1 << (loc - 1)
Expand Down Expand Up @@ -340,33 +354,33 @@ for G in [:Rx, :Ry, :Rz, :CPHASE]
end # for

@inline function YaoBase.instruct!(
state::AbstractVecOrMat{T},
::Val{:Rx},
(loc, )::Tuple{Int},
theta::Number
) where {T, N}
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)
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}
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}
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
Expand All @@ -375,9 +389,9 @@ end
function YaoBase.instruct!(
state::AbstractVecOrMat{T},
::Val{:CPHASE},
locs::NTuple{N, Int},
theta::Number
) where {T, N}
locs::NTuple{N,Int},
theta::Number,
) where {T,N}
m = rot_mat(T, Val(:CPHASE), theta)
instruct!(state, m, locs)
return state
Expand Down
39 changes: 21 additions & 18 deletions test/instruct.jl
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 All @@ -148,9 +148,12 @@ 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)
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 2e9b404

Please sign in to comment.