Documentation for TensorQEC.
TensorQEC.AbstractDecoderTensorQEC.AbstractDecodingProblemTensorQEC.CSSBimatrixTensorQEC.CSSErrorPatternTensorQEC.CSSTannerGraphTensorQEC.ClassicalDecodingProblemTensorQEC.CliffordSimulateResultTensorQEC.Code1573TensorQEC.Code422TensorQEC.Code513TensorQEC.Code832TensorQEC.CompiledDecoderTensorQEC.GeneralDecodingProblemTensorQEC.IPDecoderTensorQEC.IndependentDepolarizingDecodingProblemTensorQEC.MatchingDecoderTensorQEC.PauliStringTensorQEC.QCInfoTensorQEC.ShorCodeTensorQEC.SimpleTannerGraphTensorQEC.SimpleTannerGraphTensorQEC.SteaneCodeTensorQEC.SurfaceCodeTensorQEC.TNMAPTensorQEC.TruthTableTensorQEC.SimpleMultiprocessing.multiprocess_runTensorQEC.ToricCodeTensorQEC.annotate_historyTensorQEC.clifford_groupTensorQEC.clifford_networkTensorQEC.clifford_simulateTensorQEC.coherent_error_unitaryTensorQEC.correction_circuitTensorQEC.correction_dictTensorQEC.correction_pauli_stringTensorQEC.decodeTensorQEC.encode_stabilizersTensorQEC.error_pairsTensorQEC.error_quantum_circuitTensorQEC.error_quantum_circuit_pair_replaceTensorQEC.fidelity_tensornetworkTensorQEC.get_graphTensorQEC.inferenceTensorQEC.measure_circuit_fault_tolTensorQEC.measure_circuit_steaneTensorQEC.measure_syndrome!TensorQEC.pauli_basisTensorQEC.pauli_decompositionTensorQEC.pauli_mappingTensorQEC.pauli_string_map_iterTensorQEC.paulistringTensorQEC.perm_of_paulistringTensorQEC.place_qubitsTensorQEC.random_error_qubitsTensorQEC.simulation_tensornetworkTensorQEC.stabilizersTensorQEC.syndrome_extractionTensorQEC.syndrome_inferenceTensorQEC.to_perm_matrixTensorQEC.transformed_syndrome_dict
TensorQEC.AbstractDecoder — TypeAbstractDecoderThe abstract type for a decoder.
TensorQEC.AbstractDecodingProblem — TypeAbstractDecodingProblemThe abstract type for a decoding problem.
TensorQEC.CSSBimatrix — TypeCSSBimatrixSince the encding process may alter the generators of stabilizer group, we introduce the CSSBimatrix structure to store the information of encoding process. The CSSBimatrix structure contains the following fields
matrix: The bimatrix representation of the stabilizers.Q: The matrix records the Gaussian elimination process, whcih is used to recover the original stabilizers.ordering: The ordering of qubits.xcodenum: The number of X stabilizers.
TensorQEC.CSSErrorPattern — TypeCSSErrorPattern(xerror::Vector{Mod2}, zerror::Vector{Mod2})A CSS error pattern with X and Z errors. Fields:
xerror::Vector{Mod2}: the X errorszerror::Vector{Mod2}: the Z errors
TensorQEC.CSSTannerGraph — TypeCSSCSSTannerGraph(stgx::SimpleTannerGraph, stgz::SimpleTannerGraph)
CSSTannerGraph(nq::Int, stxs::Vector{Vector{Int}}, stzs::Vector{Vector{Int}})
CSSTannerGraph(sts::Vector{PauliString{N}}) where N
CSSTannerGraph(cqc::CSSQuantumCode)Two tanner graph for a CSS code, one for X stabilizers and one for Z stabilizers. Fields: stgx: Tanner graph for X stabilizers stgz: Tanner graph for Z stabilizers
TensorQEC.ClassicalDecodingProblem — TypeClassicalDecodingProblem(tanner::SimpleTannerGraph, pvec::Vector{Float64})A classical decoding problem. Fields:
tanner::SimpleTannerGraph: the Tanner graphpvec::Vector{Float64}: the independent probability distributions on each bit
TensorQEC.CliffordSimulateResult — TypeCliffordSimulateResult{N}The result of simulating a Pauli string by a Clifford circuit.
Fields
output::PauliString{N}: A mapped Pauli string as the output.phase::ComplexF64: The phase factor.circuit::ChainBlock: The circuit (simplified, with linear structure).history::Vector{PauliString{N}}: The history of Pauli strings, its length islength(circuit)+1.
TensorQEC.Code1573 — TypeCode1573Construct a [[15,7,3]] CSS code instance.
TensorQEC.Code422 — TypeCode422Construct a [[4,2,2]] CSS code instance.
TensorQEC.Code513 — TypeCode513Construct a [[5,1,3]] code instance.
TensorQEC.Code832 — TypeCode832Construct a [[8,3,2]] CSS code instance.
TensorQEC.CompiledDecoder — TypeCompiledDecoderCompile the decoder to specific tanner graph and prior probability distributions.
TensorQEC.GeneralDecodingProblem — TypeGeneralDecodingProblem(tanner::SimpleTannerGraph, ptn::TensorNetwork)A general decoding problem. Fields:
tanner::SimpleTannerGraph: the Tanner graphptn::TensorNetwork: the probability distributions, qubits are labeled as 1:qubit_num
TensorQEC.IPDecoder — TypeIPDecoder <: AbstractGeneralDecoderAn integer programming decoder.
TensorQEC.IndependentDepolarizingDecodingProblem — TypeIndependentDepolarizingDecodingProblem(tanner::CSSTannerGraph, pvec::IndependentDepolarizingError)A decoding problem with independent depolarizing error model. Fields:
tanner::CSSTannerGraph: the Tanner graphpvec::IndependentDepolarizingError: the independent probability distributions on each qubit
TensorQEC.MatchingDecoder — TypeMatchingDecoder{T<:MatchingSolver} <: AbstractDecoderA decoder that uses matching algorithm. Fields:
solver::T: the matching solver
TensorQEC.PauliString — TypePauliString{N} <: CompositeBlock{2}A Pauli string is a tensor product of Pauli gates, e.g. XYZ. The matrix representation of a Pauli string is evaluated as
\[A = \bigotimes_{i=1}^N \sigma_{ids[N-i+1]}\]
where ids is the array of integers representing the Pauli gates. Note the order of ids is following the little-endian convention, i.e. the first qubit is the least significant qubit. For example, the Pauli string XYZ has matrix representation Z ⊗ Y ⊗ X.
Fields
ids::NTuple{N, Int}: the array of integers (1-4) representing the Pauli gates.- 1: I ($σ_0$)
 - 2: X ($σ_1$)
 - 3: Y ($σ_2$)
 - 4: Z ($σ_3$)
 
