Skip to content

Commit 82a1657

Browse files
committed
Introduce an Initialize operation.
1 parent 5f5b3dc commit 82a1657

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Quantikz"
22
uuid = "b0d11df0-eea3-4d79-b4a5-421488cbf74b"
33
authors = ["Stefan Krastanov <[email protected]>"]
4-
version = "1.0.7"
4+
version = "1.1.0"
55

66
[deps]
77
EndpointRanges = "340492b5-2a47-5f55-813d-aca7ddf97656"

docs/src/useful.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ MultiControl([1],[2],[3],[4])
7979
ParityMeasurement(["X","Y","Z"], [1,2,4])
8080
```
8181

82+
### Initial states or resets
83+
84+
#### At the beginning of a circuit
85+
86+
```@example 1
87+
Initialize("\\ket{\\phi}", [1,2,3])
88+
```
89+
90+
```@example 1
91+
Initialize("\\ket{\\phi}", [1,2,4])
92+
```
93+
94+
#### Midway through a circuit
95+
96+
```@example 1
97+
[CNOT(1,2), Measurement(3), Initialize("\\ket{\\phi}", [2,3])]
98+
```
99+
100+
```@example 1
101+
[CNOT(1,2), Measurement(1), Measurement(3), Initialize("\\ket{\\phi}", [1,3])]
102+
```
103+
82104
### Noise Events
83105

84106
```@example 1

src/Quantikz.jl

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export MultiControl, CNOT, CPHASE, SWAP, H, P, Id, U,
1616
ClassicalDecision,
1717
Measurement, ParityMeasurement,
1818
Noise, NoiseAll,
19+
Initialize,
1920
circuit2table, table2string,
2021
circuit2string,
2122
string2image,
@@ -151,19 +152,24 @@ function update_table!(qtable,step,g::MultiControlU)
151152
qtable
152153
end
153154

154-
function draw_rectangle!(table,step,targets,str)
155+
function draw_rectangle!(table,step,targets,str;checkdelete_evenfortarget=false)
155156
deleted = Int[]
156-
m, M = explicit_extrema(table, targets) # TODO m M can be deduced from targets. They are not necessary here as arguments
157+
m, M = explicit_extrema(table, targets)
157158
targets = explicit_targets(table, targets)
158-
for i in m+1:M
159+
for i in m:M
159160
if i targets
160161
if strip(table[i,step-1])==""
161162
push!(deleted, i)
162163
else
163164
table[i,step] = "\\linethrough"
164165
end
165166
else
166-
table[i,step] = "\\qw"
167+
if checkdelete_evenfortarget && strip(table[i,step-1])==""
168+
push!(deleted, i)
169+
i!=m && (table[i,step] = "")
170+
else
171+
i!=m && (table[i,step] = "\\qw")
172+
end
167173
end
168174
end
169175
offset = iseven(M-m) && ((m+M)/2 vcat(targets,deleted)) && !occursin("\\\\",str) ? ",label style={yshift=0.2cm}" : ""
@@ -243,6 +249,36 @@ nsteps(m::Measurement) = length(m.targets) > 1 ? 3 : 1
243249
affectedbits(m::Measurement) = isnothing(m.bit) ? [] : [m.bit]
244250
deleteoutputs(m::Measurement) = length(m.targets) == 1 ? m.targets : []
245251

