Skip to content

Commit

Permalink
fix property traits (#61)
Browse files Browse the repository at this point in the history
* fix property traits

* add docs
  • Loading branch information
GiggleLiu authored and Roger-luo committed Jul 23, 2019
1 parent 9e74c7b commit 8d0ae48
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 19 deletions.
22 changes: 11 additions & 11 deletions src/composite/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,26 @@ chsubblocks(x::AbstractContainer, it::AbstractBlock) = throw(NotImplementedError
# - use simple traits instead
# - each property should have a trait
# NOTE: this is a holy trait, no overhead, don't use methods on this
abstract type PreserveStyle end
struct PreserveAll <: PreserveStyle end
struct PreserveProperty{F} <: PreserveStyle end
struct PreserveNothing <: PreserveStyle end
"""
PropertyTrait
PropertyTrait(::AbstractContainer) -> PropertyTrait
Typically, it can be `PreserveAll()` for those containers that do not change `ishermitian`, `isunitary` and `isreflexive` properties, otherwise fallback to `PreserveNothing`.
"""
abstract type PropertyTrait end
struct PreserveAll <: PropertyTrait end
struct PreserveNothing <: PropertyTrait end

PreserveStyle(c::AbstractContainer) = PreserveNothing()
PropertyTrait(c::AbstractContainer) = PreserveNothing()

for METHOD in (:ishermitian, :isreflexive, :isunitary)
@eval begin
# forward to trait
YaoBase.$METHOD(x::AbstractContainer) = $METHOD(PreserveStyle(x), x)
YaoBase.$METHOD(x::AbstractContainer) = $METHOD(PropertyTrait(x), x)
# forward parent block property
YaoBase.$METHOD(::PreserveAll, c::AbstractContainer) = $METHOD(content(c))
# forward to default property by calculating the matrix
YaoBase.$METHOD(::PreserveNothing, c::AbstractContainer) = $METHOD(mat(c))
# preseve each property
YaoBase.$METHOD(::PreserveProperty{$(QuoteNode(METHOD))}, c::AbstractContainer) =
$METHOD(content(c))
# fallback
YaoBase.$METHOD(::PreserveStyle, c::AbstractContainer) = $METHOD(content(c))
end
end

Expand Down
5 changes: 2 additions & 3 deletions src/composite/concentrator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ concentrate(block::AbstractBlock, locs) = @λ(n->concentrate(n, block, locs))
concentrate(block::Function, locs) = (n->concentrate(n, block, locs))

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

function apply!(r::AbstractRegister, c::Concentrator)
_check_size(r, c)
Expand Down
2 changes: 1 addition & 1 deletion src/composite/control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ for G in [:X, :Y, :Z, :S, :T, :Sdag, :Tdag]
end
end

PreserveStyle(::ControlBlock) = PreserveAll()
PropertyTrait(::ControlBlock) = PreserveAll()

occupied_locs(c::ControlBlock) = (c.ctrl_locs..., map(x->c.locs[x], occupied_locs(c.content))...)
chsubblocks(pb::ControlBlock{N}, blk::AbstractBlock) where {N} = ControlBlock{N}(pb.ctrl_locs, pb.ctrl_config, blk, pb.locs)
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 @@ -69,7 +69,7 @@ put(pa::Pair) = @λ(n -> put(n, pa))

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()
PropertyTrait(::PutBlock) = PreserveAll()
cache_key(pb::PutBlock) = cache_key(pb.content)

mat(::Type{T}, pb::PutBlock{N, 1}) where {T, N} = u1mat(N, mat(T, pb.content), pb.locs...)
Expand Down
2 changes: 1 addition & 1 deletion src/composite/repeated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Base.repeat(x::AbstractBlock, locs) = @λ(n->repeat(n, x, locs...,))

occupied_locs(rb::RepeatedBlock) = (vcat([(i:i+nqubits(rb.content)-1) for i in rb.locs]...)...,)
chsubblocks(x::RepeatedBlock{N}, blk::AbstractBlock) where N = RepeatedBlock{N}(blk, x.locs)
PreserveProperty(x::RepeatedBlock) = PreserveAll()
PropertyTrait(x::RepeatedBlock) = PreserveAll()

mat(::Type{T}, rb::RepeatedBlock{N}) where {T, N} = hilbertkron(N, fill(mat(T, rb.content), length(rb.locs)), [rb.locs...])
mat(::Type{T}, rb::RepeatedBlock{N, 0, GT}) where {T, N, GT} = IMatrix{1<<N, T}()
Expand Down
2 changes: 1 addition & 1 deletion src/composite/tag/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ CacheServers.iscached(c::CachedBlock) = iscached(c.server, c.content)
iscacheable(c::CachedBlock) = iscacheable(c.server, c.content)
chsubblocks(cb::CachedBlock, blk::AbstractBlock) = CachedBlock(cb.server, blk, cb.level)
occupied_locs(x::CachedBlock) = occupied_locs(content(x))
PreserveStyle(::CachedBlock) = PreserveAll()
PropertyTrait(::CachedBlock) = PreserveAll()

function update_cache(c::CachedBlock)
if !iscached(c.server, c.content)
Expand Down
2 changes: 1 addition & 1 deletion src/composite/tag/dagger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ julia> QFT(2)'
Daggered(x::BT) where {N, BT<:AbstractBlock{N}} =
Daggered{BT, N}(x)

PreserveStyle(::Daggered) = PreserveAll()
PropertyTrait(::Daggered) = PreserveAll()
mat(::Type{T}, blk::Daggered) where T = adjoint(mat(T, content(blk)))

Base.adjoint(x::AbstractBlock) = ishermitian(x) ? x : Daggered(x)
Expand Down
1 change: 1 addition & 0 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
@deprecate setiparameters!(args...) setiparams!(args...)
@deprecate niparameters(args...) niparams(args...)
@deprecate parameter_type(args...) parameters_eltype(args...)
@deprecate PreserveStyle(args...) PropertyTrait(args...)
@deprecate Sum(args...) Add(args...)
@deprecate Add(blocks::AbstractVector{<:AbstractBlock{N}}) where {N} Add{N}(blocks)
1 change: 1 addition & 0 deletions test/composite/repeated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ rp = RepeatedBlock{5}(X, (1,2,3))
@test (chsubblocks(rp, [Z]) |> subblocks .== [Z]) |> all
@test occupied_locs(rp) == (1,2,3)
@test rp |> copy == rp
@test YaoBlocks.PropertyTrait(rp) == YaoBlocks.PreserveAll()

0 comments on commit 8d0ae48

Please sign in to comment.