Skip to content

Commit

Permalink
Merge branch 'master' into jg/upgrade-Luxor-5
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu authored Jul 2, 2024
2 parents 51fe39d + eabb0bf commit 013fdd4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
12 changes: 6 additions & 6 deletions lib/YaoBlocks/test/primitive/time_evolution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,18 @@ end
mpb = mat(pb)
allpass = true
for i=basis(pb), j=basis(pb)
allpass &= isapprox(pb[i, j], mpb[Int(i)+1, Int(j)+1]; atol=1e-6)
allpass &= isapprox(pb[i, j], mpb[Int(i)+1, Int(j)+1]; atol=1e-6, rtol=1e-6)
end
@test allpass

allpass = true
for j=basis(pb)
allpass &= isapprox(vec(pb[:, j]), mpb[:, Int(j)+1]; atol=1e-6)
allpass &= isapprox(vec(pb[j,:]), mpb[Int(j)+1,:]; atol=1e-6)
allpass &= isapprox(vec(pb[:, EntryTable([j], [1.0+0im])]), mpb[:, Int(j)+1]; atol=1e-6)
allpass &= isapprox(vec(pb[EntryTable([j], [1.0+0im]),:]), mpb[Int(j)+1,:]; atol=1e-6)
allpass &= isapprox(vec(pb[:, j]), mpb[:, Int(j)+1]; atol=1e-6, rtol=1e-6)
allpass &= isapprox(vec(pb[j,:]), mpb[Int(j)+1,:]; atol=1e-6, rtol=1e-6)
allpass &= isapprox(vec(pb[:, EntryTable([j], [1.0+0im])]), mpb[:, Int(j)+1]; atol=1e-6, rtol=1e-6)
allpass &= isapprox(vec(pb[EntryTable([j], [1.0+0im]),:]), mpb[Int(j)+1,:]; atol=1e-6, rtol=1e-6)
allpass &= isclean(pb[:,j])
end
@test allpass
end
end
end
13 changes: 7 additions & 6 deletions lib/YaoPlots/src/helperblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ A marker to mark a circuit applying on a continous block for better plotting.
struct LabelBlock{BT<:AbstractBlock,D} <: TagBlock{BT,D}
content::BT
name::String
color::String
end

YaoBlocks.content(cb::LabelBlock) = cb.content
function LabelBlock(x::BT, name::String) where {D,BT<:AbstractBlock{D}}
LabelBlock{BT,D}(x, name)
function LabelBlock(x::BT, name::String, color::String) where {D,BT<:AbstractBlock{D}}
LabelBlock{BT,D}(x, name, color)
end

function is_continuous_chunk(x)
Expand All @@ -24,14 +25,14 @@ end
YaoBlocks.PropertyTrait(::LabelBlock) = YaoBlocks.PreserveAll()
YaoBlocks.mat(::Type{T}, blk::LabelBlock) where {T} = mat(T, content(blk))
YaoBlocks.unsafe_apply!(reg::YaoBlocks.AbstractRegister, blk::LabelBlock) = YaoBlocks.unsafe_apply!(reg, content(blk))
YaoBlocks.chsubblocks(blk::LabelBlock, target::AbstractBlock) = LabelBlock(target, blk.name)
YaoBlocks.chsubblocks(blk::LabelBlock, target::AbstractBlock) = LabelBlock(target, blk.name, blk.color)

Base.adjoint(x::LabelBlock) = LabelBlock(adjoint(content(x)), endswith(x.name, "") ? x.name[1:end-1] : x.name*"")
Base.copy(x::LabelBlock) = LabelBlock(copy(content(x)), x.name)
Base.adjoint(x::LabelBlock) = LabelBlock(adjoint(content(x)), endswith(x.name, "") ? x.name[1:end-1] : x.name*"", x.color)
Base.copy(x::LabelBlock) = LabelBlock(copy(content(x)), x.name, x.color)
YaoBlocks.Optimise.to_basictypes(block::LabelBlock) = block

export addlabel
addlabel(b::AbstractBlock, str::String) = LabelBlock(b, str)
addlabel(b::AbstractBlock; name=string(b), color="transparent") = LabelBlock(b, name, color)

# to fix issue
function YaoBlocks.print_tree(
Expand Down
10 changes: 8 additions & 2 deletions lib/YaoPlots/src/vizcircuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,20 @@ function draw!(c::CircuitGrid, cb::ControlBlock{GT,C}, address, controls) where
draw!(c, cb.content, locs, [controls..., mycontrols...])
end

for B in [:LabelBlock, :GeneralMatrixBlock, :Add]
for B in [:GeneralMatrixBlock, :Add]
@eval function draw!(c::CircuitGrid, cb::$B, address, controls)
length(address) == 0 && return
#is_continuous_chunk(address) || error("address not continuous in a block marked as continous.")
_draw!(c, [controls..., (address, c.gatestyles.g, string(cb))])
end
end

function draw!(c::CircuitGrid, cb::LabelBlock, address, controls)
length(address) == 0 && return
CircuitStyles.gate_bgcolor[], temp = cb.color, CircuitStyles.gate_bgcolor[]
_draw!(c, [controls..., (address, c.gatestyles.g, string(cb))])
CircuitStyles.gate_bgcolor[] = temp
end

# [:KronBlock, :RepeatedBlock, :CachedBlock, :Subroutine, :(YaoBlocks.AD.NoParams)]
function draw!(c::CircuitGrid, p::CompositeBlock, address, controls)
barrier_style = CircuitStyles.barrier_for_chain[]
Expand Down
6 changes: 3 additions & 3 deletions lib/YaoPlots/test/helperblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Test

@testset "LabelBlock" begin
x = put(5, (2,3)=>matblock(rand_unitary(4)))
cb = LabelBlock(x, "x")
cb = LabelBlock(x, "x", "red")
@test mat(copy(cb)) == mat(cb)
@test isunitary(cb)
@test ishermitian(cb) == ishermitian(x)
Expand All @@ -21,8 +21,8 @@ using Test
@test YaoPlots.is_continuous_chunk([1,2,4]) == false
@test YaoPlots.is_continuous_chunk([3,2,4]) == true

c1 = chain(5, [put(5, (2,3)=>addlabel(SWAP, "SWAP")), put(5, 2=>addlabel(I2, "id")), put(5, 2=>addlabel(X, "X")), control(5, (5,3), (2,4,1)=>put(3, (1,3)=>addlabel(SWAP, "SWAP")))])
c2 = chain(5, [put(5, (2,3)=>addlabel(SWAP, "SWAP")), put(5, 2=>addlabel(I2, "id")), put(5, 2=>addlabel(X, "X")), control(5, (5,3), (2,4,1)=>put(3, (1,2)=>addlabel(SWAP, "SWAP")))])
c1 = chain(5, [put(5, (2,3)=>addlabel(SWAP; name="SWAP")), put(5, 2=>addlabel(I2; name="id")), put(5, 2=>addlabel(X; name="X")), control(5, (5,3), (2,4,1)=>put(3, (1,3)=>addlabel(SWAP; name="SWAP")))])
c2 = chain(5, [put(5, (2,3)=>addlabel(SWAP; name="SWAP")), put(5, 2=>addlabel(I2; name="id")), put(5, 2=>addlabel(X; name="X")), control(5, (5,3), (2,4,1)=>put(3, (1,2)=>addlabel(SWAP; name="SWAP")))])

@test vizcircuit(c1) isa Drawing
@test vizcircuit(c2) isa Drawing
Expand Down

0 comments on commit 013fdd4

Please sign in to comment.