Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
polish viz circuit (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu authored Dec 10, 2021
1 parent 7bd1638 commit df5f622
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.jl.cov
*.jl.mem
Manifest.toml
*.swp
62 changes: 39 additions & 23 deletions src/vizcircuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,29 @@ module CircuitStyles
const fontfamily = Ref("Helvetica Neue")
const linecolor = Ref("#000000")
const gate_bgcolor = Ref("#FFFFFF")
G() = compose(context(), rectangle(-r[], -r[], 2*r[], 2*r[]), fill(gate_bgcolor[]), stroke(linecolor[]), linewidth(lw[]))
const textcolor = Ref("#000000")
const scale = Ref(1.0)
getlw() = lw[] * scale[]
gettextsize() = textsize[] * scale[]
getparamtextsize() = paramtextsize[] * scale[]
G() = compose(context(), rectangle(-r[], -r[], 2*r[], 2*r[]), fill(gate_bgcolor[]), stroke(linecolor[]), linewidth(getlw()))
C() = compose(context(), circle(0.0, 0.0, r[]/3), fill(linecolor[]), linewidth(0))
NC() = compose(context(), circle(0.0, 0.0, r[]/3), fill(gate_bgcolor[]), stroke(linecolor[]), linewidth(lw[]))
NC() = compose(context(), circle(0.0, 0.0, r[]/3), fill(gate_bgcolor[]), stroke(linecolor[]), linewidth(getlw()))
X() = compose(context(), xgon(0.0, 0.0, r[], 4), fill(linecolor[]), linewidth(0))
NOT() = compose(context(),
(context(), circle(0.0, 0.0, r[]), stroke(linecolor[]), linewidth(lw[]), fill("transparent")),
(context(), polygon([(-r[], 0.0), (r[], 0.0)]), stroke(linecolor[]), linewidth(lw[])),
(context(), polygon([(0.0, -r[]), (0.0, r[])]), stroke(linecolor[]), linewidth(lw[]))
(context(), circle(0.0, 0.0, r[]), stroke(linecolor[]), linewidth(getlw()), fill("transparent")),
(context(), polygon([(-r[], 0.0), (r[], 0.0)]), stroke(linecolor[]), linewidth(getlw())),
(context(), polygon([(0.0, -r[]), (0.0, r[])]), stroke(linecolor[]), linewidth(getlw()))
)
WG() = compose(context(), rectangle(-1.5*r[], -r[], 3*r[], 2*r[]), fill(gate_bgcolor[]), stroke(linecolor[]), linewidth(lw[]))
MULTIGATE(h) = compose(context(), rectangle(-1.5*r[], -(h/2+r[]), 3*r[], (h+2*r[])), fill(gate_bgcolor[]), stroke(linecolor[]), linewidth(lw[]))
LINE() = compose(context(), line(), stroke(linecolor[]), linewidth(lw[]))
TEXT() = compose(context(), text(0.0, 0.0, "", hcenter, vcenter), fontsize(textsize[]), font(fontfamily[]))
PARAMTEXT() = compose(context(), text(0.0, 0.0, "", hcenter, vcenter), fontsize(paramtextsize[]), font(fontfamily[]))
WG() = compose(context(), rectangle(-1.5*r[], -r[], 3*r[], 2*r[]), fill(gate_bgcolor[]), stroke(linecolor[]), linewidth(getlw()))
MULTIGATE(h) = compose(context(), rectangle(-1.5*r[], -(h/2+r[]), 3*r[], (h+2*r[])), fill(gate_bgcolor[]), stroke(linecolor[]), linewidth(getlw()))
LINE() = compose(context(), line(), stroke(linecolor[]), linewidth(getlw()))
TEXT() = compose(context(), text(0.0, 0.0, "", hcenter, vcenter), fontsize(gettextsize()), fill(textcolor[]), font(fontfamily[]))
PARAMTEXT() = compose(context(), text(0.0, 0.0, "", hcenter, vcenter), fontsize(getparamtextsize()), fill(textcolor[]), font(fontfamily[]))
MEASURE() = compose(context(),
rectangle(-r[], -r[], 2*r[], 2*r[]), fill(gate_bgcolor[]), stroke(linecolor[]), linewidth(lw[]),
compose(context(), curve((-0.8*r[], 0.5*r[]), (-0.8*r[], -0.6*r[]), (0.8*r[], -0.6*r[]), (0.8*r[], 0.5*r[])), stroke(linecolor[]), linewidth(lw[])),
compose(context(), line([(0.0, 0.5*r[]), (0.7*r[], -0.4*r[])]), stroke(linecolor[]), linewidth(lw[])),
rectangle(-r[], -r[], 2*r[], 2*r[]), fill(gate_bgcolor[]), stroke(linecolor[]), linewidth(getlw()),
compose(context(), curve((-0.8*r[], 0.5*r[]), (-0.8*r[], -0.6*r[]), (0.8*r[], -0.6*r[]), (0.8*r[], 0.5*r[])), stroke(linecolor[]), linewidth(getlw())),
compose(context(), line([(0.0, 0.5*r[]), (0.7*r[], -0.4*r[])]), stroke(linecolor[]), linewidth(getlw())),
begin
ns = Viznet.nodestyle(:triangle, fill(linecolor[]); r=0.1*r[], θ=atan(0.7, 0.9))
Viznet.inner_most_containers(ns) do c
Expand Down Expand Up @@ -126,11 +131,18 @@ function _draw_continuous_multiqubit!(c::CircuitGrid, loc_text)
end
end

function finalize!(c::CircuitGrid; show_ending=true)
function initialize!(c::CircuitGrid; starting_texts, starting_offset)
starting_texts !== nothing && for j=1:nline(c)
CircuitStyles.TEXT() >> (c[starting_offset, j], string(starting_texts[j]))
end
end

function finalize!(c::CircuitGrid; show_ending_bar, ending_offset, ending_texts)
i = frontier(c, 1, nline(c)) + 1
for j=1:nline(c)
show_ending && CircuitStyles.LINE() >> c[(i, j-0.2); (i, j+0.2)]
show_ending_bar && CircuitStyles.LINE() >> c[(i, j-0.2); (i, j+0.2)]
CircuitStyles.LINE() >> c[(i, j); (c.frontier[j], j)]
ending_texts !== nothing && CircuitStyles.TEXT() >> (c[i+ending_offset, j], string(ending_texts[j]))
end
c.frontier .= i
end
Expand Down Expand Up @@ -223,8 +235,8 @@ end
get_brush_texts(b::ConstGate.CNOTGate) = [(CircuitStyles.C(), ""), (CircuitStyles.X(), "")]
get_brush_texts(b::ConstGate.CZGate) = [(CircuitStyles.C(), ""), (CircuitStyles.C(), "")]
get_brush_texts(b::ConstGate.ToffoliGate) = [(CircuitStyles.C(), ""), (CircuitStyles.C(), ""), (CircuitStyles.X(), "")]
get_brush_texts(b::ConstGate.SdagGate) = [(CircuitStyles.G(), "S")]
get_brush_texts(b::ConstGate.TdagGate) = [(CircuitStyles.G(), "T")]
get_brush_texts(b::ConstGate.SdagGate) = [(CircuitStyles.G(), "S'")]
get_brush_texts(b::ConstGate.TdagGate) = [(CircuitStyles.G(), "T'")]
get_brush_texts(b::ConstGate.PuGate) = [(CircuitStyles.G(), "P+")]
get_brush_texts(b::ConstGate.PdGate) = [(CircuitStyles.G(), "P-")]
get_brush_texts(b::ConstGate.P0Gate) = [(CircuitStyles.G(), "P₀")]
Expand All @@ -249,17 +261,21 @@ get_cbrush_texts(b::ZGate) = [(CircuitStyles.C(), "")]

# front end
plot(blk::AbstractBlock; kwargs...) = vizcircuit(blk; kwargs...)
function vizcircuit(blk::AbstractBlock; w_depth=0.85, w_line=0.75, scale=1.0, show_ending=true)
circuit_canvas(nqubits(blk); w_depth=w_depth, w_line=w_line, show_ending=show_ending) do c
function vizcircuit(blk::AbstractBlock; w_depth=0.85, w_line=0.75, scale=1.0, show_ending_bar=false, starting_texts=nothing, starting_offset=-0.3, ending_texts=nothing, ending_offset=0.3)
CircuitStyles.scale[] = scale
img = circuit_canvas(nqubits(blk); w_depth=w_depth, w_line=w_line, show_ending_bar=show_ending_bar, starting_texts=starting_texts, starting_offset=starting_offset, ending_texts=ending_texts, ending_offset=ending_offset) do c
basicstyle(blk) >> c
end |> rescale(scale)
end
CircuitStyles.scale[] = 1.0
return img |> rescale(scale)
end

function circuit_canvas(f, nline::Int; w_depth=0.85, w_line=0.75, show_ending=true)
function circuit_canvas(f, nline::Int; w_depth=0.85, w_line=0.75, show_ending_bar=false, starting_texts=nothing, starting_offset=-0.3, ending_texts=nothing, ending_offset=0.3)
c = CircuitGrid(nline; w_depth=w_depth, w_line=w_line)
g = canvas() do
f(c)
finalize!(c; show_ending=show_ending)
initialize!(c; starting_texts=starting_texts, starting_offset=starting_offset)
f(c)
finalize!(c; show_ending_bar=show_ending_bar, ending_texts=ending_texts, ending_offset=ending_offset)
end
a, b = (depth(c)+1)*w_depth, nline*w_line
Compose.set_default_graphic_size(a*2.5*cm, b*2.5*cm)
Expand Down

0 comments on commit df5f622

Please sign in to comment.