Skip to content

Commit

Permalink
annotate a single line
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu committed Jul 2, 2024
1 parent eabb0bf commit 8e90cc1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/src/man/registers.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ We also have some "cheating" version [`measure`](@ref) that does not collapse st
```@repl register
measure!(reg0, 1) # measure the qubit, the state collapses
measure!(reg0) # measure all qubits
measure(reg0, 3) # measure the qubit 3 times, the state does not collapse (hacky)
measure(reg0, 3) # measure the qubit at location 3, the state does not collapse (hacky)
reorder!(reg0, 7:-1:1) # reorder the qubits
measure!(reg0)
invorder!(reg0) # reverse the order of qubits
Expand Down
2 changes: 2 additions & 0 deletions lib/YaoPlots/src/YaoPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import Thebes
using Luxor: @layer, Point
using Thebes: Point3D, project
using LinearAlgebra: tr
using YaoBlocks

export CircuitStyles, vizcircuit, darktheme!, lighttheme!
export bloch_sphere, BlochStyles
export plot
export LabelBlock, addlabel, LineAnnotation, line_annotation

"""An alias of `vizcircuit`"""
plot(;kwargs...) = x->plot(x;kwargs...)
Expand Down
18 changes: 14 additions & 4 deletions lib/YaoPlots/src/helperblock.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using YaoBlocks
export LabelBlock

"""
LabelBlock{BT,D} <: TagBlock{BT,D}
Expand Down Expand Up @@ -31,7 +28,6 @@ Base.adjoint(x::LabelBlock) = LabelBlock(adjoint(content(x)), endswith(x.name, "
Base.copy(x::LabelBlock) = LabelBlock(copy(content(x)), x.name, x.color)
YaoBlocks.Optimise.to_basictypes(block::LabelBlock) = block

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

# to fix issue
Expand All @@ -49,3 +45,17 @@ function YaoBlocks.print_tree(
)
print(io, node.name)
end

"""
LineAnnotation{D} <: TrivialGate{D}
"""
struct LineAnnotation{D} <: TrivialGate{D}
name::String
color::String
end
line_annotation(name::String; color="black", nlevel=2) = LineAnnotation{nlevel}(name, color)

Base.copy(x::LineAnnotation) = LineAnnotation(x.name, x.color)
YaoBlocks.Optimise.to_basictypes(block::LineAnnotation) = block
YaoBlocks.nqudits(::LineAnnotation) = 1
YaoBlocks.print_block(io::IO, blk::LineAnnotation) = YaoBlocks.printstyled(io, blk.name; color=Symbol(blk.color))
12 changes: 12 additions & 0 deletions lib/YaoPlots/src/vizcircuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,18 @@ function draw!(c::CircuitGrid, cb::LabelBlock, address, controls)
CircuitStyles.gate_bgcolor[] = temp
end

function draw!(c::CircuitGrid, cb::LineAnnotation, address, controls)
@assert length(address) == 1 && isempty(controls) "LineAnnotation should be a single line, without control."
CircuitStyles.textcolor[], temp = cb.color, CircuitStyles.textcolor[]
_annotate!(c, address[1], cb.name)
CircuitStyles.textcolor[] = temp
end
function _annotate!(c::CircuitGrid, loc::Integer, name::AbstractString)
wspace, fontsize = text_width_and_size(name)
i = frontier(c, loc) + 0.1
CircuitStyles.render(CircuitStyles.Text(fontsize), (c[i, loc-0.2], name, wspace, fontsize))
end

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

@testset "annotate line" begin
ann = line_annotation("test"; color="red")
@test ann isa LineAnnotation
c = chain(put(5, 2=>X), put(5, 2=>ann), put(5, 3=>Y))
@test vizcircuit(c) isa Drawing
end

0 comments on commit 8e90cc1

Please sign in to comment.