Documentation

CsdLean4.Mathlib.QuantumInfo.Reversible.Depth

Layered circuits and depth (ECDLP Phase 2, Stage S1) #

Category: 1-Mathlib (CSD-free; staged as a Mathlib-upstream candidate).

The flat Circuit = List (Gate n) model (Tranche 1) costs only gate count; it cannot express parallelism, so it has no notion of circuit depth (parallel time). This module adds the depth axis: a LayeredCircuit is a list of layers, each layer a group of gates intended to run in one time step (well-formed when their wires are disjoint), and depth is the number of layers.

Two bridges keep the new axis consistent with the verified flat model:

What this gives, and what it does not: the framework plus two honest depth facts — a disjoint layer has depth 1 ≪ gate count (parallelism captured), and the ripple adder sequentialised has depth = gate count = 4n = O(n) (its carry chain is inherently sequential). The payoff the framework enables — a parallel-prefix / carry-lookahead adder at O(log n) depth, to compare against the ripple O(n) — is the next increment (S1 continuation), not proved here.

@[reducible, inline]

A layered circuit: a list of layers, each layer a Circuit (group of gates) run in one parallel time step. depth is the number of layers.

Equations
Instances For
    def Reversible.denoteLayered {n : } (lc : LayeredCircuit n) (s : State n) :

    Run a layered circuit: apply each layer in order.

    Equations
    Instances For

      Flatten a layered circuit to a flat gate list (layers concatenated in order).

      Equations
      Instances For

        The depth of a layered circuit: the number of layers (parallel time steps).

        Equations
        Instances For

          Denotation bridge. A layered circuit computes the same state map as its flattened gate list, so the flat-model correctness theorems (rippleCirc_correct, mulCircuit_correct, …) transfer to any layering of the same gates.

          theorem Reversible.layeredToffoli_eq {n : } (lc : LayeredCircuit n) :
          (circuitCost (flatten lc)).toffoli = (List.map (fun (layer : Circuit n) => (circuitCost layer).toffoli) lc).sum

          Toffoli bridge. The Toffoli count of a layered circuit is the sum of its layers' counts (equal to the flattened gate list's), so the verified Tranche-1/3 gate counts carry over to the layered model.

          Layer well-formedness (a layer is genuinely parallel iff its gates are wire-disjoint) #

          def Reversible.LayerWF {n : } (layer : Circuit n) :

          A layer is well-formed (a valid single parallel time step) when its gates act on pairwise disjoint wires. Depth is physically meaningful for layered circuits whose every layer is well-formed.

          Equations
          Instances For
            theorem Reversible.layerWF_iff {n : } (layer : Circuit n) :
            LayerWF layer List.Pairwise (fun (g h : Gate n) => Disjoint (gateWires g) (gateWires h)) layer

            Layer well-formedness, as its defining pairwise-disjointness (interface lemma, §9.1).

            A layered circuit is well-formed when every layer is.

            Equations
            Instances For

              Sequentialisation, and the ripple adder's O(n) depth #

              The trivial layering: one gate per layer (fully sequential). Always well-formed (singleton layers), with depth = gate count. The depth a circuit needs if no parallelism is exploited.

              Equations
              Instances For
                @[simp]

                The ripple adder, fully sequentialised, denotes the verified rippleCirc (correctness inherited).

                theorem Reversible.ripplePrefix_length {n m : } (L : RippleLayout m n) (k : ) :

                The first k slices of a ripple adder have 4k gates (four per slice).

                The ripple adder has 4n gates.

                Ripple adder depth is O(n) (= 4n): its carry chain is inherently sequential, so sequentialising gate-by-gate is the natural layering and gives depth equal to the gate count. (Beating this to O(log n) needs a parallel-prefix / carry-lookahead adder — the comparison this framework enables, deferred to the S1 continuation.)

                Parallelism captured: a disjoint layer has depth 1, far below its gate count #

                theorem Reversible.parallelXLayer_depth {n : } (idxs : List (Fin n)) :

                A single layer of X-flips has depth 1, while its gate count is the number of flips — the smallest witness that the depth model is non-trivial (depth can be far below gate count).

                theorem Reversible.parallelXLayer_wf {n : } (idxs : List (Fin n)) (h : idxs.Nodup) :

                That layer is well-formed (a genuine parallel step) when the wires are distinct: the X-flips act on disjoint wires and commute.

                Log-depth computation: a parallel reduction tree (the CLA building block) #

                parallelXLayer shows depth 1 for independent flips, but that is not a computation. A genuine log-depth result is a reduction tree: combine n bits pairwise in a balanced tree, ⌈log₂ n⌉ layers, each layer a set of wire-disjoint gates. Below is the concrete 4-wire instance (depth 2 = log₂ 4), fully verified: every layer is well-formed (parallel), it computes the XOR (parity) of all four inputs into wire 0, and it does so in depth 2 against 3 gates — log depth, not linear. This is the primitive a carry-lookahead adder is built from (the carry prefix is a reduction tree); the general 2^k-wire tree, the full O(log n) carry-lookahead adder, and the secp256k1 (Toffoli, depth, qubits) triple are the further S1/Phase-2 steps.

                A balanced XOR reduction tree on 4 wires: two layers of wire-disjoint CNOTs accumulating the parity of all four bits into wire 0. Depth 2 = log₂ 4, 3 gates.

                Equations
                Instances For

                  The reduction tree has depth 2 (= log₂ 4), below its 3-gate count.

                  It has 3 gates — so depth 2 < 3, logarithmic depth for a real computation (the gap widens to log₂ n vs n−1 at scale).

                  Every layer is well-formed: the CNOTs in each layer act on disjoint wires (a valid parallel step), so the depth-2 count is physically meaningful, not three sequential steps in disguise.

                  theorem Reversible.reduceTree4_correct (s : State 4) :
                  denoteLayered reduceTree4 s 0 = (s 0 ^^ s 1 ^^ s 2 ^^ s 3)

                  Correctness: the tree computes the XOR (parity) of all four input bits into wire 0, in depth 2. (Verified over all 2^4 input states.)