Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
nzy committed Feb 26, 2024
1 parent 86cd77a commit 782788b
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 28 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Zhongyi Ni"]
version = "1.0.0-DEV"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LuxorGraphPlot = "1f49bdf2-22a7-4bc4-978b-948dc219fbbc"
TensorInference = "c2297e78-99bd-40ad-871d-f50e56b81012"
Expand Down
3 changes: 2 additions & 1 deletion src/TensorQEC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ export PauliString, PauliGroup, isanticommute
export syndrome_inference, measure_syndrome!

# encoder
export Bimatrix, toric_code, stabilizers, syndrome_transform
export Bimatrix, toric_code, stabilizers, syndrome_transform, encode_stabilizers
export ToricCode, SurfaceCode

#error correction
export correction_pauli_string



Expand Down
8 changes: 8 additions & 0 deletions src/encoder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,11 @@ function stabilizers(::SurfaceCode{3})
return pauli_string
end


function encode_stabilizers(stabilizers::AbstractVector{PauliString{N}})where N
bimat = stabilizers2bimatrix(stabilizers)
gaussian_elimination!(bimat)
qc = encode_circuit(bimat)
data_qubits = bimat.ordering[size(bimat.matrix,1)+1:end]
return qc, data_qubits, bimat
end
7 changes: 4 additions & 3 deletions src/errorcorrect.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
function correction_pauli_string(qubit_num::Int, syn::Dict{Int,Bool}, prob::Dict{Int,Vector{Float64}})
ps = ones(Int, qubit_num)
for (k, v) in prob
@show syn[k]
if syn[k] #syn[k] is true, measure outcome is -1, use X or Y
ps[k]=(findmax(v)[2]) == 1 ? 2 : 3
ps[k] = (findmax(v)[2]) == 1 ? 2 : 3
elseif findmax(v)[2] == 2
ps[k]=2
ps[k] = 4
end
end
return PauliString(ps...)
return PauliString(ps[1:end]...)
end


6 changes: 2 additions & 4 deletions src/inferences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ end
function syndrome_inference(cl::CliffordNetwork{T}, syn::Dict{Int,Bool}, p::Vector{Vector{Float64}})where T
n = length(p)
ps = Dict([i=>BoundarySpec((p[i]...,), false) for i in 1:n])
qs = Dict([i=>BoundarySpec((ones(T,4)...,), false) for i in 1:n])
qs = Dict([i=>BoundarySpec((ones(T,4)...,), true) for i in 1:n])
for (k, v) in syn
@show k,v
qs[k] = BoundarySpec((v ? (0.0,1.0,1.0,0.0) : (1.0,0.0,0.0,1.0) ),true)
end
#@show qs
tn = generate_tensor_network(cl, ps, qs)
mp=marginals(tn)
mp = marginals(tn)
return result = Dict([k => mp[[cl.mapped_qubits[k]]] for k in keys(syn)])
end
6 changes: 3 additions & 3 deletions src/paulibasis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# 4. pauli_basis(2)[5]
# 5. LinearAlgebra.kron(X,I2)
# 6. IX shown in the print

# 7. Apply I on the first qubit and X on the second qubit. This will convert the state |00> to |10>.

function pauli_basis(nqubits::Int)
paulis = [I2, X, Y, Z]
Expand All @@ -23,15 +23,15 @@ function pauli_decomposition(m::AbstractMatrix)
end

