Skip to content

Commit

Permalink
Introduce an Initialize operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed Jun 7, 2021
1 parent 5f5b3dc commit 82a1657
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 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.7"
version = "1.1.0"

[deps]
EndpointRanges = "340492b5-2a47-5f55-813d-aca7ddf97656"
Expand Down
22 changes: 22 additions & 0 deletions docs/src/useful.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ MultiControl([1],[2],[3],[4])
ParityMeasurement(["X","Y","Z"], [1,2,4])
```

### Initial states or resets

#### At the beginning of a circuit

```@example 1
Initialize("\\ket{\\phi}", [1,2,3])
```

```@example 1
Initialize("\\ket{\\phi}", [1,2,4])
```

#### Midway through a circuit

```@example 1
[CNOT(1,2), Measurement(3), Initialize("\\ket{\\phi}", [2,3])]
```

```@example 1
[CNOT(1,2), Measurement(1), Measurement(3), Initialize("\\ket{\\phi}", [1,3])]
```

### Noise Events

```@example 1
Expand Down
46 changes: 40 additions & 6 deletions src/Quantikz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export MultiControl, CNOT, CPHASE, SWAP, H, P, Id, U,
ClassicalDecision,
Measurement, ParityMeasurement,
Noise, NoiseAll,
Initialize,
circuit2table, table2string,
circuit2string,
string2image,
Expand Down Expand Up @@ -151,19 +152,24 @@ function update_table!(qtable,step,g::MultiControlU)
qtable
end

function draw_rectangle!(table,step,targets,str)
function draw_rectangle!(table,step,targets,str;checkdelete_evenfortarget=false)
deleted = Int[]
m, M = explicit_extrema(table, targets) # TODO m M can be deduced from targets. They are not necessary here as arguments
m, M = explicit_extrema(table, targets)
targets = explicit_targets(table, targets)
for i in m+1:M
for i in m:M
if i targets
if strip(table[i,step-1])==""
push!(deleted, i)
else
table[i,step] = "\\linethrough"
end
else
table[i,step] = "\\qw"
if checkdelete_evenfortarget && strip(table[i,step-1])==""
push!(deleted, i)
i!=m && (table[i,step] = "")
else
i!=m && (table[i,step] = "\\qw")
end
end
end
offset = iseven(M-m) && ((m+M)/2 vcat(targets,deleted)) && !occursin("\\\\",str) ? ",label style={yshift=0.2cm}" : ""
Expand Down Expand Up @@ -243,6 +249,36 @@ nsteps(m::Measurement) = length(m.targets) > 1 ? 3 : 1
affectedbits(m::Measurement) = isnothing(m.bit) ? [] : [m.bit]
deleteoutputs(m::Measurement) = length(m.targets) == 1 ? m.targets : []

struct Initialize <: QuantikzOp
str::AbstractString
targets::AbstractVector
end

affectedqubits(r::Initialize) = r.targets
function update_table!(qtable,step,r::Initialize)
qvtable = qubitsview(qtable)
m, M = extrema(r.targets)
targets = r.targets
if collect(targets) == collect(m:M)
qvtable[m,step] = "\\midstick[wires=$(M-m+1),brackets=right]{$(r.str)}"
for i in targets[2:end]
qvtable[i,step] = ""
end
else
for i in targets
qvtable[i,step] = ""
end
draw_rectangle!(qvtable, step+1, r.targets, r.str, checkdelete_evenfortarget=true)
end
qtable
end
function nsteps(r::Initialize)
m, M = extrema(r.targets)
targets = r.targets
collect(targets) == collect(m:M) ? 1 : 2
end


struct ClassicalDecision <: QuantikzOp
str::AbstractString
targets::ArrayOrRange
Expand All @@ -260,11 +296,9 @@ ClassicalDecision(t::ArrayOrRange, c::ArrayOrRange) = ClassicalDecision("\\;\\;"
affectedqubits(g::ClassicalDecision) = g.targets
affectedbits(g::ClassicalDecision) = g.bits
function update_table!(qtable,step,g::ClassicalDecision)
table = qtable.table
qvtable = qubitsview(qtable)
bvtable = bitsview(qtable)
m, M = explicit_extrema(qvtable, g.targets)
targets = explicit_targets(qvtable, g.targets)
draw_rectangle!(qvtable,step,g.targets,g.str)
bits = explicit_targets(bvtable, g.bits)
startpoint = minimum(bits)
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ circuit = [
]
@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}"
@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}"

# Initialize
circuit = [Initialize("\\ket{\\phi}", [1,2,3])]
@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}"
circuit = [Initialize("\\ket{\\phi}", [1,2,4])]
@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}"
circuit = [CNOT(1,2), Measurement(3), Initialize("\\ket{\\phi}", [2,3])]
@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}"
@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}"
circuit = [CNOT(1,2), Measurement(1), Measurement(3), Initialize("\\ket{\\phi}", [1,3])]
@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}"
@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}"
end

@testset "No overlaps in compressed table" begin
Expand Down

2 comments on commit 82a1657

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

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.1.0 -m "<description of version>" 82a165720ee56438b52c182e01d7524dce8f3127
git push origin v1.1.0

Please sign in to comment.