TensorQEC.QCInfo — TypeQCInfo(data_qubits::Vector{Int},ancilla_qubits::Vector{Int},nq::Int)
QCInfo(data_qubits::Vector{Int},nq::Int)A struct to store the qubit information of a quantum circuit.
Fields
data_qubits: The data qubit indices.ancilla_qubits: The ancilla qubit indices. If not specified, it is set to the complement ofdata_qubitsin1:nqnq: The total number of qubits.
TensorQEC.ShorCode — TypeShorCodeConstruct a Shor code instance.
TensorQEC.SimpleTannerGraph — TypeSimpleTannerGraph(nq::Int, ns::Int, q2s::Vector{Vector{Int}}, s2q::Vector{Vector{Int}}, H::Matrix{Mod2})Tanner graph for a classical linear code. Fields: nq: number of qubits ns: number of stabilizers q2s: a list of lists, q2s[i] is the list of stabilizers that contain qubit i s2q: a list of lists, s2q[i] is the list of qubits that stabilizer i contains H: the parity check matrix
TensorQEC.SimpleTannerGraph — MethodSimpleTannerGraph(nq::Int, sts::Vector{Vector{Int}})Construct a Tanner graph from a list of stabilizers. Input: nq: number of qubits sts: a list of parity checks, each parity check is a list of bits.
TensorQEC.SteaneCode — TypeSteaneCodeConstruct a Steane code instance.
TensorQEC.SurfaceCode — TypeSurfaceCode(m::Int, n::Int)Construct a surface code with m rows and n columns. 
TensorQEC.TNMAP — TypeTNMAP(;optimizer::CodeOptimizer=default_optimizer()) <: AbstractGeneralDecoderTensor Network MAP decoder.
Keyword Arguments
optimizer::CodeOptimizer = TreeSA(): The optimizer to use for optimizing the tensor network contraction order.
TensorQEC.TruthTable — TypeTruthTableThe truth table for error correction.
Fields
table::Dict{Int,Int}: The truth table for error correction.num_qubits::Int: The number of qubits.num_st::Int: The number of stabilizers.d::Int64: The maximum number of errors.
TensorQEC.ToricCode — MethodToricCode(m::Int, n::Int)Construct a Toric code with m rows and n columns. 
TensorQEC.annotate_history — Methodannotate_history(res::CliffordSimulateResult{N})Annotate the history of Pauli strings in the result of clifford_simulate.
Arguments
res: The result ofclifford_simulate.
Returns
draw: The visualization of the history.
TensorQEC.clifford_group — Methodclifford_group(n::Int)Generate the n-qubit Clifford group.
TensorQEC.clifford_network — Methodclifford_network(qc::ChainBlock)Generate a Clifford network from a quantum circuit.
TensorQEC.clifford_simulate — Methodclifford_simulate(ps::PauliString, qc::ChainBlock)Map the Pauli string ps by a quantum circuit qc. 
Arguments
ps: The Pauli string.qc: The quantum circuit.
Returns
result: ACliffordSimulateResultrecords the output Pauli string, the phase factor, the simplified circuit, and the history of Pauli strings.
TensorQEC.coherent_error_unitary — Methodcoherent_error_unitary(u::AbstractMatrix{T}, error_rate::Real; cache::Union{Vector, Nothing} = nothing) where TGenerate the error unitary near the given error rate.
Arguments
u: The original unitary.error_rate: The error rate.cache: The vector to store the error rate.
Returns
q: The errored unitary.
TensorQEC.correction_circuit — Methodcorrection_circuit(table::Dict{Int,Int}, num_qubits::Int, num_st::Int, st_pos::AbstractVector{Int}, total_qubits::Int)Generate the error correction circuit by embedding the truth table into the quantum circuit.
Arguments
table: The truth table for error correction.num_qubits: The number of qubits in the circuit.num_st: The number of stabilizers.st_pos: The indices of ancilla qubits that measure stabilizers.total_qubits: The total number of qubits in the circuit.
Returns
qc: The error correction circuit.
TensorQEC.correction_dict — Methodcorrection_dict(st::Vector{PauliString{N}}, d::Int64; et="XZ")Generate the correction dictionary for the given stabilizers.
Arguments
st: The vector of pauli strings, composing the generator of stabilizer group.d: The maximum number of errors.et: The type of error to be corrected. It can be "X", "Z", or "XZ". Default is "XZ".
Returns
table: The correction dictionary, mapping the syndrome to the corresponding error pattern.
TensorQEC.correction_pauli_string — Methodcorrection_pauli_string(qubit_num::Int, syn::Dict{Int, Bool}, prob::Dict{Int, Vector{Float64}})Generate the error Pauli string in the coding space. To correct the error, we still need to transform it to the physical space.
Arguments
qubit_num: The number of qubits.syn: The syndrome dictionary.prob: The inferred error probability of each physical qubit in coding space.
Returns
ps: The error Pauli string in the coding space.
TensorQEC.decode — Methoddecode(decoder::AbstractDecoder, problem::AbstractDecodingProblem, syndrome::AbstractSyndrome)Decode the syndrome using the decoder.
TensorQEC.encode_stabilizers — Methodencode_stabilizers(stabilizers::AbstractVector{PauliString{N}}) where NGenerate the encoding circuit for the given stabilizers.
Arguments
stabilizers: The vector of pauli strings, composing the generator of stabilizer group.
Returns
qc: The encoding circuit.data_qubits: The indices of data qubits.bimat: The structure storing the encoding information.
TensorQEC.error_pairs — Methoderror_pairs(error_rate::T; gates = nothing) where {T <: Real}Generate the error pairs for the given error rate.
Arguments
error_rate: The error rate.gates: The gates to be errored. If not specified, it is set to[X,Y,Z,H,CCZ,ConstGate.Toffoli,ConstGate.CNOT,ConstGate.CZ]
Returns
pairs: The error pairs.vec: The vector to store the error rate to each gate.
TensorQEC.error_quantum_circuit — Methoderror_quantum_circuit(qc::ChainBlock, error_rate::T ) where {T <: Real}Generate the error quantum circuit for the given error rate.
Arguments
qc: The quantum circuit.error_rate: The error rate.
Returns
eqc: The error quantum circuit.
TensorQEC.error_quantum_circuit_pair_replace — Methoderror_quantum_circuit_pair_replace(qc::ChainBlock, error_rate::T ) where {T <: Real}
error_quantum_circuit_pair_replace(qc::ChainBlock, pairs)Generate the error quantum circuit for the given error rate. The errored gate to the same type of gate is the same.
Arguments
qc: The quantum circuit.error_rate: The error rate.pairs: The error gates used to replace the original gates.
Returns
qcf: The error quantum circuit.vec: The vector to store the error rate to each gate.
TensorQEC.fidelity_tensornetwork — Methodfidelity_tensornetwork(qc::ChainBlock,qc_info::QCInfo)Generate the tensor network representation of the quantum circuit fidelity with the given QCInfo, where ancilla qubits are initilized at zero state and partial traced after the circuit. The data qubits are traced out.
Arguments
qc: The quantum circuit.qc_info: The qubit information of the quantum circuit
Returns
tn: The tensor network representation of the quantum circuit.
TensorQEC.get_graph — Methodget_graph(tanner::SimpleTannerGraph)
get_graph(ctg::CSSTannerGraph)Get the simple graph of a simple tanner graph or a CSS tanner graph.
TensorQEC.inference — Methodinference(measure_outcome::Vector{Int}, code::CSSBimatrix, qc::ChainBlock, p::Vector{Vector{Float64}})Infer the error probability of each qubit from the measurement outcome of the stabilizers.
Arguments
measure_outcome: The measurement outcome of the stabilizers, which is either 1 or -1.code: The structure storing the encoding information.qc: The encoding circuit.p: The prior error probability of each physical qubit.
Returns
ps_ec_phy: The error Pauli string for error correction.
TensorQEC.measure_circuit_fault_tol — Methodmeasure_circuit_fault_tol(sts::Vector{PauliString{N}}) where NGenerate the Shor type measurement circuit for fault tolerant measurement.
Arguments
sts: The vector of Pauli strings, composing the generator of stabilizer group.
Returns
qc: The measurement circuit.st_pos: The ancilla qubit indices that measrue corresponding stabilizers.num_qubits: The total number of qubits in the circuit.
TensorQEC.measure_circuit_steane — Methodmeasure_circuit_steane(data_qubit::Int, sts::Vector{PauliString{N}};qcen = nothing) where NGenerate the Steane type measurement circuit.
Arguments
data_qubit: The index of the data qubit.sts: The vector of Pauli strings, composing the generator of stabilizer group.qcen: The encoding circuit. Ifnothing, the measurement circuit will not contain the encoder for ancilla qubits.
Returns
qc: The measurement circuit.st_pos: The ancilla qubit indices that measrue corresponding stabilizers.
TensorQEC.measure_syndrome! — Methodmeasure_syndrome!(reg::AbstractRegister, stabilizers::AbstractVector{PauliString{N}}) where NMeasure the given stabilizers.
Arguments
reg: The quantum register.stabilizers: The vector of pauli strings, composing the generator of stabilizer group.
Returns
measure_outcome: The measurement outcome of the stabilizers, which is either 1 or -1.
TensorQEC.pauli_basis — Methodpauli_basis(nqubits::Int)Generate the n-qubit Pauli basis.
TensorQEC.pauli_decomposition — Methodpauli_decomposition(m::AbstractMatrix)Decompose a matrix into the Pauli basis.
TensorQEC.pauli_mapping — Methodpauli_mapping(m::AbstractMatrix)Convert a linear operator to a matrix in the Pauli basis.
TensorQEC.pauli_string_map_iter — Methodpauli_string_map_iter(ps::PauliString{N}, qc::ChainBlock) where NMap the Pauli string ps by a quantum circuit qc. Return the mapped Pauli string.
TensorQEC.paulistring — Methodpaulistring(n::Int, k::Int, ids::Vector{Int}) -> PauliStringCreate a Pauli string with n qubits, where the i-th qubit is k if i is in ids, otherwise 1. k = 1 for I2, 2 for X, 3 for Y, 4 for Z.
TensorQEC.perm_of_paulistring — Methodperm_of_paulistring(ps::PauliString, operation::Pair{Vector{Int}, <:PermMatrix})Map the Pauli string ps by a permutation matrix pm. Return the mapped Pauli string and the phase factor.
Arguments
ps: The Pauli string.operation: A pair of the positions to apply the permutation and the permutation matrix.
Returns
ps: The mapped Pauli string.val: The phase factor.
TensorQEC.place_qubits — Methodplace_qubits(reg0::AbstractRegister, data_qubits::Vector{Int}, num_qubits::Int)Place the data qubits to the specified position. The other qubits are filled with zero state.
Arguments
reg0: The data register.data_qubits: The indices of data qubits.num_qubits: The total number of qubits.
Returns
reg: The register with data qubits placed at the specified position.
TensorQEC.random_error_qubits — Methodrandom_error_qubits(qubit_number::Int, em::AbstractErrorModel)Generate a random error pattern for a given number of qubits and an error model.
TensorQEC.simulation_tensornetwork — Methodsimulation_tensornetwork(qc::ChainBlock,qc_info::QCInfo)Generate the tensor network representation of the quantum circuit with the given QCInfo, where ancilla qubits are initilized at zero state and partial traced after the circuit.
Arguments
qc: The quantum circuit.qc_info: The qubit information of the quantum circuit
Returns
tn: The tensor network representation of the quantum circuit.input_indices: The input indices of the tensor network.output_indices: The output indices of the tensor network.
TensorQEC.stabilizers — Methodstabilizers(tc::ToricCode)
stabilizers(sc::SurfaceCode)
stabilizers(shor::ShorCode)
stabilizers(steane::SteaneCode)
stabilizers(code832::Code832)Get the stabilizers of the code instances.
TensorQEC.syndrome_extraction — Methodsyndrome_extraction(errored_qubits::Vector{Mod2}, H::Matrix{Mod2})
syndrome_extraction(errored_qubits::Vector{Mod2}, tanner::SimpleTannerGraph)
syndrome_extraction(error_patterns::CSSErrorPattern, tanner::CSSTannerGraph)Extract the syndrome from the error pattern.
TensorQEC.syndrome_inference — Methodsyndrome_inference(cl::CliffordNetwork{T}, syn::Dict{Int,Bool}, p::Vector{Vector{Float64}}) where TInfer the error probability of each qubit from the measurement outcome of the stabilizers.
Arguments
cl: The Clifford network.syn: The syndrome dictionary.p: The prior error probability of each physical qubit.
Returns
pinf: The inferred error probability of each physical qubit in coding space. For errored stabilizers, there are two possibilities: X or Y error. For unerrored stabilizers, there are also two possibilities: no error or Z error. For unmeasured qubits, there are four possibilities: no error, X error, Y error, Z error. Therefore the length of each vector inpinfmay be 2 or 4.
TensorQEC.to_perm_matrix — Methodto_perm_matrix([::Type{T}, ::Type{Ti}, ]matrix_or_yaoblock; atol=1e-8)Convert a Clifford gate to its permutation representation.
Arguments
T: Element type of phase factor.Ti: Element type of the permutation matrix.m: The matrix representation of the gate.atol: The tolerance to zeros in the matrix.
Returns
pm: The permutation matrix. pm.perm is the permutation vector, pm.vals is the phase factor.
TensorQEC.transformed_syndrome_dict — Methodtransformed_syndrome_dict(measure_outcome::Vector{Int}, code::CSSBimatrix)Generate the syndrome dictionary on the transformed stabilizers from the measurement outcome.
Arguments
measure_outcome: The measurement outcome of the stabilizers, which is either 1 or -1.code: The structure storing the encoding information.
Returns
syn_dict: The syndrome dictionary on the transformed stabilizers. 1 is transformed to 0, -1 is transformed to 1.
Multiprocessing
TensorQEC.SimpleMultiprocessing.multiprocess_run — Functionmultiprocess_run(func, inputs::AbstractVector)Execute function func on inputs with multiple processing.
Example
Suppose we have a file run.jl with the following contents
using GenericTensorNetworks.SimpleMultiprocessing
results = multiprocess_run(x->x^2, randn(8))In an terminal, you may run the script with 4 processes by typing
$ julia -p4 run.jl
      From worker 2:	[ Info: running argument -0.17544008350172655 on device 2
      From worker 5:	[ Info: running argument 0.34578117779452555 on device 5
      From worker 3:	[ Info: running argument 2.0312551239727705 on device 3
      From worker 4:	[ Info: running argument -0.7319353419291961 on device 4
      From worker 2:	[ Info: running argument 0.013132180639054629 on device 2
      From worker 3:	[ Info: running argument 0.9960101782201602 on device 3
      From worker 4:	[ Info: running argument -0.5613942832743966 on device 4
      From worker 5:	[ Info: running argument 0.39460402723831134 on device 5