Skip to content

Commit

Permalink
fix bugs, rewrite Sum, simpler time evolution, accelerate block prope…
Browse files Browse the repository at this point in the history
…rty (#55)

* update

* fix occupied_locs types

* fix project.toml

* change name, Sum->Add

* add deprecation warn

* add deprecation warn

* fix deprecation

* fix docstring
  • Loading branch information
GiggleLiu authored and Roger-luo committed Jul 23, 2019
1 parent a710e0f commit 9e74c7b
Show file tree
Hide file tree
Showing 32 changed files with 254 additions and 253 deletions.
7 changes: 3 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ CacheServers = "a921213e-d44a-5460-ac04-5d720a99ba71"
ExponentialUtilities = "d4d017d3-3776-5f7e-afef-a10c40355c18"
LegibleLambdas = "f1f30506-32fe-5131-bd72-7c197988f9e5"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LinearMaps = "7a12625a-238d-50fd-b39a-03d52299707e"
LuxurySparse = "d05aeea4-b7d4-55ac-b691-9e7fabb07ba2"
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand All @@ -20,10 +19,10 @@ YaoArrayRegister = "e600142f-9330-5003-8abb-0ebd767abc51"
YaoBase = "a8f54c17-34bc-5a9d-b050-f522fe3f755f"

[compat]
julia = "^1.0.0"
YaoBase = "~0.9.1"
YaoArrayRegister = "~0.3.5"
BitBasis = "~0.5"
YaoArrayRegister = "~0.3.5"
YaoBase = "~0.9.1"
julia = "^1.0.0"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
3 changes: 2 additions & 1 deletion src/YaoBlocks.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module YaoBlocks

using LinearAlgebra

include("utils.jl")
# include("traits.jl")

include("abstract_block.jl")
include("block_map.jl")

# concrete blocks
include("routines.jl")
Expand Down
11 changes: 6 additions & 5 deletions src/abstract_block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ end
"""
occupied_locs(x)
Return an iterator of occupied locations of `x`.
Return a tuple of occupied locations of `x`.
"""
@interface occupied_locs(x::AbstractBlock) = 1:nqubits(x)
@interface occupied_locs(x::AbstractBlock) = (1:nqubits(x)...,)

"""
subblocks(x)
Expand All @@ -75,8 +75,9 @@ Change the sub-blocks of a [`CompositeBlock`](@ref) with given iterator `itr`.
Transform the apply! function of specific block to dense matrix.
"""
@interface applymatrix(g::AbstractBlock) = linop2dense(r->statevec(apply!(ArrayReg(r), g)), nqubits(g))
# just use BlockMap maybe?
@interface applymatrix(T, g::AbstractBlock) = linop2dense(T, r->statevec(apply!(ArrayReg(r), g)), nqubits(g))
applymatrix(g::AbstractBlock) = applymatrix(ComplexF64, g)
# just use BlockMap maybe? No!

@interface print_block(io::IO, blk::AbstractBlock) = print_block(io, MIME("text/plain"), blk)
print_block(blk::AbstractBlock) = print_block(stdout, blk)
Expand Down Expand Up @@ -218,7 +219,7 @@ function consume!(d::Dispatcher{<:Symbol}, n::Int)
d.params
end

function consume!(d::Dispatcher{<:Number}, n::Int)
function consume!(d::Dispatcher{<:Number}, n::Int)
if n == 0
return ()
elseif n == 1
Expand Down
17 changes: 8 additions & 9 deletions src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Base.:(-)(x::AbstractBlock{N}) where {N} = Scale(Val(-1), x)
Base.:(-)(x::Scale{Val{-1}}) = content(x)
Base.:(-)(x::Scale{Val{S}}) where S = Scale(Val(-S), content(x))
Base.:(-)(x::Scale) = Scale(-x.alpha, content(x))
Base.:(+)(x::AbstractBlock) = x

Base.:(*)(x::AbstractBlock, α::Number) = α * x

Expand Down Expand Up @@ -33,14 +32,14 @@ Base.:(*)(x::Scale{Val{S}}, y::Scale) where S = (S * y.alpha) * (content(x) * co
Base.:(*)(x::Scale, y::AbstractBlock) = x.alpha * chain(y, content(x))
Base.:(*)(y::AbstractBlock, x::Scale) = x.alpha * chain(content(x), y)

Base.:(+)(xs::AbstractBlock...) = Sum(xs...)
Base.:(+)(xs::AbstractBlock...) = Add(xs...)
Base.:(*)(xs::AbstractBlock...) = chain(Iterators.reverse(xs)...)
Base.:(/)(A::AbstractBlock, x::Number) = (1/x)*A
# reduce
Base.sum(a::AbstractBlock{N}, blocks::AbstractBlock{N}...) where N = Sum(a, blocks...)
Base.prod(a::AbstractBlock{N}, blocks::AbstractBlock{N}...) where N = chain(Iterators.reverse(blocks)..., a)
Base.prod(blocks::AbstractVector{<:AbstractBlock{N}}) where N = chain(Iterators.reverse(blocks)...)
Base.sum(blocks::AbstractVector{<:AbstractBlock{N}}) where N = +(blocks...)

Base.:(-)(lhs::AbstractBlock, rhs::AbstractBlock) = Sum(lhs, -rhs)
Base.:(-)(lhs::AbstractBlock, rhs::AbstractBlock) = Add(lhs, -rhs)
Base.:(^)(x::AbstractBlock, n::Int) = chain((copy(x) for k in 1:n)...)

"""
Expand Down Expand Up @@ -132,7 +131,7 @@ export eliminate_nested
eliminate_nested(ex::AbstractBlock) = ex

# TODO: eliminate nested expr e.g chain(X, chain(X, Y))
function eliminate_nested(ex::T) where {T <: Union{ChainBlock, Sum}}
function eliminate_nested(ex::T) where {T <: Union{ChainBlock, Add}}
_flatten(x) = (x, )
_flatten(x::T) = subblocks(x)

Expand Down Expand Up @@ -184,7 +183,7 @@ combine_alpha(alpha, x::AbstractBlock) = alpha + 1
combine_alpha(alpha, x::Scale) = alpha + x.alpha
combine_alpha(alpha, x::Scale{Val{S}}) where S = alpha + S

function combine_similar(ex::Sum{N}) where N
function combine_similar(ex::Add{N}) where N
table = zeros(Bool, length(ex))
list = []; p = 1
while p <= length(ex)
Expand Down Expand Up @@ -220,9 +219,9 @@ function combine_similar(ex::Sum{N}) where N
end

if isempty(list)
return Sum{N}(())
return Add{N}()
else
return Sum(list...)
return Add(list...)
end
end

Expand Down
46 changes: 0 additions & 46 deletions src/block_map.jl

This file was deleted.

4 changes: 2 additions & 2 deletions src/blocktools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ function expect(op::AbstractBlock, reg::AbstractRegister{B}) where B
dropdims(sum(A.*C, dims=1), dims=1) |> conj
end

function expect(op::Sum, reg::AbstractRegister)
function expect(op::Add, reg::AbstractRegister)
sum(opi->expect(opi, reg), op)
end

function expect(op::Scale, reg::AbstractRegister)
factor(op)*expect(content(op), reg)
end

expect(op::Sum, reg::AbstractRegister{1}) = invoke(expect, Tuple{Sum, AbstractRegister}, op, reg)
expect(op::Add, reg::AbstractRegister{1}) = invoke(expect, Tuple{Add, AbstractRegister}, op, reg)
expect(op::Scale, reg::AbstractRegister{1}) = invoke(expect, Tuple{Scale, AbstractRegister}, op, reg)

for FUNC in [:measure!, :measure_collapseto!, :measure_remove!, :measure]
Expand Down
2 changes: 1 addition & 1 deletion src/composite/chain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ chain() = @λ(n->chain(n))

subblocks(c::ChainBlock) = c.blocks
occupied_locs(c::ChainBlock) =
unique(Iterators.flatten(occupied_locs(b) for b in subblocks(c)))
Tuple(unique(Iterators.flatten(occupied_locs(b) for b in subblocks(c))))

chsubblocks(pb::ChainBlock{N}, blocks::Vector{<:AbstractBlock}) where N = length(blocks) == 0 ? ChainBlock{N}([]) : ChainBlock(blocks)
chsubblocks(pb::ChainBlock, it) = chain(it...)
Expand Down
2 changes: 1 addition & 1 deletion src/composite/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ include("pauli_strings.jl")

chsubblocks(x::ChainBlock, it::AbstractBlock) = chsubblocks(x, (it, ))
chsubblocks(x::KronBlock, it::AbstractBlock) = chsubblocks(x, (it, ))
chsubblocks(x::Sum, it::AbstractBlock) = chsubblocks(x, (it, ))
chsubblocks(x::Add, it::AbstractBlock) = chsubblocks(x, (it, ))

# tag blocks
include("tag/tag.jl")
Expand Down
31 changes: 16 additions & 15 deletions src/composite/concentrator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ it to other blocks.
"""
struct Concentrator{N, BT <: AbstractBlock, C} <: AbstractContainer{BT, N}
content::BT
locations::NTuple{C, Int}
locs::NTuple{C, Int}
end

function Concentrator{N}(block::BT, locations::NTuple{C, Int}) where {N, M, C, BT<:AbstractBlock{M}}
if !(length(locations) == M && N>=M)
throw(LocationConflictError("length of locations must be equal to the size of block, and smaller than size of itself."))
function Concentrator{N}(block::BT, locs::NTuple{C, Int}) where {N, M, C, BT<:AbstractBlock{M}}
if !(length(locs) == M && N>=M)
throw(LocationConflictError("length of locs must be equal to the size of block, and smaller than size of itself."))
end
return Concentrator{N, BT, C}(block, locations)
return Concentrator{N, BT, C}(block, locs)
end

"""
Expand All @@ -40,7 +40,7 @@ julia> apply!(copy(r), concentrate(X, 1)) ≈ apply!(copy(r), put(1=>X))
true
```
It works for in-contigious locations as well
It works for in-contigious locs as well
```jldoctest
julia> r = rand_state(4)
Expand Down Expand Up @@ -81,35 +81,36 @@ Lazy curried version of [`concentrate`](@ref).
concentrate(block::AbstractBlock, locs) = (n->concentrate(n, block, locs))
concentrate(block::Function, locs) = (n->concentrate(n, block, locs))

occupied_locs(c::Concentrator) = c.locations
occupied_locs(c::Concentrator) = map(i->c.locs[i], c.content |> occupied_locs)
chsubblocks(pb::Concentrator{N}, blk::AbstractBlock) where N =
Concentrator{N}(blk, occupied_locs(pb))
Concentrator{N}(blk, pb.locs)
PreserveStyle(::Concentrator) = PreserveAll()

function apply!(r::AbstractRegister, c::Concentrator)
_check_size(r, c)
focus!(r, occupied_locs(c))
focus!(r, c.locs)
apply!(r, c.content)
relax!(r, occupied_locs(c), to_nactive=nqubits(c))
relax!(r, c.locs, to_nactive=nqubits(c))
return r
end

function mat(::Type{T}, c::Concentrator{N, <:AbstractBlock}) where {N, T}
mat(T, PutBlock{N}(c.content, c.locations))
mat(T, PutBlock{N}(c.content, c.locs))
end

Base.adjoint(blk::Concentrator{N}) where N =
Concentrator{N}(adjoint(blk.content), occupied_locs(blk))
Concentrator{N}(adjoint(blk.content), blk.locs)

function Base.:(==)(a::Concentrator{N, BT}, b::Concentrator{N, BT}) where {N, BT}
return a.content == b.content && a.locations == b.locations
return a.content == b.content && a.locs == b.locs
end

YaoBase.nqubits(::Concentrator{N}) where N = N
YaoBase.nactive(c::Concentrator) = length(c.locations)
YaoBase.nactive(c::Concentrator) = length(c.locs)

function YaoBase.iscommute(x::Concentrator{N}, y::Concentrator{N}) where N
if occupied_locs(x) == occupied_locs(y)
isempty(setdiff(occupied_locs(x), occupied_locs(y))) && return true
if x.locs == y.locs
return iscommute(x.content, y.content)
else
return iscommute_fallback(x, y)
Expand Down
2 changes: 1 addition & 1 deletion src/composite/kron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ julia> kron(1=>X, 3=>Y)
Base.kron(blocks::Pair{Int, <:AbstractBlock}...,) = (n->kron(n, blocks...))
Base.kron(blocks::Base.Generator) = kron(blocks...)

occupied_locs(k::KronBlock) = Iterators.flatten(map(x-> x + i - 1, occupied_locs(b)) for (i, b) in zip(k.locs, subblocks(k)))
occupied_locs(k::KronBlock) = (Iterators.flatten(map(x-> x + i - 1, occupied_locs(b)) for (i, b) in zip(k.locs, subblocks(k)))...,)
subblocks(x::KronBlock) = x.blocks
chsubblocks(pb::KronBlock{N}, it) where N = KronBlock{N}(pb.locs, collect(it))
cache_key(x::KronBlock) = [cache_key(each) for each in x.blocks]
Expand Down
2 changes: 1 addition & 1 deletion src/composite/pauli_strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ subblocks(ps::PauliString) = ps.blocks
chsubblocks(pb::PauliString, blocks::Vector) = PauliString(blocks)
chsubblocks(pb::PauliString, it) = PauliString(collect(it))

occupied_locs(ps::PauliString) = findall(x->!(x isa I2Gate), ps.blocks)
occupied_locs(ps::PauliString) = (findall(x->!(x isa I2Gate), ps.blocks)...,)

cache_key(ps::PauliString) = map(cache_key, ps.blocks)

Expand Down
2 changes: 1 addition & 1 deletion src/composite/put_block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ julia> put(1=>X)
"""
put(pa::Pair) = (n -> put(n, pa))

occupied_locs(x::PutBlock) = x.locs
occupied_locs(x::PutBlock) = map(i->x.locs[i], x.content |> occupied_locs)
chsubblocks(x::PutBlock{N}, b::AbstractBlock) where N = PutBlock{N}(b, x.locs)
PreserveStyle(::PutBlock) = PreserveAll()
cache_key(pb::PutBlock) = cache_key(pb.content)
Expand Down
Loading

0 comments on commit 9e74c7b

Please sign in to comment.