Documentation

CsdLean4.Mathlib.QuantumInfo.Reversible.Circuit

Reversible classical circuits — the gate-list DSL #

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

A minimal, derived-cost abstraction for reversible classical circuits. The design choice is deliberate: a circuit is a list of gates, its action is the fold of the per-gate semantics, and (in Cost.lean) its resource cost is a function of the gate list. Resource bounds are therefore theorems about an exhibited circuit, not numbers annotated onto an opaque Equiv — the prerequisite for cost accounting to be genuinely machine-checked. (The reversible-arithmetic application this substrate was built for lives in the Ecdsafail repository, specs/ecdsa/ecdlp-resource-plan.md there.)

Each primitive gate (X flip, CX = CNOT, CCX = Toffoli, swap) is an involution: degenerate forms (control = target) act as the identity, so every gate is its own inverse unconditionally. Hence a circuit's inverse is the reversed gate list (inverse = List.reverse), and denote is a bijection. State is Fin n → Bool; the bridge to the quantum register QReg n (basis states indexed by Fin n → Fin 2) is kept out of this classical layer — it is the embedding Reversible/Lift.lean provides (basis-state permutation semantics).

inductive Reversible.Gate (n : ) :

A reversible classical gate on n wires (state Fin n → Bool).

  • X {n : } (i : Fin n) : Gate n

    Flip wire i.

  • CX {n : } (c t : Fin n) : Gate n

    CNOT: if wire c is set, flip wire t. Acts as the identity if c = t.

  • CCX {n : } (c₁ c₂ t : Fin n) : Gate n

    Toffoli: if wires c₁ and c₂ are both set, flip wire t. Identity if t ∈ {c₁, c₂}.

  • swap {n : } (i j : Fin n) : Gate n

    Exchange wires i and j.

Instances For
    def Reversible.instDecidableEqGate.decEq {n✝ : } (x✝ x✝¹ : Gate n✝) :
    Decidable (x✝ = x✝¹)
    Equations
    Instances For
      @[reducible, inline]
      abbrev Reversible.State (n : ) :

      The Boolean state of n wires.

      Equations
      Instances For
        def Reversible.denoteGate {n : } :
        Gate nState nState n

        Semantics of a single gate as a state transformation. Degenerate forms (control = target) are the identity, which keeps every gate an involution.

        Equations
        Instances For

          Every gate is an involution (its own inverse).

          @[reducible, inline]

          A reversible circuit on n wires: a list of gates applied left to right.

          Equations
          Instances For
            def Reversible.denote {n : } (c : Circuit n) (s : State n) :

            The action of a circuit: fold the per-gate semantics over the list (first gate first).

            Equations
            Instances For
              @[simp]
              theorem Reversible.denote_nil {n : } (s : State n) :
              denote [] s = s
              theorem Reversible.denote_cons {n : } (g : Gate n) (c : Circuit n) (s : State n) :
              denote (g :: c) s = denote c (denoteGate g s)
              theorem Reversible.denote_append {n : } (c₁ c₂ : Circuit n) (s : State n) :
              denote (c₁ ++ c₂) s = denote c₂ (denote c₁ s)

              Composition correctness: running c₁ ++ c₂ is c₁ then c₂.

              theorem Reversible.reversible_comp_correct {n : } (c₁ c₂ : Circuit n) :
              denote (c₁ ++ c₂) = denote c₂ denote c₁

              reversible_comp_correct: the denotation of a concatenation is the composition of denotations (c₁ first, then c₂).

              def Reversible.inverse {n : } (c : Circuit n) :

              The inverse circuit: the reversed gate list (each gate is its own inverse).

              Equations
              Instances For
                theorem Reversible.reversible_inverse_correct {n : } (c : Circuit n) (s : State n) :
                denote (inverse c) (denote c s) = s

                reversible_inverse_correct (left): the inverse circuit undoes the circuit.

                theorem Reversible.reversible_inverse_correct' {n : } (c : Circuit n) (s : State n) :
                denote c (denote (inverse c) s) = s

                reversible_inverse_correct (right): the circuit undoes its inverse.

                A circuit's denotation is a bijection (it has the inverse circuit as a two-sided inverse).

                Wire support and the frame (locality) lemma #

                A gate only touches the wires in gateWires; every other wire is preserved. Lifted to circuits, this is the frame lemma denote_apply_of_forall_not_mem — the reusable primitive for transporting a gadget's correctness from a concrete wire layout to arbitrary Fin n wires (consumed by the ModAdd / ModMul arithmetic proofs).

                def Reversible.gateWires {n : } :
                Gate nFinset (Fin n)

                The set of wires a gate acts on. A wire outside this set is preserved by denoteGate (denoteGate_apply_of_not_mem).

                Equations
                Instances For

                  Interface lemmas (CONVENTIONS §9.1, F1): per-constructor wire sets, stated once. Not @[simp] — existing proofs unfold by name and must keep their behaviour.

                  theorem Reversible.gateWires_X {n : } (i : Fin n) :

                  The wires of an X gate.

                  theorem Reversible.gateWires_CX {n : } (c t : Fin n) :

                  The wires of a CX gate.

                  theorem Reversible.gateWires_CCX {n : } (c₁ c₂ t : Fin n) :
                  gateWires (Gate.CCX c₁ c₂ t) = {c₁, c₂, t}

                  The wires of a CCX gate.

                  theorem Reversible.gateWires_swap {n : } (i j : Fin n) :

                  The wires of a swap gate.

                  theorem Reversible.denoteGate_apply_of_not_mem {n : } {g : Gate n} {s : State n} {i : Fin n} (hi : igateWires g) :
                  denoteGate g s i = s i

                  Frame lemma (single gate). A wire outside gateWires g is left unchanged by denoteGate g.

                  theorem Reversible.denote_apply_of_forall_not_mem {n : } {i : Fin n} (c : Circuit n) :
                  (∀ gc, igateWires g)∀ (s : State n), denote c s i = s i

                  Frame lemma (circuit). A wire untouched by every gate of c is unchanged by denote c. Stated with the per-gate membership hypothesis (the form the gadget proofs consume).