Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
nzy committed Feb 27, 2024
1 parent eadd2cd commit eada65c
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 6 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ default: init test
init:
$(JL) -e 'using Pkg; Pkg.instantiate(); Pkg.precompile()'
init-docs:
$(JL) -e 'using Pkg; Pkg.instantiate(); Pkg.precompile()'
$(JL) -e 'using Pkg; Pkg.activate("docs"); Pkg.develop(path="."); Pkg.instantiate(); Pkg.precompile()'
init-examples:
$(JL) -e 'using Pkg; Pkg.activate("examples"); Pkg.develop(path="."); Pkg.instantiate(); Pkg.precompile()'

update:
$(JL) -e 'using Pkg; Pkg.update(); Pkg.precompile()'
update-docs:
$(JL) -e 'using Pkg; Pkg.activate("docs"); Pkg.update(); Pkg.precompile()'
update-examples:
$(JL) -e 'using Pkg; Pkg.activate("examples"); Pkg.update(); Pkg.precompile()'

test:
$(JL) -e 'using Pkg; Pkg.test()'

example-%:
$(JL) -e 'using Pkg; Pkg.activate("examples"); include("examples/$*.jl")'

coverage:
$(JL) -e 'using Pkg; Pkg.test(; coverage=true)'

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://nzy1997.github.io/TensorQEC.jl/dev/)
[![CI](https://github.com/nzy1997/TensorQEC.jl/actions/workflows/CI.yml/badge.svg)](https://github.com/nzy1997/TensorQEC.jl/actions/workflows/CI.yml)
[![Coverage](https://codecov.io/gh/nzy1997/TensorQEC.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/nzy1997/TensorQEC.jl)

## Examples
To install dependencies, please open a terminal and type
```bash
make init-examples
```

Example files are located in the `examples` folder. Consider the example `example-inference.jl`, to run it, please open a terminal and type
```bash
make example-inference
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions examples/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
TensorQEC = "0500ac79-7fb5-4262-aaea-37bb1845d1ef"
46 changes: 46 additions & 0 deletions examples/inference.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using TensorQEC, TensorQEC.Yao
# define the stabilizers
qubit_num = 3
st = [PauliString((1,4,4)),PauliString((4,1,4))]
@info "stabilizers: $st"

# Generate the encoding circuits of the stabilizers
qc, data_qubits, code = encode_stabilizers(st)
@info "encoding circuits: $qc, data qubits: $data_qubits"

# Create a quantum register. Qubits in "data_qubits" are randomly initilized, and the rest ancilla qubits are in the |0> state.
reg = join(rand_state(1), zero_state(2)) # join(qubit3, qubit2, qubit1)
# Apply the encoding circuits.
regcopy = copy(reg)
apply!(reg, qc)

# Apply a X error on the third qubit
apply!(reg, put(qubit_num, 3=>X))
@info "applied X error on the third qubit"

# Measure the syndrome
measure_outcome=measure_syndrome!(reg, st)
@info "measured syndrome: $measure_outcome"

# Generate the syndrome dictionary
syn_dict=TensorQEC.generate_syndrome_dict(code,syndrome_transform(code, measure_outcome))

# Generate the tensor network for syndrome inference
cl = clifford_network(qc)
p = fill([0.85,0.05,0.05,0.05],qubit_num)
pinf = syndrome_inference(cl, syn_dict, p)
@info "inferred error probability: $pinf"

# Generate the Pauli string for error correction
ps_ec_phy = TensorQEC.pauli_string_map_iter(correction_pauli_string(qubit_num, syn_dict, pinf), qc)
@info "Pauli string for error correction: $ps_ec_phy"

# Apply the error correction
apply!(reg, Yao.YaoBlocks.Optimise.to_basictypes(ps_ec_phy))

# Measure the syndrome after error correction
syndrome_result = measure_syndrome!(reg, st)
@info "measured syndrome: $syndrome_result"
apply!(reg, qc')
fidelity_after = fidelity(density_matrix(reg, [data_qubits...]), density_matrix(regcopy, [data_qubits...]))
@info "fidelity after error correction: $fidelity_after"
4 changes: 2 additions & 2 deletions src/encoder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ struct SurfaceCode{D} end
# | | /
# ∘---9---/
# 8 "∘" represent 8 stableizers.
# XXtype: 24, 1235, 5789, 68
# ZZtype: 13, 2457, 3568, 79
# X type: 24, 1235, 5789, 68
# Z type: 13, 2457, 3568, 79

function stabilizers(::SurfaceCode{3})
nq=9
Expand Down
1 change: 0 additions & 1 deletion src/inferences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ function syndrome_inference(cl::CliffordNetwork{T}, syn::Dict{Int,Bool}, p::Vect
end
tn = generate_tensor_network(cl, ps, qs)
mp = marginals(tn)
@show mp
return result = Dict([k => mp[[cl.mapped_qubits[k]]] for k in 1:n])
end
2 changes: 1 addition & 1 deletion src/tensornetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function generate_tensor_network(cl::CliffordNetwork{T}, ps::Dict{Int, BoundaryS
for (k, v) in qs
nvars = _add_boundary!(cl.mapped_qubits, v, k, factors, cards, mars, nvars)
end
@show mars
# @show mars
return TensorNetworkModel(
1:nvars,
cards,
Expand Down
5 changes: 4 additions & 1 deletion test/errorcorrect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using Test, TensorQEC, TensorQEC.Yao
p = fill([0.85,0.05,0.05,0.05],qubit_num)
pinf = syndrome_inference(cl, syn_dict, p)
ps_ec_phy = TensorQEC.pauli_string_map_iter(correction_pauli_string(qubit_num, syn_dict, pinf), qc)
@show ps_ec_phy
apply!(reg, Yao.YaoBlocks.Optimise.to_basictypes(ps_ec_phy))

@test measure_syndrome!(reg, st) == [1,1]
Expand All @@ -34,7 +35,7 @@ end
reg = join(zero_state(8),rand_state(1))
regcopy = copy(reg)
apply!(reg, qc)
apply!(reg, put(9, 3=>Z))
apply!(reg, put(9, 3=>X))
apply!(reg, put(9, 2=>X))

measure_outcome=measure_syndrome!(reg, st)
Expand All @@ -43,7 +44,9 @@ end
cl = clifford_network(qc)
p = fill([0.85,0.05,0.05,0.05],qubit_num)
pinf = syndrome_inference(cl, syn_dict, p)

ps_ec_phy = TensorQEC.pauli_string_map_iter(correction_pauli_string(qubit_num, syn_dict, pinf), qc)
@show ps_ec_phy
apply!(reg, Yao.YaoBlocks.Optimise.to_basictypes(ps_ec_phy))

@test measure_syndrome!(reg, st) == [1,1,1,1,1,1,1,1]
Expand Down

0 comments on commit eada65c

Please sign in to comment.