Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transpsoe block #488

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/YaoBlocks/src/composite/composite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ include("add.jl")
include("tag/tag.jl")
include("tag/cache.jl")
include("tag/dagger.jl")
include("tag/scale.jl")
include("tag/transpose.jl")
include("tag/scale.jl")
58 changes: 58 additions & 0 deletions lib/YaoBlocks/src/composite/tag/transpose.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export Transposed

"""
Transposed{BT, D} <: TagBlock{BT,D}

Wrapper block allowing to execute the inverse of a block of quantum circuit.
"""
struct Transposed{BT<:AbstractBlock,D} <: TagBlock{BT,D}
content::BT
end

"""
Transposed(block)

Create a [`Transposed`](@ref) block.
Let ``G`` be a input block, `G'` or `Transposed(block)` in code represents ``G^\\dagger``.

### Examples

The inverse QFT is not hermitian, thus it will be tagged with a `Transposed` block.

```jldoctest; setup=:(using YaoBlocks)
julia> A(i, j) = control(i, j=>shift(2π/(1<<(i-j+1))));

julia> B(n, i) = chain(n, i==j ? put(i=>H) : A(j, i) for j in i:n);

julia> qft(n) = chain(B(n, i) for i in 1:n);

julia> struct QFT <: PrimitiveBlock{2} n::Int end

julia> YaoBlocks.nqudits(q::QFT) = q.n


julia> circuit(q::QFT) = qft(nqubits(q));

julia> YaoBlocks.mat(x::QFT) = mat(circuit(x));

julia> QFT(2)'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example should be fixed.

[†]QFT
```
"""
Transposed(x::BT) where {D,BT<:AbstractBlock{D}} = Transposed{BT,D}(x)

PropertyTrait(::Transposed) = PreserveAll()
mat(::Type{T}, blk::Transposed) where {T} = transpose(mat(T, content(blk)))
chsubblocks(blk::Transposed, target::AbstractBlock) = Transposed(target)

Base.transpose(x::AbstractBlock) = ishermitian(x) ? x : Transposed(x)
Base.transpose(x::Transposed) = content(x)
Base.copy(x::Transposed) = Transposed(copy(content(x)))

function unsafe_getindex(::Type{T}, d::Transposed, i::Integer, j::Integer) where {T}
return unsafe_getindex(T, content(d), j, i) |> conj
end
function unsafe_getcol(::Type{T}, d::Transposed, j::DitStr{D}) where {T,D}
locs, vals = force_getrowconj(T, content(d), j)
return locs, vals
end
2 changes: 2 additions & 0 deletions lib/YaoBlocks/src/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ end
print_annotation(io::IO, node::AbstractBlock) = nothing # skip
print_annotation(io::IO, c::Daggered) =
printstyled(io, " [†]"; bold = true, color = :yellow)
print_annotation(io::IO, c::Transposed) =
printstyled(io, " [T]"; bold = true, color = :yellow)
print_annotation(io::IO, c::CachedBlock) =
printstyled(io, "[cached] "; bold = true, color = :yellow)

Expand Down
Loading