Skip to content

Commit

Permalink
permit ClassicalControl with no quantum gate
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed Aug 31, 2023
1 parent 64b437a commit 88a6f99
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 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.3"
version = "1.3.1"

[deps]
EndpointRanges = "340492b5-2a47-5f55-813d-aca7ddf97656"
Expand Down
17 changes: 13 additions & 4 deletions src/Quantikz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,17 +304,22 @@ ClassicalDecision(t::Integer, c::Integer) = ClassicalDecision("\\;\\;", [t], [c]
ClassicalDecision(t::ArrayOrRange, c::Integer) = ClassicalDecision("\\;\\;", t, [c])
ClassicalDecision(t::Integer, c::ArrayOrRange) = ClassicalDecision("\\;\\;", [t], c)
ClassicalDecision(t::ArrayOrRange, c::ArrayOrRange) = ClassicalDecision("\\;\\;", t, c)
ClassicalDecision(c::ArrayOrRange) = ClassicalDecision("", [], c)

affectedqubits(g::ClassicalDecision) = g.targets
affectedbits(g::ClassicalDecision) = g.bits
function update_table!(qtable,step,g::ClassicalDecision)
qvtable = qubitsview(qtable)
bvtable = bitsview(qtable)
m, M = explicit_extrema(qvtable, g.targets)
draw_rectangle!(qvtable,step,g.targets,g.str)
bits = explicit_targets(bvtable, g.bits)
startpoint = minimum(bits)
bvtable[startpoint,step] = "\\cwbend{$(-(qtable.qubits-M)-qtable.ancillaries-startpoint)}"
if !isa(g.targets,AbstractVector) || length(g.targets)>0
m, M = explicit_extrema(qvtable, g.targets)
draw_rectangle!(qvtable,step,g.targets,g.str)
bvtable[startpoint,step] = "\\cwbend{$(-(qtable.qubits-M)-qtable.ancillaries-startpoint)}"
else
bvtable[startpoint,step] = "\\cwbend{0}"
end
for b in sort(bits)[2:end]
bvtable[b,step] = "\\cwbend{$(startpoint-b)}"
startpoint = b
Expand Down Expand Up @@ -410,7 +415,11 @@ function circuit2table_compressed(circuit, qubits)
for op in circuit
qubits = extrema2range(explicit_extrema(qvtable, affectedqubits(op)))
if circuitwidthbits(op)!=0 || neededancillaries(op)!=0
qubits = minimum(qubits):iend
qubits = if length(qubits)==0
ibegin:iend # TODO this branch is too pessimistic
else
minimum(qubits):iend
end
end
bits = extrema2range(explicit_extrema(bvtable, affectedbits(op)))
ancillaries = neededancillaries(op)
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,19 @@ end
@testset "EndpointRanges in ClassicalDecision" begin
circuit = [U(5),ClassicalDecision(ibegin:iend,2),ClassicalDecision(1,ibegin:iend)]
@test circuit2string(circuit) == circuit2string(circuit,mode=:expanded) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\qw & \\gate[5,disable auto height]{\\;\\;} & \\gate[1]{\\;\\;} & \\qw\\\\\n\\qw & \\qw & \\qw & \\qw & \\qw\\\\\n\\qw & \\qw & \\qw & \\qw & \\qw\\\\\n\\qw & \\qw & \\qw & \\qw & \\qw\\\\\n\\qw & \\gate{\\;\\;} & \\qw & \\qw & \\qw\\\\\n\\cw & \\cw & \\cw & \\cwbend{-5} & \\cw\\\\\n\\cw & \\cw & \\cwbend{-2} & \\cwbend{-1} & \\cw\n\\end{quantikz}"
circuit2string(circuit,mode=:compressed)
circuit = [ClassicalDecision(ibegin:iend,ibegin:iend)]
@test circuit2string(circuit) == circuit2string(circuit,mode=:expanded) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\gate[1]{\\;\\;} & \\qw\\\\\n\\cw & \\cwbend{-1} & \\cw\n\\end{quantikz}"
circuit2string(circuit,mode=:compressed)
end

@testset "no quantum component in ClassicalDecision" begin
circuit = [U(5),ClassicalDecision([2]),ClassicalDecision(ibegin:iend)]
@test circuit2string(circuit) == circuit2string(circuit,mode=:expanded)
circuit2string(circuit,mode=:compressed)
end


@testset "Consecutive gates should not confuse the `deleted` count in `draw_rectangle`" begin
circuit = [MultiControlU("XYZ", [1], [2], [3,4,5]), MultiControlU("U_a", [1], [2], [3,5])]
@test circuit2string(circuit) == circuit2string(circuit,mode=:expanded) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\ctrl{0} & \\ctrl{0} & \\qw\\\\\n\\qw & \\octrl{-1} & \\octrl{-1} & \\qw\\\\\n\\qw & \\gate[3,disable auto height]{XYZ}\\vqw{-1} & \\gate[3,label style={yshift=0.2cm},disable auto height]{U_a}\\vqw{-1} & \\qw\\\\\n\\qw & \\qw & \\linethrough & \\qw\\\\\n\\qw & \\qw & \\qw & \\qw\n\\end{quantikz}"
Expand Down

2 comments on commit 88a6f99

@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/90538

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.3.1 -m "<description of version>" 88a6f991b2e843c83853f3b573ff2ba992f54162
git push origin v1.3.1

Please sign in to comment.