diff --git a/benchmark/Intrinsics/IterControl.jl b/benchmark/Intrinsics/IterControl.jl new file mode 100644 index 000000000..11c8e0cd6 --- /dev/null +++ b/benchmark/Intrinsics/IterControl.jl @@ -0,0 +1,11 @@ +using BenchmarkTools +using Yao.Intrisics + +const nbit = 16 +const V = randn(ComplexF64, 1<swaprows!($res4, x+1, x-3), $it) +@benchmark for i in $it swaprows!($res4, i+1, i-3) end +@benchmark controldo($(x->x), $it) diff --git a/src/Intrinsics/Intrinsics.jl b/src/Intrinsics/Intrinsics.jl index a2a5a2b49..1f013dc03 100644 --- a/src/Intrinsics/Intrinsics.jl +++ b/src/Intrinsics/Intrinsics.jl @@ -3,6 +3,7 @@ module Intrinsics using Compat using Compat.LinearAlgebra using Compat.SparseArrays +using StaticArrays: SVector, SMatrix using ..LuxurySparse import ..LuxurySparse: I @@ -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") @@ -38,5 +40,6 @@ include("TupleTools.jl") import Compat.LinearAlgebra: ishermitian export isunitary, isreflexive, ishermitian include("OperatorTraits.jl") +include("IterControl.jl") end diff --git a/src/Intrinsics/IterControl.jl b/src/Intrinsics/IterControl.jl index a0fa526e9..6e65dea6d 100644 --- a/src/Intrinsics/IterControl.jl +++ b/src/Intrinsics/IterControl.jl @@ -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} @@ -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<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<swaprows!($res4, x+1, x-3), $it) -#@benchmark for i in $it swaprows!($res4, i+1, i-3) end -#@benchmark controldo($(x->x), $it) diff --git a/test/Intrinsics/IterControl.jl b/test/Intrinsics/IterControl.jl new file mode 100644 index 000000000..fc1aa1bec --- /dev/null +++ b/test/Intrinsics/IterControl.jl @@ -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<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 + +