Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial implementation of naive_encoding_circuit #184

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 197 additions & 0 deletions src/ecc/ECC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using QuantumClifford
using QuantumClifford: AbstractOperation
using LinearAlgebra
using Nemo
import QuantumClifford: Stabilizer, MixedDestabilizer

abstract type AbstractECC end
Expand Down Expand Up @@ -397,5 +399,200 @@
include("./bitflipcode.jl")
include("./shorcode.jl")
include("./steanecode.jl")
include("./cleavecode.jl")
include("./fiveonethreecode.jl")
include("./fivetwotwocode.jl")

function canonicalize_cleve97(checks::Stabilizer)
d, n = size(checks)
X0 = (checks |> stab_to_gf2)[:,1:n]'
Z0 = (checks |> stab_to_gf2)[:,n+1:2n]'

Check warning on line 409 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L406-L409

Added lines #L406 - L409 were not covered by tests

# Now let's work on getting X1 and Z1
Z2field = Nemo.ResidueRing(ZZ, 2)
b = rank(Nemo.matrix(Z2field, X0))

Check warning on line 413 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L412-L413

Added lines #L412 - L413 were not covered by tests

r = d-b
k = n-d

Check warning on line 416 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L415-L416

Added lines #L415 - L416 were not covered by tests

# First let's move the 0 columns to the left:
nullColumns = []
notNullColumns = []
for j in 1:d
if count(X0[:,j])==0 # TODO make sure this is condition -> null generator
push!(nullColumns, j)

Check warning on line 423 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L419-L423

Added lines #L419 - L423 were not covered by tests
else
push!(notNullColumns, j)

Check warning on line 425 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L425

Added line #L425 was not covered by tests
end
end

Check warning on line 427 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L427

Added line #L427 was not covered by tests

# Reorder the generators/columns so 0 vectors are in front of X0:
column_order = vcat(nullColumns, notNullColumns)
X0_5 = X0[:, column_order]
Z0_5 = Z0[:, column_order]

Check warning on line 432 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L430-L432

Added lines #L430 - L432 were not covered by tests

# Second let's perform Gaussian elimination to arrive at X1 and Z1
bank = []
for j in (r+1):(r+b)
i = 1
STOP = false
while !STOP
if X0_5[i, j] != 1
i+=1
if i > n
print("ERROR")
STOP = true

Check warning on line 444 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L435-L444

Added lines #L435 - L444 were not covered by tests
end
else
STOP = true

Check warning on line 447 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L447

Added line #L447 was not covered by tests
end
end
push!(bank, i)
for a in (r+1):(r+b)
if a == j
continue

Check warning on line 453 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L449-L453

Added lines #L449 - L453 were not covered by tests
end
if X0_5[i,a] == 1
X0_5[:,a] = (X0_5[:,j]+X0_5[:,a]).%2
Z0_5[:,a] = (Z0_5[:,j]+Z0_5[:,a]).%2

Check warning on line 457 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L455-L457

Added lines #L455 - L457 were not covered by tests
end
end
end

Check warning on line 460 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L459-L460

Added lines #L459 - L460 were not covered by tests

qubit_order = []
for i in 1:n
if i ∉ bank
push!(qubit_order, i)

Check warning on line 465 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L462-L465

Added lines #L462 - L465 were not covered by tests
end
end
append!(qubit_order, bank)
X1 = X0_5[qubit_order, :]
Z1 = Z0_5[qubit_order, :]
println("Qubit order 1", qubit_order)
checks = checks[:, qubit_order]

Check warning on line 472 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L467-L472

Added lines #L467 - L472 were not covered by tests

# Now begins the march towards X2 and Z2, starting with B'
r1 = rank(Nemo.matrix(Z2field, Z1[1:r+k,1:r]))
r2 = r - r1

Check warning on line 476 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L475-L476

Added lines #L475 - L476 were not covered by tests

# First let's move the 0 columns of B to the left:
nullColumns = []
notNullColumns = []
for j in 1:r
if count(Z1[:,j])==0 # TODO make sure this is condition <-> null generator
push!(nullColumns, j)

Check warning on line 483 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L479-L483

Added lines #L479 - L483 were not covered by tests
else
push!(notNullColumns, j)

Check warning on line 485 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L485

Added line #L485 was not covered by tests
end
end
for j in r+1:d
push!(notNullColumns, j)
end

Check warning on line 490 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L487-L490

Added lines #L487 - L490 were not covered by tests

# Reorder the generators/columns so 0 vectors are in front of B:
column_order = vcat(nullColumns, notNullColumns)
Z1_5 = Z1[:, column_order]

Check warning on line 494 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L493-L494

Added lines #L493 - L494 were not covered by tests

