Prove2Me
Navigate
DiscoverCollectionsFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

The fault-tolerant circuit C′, its ExRecs, good and bad locations, the circuit C̃, local

Definition
QAlgorithms_Defs_FTCircuit

by Goku · Oct 13, 2026 · Mathlib 0df444a (Lean v4.33.1)

lecture-notesquantum-information

The fault-tolerant circuit C′, its ExRecs, good and bad locations, the circuit C̃, local

QAlgorithms.FTProtocol.HasUniformDepth

For a fault-tolerance protocol PPP with gadget circuits GκG_\kappaGκ​, one per location kind κ∈{prep,had,tgate,cnot,meas,wait}\kappa\in\{\mathrm{prep},\mathrm{had},\mathrm{tgate},\mathrm{cnot},\mathrm{meas},\mathrm{wait}\}κ∈{prep,had,tgate,cnot,meas,wait}, this is the proposition that every gadget has the same number of time steps as the preparation gadget:

∀κ:#steps(Gκ)=#steps(Gprep).\forall \kappa:\quad \#\mathrm{steps}(G_\kappa)=\#\mathrm{steps}(G_{\mathrm{prep}}).∀κ:#steps(Gκ​)=#steps(Gprep​).

The error-correction circuit of PPP is not constrained.

QAlgorithms.FTProtocol.gadgetDepth

For a fault-tolerance protocol PPP, this is the natural number