252+
struct Initialize <: QuantikzOp
253+
str::AbstractString
254+
targets::AbstractVector
255+
end
256+
257+
affectedqubits(r::Initialize) = r.targets
258+
function update_table!(qtable,step,r::Initialize)
259+
qvtable = qubitsview(qtable)
260+
m, M = extrema(r.targets)
261+
targets = r.targets
262+
if collect(targets) == collect(m:M)
263+
qvtable[m,step] = "\\midstick[wires=$(M-m+1),brackets=right]{$(r.str)}"
264+
for i in targets[2:end]
265+
qvtable[i,step] = ""
266+
end
267+
else
268+
for i in targets
269+
qvtable[i,step] = ""
270+
end
271+
draw_rectangle!(qvtable, step+1, r.targets, r.str, checkdelete_evenfortarget=true)
272+
end
273+
qtable
274+
end
275+
function nsteps(r::Initialize)
276+
m, M = extrema(r.targets)
277+
targets = r.targets
278+
collect(targets) == collect(m:M) ? 1 : 2
279+
end
280+
281+
246282
struct ClassicalDecision <: QuantikzOp
247283
str::AbstractString
248284
targets::ArrayOrRange
@@ -260,11 +296,9 @@ ClassicalDecision(t::ArrayOrRange, c::ArrayOrRange) = ClassicalDecision("\\;\\;"
260296
affectedqubits(g::ClassicalDecision) = g.targets
261297
affectedbits(g::ClassicalDecision) = g.bits
262298
function update_table!(qtable,step,g::ClassicalDecision)
263-
table = qtable.table
264299
qvtable = qubitsview(qtable)
265300
bvtable = bitsview(qtable)
266301
m, M = explicit_extrema(qvtable, g.targets)
267-
targets = explicit_targets(qvtable, g.targets)
268302
draw_rectangle!(qvtable,step,g.targets,g.str)
269303
bits = explicit_targets(bvtable, g.bits)
270304
startpoint = minimum(bits)

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ circuit = [
5555
]
5656
@test circuit2string(circuit) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\ctrl{0} & \\qw & \\gate[2,disable auto height]{XY} & \\qw & \\meterD{} & \\meterD{} & & & & \\\\\n\\qw & \\targ{}\\vqw{-1} & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\gate[2,disable auto height]{XY} & \\qw & \\qw\\\\\n\\qw & \\meterD{} & & & & \\qw & \\gate[2,disable auto height]{XY} & \\qw & \\qw & \\qw & \\qw\\\\\n\\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\meterD{} & \\\\\n & & \\lstick{} & \\ctrl{-3} & \\meterD{} & \\lstick{} & \\ctrl{-1} & \\meterD{} & & & \\\\\n\\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cwbend{-1} & \\cwbend{-3} & \\cw & \\cw\\\\\n\\cw & \\cw & \\cw & \\cw & \\cwbend{-2} & \\cw & \\cw & \\cw & \\cwbend{-1} & \\cw & \\cw\n\\end{quantikz}"
5757
@test circuit2string(circuit, mode=:expanded) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\ctrl{0} & \\qw & \\qw & \\gate[2,disable auto height]{XY} & \\qw & \\qw & \\qw & \\qw & \\meterD{} & & \\meterD{} & & \\\\\n\\qw & \\targ{}\\vqw{-1} & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\gate[2,disable auto height]{XY} & \\qw & \\qw & \\qw\\\\\n\\qw & \\qw & \\meterD{} & & & & & \\gate[2,disable auto height]{XY} & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw\\\\\n\\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\qw & \\meterD{} & \\\\\n & & & \\lstick{} & \\ctrl{-3} & \\meterD{} & \\lstick{} & \\ctrl{-1} & \\meterD{} & & & & & \\\\\n\\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cw & \\cwbend{-1} & \\cw & \\cwbend{-3} & \\cw & \\cw & \\cw\\\\\n\\cw & \\cw & \\cw & \\cw & \\cw & \\cwbend{-2} & \\cw & \\cw & \\cw & \\cw & \\cwbend{-1} & \\cw & \\cw & \\cw\n\\end{quantikz}"
58+
59+
# Initialize
60+
circuit = [Initialize("\\ket{\\phi}", [1,2,3])]
61+
@test circuit2string(circuit) == circuit2string(circuit, mode=:expanded) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\midstick[wires=3,brackets=right]{\\ket{\\phi}} & \\qw\\\\\n\\qw & & \\qw\\\\\n\\qw & & \\qw\n\\end{quantikz}"
62+
circuit = [Initialize("\\ket{\\phi}", [1,2,4])]
63+
@test circuit2string(circuit) == circuit2string(circuit, mode=:expanded) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & & \\gate[4,nwires={1,2,4},disable auto height]{\\ket{\\phi}} & \\qw\\\\\n\\qw & & & \\qw\\\\\n\\qw & \\qw & \\linethrough & \\qw\\\\\n\\qw & & & \\qw\n\\end{quantikz}"
64+
circuit = [CNOT(1,2), Measurement(3), Initialize("\\ket{\\phi}", [2,3])]
65+
@test circuit2string(circuit) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\ctrl{0} & \\qw & \\qw\\\\\n\\qw & \\targ{}\\vqw{-1} & \\midstick[wires=2,brackets=right]{\\ket{\\phi}} & \\qw\\\\\n\\qw & \\meterD{} & & \\qw\n\\end{quantikz}"
66+
@test circuit2string(circuit, mode=:expanded) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\ctrl{0} & \\qw & \\qw & \\qw\\\\\n\\qw & \\targ{}\\vqw{-1} & \\qw & \\midstick[wires=2,brackets=right]{\\ket{\\phi}} & \\qw\\\\\n\\qw & \\qw & \\meterD{} & & \\qw\n\\end{quantikz}"
67+
circuit = [CNOT(1,2), Measurement(1), Measurement(3), Initialize("\\ket{\\phi}", [1,3])]
68+
@test circuit2string(circuit) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\ctrl{0} & \\meterD{} & & \\gate[3,label style={yshift=0.2cm},nwires={1,3},disable auto height]{\\ket{\\phi}} & \\qw\\\\\n\\qw & \\targ{}\\vqw{-1} & \\qw & \\qw & \\linethrough & \\qw\\\\\n\\qw & \\meterD{} & & & & \\qw\n\\end{quantikz}"
69+
@test circuit2string(circuit, mode=:expanded) == "\\begin{quantikz}[transparent, row sep={0.8cm,between origins}]\n\\qw & \\ctrl{0} & \\meterD{} & & & \\gate[3,label style={yshift=0.2cm},nwires={1,3},disable auto height]{\\ket{\\phi}} & \\qw\\\\\n\\qw & \\targ{}\\vqw{-1} & \\qw & \\qw & \\qw & \\linethrough & \\qw\\\\\n\\qw & \\qw & \\qw & \\meterD{} & & & \\qw\n\\end{quantikz}"
5870
end
5971

6072
@testset "No overlaps in compressed table" begin

0 commit comments

Comments
 (0)