diff --git a/lib/YaoBlocks/test/primitive/time_evolution.jl b/lib/YaoBlocks/test/primitive/time_evolution.jl index 6c11bf05..c67611e7 100644 --- a/lib/YaoBlocks/test/primitive/time_evolution.jl +++ b/lib/YaoBlocks/test/primitive/time_evolution.jl @@ -108,18 +108,18 @@ end mpb = mat(pb) allpass = true for i=basis(pb), j=basis(pb) - allpass &= pb[i, j] ≈ mpb[Int(i)+1, Int(j)+1] + 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 &= vec(pb[:, j]) ≈ mpb[:, Int(j)+1] - allpass &= vec(pb[j,:]) ≈ mpb[Int(j)+1,:] - allpass &= vec(pb[:, EntryTable([j], [1.0+0im])]) ≈ mpb[:, Int(j)+1] - allpass &= vec(pb[EntryTable([j], [1.0+0im]),:]) ≈ mpb[Int(j)+1,:] + 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 \ No newline at end of file +end diff --git a/lib/YaoPlots/src/helperblock.jl b/lib/YaoPlots/src/helperblock.jl index 61aaa941..057a92bb 100644 --- a/lib/YaoPlots/src/helperblock.jl +++ b/lib/YaoPlots/src/helperblock.jl @@ -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) @@ -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( diff --git a/lib/YaoPlots/src/vizcircuit.jl b/lib/YaoPlots/src/vizcircuit.jl index d4d28cb9..76348c0b 100644 --- a/lib/YaoPlots/src/vizcircuit.jl +++ b/lib/YaoPlots/src/vizcircuit.jl @@ -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[] diff --git a/lib/YaoPlots/test/helperblock.jl b/lib/YaoPlots/test/helperblock.jl index 85baaf70..a279634b 100644 --- a/lib/YaoPlots/test/helperblock.jl +++ b/lib/YaoPlots/test/helperblock.jl @@ -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) @@ -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