# Now Gaussian elimination again, this time over B'
bank = []
for j in (r2+1):(r2+r1)
i = 1
STOP = false
while !STOP
if Z1_5[i, j] != 1
i+=1
if i > n
print("ERROR")
STOP = true

Check warning on line 506 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L497-L506

Added lines #L497 - L506 were not covered by tests
end
else
STOP = true

Check warning on line 509 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L509

Added line #L509 was not covered by tests
end
end
push!(bank, i)
for a in (r2+1):(r2+r1)
if a == j
continue

Check warning on line 515 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L511-L515

Added lines #L511 - L515 were not covered by tests
end
if Z1_5[i,a] == 1
Z1_5[:,a] = (Z1_5[:,j]+Z1_5[:,a]).%2

Check warning on line 518 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L517-L518

Added lines #L517 - L518 were not covered by tests
end
end
end

Check warning on line 521 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L520-L521

Added lines #L520 - L521 were not covered by tests

qubit_order = []
for i in 1:n
if i ∉ bank && i<= k+r
push!(qubit_order, i)

Check warning on line 526 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L523-L526

Added lines #L523 - L526 were not covered by tests
end
end
for i in k+r+1:n # rows not in B
push!(bank, i)
end
append!(qubit_order, bank)
Z2 = Z1_5[qubit_order, :]
X2 = X1[qubit_order, :] # X is unchanged by operations on b, except for reindexing of qubits
println("Qubit order 2", qubit_order)
checks = checks[:, qubit_order]

Check warning on line 536 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L528-L536

Added lines #L528 - L536 were not covered by tests

# Now time for the final steps before arriving at Xstar Zstar
B1 = Z2[1:k,1+r2:r2+r1]

Check warning on line 539 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L539

Added line #L539 was not covered by tests

