Skip to content

Commit

Permalink
Finished initial implementation of naive_encoding_circuit - Implement…
Browse files Browse the repository at this point in the history
…ed the B' part, and fixed the rank calculations
  • Loading branch information
amicciche committed Sep 4, 2023
1 parent 767b114 commit 9f50184
Showing 1 changed file with 69 additions and 12 deletions.
81 changes: 69 additions & 12 deletions src/ecc/ECC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module ECC
using QuantumClifford
using QuantumClifford: AbstractOperation
using LinearAlgebra
using Nemo
import QuantumClifford: Stabilizer, MixedDestabilizer

abstract type AbstractECC end
Expand Down Expand Up @@ -416,7 +417,9 @@ function canonicalize_cleve97(checks::Stabilizer)
Z0 = (checks |> stab_to_gf2)[:,n+1:2n]'

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L414-L417

Added lines #L414 - L417 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L420-L421

Added lines #L420 - L421 were not covered by tests

r = d-b
k = n-d

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L423-L424

Added lines #L423 - L424 were not covered by tests

Expand Down Expand Up @@ -452,7 +455,6 @@ function canonicalize_cleve97(checks::Stabilizer)
STOP = true

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L455

Added line #L455 was not covered by tests
end
end
println(i)
push!(bank, i)
for a in (r+1):(r+b)
if a == j
Expand All @@ -472,22 +474,73 @@ function canonicalize_cleve97(checks::Stabilizer)
end
end
append!(qubit_order, bank)
println(qubit_order)

X1 = X0_5[qubit_order, :]
Z1 = Z0_5[qubit_order, :]

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L475-L478

Added lines #L475 - L478 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L481-L482

Added lines #L481 - L482 were not covered by tests
println("WARN: If r2 is greater than 0, this will not work. r2: ", r2)

# TODO implement calculation of B' using the same alogirthm as above
# For the Cleve8 code r2 = 0, so X1= X2 and Z1 = Z2
X2 = X1; Z2 = Z1; # TODO this is generally wrong - see above comment.
# 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 489 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L485-L489

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

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L491

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

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L493-L496

Added lines #L493 - L496 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 500 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L499-L500

Added lines #L499 - L500 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 512 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L503-L512

Added lines #L503 - L512 were not covered by tests
end
else
STOP = true

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L515

Added line #L515 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 521 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L517-L521

Added lines #L517 - L521 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 524 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L523-L524

Added lines #L523 - L524 were not covered by tests
end
end
end

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L526-L527

Added lines #L526 - L527 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 532 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L529-L532

Added lines #L529 - L532 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

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L534-L540

Added lines #L534 - L540 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L543

Added line #L543 was not covered by tests

XI = zeros(Bool, k , k)
for i in 1:k
Expand All @@ -496,16 +549,20 @@ function canonicalize_cleve97(checks::Stabilizer)
Xs = (vcat(XI, zeros(Bool,r2, k), B1', zeros(Bool, b, k)))
Zs = zeros(Bool, n, k)

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L545-L550

Added lines #L545 - L550 were not covered by tests

println("k, r2, r1, b: ", k, " ", r2, " ", r1, " ", b)
Xstar = hcat(Xs,X2)
Zstar = hcat(Zs,Z2)
return Xstar, Zstar, Stabilizer(X2',Z2') # TODO at some point unreorder the qubits
return Xstar, Zstar, Stabilizer(X2',Z2')# TODO at some point unreorder the qubits? Recall they get reordered twice.

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L552-L555

Added lines #L552 - L555 were 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 561 in src/ecc/ECC.jl

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L559-L561

Added lines #L559 - L561 were not covered by tests
b = rank(X0)

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

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L563-L564

Added lines #L563 - L564 were not covered by tests

r = d-b
k = n-d

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

View check run for this annotation

Codecov / codecov/patch

src/ecc/ECC.jl#L566-L567

Added lines #L566 - L567 were not covered by tests

Expand Down

0 comments on commit 9f50184

Please sign in to comment.