Skip to content

Commit

Permalink
Merge branch 'master' into boostgeneral
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed Jun 22, 2018
2 parents 417d0ab + bfb203d commit a253d6c
Show file tree
Hide file tree
Showing 20 changed files with 382 additions and 401 deletions.
602 changes: 303 additions & 299 deletions examples/QCBM.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ include("MatrixBlock.jl")
# others
include("Measure.jl")
include("Sequential.jl")
include("Functor.jl")
include("Function.jl")
include("IOSyntax.jl")
include("blockoperations.jl")

Expand Down
17 changes: 17 additions & 0 deletions src/Blocks/Function.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export FunctionBlock

"""
FunctionBlock <: AbstractBlock
This block contains a general function that perform an in-place operation over a register
"""
struct FunctionBlock{FT} <: AbstractBlock
apply!
function FunctionBlock{FT}(f) where FT
!isempty(methods(f)) || throw(ArgumentError("Input is not callable!"))
new{FT}(f)
end
end

FunctionBlock(f) = FunctionBlock{typeof(f)}(f)
apply!(reg::AbstractRegister, f::FunctionBlock) = f.apply!(reg)
17 changes: 0 additions & 17 deletions src/Blocks/Functor.jl

This file was deleted.

25 changes: 0 additions & 25 deletions src/Interfaces/Callables.jl

This file was deleted.

4 changes: 0 additions & 4 deletions src/Interfaces/Composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ function control(controls, target)
total->ControlBlock{total}(decode_sign(controls...)..., target.second, target.first)
end

function control(total::Int, controls)
x::Pair->ControlBlock{total}(decode_sign(controls...)..., x.second, x.first)
end

function control(controls)
function _control(x::Pair)
total->ControlBlock{total}(decode_sign(controls...)..., x.second, x.first)
Expand Down
37 changes: 37 additions & 0 deletions src/Interfaces/Function.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export @fn, InvOrder, addbit, Reset

macro fn(f)
:(FunctionBlock($(esc(f))))
end

macro fn(name::Symbol, f)
FT = FunctionBlock{name}
:($FT($(esc(f))))
end

"""
macro fn([name,] f)
Define a in-place function on a register inside circuits.
"""
:(@fn)

"""
InvOrder
Return a [`FunctionBlock`](@ref) of inversing the order.
"""
const InvOrder = @fn InvOrder invorder!

"""
Reset
"""
const Reset = @fn Reset reset!


"""
addbit(n::Int) -> FunctionBlock{:AddBit}
Return a [`FunctionBlock`](@ref) of adding n bits.
"""
addbit(n::Int) = FunctionBlock{Tuple{:AddBit, n}}(reg->addbit!(reg, n))
30 changes: 0 additions & 30 deletions src/Interfaces/Functor.jl

This file was deleted.

2 changes: 1 addition & 1 deletion src/Interfaces/Interfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ include("Signal.jl")
include("Primitive.jl")
include("Composite.jl")
include("Measure.jl")
include("Functor.jl")
include("Function.jl")
include("Sequential.jl")
include("Cache.jl")

Expand Down
1 change: 0 additions & 1 deletion src/Registers/Default.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ function addbit!(reg::DefaultRegister{B, T}, n::Int) where {B, T}
end

function reset!(reg::DefaultRegister)
println(reg)
reg.state .= 0
reg.state[1,:] .= 1
reg
Expand Down
4 changes: 2 additions & 2 deletions test/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ end
include("Measure.jl")
end

@testset "functor" begin
include("Functor.jl")
@testset "function" begin
include("Function.jl")
end

@testset "sequential" begin
Expand Down
1 change: 0 additions & 1 deletion test/Blocks/Daggered.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ end
@test GP isa Daggered
@test mat(GP) == mat(ConstG)'
@test adjoint(adjoint(ConstG)) === ConstG
println(chain(GP, GP))
end

@testset "copy dispatch" begin
Expand Down
11 changes: 11 additions & 0 deletions test/Blocks/Function.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Compat
using Compat.Test

