From 82a165720ee56438b52c182e01d7524dce8f3127 Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Mon, 7 Jun 2021 00:31:22 -0400 Subject: [PATCH] Introduce an Initialize operation. --- Project.toml | 2 +- docs/src/useful.md | 22 ++++++++++++++++++++++ src/Quantikz.jl | 46 ++++++++++++++++++++++++++++++++++++++++------ test/runtests.jl | 12 ++++++++++++ 4 files changed, 75 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 86969dc..cb9e755 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Quantikz" uuid = "b0d11df0-eea3-4d79-b4a5-421488cbf74b" authors = ["Stefan Krastanov "] -version = "1.0.7" +version = "1.1.0" [deps] EndpointRanges = "340492b5-2a47-5f55-813d-aca7ddf97656" diff --git a/docs/src/useful.md b/docs/src/useful.md index bee3956..1d24bb1 100644 --- a/docs/src/useful.md +++ b/docs/src/useful.md @@ -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 diff --git a/src/Quantikz.jl b/src/Quantikz.jl index c73f7f5..8591c49 100644 --- a/src/Quantikz.jl +++ b/src/Quantikz.jl @@ -16,6 +16,7 @@ export MultiControl, CNOT, CPHASE, SWAP, H, P, Id, U, ClassicalDecision, Measurement, ParityMeasurement, Noise, NoiseAll, + Initialize, circuit2table, table2string, circuit2string, string2image, @@ -151,11 +152,11 @@ 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) @@ -163,7 +164,12 @@ function draw_rectangle!(table,step,targets,str) 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}" : "" @@ -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 @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 34a1542..b6160fe 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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