Documentation

CslibFrontier.Computability.Circuit.Quantum.Classical

Classical gate codes on basis states #

The classical gate codes NOT, CNOT and Toffoli of CslibFrontier/Computability/Circuit/Quantum/Code.lean permute the computational basis: the code (false, t, cs) flips bit t when every bit of cs is one (Cslib.Quantum.GateCode.classicalStep). A list of classical codes therefore maps the basis state |x⟩ to the basis state of the composed permutation (Cslib.Quantum.Circuit.eval_ofCodes_ket), and it acts on any state by reading amplitudes back through the inverse permutation (Cslib.Quantum.Circuit.eval_ofCodes_apply). Measuring after a classical circuit is measuring before it and then applying the permutation to the outcome (Cslib.Quantum.Circuit.measureProb_eval_ofCodes).

Bits of a basis string are read by their number (Cslib.Quantum.readBit), as in gate codes.

Main definitions #

Main results #

def Cslib.Quantum.readBit {m : ℕ} (x : Fin m → Bool) (c : ℕ) :

Bit number c of a basis string, and false past its end.

theorem Cslib.Quantum.readBit_of_lt {m : ℕ} (x : Fin m → Bool) {c : ℕ} (h : c < m) :
readBit x c = x ⟨c, h⟩
theorem Cslib.Quantum.readBit_of_le {m : ℕ} (x : Fin m → Bool) {c : ℕ} (h : m ≤ c) :
@[simp]
theorem Cslib.Quantum.readBit_fin {m : ℕ} (x : Fin m → Bool) (i : Fin m) :
readBit x ↑i = x i
theorem Cslib.Quantum.funext_readBit {m : ℕ} {x y : Fin m → Bool} (h : ∀ (c : ℕ), readBit x c = readBit y c) :
x = y

Basis strings are equal when they have the same bits.

theorem Cslib.Quantum.readBit_inputState_of_le {m : ℕ} {x : List Bool} {c : ℕ} (hc : x.length ≤ c) :

The bits of the input state past the end of the input are zero.

theorem Cslib.Quantum.readBit_inputState {m : ℕ} {x : List Bool} (hx : x.length ≤ m) (c : ℕ) :

The bits of the input state are those of the input, when the input fits in the register.

@[reducible, inline]

The target wire t of a gate code (h, t, cs).

@[reducible, inline]

The control wire list cs of a gate code (h, t, cs).

@[simp]
theorem Cslib.Quantum.GateCode.target_mk (h : Bool) (t : ℕ) (cs : List ℕ) :
target (h, t, cs) = t
@[simp]
theorem Cslib.Quantum.GateCode.controls_mk (h : Bool) (t : ℕ) (cs : List ℕ) :
controls (h, t, cs) = cs
def Cslib.Quantum.GateCode.classicalStep {m : ℕ} (g : GateCode) (x : Fin m → Bool) :
Fin m → Bool

The action on basis strings of the classical gate code (false, t, cs): bit t flips when every bit of cs is one. It is the identity when t is out of range, and a permutation when t is not in cs (classicalStep_classicalStep).

A classical code changes only its target bit, flipping it when all its controls are one.

theorem Cslib.Quantum.GateCode.all_readBit_classicalStep {m : ℕ} {g : GateCode} (x : Fin m → Bool) (cs : List ℕ) (hcs : g.target ∉ cs) :
cs.all (readBit (g.classicalStep x)) = cs.all (readBit x)

Bits other than the target of a classical code read the same after it acts.

A classical code is an involution when its target is not one of its controls.

A list of classical gate codes, well formed on m qubits: NOT, CNOT and Toffoli gates.

theorem Cslib.Quantum.GateCode.IsClassical.flatMap {m : ℕ} {β : Type u_1} {l : List β} {f : β → List GateCode} (h : ∀ b ∈ l, IsClassical m (f b)) :
theorem Cslib.Quantum.GateCode.target_notMem_of_wellFormed {m : ℕ} {g : GateCode} (h₁ : g.1 = false) (h₂ : WellFormed m g) :

The target of a well-formed classical code is not one of its controls.

def Cslib.Quantum.GateCode.classicalRun {m : ℕ} (gs : List GateCode) (x : Fin m → Bool) :
Fin m → Bool

The composed action of a list of classical codes, applied first to last.

The steps of classicalRun applied last to first: the inverse of classicalRun when IsClassical m gs (classicalUnrun_classicalRun, classicalRun_classicalUnrun).

theorem Cslib.Quantum.GateCode.classicalRun_flatMap {m : ℕ} {β : Type u_1} (l : List β) (f : β → List GateCode) (x : Fin m → Bool) :
classicalRun (List.flatMap f l) x = List.foldl (fun (y : Fin m → Bool) (b : β) => classicalRun (f b) y) x l
theorem Cslib.Quantum.GateCode.readBit_classicalRun_of_forall_notMem {m : ℕ} {gs : List GateCode} (h : IsClassical m gs) (hread : ∀ g ∈ gs, ∀ g' ∈ gs, g.target ∉ g'.controls) (x : Fin m → Bool) (p : ℕ) :
readBit (classicalRun gs x) p = List.foldl (fun (b : Bool) (g : GateCode) => b ^^ decide (p = g.target) && g.controls.all (readBit x)) (readBit x p) gs