using Yao
using Yao.Blocks

@testset "functor" begin
Test_InvOrder = FunctionBlock(invorder!)
reg = rand_state(4)
@test copy(reg) |> invorder! == apply!(copy(reg), Test_InvOrder)
end
6 changes: 0 additions & 6 deletions test/Boost/applys.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ using Yao.Blocks
using Yao.Intrinsics
using Yao.LuxurySparse

# import Yao: xapply!, yapply!, zapply!, cxapply!, cyapply!, czapply!

# struct Register{N, T<:Complex}
# state::Vector{T}
# end

@testset "xyz" begin
@test linop2dense(s->xapply!(s, [1]), 1) == mat(X)
@test linop2dense(s->yapply!(s, [1]), 1) == mat(Y)
Expand Down
15 changes: 5 additions & 10 deletions test/Interfaces/Interfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ end
@test control(3, [1, 2], 3=>X) isa ControlBlock
@test control(5, 1:2, 3=>X) isa ControlBlock

@test (1=>X) |> control(8, i for i in [2, 3, 6, 7]) isa ControlBlock
@test ((1=>X) |> control(i for i in [2, 3, 6, 7]))(8) isa ControlBlock

@test ((1=>X) |> C(2, 3))(4) isa ControlBlock
end

Expand Down Expand Up @@ -100,17 +97,16 @@ end
@test Rz(1) isa RotationGate
end

@testset "functors" begin
@testset "functions" begin
reg = rand_state(3, 2)
println(typeof(InvOrder))
@test InvOrder isa Functor{:InvOrder}
@test InvOrder isa FunctionBlock{:InvOrder}
@test apply!(copy(reg), InvOrder) == copy(reg) |> invorder!

@test addbit(3) isa Functor{:AddBit}
@test addbit(3) isa FunctionBlock{Tuple{:AddBit, 3}}
@test apply!(copy(reg), addbit(2)) |> state == kron(zero_state(2) |> state, reg |> state)

Probs = @functor probs
@test Probs isa Functor{:Default}
Probs = @fn probs
@test Probs isa FunctionBlock{typeof(probs)}
@test apply!(copy(reg), Probs) == reg |> probs
end

Expand All @@ -122,7 +118,6 @@ end
@test sqs == sequence(kron(5, 3=>X), addbit(3), MEASURE) == sequence((kron(5, 3=>X), addbit(3), MEASURE))
insert!(sqs, 3, kron(8, 8=>X))
push!(sqs, Reset)
println(sqs)
reg = register(bit"11111") |> sqs
@test MEASURE.result[] == 155
@test reg == zero_state(8)
Expand Down
4 changes: 4 additions & 0 deletions test/Registers/Registers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ using Compat.SparseArrays
include("Default.jl")
include("Focus.jl")
end

@testset "reorder" begin
include("reorder.jl")
end
1 change: 0 additions & 1 deletion test/Registers/reorder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ using Yao.Intrinsics


v1, v2, v3 = randn(2), randn(2), randn(2)
using Compat.Test
@test repeat(register(v1 v2 v3), 2) |> invorder! repeat(register(v3 v2 v1), 2)

end
1 change: 0 additions & 1 deletion test/Zoo/Differential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ using Compat.Test

@testset "rotter, collect_rotblocks, num_gradient, opgrad" begin
c = diff_circuit(4, 3, [1=>3, 2=>4, 2=>3, 4=>1])
println(c)
rots = collect_rotblocks(c)
@test length(rots) == nparameters(c) == 40

Expand Down
1 change: 0 additions & 1 deletion test/Zoo/QFT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ end
@test Matrix(mat(chain(3, QFT(3) |> adjoint, QFT(3)))) eye(1<<3)

# test ifft
println(ifftblock)
reg1 = copy(reg) |>ifftblock

# permute lines (Manually)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end
include("Interfaces/Interfaces.jl")
end

@testset "gallery" begin
@testset "zoo" begin
include("Zoo/Zoo.jl")
end

Expand Down

0 comments on commit a253d6c

Please sign in to comment.