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

Commit

Permalink
new pswap instruct (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu authored and Roger-luo committed Jul 2, 2019
1 parent 7a3cf82 commit 75e0add
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/instruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,29 @@ function YaoBase.instruct!(
end
return state
end

function YaoBase.instruct!(
state::AbstractVecOrMat{T},
::Val{:PSWAP},
locs::Tuple{Int, Int},
theta::Real) where T
mask1 = bmask(locs[1])
mask2 = bmask(locs[2])
mask12 = mask1|mask2
a = T(cos(theta/2))
c = T(-im * sin(theta/2))
e = T(exp(-im/2*theta))
for b in basis(state)
if b&mask1==0
i = b+1
i_ = b mask12 + 1
if b&mask2==mask2
u1rows!(state, i, i_, a, c, c, a)
else
mulrow!(state, i, e)
mulrow!(state, i_, e)
end
end
end
return state
end
6 changes: 6 additions & 0 deletions test/instruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ end
@test instruct!(copy(ST), Val(:SWAP), (1, 2)) SWAP * ST
end

@testset "pswap instruction" begin
ST = randn(ComplexF64, 1 << 2)
θ = π/3
@test instruct!(copy(ST), Val(:PSWAP), (1, 2), θ) (cos/2)*IMatrix{4}() - im*sin/2)* SWAP) * ST
end

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

0 comments on commit 75e0add

Please sign in to comment.