Skip to content

Commit

Permalink
add itercontrol
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed Jun 22, 2018
1 parent a253d6c commit 05b547a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 55 deletions.
11 changes: 11 additions & 0 deletions benchmark/Intrinsics/IterControl.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using BenchmarkTools
using Yao.Intrisics

const nbit = 16
const V = randn(ComplexF64, 1<<nbit)
const res4 = copy(V)

it = itercontrol(nbit, [3],[1])
@benchmark controldo(x->swaprows!($res4, x+1, x-3), $it)
@benchmark for i in $it swaprows!($res4, i+1, i-3) end
@benchmark controldo($(x->x), $it)
3 changes: 3 additions & 0 deletions src/Intrinsics/Intrinsics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Intrinsics
using Compat
using Compat.LinearAlgebra
using Compat.SparseArrays
using StaticArrays: SVector, SMatrix

using ..LuxurySparse
import ..LuxurySparse: I
Expand All @@ -26,6 +27,7 @@ export indices_with, bitarray, packbits, controller
export bdistance
export onehotvec
export hypercubic, reordered_basis, Reorderer, reorder, invorder
export itercontrol, IterControl, controldo

include("Basis.jl")

Expand All @@ -38,5 +40,6 @@ include("TupleTools.jl")
import Compat.LinearAlgebra: ishermitian
export isunitary, isreflexive, ishermitian
include("OperatorTraits.jl")
include("IterControl.jl")

end
55 changes: 0 additions & 55 deletions src/Intrinsics/IterControl.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
using Compat
using Yao
using Yao.Intrinsics
using Yao.Blocks
using Yao.LuxurySparse
using StaticArrays: SVector, SMatrix

# add a tag initial control
"""
IterControl{N, C}
Expand Down Expand Up @@ -67,50 +59,3 @@ function controldo(func::Function, ic::IterControl{N, C}) where {N, C}
func(i+ic.base)
end
end

################### Test for subspace and itercontrol #################
using Compat.Test

@testset "private functions: group_shift and lmove" begin
@test group_shift(5, [1,2,5]) == ([0, 15], [2, 1])
@test group_shift(5, [2,3]) == ([1], [2])
@test group_shift(5, [1,3,5]) == ([0, 3, 15], [1, 1, 1])

@test lmove(5, 1, 2) |> bin == "10001"
end

@testset "iterator interface" begin
v = randn(ComplexF64, 1<<4)
it = itercontrol(4, [3],[1])
vec = Int[]
it2 = itercontrol(4, [3, 4], [0, 0])
for i in it2
push!(vec, i)
end
@test vec == [0,1,2,3]

vec = Int[]
it4 = itercontrol(4, [4,2, 1], [1, 1, 1])
for i in it4
push!(vec, i)
end
@test vec == [11, 15]
@test (rrr=copy(v); controldo(x->mulrow!(rrr, x+1, -1.0), it4); rrr) mat(control(4, (4,2), 1=>Z)) * v
nbit = 8
it = itercontrol(nbit, [3],[1])
V = randn(ComplexF64, 1<<nbit)
res = mat(kron(nbit, 3=>X))*V
@test (rrr=copy(V); controldo(x->swaprows!(rrr, x+1, x-3), it); rrr) res
@test (rrr=copy(V); controldo(x->mulrow!(rrr, x+1, -1), itercontrol(nbit, [3,7, 6], [1, 1, 1])); rrr) mat(control(nbit, (3,7), 6=>Z)) * V
end


using BenchmarkTools
#const nbit = 16
#const V = randn(ComplexF64, 1<<nbit)

#const res4 = copy(V)
#it = itercontrol(nbit, [3],[1])
#@benchmark controldo(x->swaprows!($res4, x+1, x-3), $it)
#@benchmark for i in $it swaprows!($res4, i+1, i-3) end
#@benchmark controldo($(x->x), $it)
41 changes: 41 additions & 0 deletions test/Intrinsics/IterControl.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Compat
using Compat.Test
using Yao
using Yao.Intrinsics
using Yao.Intrinsics: group_shift, itercontrol, controldo, IterControl, lmove
using Yao.LuxurySparse

@testset "private functions: group_shift and lmove" begin
@test group_shift(5, [1,2,5]) == ([0, 15], [2, 1])
@test group_shift(5, [2,3]) == ([1], [2])
@test group_shift(5, [1,3,5]) == ([0, 3, 15], [1, 1, 1])

@test lmove(5, 1, 2) |> bin == "10001"
end

@testset "iterator interface" begin
v = randn(ComplexF64, 1<<4)
it = itercontrol(4, [3],[1])
vec = Int[]
it2 = itercontrol(4, [3, 4], [0, 0])
for i in it2
push!(vec, i)
end
@test vec == [0,1,2,3]

vec = Int[]
it4 = itercontrol(4, [4,2, 1], [1, 1, 1])
for i in it4
push!(vec, i)
end
@test vec == [11, 15]
@test (rrr=copy(v); controldo(x->mulrow!(rrr, x+1, -1.0), it4); rrr) mat(control(4, (4,2), 1=>Z)) * v
nbit = 8
it = itercontrol(nbit, [3],[1])
V = randn(ComplexF64, 1<<nbit)
res = mat(kron(nbit, 3=>X))*V
@test (rrr=copy(V); controldo(x->swaprows!(rrr, x+1, x-3), it); rrr) res
@test (rrr=copy(V); controldo(x->mulrow!(rrr, x+1, -1), itercontrol(nbit, [3,7, 6], [1, 1, 1])); rrr) mat(control(nbit, (3,7), 6=>Z)) * V
end


0 comments on commit 05b547a

Please sign in to comment.