Skip to content

Commit

Permalink
Fix drawing of rectangles on top of deleted wires.
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed May 1, 2021
1 parent ad78f14 commit 92c46d2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Quantikz"
uuid = "b0d11df0-eea3-4d79-b4a5-421488cbf74b"
authors = ["Stefan Krastanov <[email protected]>"]
version = "1.0.3"
version = "1.0.4"

[deps]
EndpointRanges = "340492b5-2a47-5f55-813d-aca7ddf97656"
Expand Down
50 changes: 22 additions & 28 deletions src/Quantikz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ MultiControlU(str::AbstractString,control::Integer,target::Integer) = MultiContr
MultiControlU(control::Integer,target::Integer) = MultiControlU("\\;\\;",[control],[],[target])

affectedqubits(g::MultiControlU) = [g.control...,g.ocontrol...,g.target...]
function update_table!(qtable,step,g::MultiControlU) # TODO displaycircuit([CNOT([1,4],[3],[2,5])]) has bad ocircle covered by line and a disconnected target
function update_table!(qtable,step,g::MultiControlU)
table = qtable.table
control = g.control
ocontrol = g.ocontrol
Expand All @@ -130,15 +130,7 @@ function update_table!(qtable,step,g::MultiControlU) # TODO displaycircuit([CNOT
else
startpoint = min(M,controls[1][2])
end
offset = iseven(M-m) && (m+M)/2 target ? ",label style={yshift=0.2cm}" : ""
table[m,step] = "\\gate[$(M-m+1)$(offset)]{$(g.str)}"
for i in m+1:M
if i target
table[i,step] = "\\linethrough"
else
table[i,step] = ""
end
end
draw_rectangle!(table,m,M,step,target,g.str)
for (str, i) in controls
if i > M && startpoint < M
if startpoint < m
Expand All @@ -155,6 +147,24 @@ function update_table!(qtable,step,g::MultiControlU) # TODO displaycircuit([CNOT
qtable
end

function draw_rectangle!(table,m,M,step,targets,str)
deleted = Int[]
for i in m+1:M
if i targets
if strip(table[i,step-1])==""
push!(deleted, i)
else
table[i,step] = "\\linethrough"
end
else
table[i,step] = ""
end
end
offset = iseven(M-m) && ((m+M)/2 vcat(targets,deleted)) ? ",label style={yshift=0.2cm}" : ""
nwires = isempty(deleted) ? "" : ",nwires={$(join(deleted,","))}"
table[m,step] = "\\gate[$(M-m+1)$(offset)$(nwires)]{$(str)}"
end

struct U <: QuantikzOp
str::AbstractString
target::Integer
Expand Down Expand Up @@ -210,15 +220,7 @@ function update_table!(qtable,step,meas::Measurement)
else
step = step+1
m,M = extrema(meas.targets)
offset = iseven(M-m) && (m+M)/2 meas.targets ? ",label style={yshift=0.2cm}" : ""
table[m,step] = "\\gate[$(M-m+1)$(offset)]{$(meas.str)}"
for i in m+1:M
if i meas.targets
table[i,step] = "\\linethrough"
else
table[i,step] = ""
end
end
draw_rectangle!(table,m,M,step,meas.targets,meas.str)
qubitsview(qtable)[meas.targets,step+1] .= "\\qw"
ancillaryview(qtable)[1,step-1] = "\\lstick{}"
ancillaryview(qtable)[1,step] = "\\ctrl{$(M-qtable.qubits-1)}"
Expand Down Expand Up @@ -250,15 +252,7 @@ affectedbits(g::ClassicalDecision) = g.bits
function update_table!(qtable,step,g::ClassicalDecision)
table = qtable.table
m,M = extrema(g.targets) # TODO this piece of code is repeated frequently, abstract it away
offset = iseven(M-m) && (m+M)/2 g.targets ? ",label style={yshift=0.2cm}" : ""
table[m,step] = "\\gate[$(M-m+1)$(offset)]{$(g.str)}"
for i in m+1:M
if i g.targets
table[i,step] = "\\linethrough"
else
table[i,step] = ""
end
end
draw_rectangle!(table,m,M,step,g.targets,g.str)
startpoint = minimum(g.bits)
bitsview(qtable)[startpoint,step] = "\\cwbend{$(-(qtable.qubits-M)-qtable.ancillaries-startpoint)}"
for b in sort(g.bits)[2:end]
Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ end
@test circuit2string([Measurement(2),Measurement(1,1)]) == circuit2string([Measurement(2),Measurement(1,1)], mode=:expanded) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\qw & \\meterD{} & \\\\\n\\qw & \\meterD{} & & \\\\\n\\cw & \\cw & \\cwbend{-2} & \\cw\n\\end{quantikz}"
end

@testset "Avoiding placement of already deleted wires on top of rectangles" begin
c =[Measurement(2),
U(3), U(3),
MultiControlU("A", [1, 3]),
U(3),
U("A", [1,4]),
U(3),
U([1,3]),
U(3),
Measurement([1,3]),
U(3),
ClassicalDecision([1,3],1)]
@test circuit2string(c) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\qw & \\qw & \\gate[3,nwires={2}]{A} & \\qw & \\gate[4,nwires={2}]{A} & \\qw & \\gate[3,nwires={2}]{\\;\\;} & \\qw & \\qw & \\gate[3,nwires={2}]{\\;\\;} & \\qw & \\qw & \\gate[3,nwires={2}]{\\;\\;} & \\qw\\\\\n\\qw & \\meterD{} & & & & & & & & & & & & & \\\\\n\\qw & \\gate{\\;\\;} & \\gate{\\;\\;} & & \\gate{\\;\\;} & \\linethrough & \\gate{\\;\\;} & & \\gate{\\;\\;} & \\qw & & \\qw & \\gate{\\;\\;} & & \\qw\\\\\n\\qw & \\qw & \\qw & \\qw & \\qw & & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw\\\\\n & & & & & & & & & \\lstick{} & \\ctrl{-2} & \\meterD{} & & & \\\\\n\\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cwbend{-3} & \\cw\n\\end{quantikz}"
@test circuit2string(c,mode=:expanded) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\qw & \\qw & \\qw & \\gate[3,nwires={2}]{A} & \\qw & \\gate[4,nwires={2}]{A} & \\qw & \\gate[3,nwires={2}]{\\;\\;} & \\qw & \\qw & \\gate[3,nwires={2}]{\\;\\;} & \\qw & \\qw & \\gate[3,nwires={2}]{\\;\\;} & \\qw\\\\\n\\qw & \\meterD{} & & & & & & & & & & & & & & \\\\\n\\qw & \\qw & \\gate{\\;\\;} & \\gate{\\;\\;} & & \\gate{\\;\\;} & \\linethrough & \\gate{\\;\\;} & & \\gate{\\;\\;} & \\qw & & \\qw & \\gate{\\;\\;} & & \\qw\\\\\n\\qw & \\qw & \\qw & \\qw & \\qw & \\qw & & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw\\\\\n & & & & & & & & & & \\lstick{} & \\ctrl{-2} & \\meterD{} & & & \\\\\n\\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cwbend{-3} & \\cw\n\\end{quantikz}"
end


end

function filetests()
Expand Down

2 comments on commit 92c46d2

@Krastanov
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/35799

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.4 -m "<description of version>" 92c46d26bfdf95ef005b741185763b8f7936980c
git push origin v1.0.4

Please sign in to comment.