# defined the linear mapping in the pauli basis
# from the coding space to the physical qubits, i.e. (physical..., coding...)
# from the coding space to the physical qubits, i.e. (physical..., coding...) or (output..., input...)
function pauli_mapping(m::AbstractMatrix)
nqubits = Int(log2(size(m, 1)))
paulis = pauli_basis(nqubits)
return [real(tr(pi * m * pj * m')/size(m, 1)) for pi in paulis, pj in paulis]
end

function pauli_string_map(ps::PauliString{N}, paulimapping::Array, qubits::Vector{Int}) where N
c=findall(!iszero, paulimapping[ps.ids[qubits]...,:,:])[1]
c=findall(!iszero, paulimapping[fill(:,length(size(paulimapping)) ÷ 2)...,ps.ids[qubits]...])[1]
return PauliString(([k qubits ? c[findfirst(==(k),qubits)] : ps.ids[k] for k in 1:N]...,))
end

Expand Down
11 changes: 11 additions & 0 deletions test/encoder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,15 @@ end
for i in 1:size(code.matrix, 1)
@test u * mat(ComplexF64, put(9, code.ordering[i] => Z)) * u' mat(ComplexF64, stabilizers[i])
end
end


@testset "encode_stabilizers" begin
result = TensorQEC.stabilizers(SurfaceCode{3}())
qc, data_qubits, bimat = TensorQEC.encode_stabilizers(result)
u = mat(ComplexF64, qc)
stabilizers = TensorQEC.bimatrix2stabilizers(bimat)
for i in 1:size(bimat.matrix, 1)
@test u * mat(ComplexF64, put(9, bimat.ordering[i] => Z)) * u' mat(ComplexF64, stabilizers[i])
end
end
5 changes: 5 additions & 0 deletions test/errorcrrect.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
using Test, TensorQEC, TensorQEC.Yao
using Distributions

function random_pauli_string(N::Int,p::Vector)
values = [1, 2, 3, 4] # 1: I, 2: X, 3: Y, 4: Z
return PauliString(([values[rand(Categorical(p[i]))] for i in 1:N]...,))
end
67 changes: 52 additions & 15 deletions test/inferences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ using Test, TensorQEC, TensorQEC.Yao
result=stabilizers(SurfaceCode{3}())
code = TensorQEC.stabilizers2bimatrix(result)
TensorQEC.gaussian_elimination!(code)
st = TensorQEC.bimatrix2stabilizers(code)
qc = TensorQEC.encode_circuit(code)
#display(vizcircuit(qc))

Expand Down Expand Up @@ -42,27 +41,65 @@ end
@test length(pinf) == 2
end

# simple example: Z1Z2 Z2Z3 stabilizers on 3 qubits
@testset "simple_example_inference" begin
stabilizers = [PauliString((1,4,4)),PauliString((4,4,1))]
qc, data_qubits, code = TensorQEC.encode_stabilizers(stabilizers)
reg = join(rand_state(1), zero_state(2))
apply!(reg, put(3, 3=>X))
@show reg.state
regcopy = copy(reg)
@show qc
@show data_qubits
@show code.matrix

apply!(reg, qc)
@show reg.state
#apply!(reg, put(3, 2=>Y))
apply!(reg, put(3, 3=>X))
measure_outcome=measure_syndrome!(reg, stabilizers)
@show measure_outcome
syn=syndrome_transform(code, measure_outcome)
syn_dict=TensorQEC.generate_syndrome_dict(code,syn)

cl = clifford_network(qc)
p = fill([0.85,0.05,0.05,0.05],3)
pinf = syndrome_inference(cl, syn_dict, p)
@show pinf
ps_ec = correction_pauli_string(3, syn_dict, pinf)
ps_ec_phy = TensorQEC.pauli_string_map_iter(ps_ec, qc)
@show ps_ec,ps_ec_phy
@show reg.state
apply!(reg, Yao.YaoBlocks.Optimise.to_basictypes(ps_ec_phy))
@show reg.state
@test measure_syndrome!(reg, stabilizers) == [1,1]
@test fidelity(density_matrix(reg, [3]), density_matrix(regcopy, [3])) 1.0
end

@testset "syndrome inference for SurfaceCode3" begin
result=stabilizers(SurfaceCode{3}())
code = TensorQEC.stabilizers2bimatrix(result)
TensorQEC.gaussian_elimination!(code)
st = TensorQEC.bimatrix2stabilizers(code)
qc = TensorQEC.encode_circuit(code)
result = stabilizers(SurfaceCode{3}())
qc, data_qubits, code = TensorQEC.encode_stabilizers(result)

reg = join(rand_state(1), zero_state(8))
regcopy=copy(reg)
reg = join(zero_state(8),rand_state(1))
regcopy = copy(reg)
apply!(reg, qc)
apply!(reg, put(9, 9=>X))
apply!(reg, put(9, 5=>Y))
apply!(reg, put(9, 3=>Z))
measure_outcome=measure_syndrome!(reg, result)
@show measure_outcome
syn=syndrome_transform(code, measure_outcome)
syn_dict=TensorQEC.generate_syndrome_dict(code,syn)

cl = clifford_network(qc)
p=fill([0.85,0.05,0.05,0.05],9)
pinf=syndrome_inference(cl, syn_dict, p)
ps_ec=correction_pauli_string(9, syn_dict, pinf)
ps_ec_phy=TensorQEC.pauli_string_map_iter(ps_ec, qc)
p = fill([0.94,0.02,0.02,0.02],9)
pinf = syndrome_inference(cl, syn_dict, p)
@show pinf
ps_ec = correction_pauli_string(9, syn_dict, pinf)
ps_ec_phy = TensorQEC.pauli_string_map_iter(ps_ec, qc)
@show
apply!(reg, Yao.YaoBlocks.Optimise.to_basictypes(ps_ec_phy))
@test measure_syndrome!(reg, result)==[1,1,1,1,1,1,1,1]

@test measure_syndrome!(reg, result) == [1,1,1,1,1,1,1,1]
apply!(reg, qc')
#fidelity
@test fidelity(density_matrix(reg, [9]),density_matrix(regcopy, [9])) 1.0
end
8 changes: 6 additions & 2 deletions test/paulibasis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ end
end

@testset "pauli_string_map" begin
ps = PauliString(1,3,4,2,1)
@test TensorQEC.pauli_string_map(ps, pauli_mapping(mat(ComplexF64,cnot(2,1,2))), [4,5]).ids ==(1,3,4,2,2)
ps = PauliString(2,3,4,3,2,1)
@test TensorQEC.pauli_string_map(ps, pauli_mapping(mat(ComplexF64,cnot(2,1,2))), [5,6]).ids ==(2,3,4,3,2,2)
end

@testset "pauli_string_map_iter" begin
ps = PauliString(1,3,4,2,1)
qc = chain(cnot(5,4,5), put(5, 3=>H))
@test TensorQEC.pauli_string_map_iter(ps, qc).ids ==(1,3,2,2,2)

ps = PauliString(1,2,2)
qc = chain(cnot(3,3,2), cnot(3,3,1))
@test TensorQEC.pauli_string_map_iter(ps, qc).ids == (2,1,2)
end

0 comments on commit 782788b

Please sign in to comment.