max⁡(#steps(Gprep),#steps(Ghad),#steps(Gtgate),#steps(Gcnot),#steps(Gmeas),#steps(Gwait),0).\max\big(\#\mathrm{steps}(G_{\mathrm{prep}}),\#\mathrm{steps}(G_{\mathrm{had}}),\#\mathrm{steps}(G_{\mathrm{tgate}}),\#\mathrm{steps}(G_{\mathrm{cnot}}),\#\mathrm{steps}(G_{\mathrm{meas}}),\#\mathrm{steps}(G_{\mathrm{wait}}),0\big).max(#steps(Gprep​),#steps(Ghad​),#steps(Gtgate​),#steps(Gcnot​),#steps(Gmeas​),#steps(Gwait​),0).

This is the maximum number of time steps (step lists, which may be empty) over the six gadget circuits of PPP. The error-correction circuit is not included.

QAlgorithms.FTProtocol.period

For a fault-tolerance protocol PPP, this is the natural number

period(P)=gadgetDepth(P)+#steps(GEC).\mathrm{period}(P)=\mathrm{gadgetDepth}(P)+\#\mathrm{steps}(G_{\mathrm{EC}}).period(P)=gadgetDepth(P)+#steps(GEC​).

The first term is the maximum step count over the six gadget circuits (as above). The second term is the number of time steps of PPP's error-correction circuit.

QAlgorithms.FTInstance

For a natural number NNN, FTInstance(N)\mathrm{FTInstance}(N)FTInstance(N) is a tagged disjoint union with two kinds of elements:

  • gadget(ℓ)\mathrm{gadget}(\ell)gadget(ℓ) for an arbitrary pair ℓ=(s,j)∈N×N\ell=(s,j)\in\mathbb N\times\mathbb Nℓ=(s,j)∈N×N;
  • ec(s,q)\mathrm{ec}(s,q)ec(s,q) for an arbitrary s∈Ns\in\mathbb Ns∈N and q∈{0,…,N−1}q\in\{0,\dots,N-1\}q∈{0,…,N−1}.

Two elements are equal exactly when they have the same tag and equal data, and this equality is decidable. No bound ties ℓ\ellℓ or sss to any circuit.

QAlgorithms.blockEmb

The inputs are natural numbers n,A,k,Nn,A,k,Nn,A,k,N and an injective map w:{0,…,k−1}→{0,…,N−1}w:\{0,\dots,k-1\}\to\{0,\dots,N-1\}w:{0,…,k−1}→{0,…,N−1}. Set B=n+AB=n+AB=n+A. Then blockEmb(n,A,w)\mathrm{blockEmb}(n,A,w)blockEmb(n,A,w) is the injective map

{0,…,kB−1}→{0,…,NB−1},aB+p ↦ w(a) B+p(0≤a<k, 0≤p<B).\{0,\dots,kB-1\}\to\{0,\dots,NB-1\},\qquad aB+p\ \mapsto\ w(a)\,B+p\qquad(0\le a<k,\ 0\le p<B).{0,…,kB−1}→{0,…,NB−1},aB+p ↦ w(a)B+p(0≤a<k, 0≤p<B).

In words, the index is split into a block number a=⌊x/B⌋a=\lfloor x/B\rfloora=⌊x/B⌋ and an offset p=x mod Bp=x\bmod Bp=xmodB. Block aaa is sent to block w(a)w(a)w(a) with the same offset. Injectivity follows from that of www. If B=0B=0B=0 both index sets are empty.

QAlgorithms.ecEmb

The inputs are natural numbers n,A,Nn,A,Nn,A,N and q∈{0,…,N−1}q\in\{0,\dots,N-1\}q∈{0,…,N−1}. Set B=n+AB=n+AB=n+A. Then ecEmb(n,A,q)\mathrm{ecEmb}(n,A,q)ecEmb(n,A,q) is the injective map

{0,…,B−1}→{0,…,NB−1},p↦qB+p,\{0,\dots,B-1\}\to\{0,\dots,NB-1\},\qquad p\mapsto qB+p,{0,…,B−1}→{0,…,NB−1},p↦qB+p,

which places a single block of BBB wires as the qqq-th block.

QAlgorithms.gadgetOffset

The inputs are a fault-tolerance protocol PPP, a located circuit CCC on NNN qubits (a list of time steps, each a list of locations of some kind), and s,j,d∈Ns,j,d\in\mathbb Ns,j,d∈N. The value is

gadgetOffset(P,C,s,j,d)=∑l∈first j locations of step s of C#(locations in step d of Gkind(l)).\mathrm{gadgetOffset}(P,C,s,j,d)=\sum_{l\in\text{first }j\text{ locations of step }s\text{ of }C}\#\big(\text{locations in step }d\text{ of }G_{\mathrm{kind}(l)}\big).gadgetOffset(P,C,s,j,d)=l∈first j locations of step s of C∑​#(locations in step d of Gkind(l)​).

The edge cases are:

  • step sss of CCC is taken to be empty if s≥#steps(C)s\ge\#\mathrm{steps}(C)s≥#steps(C), giving value 000;
  • if jjj exceeds the length of step sss, all of its locations are used;
  • step ddd of a gadget is taken to be empty (contributing 000) if ddd is beyond the gadget's number of steps.

QAlgorithms.gadgetGlobalId

The inputs are a fault-tolerance protocol PPP, a located circuit CCC on NNN qubits, s,j∈Ns,j\in\mathbb Ns,j∈N, and a local location id lid=(d,i)∈N2\mathrm{lid}=(d,i)\in\mathbb N^2lid=(d,i)∈N2. The value is the pair

(s⋅period(P)+d,  gadgetOffset(P,C,s,j,d)+i).\big(s\cdot\mathrm{period}(P)+d,\ \ \mathrm{gadgetOffset}(P,C,s,j,d)+i\big).(s⋅period(P)+d,  gadgetOffset(P,C,s,j,d)+i).

Here period(P)\mathrm{period}(P)period(P) is the maximum gadget step count plus the step count of PPP's error-correction circuit. gadgetOffset(P,C,s,j,d)\mathrm{gadgetOffset}(P,C,s,j,d)gadgetOffset(P,C,s,j,d) is the total number of locations in step ddd of the gadgets of the first jjj locations of step sss of CCC, with out-of-range steps counted as empty. No range checks are made on sss, jjj, ddd, or iii.

QAlgorithms.ecBlocks

For a located circuit CCC on NNN qubits and s∈Ns\in\mathbb Ns∈N, this is the list, in increasing order, of those q∈{0,…,N−1}q\in\{0,\dots,N-1\}q∈{0,…,N−1} that satisfy the following: some location lll in step sss of CCC has kind different from measurement and acts on qqq (that is, qqq is among the wires of lll). If s≥#steps(C)s\ge\#\mathrm{steps}(C)s≥#steps(C), the step is treated as empty and the list is empty.

QAlgorithms.ecGlobalId

The inputs are a fault-tolerance protocol PPP, s,idx∈Ns,\mathrm{idx}\in\mathbb Ns,idx∈N, and a local id lid=(d,i)∈N2\mathrm{lid}=(d,i)\in\mathbb N^2lid=(d,i)∈N2. The value is the pair

(s⋅period(P)+gadgetDepth(P)+d,  idx⋅md+i),\big(s\cdot\mathrm{period}(P)+\mathrm{gadgetDepth}(P)+d,\ \ \mathrm{idx}\cdot m_d+i\big),(s⋅period(P)+gadgetDepth(P)+d,  idx⋅md​+i),

where mdm_dmd​ is the number of locations in step ddd of PPP's error-correction circuit, and md=0m_d=0md​=0 if ddd is beyond its number of steps. In that case the second component is just iii. Here gadgetDepth(P)\mathrm{gadgetDepth}(P)gadgetDepth(P) is the maximum step count over the six gadgets, and period(P)\mathrm{period}(P)period(P) is that plus the error-correction step count. No range checks are made.

QAlgorithms.decodedOutcomes

The inputs are:

  • a fault-tolerance protocol PPP;
  • a located circuit CCC on NNN qubits;
  • a partial outcome assignment O:N2→{none}∪{0,1}O:\mathbb N^2\to\{\text{none}\}\cup\{0,1\}O:N2→{none}∪{0,1}.

The result is a partial assignment on ids cid=(c1,c2)∈N2\mathrm{cid}=(c_1,c_2)\in\mathbb N^2cid=(c1​,c2​)∈N2, defined as follows.

  • No location or non-measurement. If CCC has no location at cid\mathrm{cid}cid, the value is none. "No location" means c1≥#steps(C)c_1\ge\#\mathrm{steps}(C)c1​≥#steps(C) or c2≥c_2\gec2​≥ the length of step c1c_1c1​. If CCC has a location there but its kind is not measurement, the value is also none.
  • Measurement location. Let (λ1,…,λM)(\lambda_1,\dots,\lambda_M)(λ1​,…,λM​) be the list of positions (d,i)(d,i)(d,i) of the measurement locations in the measurement gadget GmeasG_{\mathrm{meas}}Gmeas​, ordered by step and then by position within the step.
    • If O(gadgetGlobalId(P,C,c1,c2,λt))O\big(\mathrm{gadgetGlobalId}(P,C,c_1,c_2,\lambda_t)\big)O(gadgetGlobalId(P,C,c1​,c2​,λt​)) is defined for every ttt, the value is the bit
measDecode([O(gadgetGlobalId(P,C,c1,c2,λ1)),…,O(gadgetGlobalId(P,C,c1,c2,λM))]).\mathrm{measDecode}\Big(\big[O(\mathrm{gadgetGlobalId}(P,C,c_1,c_2,\lambda_1)),\dots,O(\mathrm{gadgetGlobalId}(P,C,c_1,c_2,\lambda_M))\big]\Big).measDecode([O(gadgetGlobalId(P,C,c1​,c2​,λ1​)),…,O(gadgetGlobalId(P,C,c1​,c2​,λM​))]).
  • Otherwise the value is none.

If M=0M=0M=0, the "all defined" test holds vacuously, and the value is measDecode\mathrm{measDecode}measDecode of the empty list.

QAlgorithms.gadgetPhaseStep

Inputs.

  • PPP is a fault-tolerance protocol. It has a code block size nnn, an ancilla count AAA, a gadget circuit GκG_\kappaGκ​ for each location kind κ∈{prep,had,tgate,cnot,meas,wait}\kappa\in\{\mathrm{prep},\mathrm{had},\mathrm{tgate},\mathrm{cnot},\mathrm{meas},\mathrm{wait}\}κ∈{prep,had,tgate,cnot,meas,wait}, and other fields not used here. GκG_\kappaGκ​ acts on ar(κ) (n+A)\mathrm{ar}(\kappa)\,(n+A)ar(κ)(n+A) wires, where the arity ar\mathrm{ar}ar is 222 for cnot and 111 otherwise.
  • CCC is a located circuit on NNN wires, with NNN implicit. It is a list of time steps C0,C1,…C_0, C_1, \dotsC0​,C1​,…. Each step is a list of locations lll, and each location has:
    • a kind l.κl.\kappal.κ;
    • an injective wire map l.w:{0,…,ar(l.κ)−1}→{0,…,N−1}l.w:\{0,\dots,\mathrm{ar}(l.\kappa)-1\}\to\{0,\dots,N-1\}l.w:{0,…,ar(l.κ)−1}→{0,…,N−1};
    • a condition l.condl.\mathrm{cond}l.cond, a Boolean function of an outcome record O:N×N→{true,false,undefined}O:\mathbb N\times\mathbb N\to\{\text{true},\text{false},\text{undefined}\}O:N×N→{true,false,undefined}.
  • s,d∈Ns,d\in\mathbb Ns,d∈N.

Output. The result is a list of pairs (location on N(n+A)N(n+A)N(n+A) wires, tag). It is built as follows. Let CsC_sCs​ be step sss of CCC; it is the empty list if sss is out of range. Write its entries as (lj)j(l_j)_j(lj​)j​ with 000-based positions jjj. For each jjj in order, and for each location ggg of step ddd of Glj.κG_{l_j.\kappa}Glj​.κ​ in order (empty if ddd is out of range), the list contains one entry:

  • kind: g.κg.\kappag.κ.
  • wires: βlj∘g.w\beta_{l_j}\circ g.wβlj​​∘g.w. Here βl\beta_{l}βl​ is the block embedding (i,p)↦l.w(i)⋅(n+A)+p(i,p)\mapsto l.w(i)\cdot(n+A)+p(i,p)↦l.w(i)⋅(n+A)+p. It sends position p∈{0,…,n+A−1}p\in\{0,\dots,n+A-1\}p∈{0,…,n+A−1} of the iii-th sub-block of the gadget to position ppp of the block attached to wire l.w(i)l.w(i)l.w(i). A gadget wire index xxx is first decomposed as x=i(n+A)+px = i(n+A)+px=i(n+A)+p.
  • condition:
O  ↦  g.cond(lid↦O(γs,j(lid)))  ∧  {lj.cond(δ(O))lj.κ∈{had,tgate,cnot}trueotherwise.O\;\mapsto\; g.\mathrm{cond}\big(\mathrm{lid}\mapsto O(\gamma_{s,j}(\mathrm{lid}))\big)\;\wedge\;\begin{cases} l_j.\mathrm{cond}(\delta(O)) & l_j.\kappa\in\{\mathrm{had},\mathrm{tgate},\mathrm{cnot}\}\\ \text{true} & \text{otherwise.}\end{cases}O↦g.cond(lid↦O(γs,j​(lid)))∧{lj​.cond(δ(O))true​lj​.κ∈{had,tgate,cnot}otherwise.​
  • γs,j(a,b)=(s⋅π+a, offs,j(a)+b)\gamma_{s,j}(a,b) = \big(s\cdot\pi + a,\ \mathrm{off}_{s,j}(a) + b\big)γs,j​(a,b)=(s⋅π+a, offs,j​(a)+b).
  • π=D+∣Ec∣\pi = D + |E_c|π=D+∣Ec​∣ is the period. DDD is the maximum number of steps over the six gadgets (the value 000 if all are empty), and ∣Ec∣|E_c|∣Ec​∣ is the number of steps of PPP's error-correction circuit.
  • offs,j(a)\mathrm{off}_{s,j}(a)offs,j​(a) is the sum, over the first jjj locations lll of CsC_sCs​, of the length of step aaa of Gl.κG_{l.\kappa}Gl.κ​ (that length is 000 if the step is absent).
  • The decoded record δ(O)\delta(O)δ(O) is defined at a position ccc as follows. If CCC has a measurement location at ccc, and all values O(γc1,c2(lid))O(\gamma_{c_1,c_2}(\mathrm{lid}))O(γc1​,c2​​(lid)) are defined for the measurement ids lid\mathrm{lid}lid of GmeasG_{\mathrm{meas}}Gmeas​ (in their list order), then δ(O)(c)\delta(O)(c)δ(O)(c) is PPP's measurement decoder applied to that list of bits. In every other case δ(O)(c)\delta(O)(c)δ(O)(c) is undefined.
  • tag: "gadget at (s,j)(s,j)(s,j)".

All these entries are concatenated in the order of jjj, then of ggg.

QAlgorithms.ecPhaseStep

Inputs.

  • PPP is a fault-tolerance protocol with block size nnn, ancilla count AAA, and error-correction circuit EcE_cEc​ on n+An+An+A wires.
  • CCC is a located circuit on NNN wires, with NNN implicit. Its steps are lists of locations; each location carries a kind, an injective wire map into {0,…,N−1}\{0,\dots,N-1\}{0,…,N−1}, and a Boolean condition on outcome records OOO.
  • s,e∈Ns,e\in\mathbb Ns,e∈N.

The list of blocks. Let Bs=(q0,q1,… )B_s=(q_0,q_1,\dots)Bs​=(q0​,q1​,…) be the list of wires q∈{0,…,N−1}q\in\{0,\dots,N-1\}q∈{0,…,N−1}, in increasing order, such that some location of step sss of CCC has kind other than measurement and has qqq among its wires. If sss is out of range the step is taken to be empty, and then BsB_sBs​ is empty.

Output. For each index idx\mathrm{idx}idx and wire q=qidxq=q_{\mathrm{idx}}q=qidx​ of BsB_sBs​, and each location ggg of step eee of EcE_cEc​ (empty if eee is out of range), the list contains one entry:

  • kind: g.κg.\kappag.κ.
  • wires: p↦q(n+A)+pp\mapsto q(n+A)+pp↦q(n+A)+p composed with g.wg.wg.w, i.e. g.wg.wg.w followed by placement into the block of wire qqq.
  • condition: O↦g.cond(lid↦O(ϵs,idx(lid)))O\mapsto g.\mathrm{cond}\big(\mathrm{lid}\mapsto O(\epsilon_{s,\mathrm{idx}}(\mathrm{lid}))\big)O↦g.cond(lid↦O(ϵs,idx​(lid))), where
ϵs,idx(a,b)=(sπ+D+a, idx⋅La+b).\epsilon_{s,\mathrm{idx}}(a,b)=\big(s\pi + D + a,\ \mathrm{idx}\cdot L_a + b\big).ϵs,idx​(a,b)=(sπ+D+a, idx⋅La​+b).
  • DDD is the maximum step count over PPP's six gadgets.
  • π=D+∣Ec∣\pi = D+|E_c|π=D+∣Ec​∣.
  • LaL_aLa​ is the length of step aaa of EcE_cEc​, or 000 if that step is absent.
  • tag: "error correction at step sss on wire qqq".

The entries are concatenated in the order of idx\mathrm{idx}idx, then of ggg.

QAlgorithms.ftTaggedSteps

Inputs. PPP is a fault-tolerance protocol with block size nnn and ancilla count AAA. CCC is a located circuit on NNN wires (NNN implicit) with LLL steps.

Notation.

  • DDD is the maximum number of steps over PPP's six gadget circuits (the value 000 if all are empty).
  • ∣Ec∣|E_c|∣Ec​∣ is the number of steps of PPP's error-correction circuit.
  • GP(s,d)\mathrm{GP}(s,d)GP(s,d) denotes the gadget-phase step list: for every location ljl_jlj​ of step sss of CCC, the locations of step ddd of the gadget for ljl_jlj​'s kind, placed onto the blocks of ljl_jlj​'s wires and tagged "gadget (s,j)(s,j)(s,j)", with conditions as defined for gadgetPhaseStep.
  • EP(s,e)\mathrm{EP}(s,e)EP(s,e) denotes the EC-phase step list: for each wire touched in step sss by a non-measurement location, the locations of step eee of the EC circuit, placed on that wire's block and tagged "EC (s,q)(s,q)(s,q)", as defined for ecPhaseStep.

Output. A list of time steps on N(n+A)N(n+A)N(n+A) wires, each step a list of (location, tag) pairs, equal to

[GP(0,0),…,GP(0,D−1), EP(0,0),…,EP(0,∣Ec∣−1),  GP(1,0),…  …,EP(L−1,∣Ec∣−1)].\big[\mathrm{GP}(0,0),\dots,\mathrm{GP}(0,D-1),\ \mathrm{EP}(0,0),\dots,\mathrm{EP}(0,|E_c|-1),\ \ \mathrm{GP}(1,0),\dots\ \ \dots,\mathrm{EP}(L-1,|E_c|-1)\big].[GP(0,0),…,GP(0,D−1), EP(0,0),…,EP(0,∣Ec​∣−1),  GP(1,0),…  …,EP(L−1,∣Ec​∣−1)].

It has exactly L (D+∣Ec∣)L\,(D+|E_c|)L(D+∣Ec​∣) steps.

  • If L=0L=0L=0 the result is the empty list.
  • A gadget phase step GP(s,d)\mathrm{GP}(s,d)GP(s,d) with ddd beyond some gadget's length contributes nothing for locations of that kind.

QAlgorithms.ftCircuit

Inputs. PPP is a fault-tolerance protocol with block size nnn and ancilla count AAA. CCC is a located circuit on NNN wires (NNN implicit) with LLL steps.

Output. The located circuit on N(n+A)N(n+A)N(n+A) wires obtained from the tagged step list of ftTaggedSteps, keeping each location and discarding its tag.

The tagged step list. For each original step s=0,…,L−1s=0,\dots,L-1s=0,…,L−1, in order, it consists of:

  • DDD gadget-phase steps. DDD is the maximum step count over PPP's six gadgets. Gadget-phase step ddd collects, for every location ljl_jlj​ of CCC's step sss, the ddd-th step of the gadget for ljl_jlj​'s kind. Its wires are mapped so that sub-block iii, position ppp goes to wire lj.w(i)(n+A)+pl_j.w(i)(n+A)+plj​.w(i)(n+A)+p. Its conditions read outcomes at shifted global ids. For gate kinds they are additionally conjoined with ljl_jlj​'s own condition evaluated on decoded measurement outcomes.
  • ∣Ec∣|E_c|∣Ec​∣ EC-phase steps. EC-phase step eee applies the eee-th step of PPP's EC circuit to the block of every wire touched in step sss by a non-measurement location.

QAlgorithms.ftOwner

Inputs.

  • PPP is a fault-tolerance protocol.
  • CCC is a located circuit on NNN wires (NNN implicit).
  • id=(a,b)∈N×N\mathrm{id}=(a,b)\in\mathbb N\times\mathbb Nid=(a,b)∈N×N.

Output. An optional tag.

  • Defined case. Consider step aaa of the tagged step list ftTaggedSteps(P,C)\mathrm{ftTaggedSteps}(P,C)ftTaggedSteps(P,C) (the empty list if aaa is out of range). If its bbb-th entry (0-based) exists, the output is that entry's tag. The tag is either "gadget (s,j)(s,j)(s,j)", meaning the location belongs to the gadget replacing location jjj of step sss of CCC, or "EC (s,q)(s,q)(s,q)", meaning it belongs to the error-correction block on wire qqq after step sss.
  • Undefined case. If the entry does not exist, the output is "none".

QAlgorithms.ftDecode

Inputs.

  • PPP is a fault-tolerance protocol.
  • CCC is a located circuit on NNN wires (NNN implicit).
  • rrr is a finite list of bits, the measurement record of the fault-tolerant circuit C~=ftCircuit(P,C)\widetilde C=\mathrm{ftCircuit}(P,C)C=ftCircuit(P,C).

The output-reading map out(c)\mathrm{out}(c)out(c). The measurement ids of a located circuit are the positions (s,j)(s,j)(s,j) of its measurement-kind locations, listed by step and then by position. For a position ccc:

  • If ccc is the kkk-th measurement id of C~\widetilde CC and rrr has a kkk-th entry, then out(c)\mathrm{out}(c)out(c) is that entry (000-based).
  • Otherwise out(c)\mathrm{out}(c)out(c) is false.

The id map γs,j\gamma_{s,j}γs,j​. It is defined by

γs,j(a,b)=(sπ+a, offs,j(a)+b).\gamma_{s,j}(a,b) = \big(s\pi + a,\ \mathrm{off}_{s,j}(a)+b\big).γs,j​(a,b)=(sπ+a, offs,j​(a)+b).
  • π=D+∣Ec∣\pi = D+|E_c|π=D+∣Ec​∣, where DDD is the maximum gadget step count and ∣Ec∣|E_c|∣Ec​∣ is the EC step count.
  • offs,j(a)\mathrm{off}_{s,j}(a)offs,j​(a) is the sum over the first jjj locations lll of CCC's step sss of the length of step aaa of the gadget for lll's kind.

Output. The list indexed by the measurement ids c=(c1,c2)c=(c_1,c_2)c=(c1​,c2​) of CCC, in order, whose entry for ccc is

measDecodeP([out(γc1,c2(lid))]lid∈meas-ids of Gmeas),\mathrm{measDecode}_P\Big(\big[\mathrm{out}(\gamma_{c_1,c_2}(\mathrm{lid}))\big]_{\mathrm{lid}\in\text{meas-ids of }G_{\mathrm{meas}}}\Big),measDecodeP​([out(γc1​,c2​​(lid))]lid∈meas-ids of Gmeas​​),

where measDecodeP\mathrm{measDecode}_PmeasDecodeP​ is PPP's map from bit lists to a bit.

QAlgorithms.exRecLocs

Inputs.

  • PPP is a fault-tolerance protocol.
  • CCC is a located circuit on NNN wires (NNN implicit).
  • ℓ=(ℓ1,ℓ2)∈N×N\ell=(\ell_1,\ell_2)\in\mathbb N\times\mathbb Nℓ=(ℓ1​,ℓ2​)∈N×N.
  • trunc\mathrm{trunc}trunc is a finite set of wires.

The set of all location ids. For a located circuit, this is {(s,j):s<#steps, j<∣step s∣}\{(s,j): s<\#\text{steps},\ j<|\text{step }s|\}{(s,j):s<#steps, j<∣step s∣}.

Output.

  • No location at ℓ\ellℓ. If CCC has no location at position ℓ\ellℓ (step ℓ1\ell_1ℓ1​, index ℓ2\ell_2ℓ2​), the result is ∅\emptyset∅.
  • A location lll at ℓ\ellℓ. Otherwise, let lll be that location, with qubit set QlQ_lQl​ (the image of its wire map). The result is the set of location ids id\mathrm{id}id of C~=ftCircuit(P,C)\widetilde C=\mathrm{ftCircuit}(P,C)C=ftCircuit(P,C) whose owner tag (as given by ftOwner) satisfies one of the following:
    • the tag is "gadget ℓ′\ell'ℓ′" with ℓ′=ℓ\ell'=\ellℓ′=ℓ;
    • the tag is "EC (s,q)(s,q)(s,q)" with q∈Qlq\in Q_lq∈Ql​ and either lll's kind is not prep and s+1=ℓ1s+1=\ell_1s+1=ℓ1​ (a leading EC), or lll's kind is not meas, s=ℓ1s=\ell_1s=ℓ1​, and q∉truncq\notin\mathrm{trunc}q∈/trunc (a trailing EC).

Remarks.

  • Ids without an owner are excluded. Every id of C~\widetilde CC in fact has one.
  • When ℓ1=0\ell_1=0ℓ1​=0 no leading EC can qualify.
  • EC instances exist only for wires touched by a non-measurement location in that step.

QAlgorithms.truncOf

Inputs.

  • CCC is a located circuit on NNN wires (NNN implicit).
  • BBB is a finite set of positions in N×N\mathbb N\times\mathbb NN×N.
  • ℓ=(ℓ1,ℓ2)\ell=(\ell_1,\ell_2)ℓ=(ℓ1​,ℓ2​).

Output. A set of wires.

  • If CCC has no location at ℓ\ellℓ, the result is ∅\emptyset∅.
  • Otherwise, with lll the location at ℓ\ellℓ and QlQ_lQl​ its set of wires, the result is
{ q∈Ql  :  ∃ ℓ′∈B, ℓ1′=ℓ1+1, C has a location l′ at ℓ′ with q∈Ql′ }.\{\,q\in Q_l \;:\; \exists\,\ell'\in B,\ \ell'_1=\ell_1+1,\ C \text{ has a location } l' \text{ at } \ell' \text{ with } q\in Q_{l'}\,\}.{q∈Ql​:∃ℓ′∈B, ℓ1′​=ℓ1​+1, C has a location l′ at ℓ′ with q∈Ql′​}.

In words, these are the wires of lll that are also used by some location of BBB lying in the immediately following step.

QAlgorithms.badAbove

Inputs.

  • PPP is a fault-tolerance protocol with fault parameter ttt.
  • CCC is a located circuit on NNN wires (NNN implicit) with LLL steps.
  • SSS is a finite set of positions, interpreted as fault positions in ftCircuit(P,C)\mathrm{ftCircuit}(P,C)ftCircuit(P,C).
  • k∈Nk\in\mathbb Nk∈N.

Definition (recursion on kkk).

  • Base case: bad0=∅\mathrm{bad}_0=\emptysetbad0​=∅.
  • Step: let σ=L−(k+1)\sigma = L-(k+1)σ=L−(k+1), computed with truncated subtraction, so σ=0\sigma=0σ=0 whenever k+1≥Lk+1\ge Lk+1≥L. Then
badk+1=badk ∪ {(σ,j):j<∣Cσ∣,  t<∣ S∩ExRec((σ,j), trunck(σ,j))∣}.\mathrm{bad}_{k+1}=\mathrm{bad}_k\ \cup\ \Big\{(\sigma,j): j<|C_\sigma|,\ \ t < \big|\,S\cap \mathrm{ExRec}\big((\sigma,j),\ \mathrm{trunc}_k(\sigma,j)\big)\big|\Big\}.badk+1​=badk​ ∪ {(σ,j):j<∣Cσ​∣,  t<​S∩ExRec((σ,j), trunck​(σ,j))​}.
  • ∣Cσ∣|C_\sigma|∣Cσ​∣ is the length of step σ\sigmaσ; it is 000 if the step is absent, and then nothing is added.
  • trunck(ℓ)\mathrm{trunc}_k(\ell)trunck​(ℓ) is the set of wires of the location at ℓ\ellℓ that are also wires of some location in badk\mathrm{bad}_kbadk​ in step ℓ1+1\ell_1+1ℓ1​+1, or ∅\emptyset∅ if there is no location at ℓ\ellℓ.
  • ExRec(ℓ,trunc)\mathrm{ExRec}(\ell,\mathrm{trunc})ExRec(ℓ,trunc) is the set of ids of ftCircuit(P,C)\mathrm{ftCircuit}(P,C)ftCircuit(P,C) belonging to: the gadget replacing ℓ\ellℓ; the EC blocks of step ℓ1−1\ell_1-1ℓ1​−1 on ℓ\ellℓ's wires (only if ℓ\ellℓ is not a prep); and the EC blocks of step ℓ1\ell_1ℓ1​ on ℓ\ellℓ's wires outside trunc\mathrm{trunc}trunc (only if ℓ\ellℓ is not a meas).

Effect. Steps are processed from the last (L−1L-1L−1) downward. A location is declared bad when strictly more than ttt faults of SSS fall in its truncated extended rectangle.

Edge case. For k≥Lk\ge Lk≥L, step 000 is re-examined at each further iteration.

QAlgorithms.badAssignment

Inputs.

  • PPP is a fault-tolerance protocol with fault parameter ttt.
  • CCC is a located circuit on NNN wires (NNN implicit) with LLL steps.
  • SSS is a finite set of positions in ftCircuit(P,C)\mathrm{ftCircuit}(P,C)ftCircuit(P,C).

Output. badL\mathrm{bad}_LbadL​, the result of badAbove after exactly LLL iterations. The recursion examines the steps L−1,L−2,…,0L-1, L-2, \dots, 0L−1,L−2,…,0 of CCC, each once, in that order. It has two ingredients:

  • Bad locations. A location ℓ=(σ,j)\ell=(\sigma,j)ℓ=(σ,j) is added when ∣S∩ExRec(ℓ,trunc(ℓ))∣>t|S\cap \mathrm{ExRec}(\ell,\mathrm{trunc}(\ell))|>t∣S∩ExRec(ℓ,trunc(ℓ))∣>t.
  • Truncation. trunc(ℓ)\mathrm{trunc}(\ell)trunc(ℓ) is the set of ℓ\ellℓ's wires shared with locations already declared bad in step σ+1\sigma+1σ+1.

ExRec(ℓ,trunc)\mathrm{ExRec}(\ell,\mathrm{trunc})ExRec(ℓ,trunc) consists of the ids of ftCircuit(P,C)\mathrm{ftCircuit}(P,C)ftCircuit(P,C) belonging to:

  • the gadget for ℓ\ellℓ;
  • the preceding-step EC blocks on ℓ\ellℓ's wires, unless ℓ\ellℓ is a prep;
  • the same-step EC blocks on ℓ\ellℓ's wires not in trunc\mathrm{trunc}trunc, unless ℓ\ellℓ is a meas.

If L=0L=0L=0 the result is ∅\emptyset∅.

QAlgorithms.liftReg

Inputs.

  • α\alphaα is a type, k∈Nk\in\mathbb Nk∈N, and NNN is implicit.
  • w:{0,…,k−1}↪{0,…,N−1}w:\{0,\dots,k-1\}\hookrightarrow\{0,\dots,N-1\}w:{0,…,k−1}↪{0,…,N−1} is injective.
  • Ψ\PsiΨ is an arbitrary map from complex matrices indexed by αk\alpha^{k}αk (functions {0,…,k−1}→α\{0,\dots,k-1\}\to\alpha{0,…,k−1}→α) to such matrices.

The overwrite operation. For y∈αNy\in\alpha^Ny∈αN and z∈αkz\in\alpha^kz∈αk, oww(y,z)∈αN\mathrm{ow}_w(y,z)\in\alpha^Noww​(y,z)∈αN agrees with yyy off the range of www. At a position w(i)w(i)w(i) it takes the value ziz_izi​; the choice of iii is unique by injectivity.

Output. The map sending a matrix XXX indexed by αN\alpha^NαN to the matrix with entries

(liftRegwΨ (X))y,y′  =  Ψ(Zy,y′) y∘w, y′∘w,Zz,z′y,y′=Xoww(y,z), oww(y′,z′).\big(\mathrm{liftReg}_w\Psi\,(X)\big)_{y,y'} \;=\; \Psi\big(Z^{y,y'}\big)_{\,y\circ w,\ y'\circ w},\qquad Z^{y,y'}_{z,z'} = X_{\mathrm{ow}_w(y,z),\ \mathrm{ow}_w(y',z')}.(liftRegw​Ψ(X))y,y′​=Ψ(Zy,y′)y∘w, y′∘w​,Zz,z′y,y′​=Xoww​(y,z), oww​(y′,z′)​.

Here y∘w∈αky\circ w\in\alpha^ky∘w∈αk is the restriction of yyy to the wires w(0),…,w(k−1)w(0),\dots,w(k-1)w(0),…,w(k−1).

This is what the definition reduces to when one unfolds it. The definition attaches a trivial one-point environment to XXX, applies the general wire-lifting construction (with Ψ\PsiΨ adapted to that trivial environment), and removes the environment again.

No property of Ψ\PsiΨ, such as linearity or complete positivity, is assumed.

QAlgorithms.pairLift

Inputs.

  • σ,k∈N\sigma,k\in\mathbb Nσ,k∈N, and NNN is implicit.
  • w:{0,…,k−1}↪{0,…,N−1}w:\{0,\dots,k-1\}\hookrightarrow\{0,\dots,N-1\}w:{0,…,k−1}↪{0,…,N−1} is injective.
  • Ψ\PsiΨ is an arbitrary map on complex matrices indexed by ({0,1}×{0,…,σ−1})k(\{0,1\}\times\{0,\dots,\sigma-1\})^{k}({0,1}×{0,…,σ−1})k, i.e. kkk (qubit, environment-symbol) pairs.

The regrouping. Let eee be the regrouping bijection

e:({0,1}×{0..σ−1})N→{0,1}N×{0..σ−1}N,f↦((fi)1)i, ((fi)2)i).e:(\{0,1\}\times\{0..\sigma-1\})^N\to\{0,1\}^N\times\{0..\sigma-1\}^N,\qquad f\mapsto\big((f_i)_1)_i,\ ((f_i)_2)_i\big).e:({0,1}×{0..σ−1})N→{0,1}N×{0..σ−1}N,f↦((fi​)1​)i​, ((fi​)2​)i​).

Its inverse zips a qubit string bbb and an environment string ccc into (bi,ci)i(b_i,c_i)_i(bi​,ci​)i​.

Output. The map on matrices XXX indexed by {0,1}N×{0,…,σ−1}N\{0,1\}^N\times\{0,\dots,\sigma-1\}^N{0,1}N×{0,…,σ−1}N (the NNN qubits together with one environment symbol per wire) given by

pairLiftwΨ(X)(b,c),(b′,c′)  =  liftRegwΨ(X∘e) e−1(b,c), e−1(b′,c′).\mathrm{pairLift}_w\Psi(X)_{(b,c),(b',c')} \;=\; \mathrm{liftReg}_w\Psi\big(X\circ e\big)_{\,e^{-1}(b,c),\ e^{-1}(b',c')}.pairLiftw​Ψ(X)(b,c),(b′,c′)​=liftRegw​Ψ(X∘e)e−1(b,c), e−1(b′,c′)​.
  • (X∘e)f,f′=Xe(f),e(f′)(X\circ e)_{f,f'} = X_{e(f),e(f')}(X∘e)f,f′​=Xe(f),e(f′)​.
  • liftRegwΨ(Y)y,y′=Ψ(Z)y∘w, y′∘w\mathrm{liftReg}_w\Psi(Y)_{y,y'} = \Psi(Z)_{y\circ w,\,y'\circ w}liftRegw​Ψ(Y)y,y′​=Ψ(Z)y∘w,y′∘w​, with Zz,z′=Yoww(y,z), oww(y′,z′)Z_{z,z'} = Y_{\mathrm{ow}_w(y,z),\,\mathrm{ow}_w(y',z')}Zz,z′​=Yoww​(y,z),oww​(y′,z′)​.
  • oww(y,z)\mathrm{ow}_w(y,z)oww​(y,z) replaces the entries of yyy at positions w(i)w(i)w(i) by ziz_izi​.

So Ψ\PsiΨ acts jointly on qubits w(0),…,w(k−1)w(0),\dots,w(k-1)w(0),…,w(k−1) and on the environment symbols attached to those same wires. No property of Ψ\PsiΨ is assumed.

QAlgorithms.IsTildeErrors

Setting. This is a predicate on four pieces of data:

  • CCC, a circuit of "locations" on NNN wires;
  • a finite set BBB of location labels;
  • a natural number σ\sigmaσ;
  • an error assignment err\mathrm{err}err.

The circuit CCC is a list of time steps. Each step is a list of locations. A location has:

  • a kind: preparation, Hadamard, TTT, CNOT, measurement, or wait;
  • an ordered tuple of distinct wires of length kkk, where k=2k = 2k=2 for CNOT and k=1k = 1k=1 otherwise;
  • a classical condition.

A label ℓ=(s,j)∈N×N\ell = (s, j) \in \mathbb N \times \mathbb Nℓ=(s,j)∈N×N refers to the jjj-th location (0-based) of step sss. Write C(ℓ)C(\ell)C(ℓ) for that location; it is "absent" if sss or jjj is out of range.

The error assignment err\mathrm{err}err assigns to each label either "no error" or a linear map on matrices (a superoperator) over the index set {0,1}N×{0,…,σ−1}N\{0,1\}^N \times \{0,\dots,\sigma-1\}^N{0,1}N×{0,…,σ−1}N. So every wire carries one qubit plus one σ\sigmaσ-level environment symbol.

A superoperator Φ\PhiΦ is a channel if there exist a finite mmm and matrices E1,…,EmE_1,\dots,E_mE1​,…,Em​ with

∑jEj†Ej=IandΦ(ρ)=∑jEjρEj†  for all ρ.\sum_j E_j^\dagger E_j = I \quad\text{and}\quad \Phi(\rho) = \sum_j E_j \rho E_j^\dagger \ \text{ for all } \rho.j∑​Ej†​Ej​=IandΦ(ρ)=j∑​Ej​ρEj†​  for all ρ.

The predicate. IsTildeErrors(C,B,σ,err)\mathrm{IsTildeErrors}(C, B, \sigma, \mathrm{err})IsTildeErrors(C,B,σ,err) holds iff for every label ℓ\ellℓ both of the following hold:

  1. If C(ℓ)C(\ell)C(ℓ) is absent, then err(ℓ)\mathrm{err}(\ell)err(ℓ) is "no error".
  2. If C(ℓ)=lC(\ell) = lC(ℓ)=l is a location with kkk wires w0,…,wk−1w_0,\dots,w_{k-1}w0​,…,wk−1​, then there is a channel Ψ\PsiΨ on the paired index set ({0,1}×{0,…,σ−1})k(\{0,1\} \times \{0,\dots,\sigma-1\})^k({0,1}×{0,…,σ−1})k with two properties:
    • (a) If ℓ∉B\ell \notin Bℓ∈/B, then there is a channel VVV on {0,…,σ−1}k\{0,\dots,\sigma-1\}^k{0,…,σ−1}k such that Ψ=id⊗V\Psi = \mathrm{id} \otimes VΨ=id⊗V. Here the space is regrouped as {0,1}k×{0,…,σ−1}k\{0,1\}^k \times \{0,\dots,\sigma-1\}^k{0,1}k×{0,…,σ−1}k, and VVV acts on the environment symbols only. Formally this is the paired map with unitary U=IU = IU=I, i.e. conjugation by I⊗II \otimes II⊗I after applying id⊗V\mathrm{id}\otimes Vid⊗V.
    • (b) err(ℓ)\mathrm{err}(\ell)err(ℓ) is the lift of Ψ\PsiΨ to all NNN wires along www.

The lift in (b). The space is regrouped as ({0,1}×{0,…,σ−1})N(\{0,1\}\times\{0,\dots,\sigma-1\})^N({0,1}×{0,…,σ−1})N. The (p,q)(p,q)(p,q) entry of the lifted map applied to XXX is obtained in three steps:

  • take the kkk-site submatrix of XXX in which the sites outside {w0,…,wk−1}\{w_0,\dots,w_{k-1}\}{w0​,…,wk−1​} are frozen to ppp (rows) and qqq (columns), while the sites wiw_iwi​ vary;
  • apply Ψ\PsiΨ to this submatrix;
  • read off the entry at the restrictions p∘wp\circ wp∘w and q∘wq \circ wq∘w.

In other words, Ψ\PsiΨ acts on the qubits and environment symbols of the location's own wires, tensored with the identity on all other wires.

Edge cases.

  • For labels in BBB, Ψ\PsiΨ is an arbitrary channel on those kkk paired sites.
  • No condition is placed on labels in BBB that are absent from CCC, beyond clause 1.
  • If σ=0\sigma = 0σ=0 and N≥1N \ge 1N≥1, all the index sets involved are empty and the conditions on Ψ\PsiΨ and VVV hold trivially.

QAlgorithms.LocalStochasticModel

A local stochastic model on NNN qubits is a bundle of four pieces of data, with no constraints imposed:

  • a natural number eee, the environment dimension;
  • a complex e×ee\times ee×e matrix σ0\sigma_0σ0​, the initial environment state;
  • a real-valued function prob\mathrm{prob}prob on finite sets S⊆N×NS \subseteq \mathbb N\times\mathbb NS⊆N×N of location labels;
  • for each finite set SSS and each label ℓ\ellℓ, a superoperator errS(ℓ)\mathrm{err}_S(\ell)errS​(ℓ) on matrices indexed by {0,1}N×{0,…,e−1}\{0,1\}^N \times \{0,\dots,e-1\}{0,1}N×{0,…,e−1}.

Nothing requires the following properties:

  • that σ0\sigma_0σ0​ is a density matrix;
  • that prob\mathrm{prob}prob is nonnegative or sums to 111;
  • that the errS(ℓ)\mathrm{err}_S(\ell)errS​(ℓ) are channels or local.

The case e=0e = 0e=0 is allowed.

QAlgorithms.noisyOutDist

Inputs.

  • A location circuit CCC on NNN wires.
  • A local stochastic model M=(e,σ0,prob,err)M = (e, \sigma_0, \mathrm{prob}, \mathrm{err})M=(e,σ0​,prob,err).

Notation.

  • locs(C)\mathrm{locs}(C)locs(C) is the finite set of valid labels (s,j)(s,j)(s,j), with sss below the number of steps and jjj below the length of step sss.
  • mmm is the number of measurement locations of CCC.

The definition. noisyOutDist(C,M)\mathrm{noisyOutDist}(C,M)noisyOutDist(C,M) is the real function on bit strings r∈{0,1}mr \in \{0,1\}^mr∈{0,1}m given by

noisyOutDist(C,M)(r)=∑S⊆locs(C)prob(S)⋅outC(errS,σ0)(r).\mathrm{noisyOutDist}(C,M)(r) = \sum_{S \subseteq \mathrm{locs}(C)} \mathrm{prob}(S)\cdot \mathrm{out}_C(\mathrm{err}^S, \sigma_0)(r).noisyOutDist(C,M)(r)=S⊆locs(C)∑​prob(S)⋅outC​(errS,σ0​)(r).

Here errS(ℓ)=errS(ℓ)\mathrm{err}^S(\ell) = \mathrm{err}_S(\ell)errS(ℓ)=errS​(ℓ) if ℓ∈S\ell \in Sℓ∈S, and "no error" otherwise.

The output function. outC(err′,σ0)(r)\mathrm{out}_C(\mathrm{err}',\sigma_0)(r)outC​(err′,σ0​)(r) is the real part of the trace of the matrix that the noisy run of CCC produces at the outcome record rrr.

The run starts from the classical–quantum state that assigns:

  • ∣0N⟩⟨0N∣⊗σ0|0^N\rangle\langle 0^N| \otimes \sigma_0∣0N⟩⟨0N∣⊗σ0​ to the empty record;
  • the zero matrix to every other record.

It then processes the steps in order, and the locations within each step in order. At location (s,j)(s,j)(s,j) acting on first wire qqq, with error map E\mathcal EE (the identity if there is no error), the update is as follows.

  • Preparation: replace ρ(r)\rho(r)ρ(r) by E(∑bKbρ(r)Kb†)\mathcal E\big(\sum_b K_b \rho(r) K_b^\dagger\big)E(∑b​Kb​ρ(r)Kb†​), where Kb=∣0⟩⟨b∣K_b = |0\rangle\langle b|Kb​=∣0⟩⟨b∣ on qubit qqq.
  • Measurement:
    • if rrr is non-empty with last bit bbb, the new ρ(r)\rho(r)ρ(r) is Πb E(ρ(r′)) Πb\Pi_b\, \mathcal E(\rho(r'))\, \Pi_bΠb​E(ρ(r′))Πb​, where r′r'r′ is rrr with its last bit dropped and Πb=∣b⟩⟨b∣\Pi_b = |b\rangle\langle b|Πb​=∣b⟩⟨b∣ on qqq;
    • the new ρ\rhoρ of the empty record is 000.
  • Wait: ρ(r)\rho(r)ρ(r) becomes E(ρ(r))\mathcal E(\rho(r))E(ρ(r)).
  • Gate (Hadamard, TTT, CNOT): ρ(r)\rho(r)ρ(r) becomes E(Uρ(r)U†)\mathcal E(U\rho(r)U^\dagger)E(Uρ(r)U†) if the location's condition holds, and E(ρ(r))\mathcal E(\rho(r))E(ρ(r)) otherwise. Here UUU is the ideal gate on the location's wires. The condition reads the outcome bits recorded in rrr for measurement locations in strictly earlier steps; all other outcomes are "unknown".

No normalisation is applied, and none of the properties of MMM listed above are assumed.

QAlgorithms.IsLocalStochastic

Inputs.

  • A location circuit CCC on NNN wires.
  • A local stochastic model M=(e,σ0,prob,err)M = (e, \sigma_0, \mathrm{prob}, \mathrm{err})M=(e,σ0​,prob,err).
  • A real function bound\mathrm{bound}bound on labels.

The predicate. IsLocalStochastic(C,M,bound)\mathrm{IsLocalStochastic}(C, M, \mathrm{bound})IsLocalStochastic(C,M,bound) is the conjunction of six conditions:

  1. σ0\sigma_0σ0​ is a density matrix: positive semidefinite with trace 111.
  2. prob(S)≥0\mathrm{prob}(S) \ge 0prob(S)≥0 for every finite set SSS of labels.
  3. prob(S)=0\mathrm{prob}(S) = 0prob(S)=0 whenever S⊈locs(C)S \not\subseteq \mathrm{locs}(C)S⊆locs(C).
  4. ∑S⊆locs(C)prob(S)=1\displaystyle\sum_{S\subseteq \mathrm{locs}(C)} \mathrm{prob}(S) = 1S⊆locs(C)∑​prob(S)=1.
  5. Locality. Take any SSS, any ℓ∈S\ell \in Sℓ∈S, and suppose C(ℓ)C(\ell)C(ℓ) is a location with wires w0,…,wk−1w_0,\dots,w_{k-1}w0​,…,wk−1​. Then there is a channel Ψ\PsiΨ on {0,1}k×{0,…,e−1}\{0,1\}^k \times \{0,\dots,e-1\}{0,1}k×{0,…,e−1} such that errS(ℓ)\mathrm{err}_S(\ell)errS​(ℓ) is the lift of Ψ\PsiΨ along www.
    • The (p,q)(p,q)(p,q) entry of the lift applied to XXX is obtained by applying Ψ\PsiΨ to the submatrix of XXX in which the qubits outside the wiw_iwi​ are frozen to those of ppp (rows) and qqq (columns).
    • In that submatrix, the location's kkk qubits and the entire shared environment vary.
    • The entry is then read at (p∣w,penv)(p|_w, p_{\mathrm{env}})(p∣w​,penv​) and (q∣w,qenv)(q|_w, q_{\mathrm{env}})(q∣w​,qenv​).
    • No condition is imposed for labels in SSS that are not locations of CCC.
  6. Tail bound. For every R⊆locs(C)R \subseteq \mathrm{locs}(C)R⊆locs(C),
∑S⊆locs(C)R⊆Sprob(S)  ≤  ∏i∈Rbound(i).\sum_{\substack{S \subseteq \mathrm{locs}(C)\\ R \subseteq S}} \mathrm{prob}(S) \;\le\; \prod_{i\in R} \mathrm{bound}(i).S⊆locs(C)R⊆S​∑​prob(S)≤i∈R∏​bound(i).

Remarks.

  • For R=∅R=\varnothingR=∅, condition 6 reads 1≤11 \le 11≤1.
  • bound\mathrm{bound}bound is unconstrained. If bound(i)<0\mathrm{bound}(i) < 0bound(i)<0 for some location iii of CCC, then condition 6 with R={i}R=\{i\}R={i} cannot hold, because by condition 2 its left side is nonnegative. In that case the predicate is unsatisfiable.

QAlgorithms.LocCircuit.castN

Given a proof that N=N′N = N'N=N′ and a location circuit CCC on NNN wires, castN\mathrm{castN}castN returns the same circuit regarded as a circuit on N′N'N′ wires. It has the same steps, locations, kinds, wires and conditions, transported along the equality.

QAlgorithms.concatCircuit

Inputs.

  • A fault-tolerance protocol PPP with code-block size nnn and ancilla count AAA, so each block has n+An+An+A wires.
  • A level L∈NL\in\mathbb NL∈N.
  • A location circuit CCC on NNN wires.

The recursion. concatCircuit(P,L,C)\mathrm{concatCircuit}(P,L,C)concatCircuit(P,L,C) is a location circuit on N(n+A)LN(n+A)^LN(n+A)L wires, defined by:

  • Level 0: CCC itself, recast to N(n+A)0=NN(n+A)^0 = NN(n+A)0=N wires.
  • Level L+1L+1L+1: ftP\mathrm{ft}_PftP​ applied to the level-LLL circuit CLC_LCL​, recast from N(n+A)L⋅(n+A)N(n+A)^L\cdot(n+A)N(n+A)L⋅(n+A) to N(n+A)L+1N(n+A)^{L+1}N(n+A)L+1 wires.

The compilation ftP\mathrm{ft}_PftP​. For a circuit C′C'C′ on N′N'N′ wires:

  • Logical wire uuu becomes the block of physical wires u(n+A)+iu(n+A)+iu(n+A)+i, for 0≤i<n+A0 \le i < n+A0≤i<n+A.
  • Let DDD be the maximum step count of PPP's six gadgets, and let EEE be the step count of PPP's error-correction (EC) circuit.
  • Each step sss of C′C'C′ is replaced by DDD gadget steps followed by EEE EC steps.

Gadget step ddd contains, for each location lll of step sss (in order), the ddd-th step of the gadget for lll's kind.

  • If that gadget has fewer than d+1d+1d+1 steps, it contributes nothing.
  • Its wires are mapped blockwise through lll's wires.
  • Each gadget location's condition is evaluated on outcomes relabelled to global labels. The relabelling is (s⋅(D+E)+s′, offset+j′)(s\cdot(D+E)+s',\ \text{offset}+j')(s⋅(D+E)+s′, offset+j′), where the offset is the total length of step s′s's′ of the gadgets of the earlier locations in step sss.
  • If lll is a gate (Hadamard, TTT, CNOT), the condition is additionally conjoined with lll's own condition. That condition is evaluated on decoded outcomes: a measurement location of C′C'C′ gets the value measDecode\mathrm{measDecode}measDecode of its measurement gadget's outcome list if all of those outcomes are known, and is unknown otherwise.

EC step eee contains, for each block qqq touched by some non-measurement location of step sss (in increasing order, indexed by its position idx\mathrm{idx}idx), the eee-th step of the EC circuit on block qqq. Conditions are relabelled to (s(D+E)+D+e′, idx⋅(length of EC step e′)+j′)(s(D+E)+D+e',\ \mathrm{idx}\cdot(\text{length of EC step } e')+j')(s(D+E)+D+e′, idx⋅(length of EC step e′)+j′).

QAlgorithms.concatDecode

Inputs. A protocol PPP, a level LLL, and a location circuit CCC.

The recursion. concatDecode(P,L,C)\mathrm{concatDecode}(P,L,C)concatDecode(P,L,C) is a map from bit lists to bit lists, defined by:

  • Level 0: the identity.
  • Level L+1L+1L+1: r↦concatDecode(P,L,C)(ftDecodeP(CL,r))r \mapsto \mathrm{concatDecode}(P,L,C)\big(\mathrm{ftDecode}_P(C_L, r)\big)r↦concatDecode(P,L,C)(ftDecodeP​(CL​,r)), where CL=concatCircuit(P,L,C)C_L = \mathrm{concatCircuit}(P,L,C)CL​=concatCircuit(P,L,C).

So the outcomes of the top-level circuit are first decoded into level-LLL outcomes, then recursively down to level 000.

The decoding step. ftDecodeP(C′,r)\mathrm{ftDecode}_P(C', r)ftDecodeP​(C′,r) lists one bit for each measurement location ccc of C′C'C′, in C′C'C′'s order of measurement locations. That bit is measDecode\mathrm{measDecode}measDecode applied to the list of outcomes, read from rrr, of the measurement-gadget's measurements at their global labels in ftP(C′)\mathrm{ft}_P(C')ftP​(C′).

Here rrr is matched positionally against the ordered list of measurement locations of ftP(C′)\mathrm{ft}_P(C')ftP​(C′). Any outcome not found, for example because rrr is too short, is read as 000 (false). Bits of rrr beyond that list are ignored.

Definition code
import Definitions.Def_QAlgorithms_Defs_FaultTolerance
/-!
# The fault-tolerant circuit C′, its ExRecs, good and bad locations, the circuit C̃, local
stochastic error models, concatenation (shared definition layer)

D. Gottesman, *An introduction to quantum error correction and fault-tolerant quantum
computation* (arXiv:0904.2557), cited "Gottesman intro p.N" (PDF pages), §§5.1–5.2, Defs. 6, 7,
10, 12, Thms 8–10.

Layout of `C′ = ftCircuit P C` (Def. 6). Qubit `q` of `C` owns the block of physical qubits
`finProdFinEquiv (q, j)`, `j < n + A`. Time step `s` of `C` becomes `period = D + D_ec` time steps
of `C′`: a gadget phase of `D` steps (`D` the gadget depth; every location of step `s` replaced by
its gadget on the blocks of its qubits, in parallel) followed by an EC phase of `D_ec` steps (the
EC gadget on the block of every qubit whose step-`s` location is not a measurement, in parallel:
the EC between consecutive locations). Within a `C′` step the gadget instances appear in the order
of the `C` locations (resp. of the qubits). A classically controlled gate location of `C` (these
arise in `C^{(k)}`, `k ≥ 1`) becomes its gadget with every location's condition conjoined with the
location's condition on the decoded logical outcomes.
-/

namespace QAlgorithms

open scoped Matrix

namespace FTProtocol

variable (P : FTProtocol)

/-- Gottesman intro p.33 (Def. 6: "C can be divided up into time steps"): all location gadgets have
the same number of time steps, so that the gadgets of one time step of `C` run in parallel without
padding. -/
def HasUniformDepth : Prop :=
  ∀ κ : LocKind, (P.gadget κ).steps.length = (P.gadget .prep).steps.length

/-- The number of time steps of the gadget phase of `C′`: the largest gadget depth. -/
def gadgetDepth : ℕ :=
  [LocKind.prep, .had, .tgate, .cnot, .meas, .wait].foldr (fun κ acc => max (P.gadget κ).steps.length acc) 0

/-- The number of time steps of `C′` per time step of `C`. -/
def period : ℕ :=
  P.gadgetDepth + P.ec.steps.length

end FTProtocol

/-- The gadget instances of `C′`: the gadget of the `C` location `ℓ`, or the EC gadget after time
step `s` of `C` on the block of qubit `q`. -/
inductive FTInstance (N : ℕ) where
  | gadget (ℓ : ℕ × ℕ)
  | ec (s : ℕ) (q : Fin N)
  deriving DecidableEq

/-- The physical qubits of the blocks of the qubits `w` of a `C` location: block `i`, position `p`
of the gadget is block `w i`, position `p` of `C′`. -/
def blockEmb (n A : ℕ) {k N : ℕ} (w : Fin k ↪ Fin N) : Fin (k * (n + A)) ↪ Fin (N * (n + A)) :=
  ⟨fun x => finProdFinEquiv (w (finProdFinEquiv.symm x).1, (finProdFinEquiv.symm x).2), by
    intro x y h
    have h' := Prod.ext_iff.1 (finProdFinEquiv.injective h)
    exact finProdFinEquiv.symm.injective (Prod.ext (w.injective h'.1) h'.2)⟩

/-- The physical qubits of the block of qubit `q`. -/
def ecEmb (n A : ℕ) {N : ℕ} (q : Fin N) : Fin (n + A) ↪ Fin (N * (n + A)) :=
  ⟨fun p => finProdFinEquiv (q, p), fun _ _ h => (Prod.ext_iff.1 (finProdFinEquiv.injective h)).2⟩

variable {N : ℕ}

/-- The position in its `C′` step at which the gadget of the `C` location `(s, j)` starts, at local
step `d` of the gadget. -/
def gadgetOffset (P : FTProtocol) (C : LocCircuit N) (s j d : ℕ) : ℕ :=
  (((C.steps.getD s []).take j).map fun l => ((P.gadget l.kind).steps.getD d []).length).sum

/-- The id in `C′` of the location with local id `lid` of the gadget of the `C` location
`(s, j)`. -/
def gadgetGlobalId (P : FTProtocol) (C : LocCircuit N) (s j : ℕ) (lid : ℕ × ℕ) : ℕ × ℕ :=
  (s * P.period + lid.1, gadgetOffset P C s j lid.1 + lid.2)

/-- The qubits of `C` receiving an EC gadget after time step `s`: those whose step-`s` location is
not a measurement (the EC between consecutive locations, Def. 6). -/
def ecBlocks (C : LocCircuit N) (s : ℕ) : List (Fin N) :=
  (List.finRange N).filter fun q =>
    (C.steps.getD s []).any fun l => decide (l.kind ≠ .meas) && decide (q ∈ l.qubits)

/-- The id in `C′` of the location with local id `lid` of the EC gadget after step `s` on the
`idx`-th block of `ecBlocks C s`. -/
def ecGlobalId (P : FTProtocol) (s idx : ℕ) (lid : ℕ × ℕ) : ℕ × ℕ :=
  (s * P.period + P.gadgetDepth + lid.1, idx * (P.ec.steps.getD lid.1 []).length + lid.2)

/-- The decoded logical outcomes of the measurement locations of `C`, read from the outcomes `O`
of `C′` (`none` until every outcome of the measurement gadget is available). -/
def decodedOutcomes (P : FTProtocol) (C : LocCircuit N) (O : ℕ × ℕ → Option Bool) :
    ℕ × ℕ → Option Bool :=
  fun cid => match C.locAt cid with
    | some l =>
        if l.kind = .meas then
          if ((P.gadget .meas).measIds.map fun lid => O (gadgetGlobalId P C cid.1 cid.2 lid)).all
              Option.isSome then
            some (P.measDecode ((P.gadget .meas).measIds.map fun lid =>
              (O (gadgetGlobalId P C cid.1 cid.2 lid)).getD false))
          else none
        else none
    | none => none

/-- Time step `d` of the gadget phase for time step `s` of `C`, its locations tagged with their
gadget instance. -/
def gadgetPhaseStep (P : FTProtocol) (C : LocCircuit N) (s d : ℕ) :
    List (Loc (N * (P.n + P.A)) × FTInstance N) :=
  (C.steps.getD s []).zipIdx.flatMap fun p =>
    ((P.gadget p.1.kind).steps.getD d []).map fun g =>
      (⟨g.kind, g.wires.trans (blockEmb P.n P.A p.1.wires),
        fun O => g.cond (fun lid => O (gadgetGlobalId P C s p.2 lid)) &&
          (if p.1.kind.IsGate then p.1.cond (decodedOutcomes P C O) else true)⟩,
       FTInstance.gadget (s, p.2))

/-- Time step `e` of the EC phase after time step `s` of `C`, its locations tagged with their
gadget instance. -/
def ecPhaseStep (P : FTProtocol) (C : LocCircuit N) (s e : ℕ) :
    List (Loc (N * (P.n + P.A)) × FTInstance N) :=
  (ecBlocks C s).zipIdx.flatMap fun p =>
    (P.ec.steps.getD e []).map fun g =>
      (⟨g.kind, g.wires.trans (ecEmb P.n P.A p.1),
        fun O => g.cond (fun lid => O (ecGlobalId P s p.2 lid))⟩,
       FTInstance.ec s p.1)

/-- The time steps of `C′`, every location tagged with its gadget instance. -/
def ftTaggedSteps (P : FTProtocol) (C : LocCircuit N) :
    List (List (Loc (N * (P.n + P.A)) × FTInstance N)) :=
  (List.range C.steps.length).flatMap fun s =>
    (List.range P.gadgetDepth).map (gadgetPhaseStep P C s) ++
      (List.range P.ec.steps.length).map (ecPhaseStep P C s)

/-- Gottesman intro p.33 (Def. 6: "a quantum circuit `C′` constructed by replacing each location
`C_i` with a fault-tolerant gadget for `C_i`, and adding fault-tolerant error correction gadgets
between any pair of consecutive locations") and p.34 (Fig. 7): the fault-tolerant circuit for
`C`. -/
def ftCircuit (P : FTProtocol) (C : LocCircuit N) : LocCircuit (N * (P.n + P.A)) :=
  ⟨(ftTaggedSteps P C).map fun st => st.map Prod.fst⟩

/-- The gadget instance the location `id` of `C′` belongs to. -/
def ftOwner (P : FTProtocol) (C : LocCircuit N) (id : ℕ × ℕ) : Option (FTInstance N) :=
  ((ftTaggedSteps P C).getD id.1 [])[id.2]?.map Prod.snd

/-- Gottesman intro p.19 ("the final fault-tolerant measurement gadgets will produce classical
information which should … give the same outcome as the original circuit") and p.17 (classical
computation is perfect): the measurement record of `C` decoded from that of `C′`: for each
measurement location of `C` (in `C`'s record order), `measDecode` of the outcomes of its
measurement gadget; EC and other outcomes are discarded. -/
def ftDecode (P : FTProtocol) (C : LocCircuit N) (r : List Bool) : List Bool :=
  C.measIds.map fun cid => P.measDecode ((P.gadget .meas).measIds.map fun lid =>
    ((ftCircuit P C).outcomeAt r (gadgetGlobalId P C cid.1 cid.2 lid)).getD false)

/-- Gottesman intro p.34 (Def. 7) and p.38 (Def. 10): the locations of `C′` in the ExRec of the `C`
location `ℓ`: its gadget, the leading ECs (the EC after the previous step on each of its qubits,
unless `ℓ` is a preparation) and the trailing ECs (the EC after its step on each of its qubits not
in `trunc`, unless `ℓ` is a measurement). `trunc = ∅` is the full ExRec. -/
noncomputable def exRecLocs (P : FTProtocol) (C : LocCircuit N) (ℓ : ℕ × ℕ) (trunc : Finset (Fin N)) :
    Finset (ℕ × ℕ) :=
  open Classical in
  match C.locAt ℓ with
  | none => ∅
  | some l => (ftCircuit P C).locs.filter fun id =>
      match ftOwner P C id with
      | some (.gadget ℓ') => ℓ' = ℓ
      | some (.ec s q) => q ∈ l.qubits ∧
          ((l.kind ≠ .prep ∧ s + 1 = ℓ.1) ∨ (l.kind ≠ .meas ∧ s = ℓ.1 ∧ q ∉ trunc))
      | none => False

/-- Gottesman intro p.38 (Def. 10): the qubits of the `C` location `ℓ` whose trailing EC is removed
because it is the leading EC of a bad location (of the next time step) in `B`. -/
noncomputable def truncOf (C : LocCircuit N) (B : Finset (ℕ × ℕ)) (ℓ : ℕ × ℕ) : Finset (Fin N) :=
  open Classical in
  match C.locAt ℓ with
  | none => ∅
  | some l => l.qubits.filter fun q =>
      ∃ ℓ' ∈ B, ℓ'.1 = ℓ.1 + 1 ∧ ∃ l', C.locAt ℓ' = some l' ∧ q ∈ l'.qubits

/-- The bad locations among the last `k` time steps of `C`, for the set `S` of faulty locations of
`C′` (Def. 10's recursion, from the end of the circuit backwards). -/
noncomputable def badAbove (P : FTProtocol) (C : LocCircuit N) (S : Finset (ℕ × ℕ)) :
    ℕ → Finset (ℕ × ℕ)
  | 0 => ∅
  | k + 1 =>
      open Classical in
      badAbove P C S k ∪
        ((Finset.range (C.steps.getD (C.steps.length - (k + 1)) []).length).image
          fun j => (C.steps.length - (k + 1), j)).filter fun ℓ =>
            P.t < (S ∩ exRecLocs P C ℓ (truncOf C (badAbove P C S k) ℓ)).card

/-- Gottesman intro p.38 (Def. 10): the set of locations of `C` whose ExRec (full or truncated) is
bad, i.e. contains more than `t` of the faulty locations `S` of `C′`; an ExRec is truncated on the
qubits whose trailing EC is a leading EC of a bad ExRec, determined from the end of the circuit
backwards. -/
noncomputable def badAssignment (P : FTProtocol) (C : LocCircuit N) (S : Finset (ℕ × ℕ)) :
    Finset (ℕ × ℕ) :=
  badAbove P C S C.steps.length

/-- The superoperator `Ψ` on `k` registers (of type `α`) lifted to `N` registers along `w`, the
identity on the others. -/
noncomputable def liftReg {α : Type*} {k : ℕ} (w : Fin k ↪ Fin N)
    (Ψ : Superop (Fin k → α) (Fin k → α)) : Superop (Fin N → α) (Fin N → α) :=
  fun X =>
    (liftSuperop (E := Unit) w
      (fun Y => (Ψ (Y.submatrix (fun x => (x, ())) (fun x => (x, ())))).submatrix Prod.fst Prod.fst)
      (X.submatrix Prod.fst Prod.fst)).submatrix (fun x => (x, ())) (fun x => (x, ()))

/-- A superoperator `Ψ` on the (data qubit, syndrome register) pairs of `k` qubits, lifted to the
qubits of `C` with one syndrome register per qubit as environment. -/
noncomputable def pairLift {σ k : ℕ} (w : Fin k ↪ Fin N)
    (Ψ : Superop (Fin k → Bool × Fin σ) (Fin k → Bool × Fin σ)) :
    Superop (Qubits N × (Fin N → Fin σ)) (Qubits N × (Fin N → Fin σ)) :=
  fun X =>
    (liftReg w Ψ (X.submatrix (Equiv.arrowProdEquivProdArrow (Fin N) (fun _ => Bool) (fun _ => Fin σ))
      (Equiv.arrowProdEquivProdArrow (Fin N) (fun _ => Bool) (fun _ => Fin σ)))).submatrix
      (Equiv.arrowProdEquivProdArrow (Fin N) (fun _ => Bool) (fun _ => Fin σ)).symm
      (Equiv.arrowProdEquivProdArrow (Fin N) (fun _ => Bool) (fun _ => Fin σ)).symm

/-- Gottesman intro pp.37–39 (eqs. (82), (83), Thm 8): `err` is an error assignment of a circuit
`C̃` for `C` with bad set `B` and syndrome registers `Fin σ` (one per qubit of `C`, the environment;
"`C̃` uses ancilla registers to control the types of `U′` errors"): no error off the locations; at
every location `ℓ`, after its ideal operation, a quantum operation `Ψ` on the qubits of `ℓ` and
their syndrome registers; if `ℓ ∉ B` ("include `C_i` unchanged"), `Ψ` is the identity on the qubits
tensored with a quantum operation `V` on the syndrome registers (eq. (83)); if `ℓ ∈ B`, `Ψ` is
arbitrary (the erroneous `U′` of (82), controlled by the syndrome). -/
def IsTildeErrors (C : LocCircuit N) (B : Finset (ℕ × ℕ)) (σ : ℕ)
    (err : ErrAssign N (Fin N → Fin σ)) : Prop :=
  ∀ ℓ : ℕ × ℕ, (C.locAt ℓ = none → err ℓ = none) ∧
    ∀ l : Loc N, C.locAt ℓ = some l →
      ∃ Ψ : Superop (Fin l.kind.arity → Bool × Fin σ) (Fin l.kind.arity → Bool × Fin σ),
        IsChannel Ψ ∧
        (ℓ ∉ B → ∃ V : Superop (Fin l.kind.arity → Fin σ) (Fin l.kind.arity → Fin σ),
          IsChannel V ∧ Ψ = pairedSuperop 1 V) ∧
        err ℓ = some (pairLift l.wires Ψ)

/-- Gottesman intro p.40 (Def. 12): an error model on a circuit with `N` qubits: an environment
register `Fin e` in the state `σ0`, the probability `prob S` of faults at precisely the set `S` of
locations, and the errors `err S ℓ` chosen (by an adversary) when the faulty set is `S`; they act
at the locations of `S` in chronological order and may be entangled through the environment. -/
structure LocalStochasticModel (N : ℕ) where
  e : ℕ
  σ0 : Matrix (Fin e) (Fin e) ℂ
  prob : Finset (ℕ × ℕ) → ℝ
  err : Finset (ℕ × ℕ) → ℕ × ℕ → Superop (Qubits N × Fin e) (Qubits N × Fin e)

/-- Gottesman intro p.40 (Def. 12): the output distribution of `C` under the error model: the
mixture over the faulty set `S` of the output distributions with the errors at `S`. -/
noncomputable def noisyOutDist (C : LocCircuit N) (M : LocalStochasticModel N) :
    (Fin C.measCount → Bool) → ℝ :=
  fun r => ∑ S ∈ C.locs.powerset,
    M.prob S * C.outDist (fun ℓ => if ℓ ∈ S then some (M.err S ℓ) else none) M.σ0 r

/-- Gottesman intro p.40 (Def. 12): `M` is a local stochastic error model on `C` with error bounds
`bound`: the environment state is a density matrix; `prob` is a probability distribution on the
sets of locations of `C`; each error at a faulty location is any quantum operation on that
location's qubits and the environment; and for every set `R` of locations the probability of
faults on every location of `R` (and possibly elsewhere) is at most `Π_{i ∈ R} p_i`. The side
condition `p_i < 1` of Def. 12 is a separate hypothesis of the statements (Thm 9's induced bounds
(84) may exceed 1). -/
def IsLocalStochastic (C : LocCircuit N) (M : LocalStochasticModel N) (bound : ℕ × ℕ → ℝ) : Prop :=
  IsDensityMatrix M.σ0 ∧ (∀ S, 0 ≤ M.prob S) ∧ (∀ S, ¬ S ⊆ C.locs → M.prob S = 0) ∧
    ∑ S ∈ C.locs.powerset, M.prob S = 1 ∧
    (∀ (S : Finset (ℕ × ℕ)) (ℓ : ℕ × ℕ) (l : Loc N), ℓ ∈ S → C.locAt ℓ = some l →
      IsLocError l.wires (M.err S ℓ)) ∧
    ∀ R ⊆ C.locs, ∑ S ∈ C.locs.powerset.filter (fun S => R ⊆ S), M.prob S ≤ ∏ i ∈ R, bound i

/-- Transport of a circuit along an equality of qubit numbers. -/
def LocCircuit.castN {N N' : ℕ} (h : N = N') (C : LocCircuit N) : LocCircuit N' :=
  h ▸ C

/-- Gottesman intro p.36 (§5.2: "a sequence of circuits `C^{(k)}`, `k = 1, …, L`, where each `C^{(k)}`
is a fault-tolerant simulation of `C^{(k−1)}`, with `C^{(0)} = C`") and p.42 (proof of Thm 10): the
`L`-level concatenated fault-tolerant circuit for `C`, on `N (n + A)^L` qubits. -/
def concatCircuit (P : FTProtocol) : (L : ℕ) → LocCircuit N → LocCircuit (N * (P.n + P.A) ^ L)
  | 0, C => C.castN (by simp)
  | L + 1, C => (ftCircuit P (concatCircuit P L C)).castN (by rw [pow_succ, mul_assoc])

/-- The classical decoding of the record of `C^{(L)}` to the record of `C`, level by level. -/
def concatDecode (P : FTProtocol) : (L : ℕ) → LocCircuit N → List Bool → List Bool
  | 0, _ => id
  | L + 1, C => fun r => concatDecode P L C (ftDecode P (concatCircuit P L C) r)

/-! ### Sanity tests -/

example (P : FTProtocol) : ftCircuit P (⟨[]⟩ : LocCircuit N) = ⟨[]⟩ := by
  simp [ftCircuit, ftTaggedSteps]

example (P : FTProtocol) (S : Finset (ℕ × ℕ)) : badAssignment P (⟨[]⟩ : LocCircuit N) S = ∅ := rfl

example (P : FTProtocol) (C : LocCircuit N) : concatDecode P 0 C = id := rfl

example (P : FTProtocol) (r : List Bool) : ftDecode P (⟨[]⟩ : LocCircuit N) r = [] := rfl

example (n A : ℕ) (q : Fin N) (p : Fin (n + A)) :
    blockEmb n A (Stabilizer.wire1 q) (finProdFinEquiv (0, p)) = finProdFinEquiv (q, p) := by
  show finProdFinEquiv (_, _) = _
  rw [Equiv.symm_apply_apply]
  rfl

example (C : LocCircuit N) (M : LocalStochasticModel N) (h : C.locs = ∅) (r : Fin C.measCount → Bool) :
    noisyOutDist C M r = M.prob ∅ * C.outDist (fun _ => none) M.σ0 r := by
  simp [noisyOutDist, h]

end QAlgorithms
Source
https://github.com/shiy1022/lean-quantum-complexity/blob/main/algorithms/QAlgorithms/Defs/FTCircuit.lean . Exact primary-source page locators are preserved in the declaration docstrings.

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me