XI = zeros(Bool, k , k)
for i in 1:k
XI[i,i] = 1
end
Xs = (vcat(XI, zeros(Bool,r2, k), B1', zeros(Bool, b, k)))
Zs = zeros(Bool, n, k)

Check warning on line 546 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L541-L546

Added lines #L541 - L546 were not covered by tests

println("k, r2, r1, b: ", k, " ", r2, " ", r1, " ", b)
Xstar = hcat(Xs,X2)
Zstar = hcat(Zs,Z2)

Check warning on line 550 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L548-L550

Added lines #L548 - L550 were not covered by tests

#return X0, X0_5, X1, X2, Xstar, Z0, Z0_5, Z1, Z1_5, Z2, Zstar
return Xstar, Zstar, Stabilizer(X2', Z2')# TODO Return reordered checks or Stab(X2', Z2')?

Check warning on line 553 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L553

Added line #L553 was not covered by tests
end

""" The naive implementation of the encoding circuit by arXiv:quant-ph/9607030 """
function naive_encoding_circuit(checks::Stabilizer)
d, n = size(checks)
X0 = (checks |> stab_to_gf2)[:,1:n]'

Check warning on line 559 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L557-L559

Added lines #L557 - L559 were not covered by tests

Z2field = Nemo.ResidueRing(ZZ, 2)
b = rank(Nemo.matrix(Z2field, X0))

Check warning on line 562 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L561-L562

Added lines #L561 - L562 were not covered by tests

r = d-b
k = n-d

Check warning on line 565 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L564-L565

Added lines #L564 - L565 were not covered by tests

Xstar, Zstar, standard_tab = canonicalize_cleve97(checks)

Check warning on line 567 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L567

Added line #L567 was not covered by tests

naive_ec = AbstractOperation[]
for i in (r+k+1):(r+k+b)
push!(naive_ec, sHadamard(i))
end

Check warning on line 572 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L569-L572

Added lines #L569 - L572 were not covered by tests

for j in 1:n
if count(Xstar[:,j]) == 0 # Secondary generators are ignored
continue

Check warning on line 576 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L574-L576

Added lines #L574 - L576 were not covered by tests
end
if Zstar[j,j]==1
push!(naive_ec, sZ(j))

Check warning on line 579 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L578-L579

Added lines #L578 - L579 were not covered by tests
end
for i in 1:n
if i == j
continue

Check warning on line 583 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L581-L583

Added lines #L581 - L583 were not covered by tests
end
if Xstar[i,j] == 1 && Zstar[i,j] == 0
push!(naive_ec, sZCX(j,i))
elseif Xstar[i,j] == 0 && Zstar[i,j] == 1
push!(naive_ec, sZCZ(j,i))
elseif Xstar[i,j] == 1 && Zstar[i,j] == 1
push!(naive_ec, sZCY(j,i))

Check warning on line 590 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L585-L590

Added lines #L585 - L590 were not covered by tests
end
end
end

Check warning on line 593 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L592-L593

Added lines #L592 - L593 were not covered by tests

return naive_ec, standard_tab

Check warning on line 595 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L595

Added line #L595 was not covered by tests
end

end #module
55 changes: 55 additions & 0 deletions src/ecc/cleavecode.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""A pedagogical example of a quantum error correcting [8,3] code used in [cleve1997efficient](@cite)."""
struct Cleve8 <: AbstractECC end

code_n(c::Cleve8) = 8
code_k(c::Cleve8) = 3

Check warning on line 5 in src/ecc/cleavecode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/cleavecode.jl#L4-L5

Added lines #L4 - L5 were not covered by tests

parity_checks(c::Cleve8) = S"XXXXXXXX

Check warning on line 7 in src/ecc/cleavecode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/cleavecode.jl#L7

Added line #L7 was not covered by tests
ZZZZZZZZ
XIXIZYZY
XIYZXIYZ
XZIYIYXZ"

function encoding_circuit(c::Cleve8)
c1 = sCNOT(1,4)
c2 = sCNOT(2,4)
c3 = sCNOT(3,4)
h1 = sHadamard(5)
z1 = sZ(5)
h2 = sHadamard(6)
h3 = sHadamard(7)
z3 = sZ(7)
h4 = sHadamard(8)
z4 = sZ(8)
first_part = [c1,c2,c3,h1,z1,h2,h3,z3,h4,z4]

Check warning on line 24 in src/ecc/cleavecode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/cleavecode.jl#L13-L24

Added lines #L13 - L24 were not covered by tests

c1 = sZCX(5, 1)
c2 = sZCX(5, 2)
c3 = sZCY(5, 3)
c4 = sZCZ(5, 4)
c5 = sZCZ(5, 6)
column1 = [c1,c2,c3,c4,c5] # 1st non null column of Zstar

Check warning on line 31 in src/ecc/cleavecode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/cleavecode.jl#L26-L31

Added lines #L26 - L31 were not covered by tests

c1 = sZCX(6, 1)
c2 = sZCY(6, 2)
c3 = sZCY(6, 4)
c4 = sZCZ(6, 5)
c5 = sZCZ(6, 7)
column2 = [c1,c2,c3,c4,c5]

Check warning on line 38 in src/ecc/cleavecode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/cleavecode.jl#L33-L38

Added lines #L33 - L38 were not covered by tests

c1 = sZCX(7, 1)
c2 = sZCY(7, 3)
c3 = sZCX(7, 4)
c4 = sZCZ(7, 5)
c5 = sZCZ(7, 8)
column3 = [c1,c2,c3,c4,c5]

Check warning on line 45 in src/ecc/cleavecode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/cleavecode.jl#L40-L45

Added lines #L40 - L45 were not covered by tests

c1 = sZCY(8, 2)
c2 = sZCX(8, 3)
c3 = sZCX(8, 4)
c4 = sZCZ(8, 5)
c5 = sZCZ(8, 6)
column4 = [c1,c2,c3,c4,c5]

Check warning on line 52 in src/ecc/cleavecode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/cleavecode.jl#L47-L52

Added lines #L47 - L52 were not covered by tests

return vcat(first_part, column1, column2, column3, column4)

Check warning on line 54 in src/ecc/cleavecode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/cleavecode.jl#L54

Added line #L54 was not covered by tests
end
9 changes: 9 additions & 0 deletions src/ecc/fiveonethreecode.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""http://www.codetables.de/QECC.php?q=4&n=5&k=1"""
struct FiveOneThree <: AbstractECC end

code_n(c::FiveOneThree) = 8

Check warning on line 4 in src/ecc/fiveonethreecode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/fiveonethreecode.jl#L4

Added line #L4 was not covered by tests

parity_checks(c::FiveOneThree) = S"YIZXY

Check warning on line 6 in src/ecc/fiveonethreecode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/fiveonethreecode.jl#L6

Added line #L6 was not covered by tests
ZXIZY
ZIXYZ
IZZZZ"
8 changes: 8 additions & 0 deletions src/ecc/fivetwotwocode.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""http://www.codetables.de/QECC.php?q=4&n=5&k=2"""
struct FiveTwoTwo <: AbstractECC end

code_n(c::FiveTwoTwo) = 8

Check warning on line 4 in src/ecc/fivetwotwocode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/fivetwotwocode.jl#L4

Added line #L4 was not covered by tests

parity_checks(c::FiveTwoTwo) = S"XXXXI

Check warning on line 6 in src/ecc/fivetwotwocode.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/fivetwotwocode.jl#L6

Added line #L6 was not covered by tests
IIIIX
ZZZZI"
Loading