Classical codes that never read a written bit add to each bit the controls' values on the initial string, one for each code writing that bit.

theorem Cslib.Quantum.GateCode.readBit_classicalRun_of_forall_ne {m : ℕ} {gs : List GateCode} {q : ℕ} (h : ∀ g ∈ gs, g.target ≠ q) (x : Fin m → Bool) :

Classical codes none of which targets q leave bit q unchanged.

theorem Cslib.Quantum.GateCode.readBit_classicalRun_of_mem {m : ℕ} {gs : List GateCode} (hlt : ∀ g ∈ gs, g.target < m) (hnd : (List.map target gs).Nodup) (hread : ∀ g ∈ gs, ∀ g' ∈ gs, g.target ∉ g'.controls) (x : Fin m → Bool) {g : GateCode} (hg : g ∈ gs) :

Classical codes with distinct targets that no code reads flip each target exactly when the controls of its code are one on the initial string.

theorem Cslib.Quantum.GateCode.foldl_xor_range (T i₀ : ℕ) (f : ℕ → Bool) (b : Bool) (hf : ∀ i < T, i ≠ i₀ → f i = false) :
List.foldl (fun (b : Bool) (i : ℕ) => b ^^ f i) b (List.range T) = (b ^^ decide (i₀ < T) && f i₀)

An exclusive or over a range in which only the term i₀ can be nonzero.

theorem Cslib.Quantum.GateCode.exists_toPlacedGate?_eval_ket {m : ℕ} {g : GateCode} (h₁ : g.1 = false) (h₂ : WellFormed m g) :
∃ (pg : PlacedGate m), toPlacedGate? m g = some pg ∧ ∀ (x : Fin m → Bool), pg.eval (ket x) = ket (g.classicalStep x)

A well-formed classical code denotes a gate that maps |x⟩ to |classicalStep g x⟩.

theorem Cslib.Quantum.GateCode.exists_toPlacedGate?_eval_apply {m : ℕ} {g : GateCode} (h₁ : g.1 = false) (h₂ : WellFormed m g) :
∃ (pg : PlacedGate m), toPlacedGate? m g = some pg ∧ ∀ (ψ : Qubits m) (y : Fin m → Bool), (pg.eval ψ).ofLp y = ψ.ofLp (g.classicalStep y)

A well-formed classical code reads each amplitude at the permuted basis string.

Swapping two qubits #

Three CNOT gates swapping wires left and right.

@[simp]
theorem Cslib.Quantum.GateCode.isClassical_swapWireCodes {left right width : ℕ} (hne : left ≠ right) (hleft : left < width) (hright : right < width) :
IsClassical width (swapWireCodes left right)

Swapping two distinct valid wires is a well-formed classical circuit.

theorem Cslib.Quantum.GateCode.readBit_classicalRun_swapWireCodes {left right width : ℕ} (hne : left ≠ right) (hleft : left < width) (hright : right < width) (x : Fin width → Bool) :
readBit (classicalRun (swapWireCodes left right) x) left = readBit x right

The left wire after a swap contains the former right-wire bit.

theorem Cslib.Quantum.GateCode.readBit_classicalRun_swapWireCodes_of_ne {left right q width : ℕ} (hql : q ≠ left) (hqr : q ≠ right) (x : Fin width → Bool) :
readBit (classicalRun (swapWireCodes left right) x) q = readBit x q

A wire outside a swapped pair is unchanged.

For a ≠ 0, swap qubit a into qubit 0.

theorem Cslib.Quantum.GateCode.isClassical_swapCodes {N a : ℕ} (ha : 0 < a) (hN : a < N) :
theorem Cslib.Quantum.GateCode.readBit_classicalRun_swapCodes {N a : ℕ} (ha : 0 < a) (hN : a < N) (X : Fin N → Bool) :

The swap moves qubit a into qubit 0.

A classical circuit permutes basis states.

A classical circuit started in |x⟩ ends in |classicalRun gs x⟩.

theorem Cslib.Quantum.Circuit.eval_ofCodes_apply {m : ℕ} {gs : List GateCode} (h : GateCode.IsClassical m gs) (ψ : Qubits m) (y : Fin m → Bool) :
((ofCodes m gs).eval ψ).ofLp y = ψ.ofLp (GateCode.classicalUnrun gs y)

A classical circuit reads amplitudes through the inverse permutation.

theorem Cslib.Quantum.Circuit.measureProb_eval_ofCodes {m : ℕ} {gs : List GateCode} (h : GateCode.IsClassical m gs) (ψ : Qubits m) (S : Finset (Fin m → Bool)) :
measureProb ((ofCodes m gs).eval ψ) S = ∑ x : Fin m → Bool, if GateCode.classicalRun gs x ∈ S then ‖ψ.ofLp x‖ ^ 2 else 0

Measuring after a classical circuit is measuring before it and permuting the outcome.