diff --git a/Makefile b/Makefile index 45b8a87..fcc7861 100644 --- a/Makefile +++ b/Makefile @@ -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)' diff --git a/README.md b/README.md index 3db20e3..d4c50eb 100644 --- a/README.md +++ b/README.md @@ -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 +``` \ No newline at end of file diff --git a/example/learn.jl b/_learn/learn.jl similarity index 100% rename from example/learn.jl rename to _learn/learn.jl diff --git a/example/learn_OME.jl b/_learn/learn_OME.jl similarity index 100% rename from example/learn_OME.jl rename to _learn/learn_OME.jl diff --git a/example/learn_tensorinference.jl b/_learn/learn_tensorinference.jl similarity index 100% rename from example/learn_tensorinference.jl rename to _learn/learn_tensorinference.jl diff --git a/examples/Project.toml b/examples/Project.toml new file mode 100644 index 0000000..21b5620 --- /dev/null +++ b/examples/Project.toml @@ -0,0 +1,2 @@ +[deps] +TensorQEC = "0500ac79-7fb5-4262-aaea-37bb1845d1ef" diff --git a/examples/inference.jl b/examples/inference.jl new file mode 100644 index 0000000..c12f1f9 --- /dev/null +++ b/examples/inference.jl @@ -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" \ No newline at end of file diff --git a/src/encoder.jl b/src/encoder.jl index 86750bd..55bccd2 100644 --- a/src/encoder.jl +++ b/src/encoder.jl @@ -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 diff --git a/src/inferences.jl b/src/inferences.jl index 0789781..788312c 100644 --- a/src/inferences.jl +++ b/src/inferences.jl @@ -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 diff --git a/src/tensornetwork.jl b/src/tensornetwork.jl index 0093197..2555d5f 100644 --- a/src/tensornetwork.jl +++ b/src/tensornetwork.jl @@ -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, diff --git a/test/errorcorrect.jl b/test/errorcorrect.jl index 9367ee9..980726f 100644 --- a/test/errorcorrect.jl +++ b/test/errorcorrect.jl @@ -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] @@ -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) @@ -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]