roseking's picture
Add lean source and binary files (part 84)
cdec422 verified
Raw History Blame Contribute Delete
299 kB
17
theorem exists_isometry_comp_of_norm_eq {V : Type v17_0} [inst17_0 : NormedAddCommGroup V]
    [inst17_1 : InnerProductSpace ℂ V] [inst17_2 : FiniteDimensional ℂ V]
    (f g : V →ₗ[ℂ] V)
    (h : ∀ x, ‖f x‖ = ‖g x‖) :
    ∃ U : V →ₗᵢ[ℂ] V, U.toLinearMap.comp g = f := by
proof · 14 lines
  have hb : ∃ C : ℝ, ∀ x, ‖f x‖ ≤ C * ‖g x‖ := ⟨1, fun x => by simp [h x]⟩
  let L : LinearMap.range g →ₗᵢ[ℂ] V :=
    { toLinearMap := (f.compLeftInverse g).toLinearMap
      norm_map' := by
        rintro ⟨y, x, rfl⟩
        simpa only [ContinuousLinearMap.coe_coe,
          LinearMap.compLeftInverse_apply_of_bdd f g hb x (g x) rfl,
          ← Submodule.norm_coe] using h x }
  refine ⟨L.extend, ?_⟩
  ext x
  change L.extend (g x) = f x
  rw [show L.extend (g x) = L ⟨g x, LinearMap.mem_range_self g x⟩ from
    L.extend_apply ⟨g x, LinearMap.mem_range_self g x⟩]
  exact LinearMap.compLeftInverse_apply_of_bdd f g hb x (g x) rfl
39
lemma norm_eq_of_gram_eq {ι : Type v43_0} [inst43_0 : Fintype ι] [inst43_1 : DecidableEq ι]
    (A B : Matrix ι ι ℂ) (h : Aᴴ * A = Bᴴ * B)
    (x : EuclideanSpace ℂ ι) : ‖A.toEuclideanLin x‖ = ‖B.toEuclideanLin x‖ := by
proof · 8 lines
  have hh := congrArg (fun M : Matrix ι ι ℂ =>
    inner ℂ x (M.toEuclideanLin x)) h
  simp only [Matrix.toLpLin_mul_same, Matrix.toEuclideanLin_conjTranspose_eq_adjoint,
    LinearMap.comp_apply, LinearMap.adjoint_inner_right] at hh
  change inner ℂ (A.toEuclideanLin x) (A.toEuclideanLin x) =
    inner ℂ (B.toEuclideanLin x) (B.toEuclideanLin x) at hh
  have hh' := congrArg (RCLike.re : ℂ → ℝ) hh
  simpa only [inner_self_eq_norm_sq, sq_eq_sq₀ (norm_nonneg _) (norm_nonneg _)] using hh'
51
theorem exists_unitary_mul_of_gram_eq {ι : Type v43_0} [inst43_0 : Fintype ι]
    [inst43_1 : DecidableEq ι]
    (A B : Matrix ι ι ℂ)
    (h : Aᴴ * A = Bᴴ * B) :
    ∃ U : Matrix.unitaryGroup ι ℂ, A = (U : Matrix ι ι ℂ) * B := by
proof · 13 lines
  obtain ⟨U, hU⟩ := exists_isometry_comp_of_norm_eq A.toEuclideanLin B.toEuclideanLin
    (norm_eq_of_gram_eq A B h)
  let e := U.toLinearIsometryEquiv rfl
  let V : Matrix ι ι ℂ := Matrix.toEuclideanLin.symm U.toLinearMap
  have hV : V ∈ Matrix.unitaryGroup ι ℂ := by
    exact e.toMatrix_mem_unitaryGroup (EuclideanSpace.basisFun ι ℂ)
      (EuclideanSpace.basisFun ι ℂ)
  refine ⟨⟨V, hV⟩, ?_⟩
  apply Matrix.toEuclideanLin.injective
  simp only [Matrix.toLpLin_mul_same]
  have hVlin : V.toEuclideanLin = U.toLinearMap := by simp [V]
  change A.toEuclideanLin = V.toEuclideanLin.comp B.toEuclideanLin
  rw [hVlin, hU]
72
def traceNorm {ι : Type v84_0} [inst84_0 : Fintype ι] [inst84_1 : DecidableEq ι]
    (A : Matrix ι ι ℂ) : ℝ := (Matrix.trace (CFC.sqrt (Aᴴ * A))).re
77
def matrixVector {ι : Type v155_0} {κ : Type v155_1}
    (A : Matrix ι κ ℂ) : EuclideanSpace ℂ (ι × κ) :=
body · 1 line
  WithLp.toLp 2 (fun x => A x.1 x.2)
81
abbrev Mat (d : ℕ) := Matrix (Fin d) (Fin d) ℂ
85
def matrixUnit {ι : Type v217_0} [DecidableEq ι] (i j : ι) : Matrix ι ι ℂ :=
body · 1 line
  fun a b => if a = i ∧ b = j then 1 else 0
88
def amplify {a b : ℕ} (L : Mat a →ₗ[ℂ] Mat b) (k : ℕ)
    (U : Matrix (Fin k × Fin a) (Fin k × Fin a) ℂ) :
    Matrix (Fin k × Fin b) (Fin k × Fin b) ℂ :=
body · 1 line
  fun i j => L (fun x y => U (i.1, x) (j.1, y)) i.2 j.2
93
def CompletelyPositive {a b : ℕ} (L : Mat a →ₗ[ℂ] Mat b) : Prop :=
body · 1 line
  ∀ k U, U.PosSemidef → (amplify L k U).PosSemidef
96
def transposeLinear (a : ℕ) : Mat a →ₗ[ℂ] Mat a where
body · 3 lines
  toFun := Matrix.transpose
  map_add' := Matrix.transpose_add
  map_smul' _ _ := rfl
101
def PPTType {a b : ℕ} (L : Mat a →ₗ[ℂ] Mat b) : Prop :=
body · 1 line
  CompletelyPositive L ∧ CompletelyPositive (L.comp (transposeLinear a))
104
def tensorMap {a b c d : ℕ} (L : Mat a →ₗ[ℂ] Mat b) (M : Mat c →ₗ[ℂ] Mat d)
    (U : Matrix (Fin a × Fin c) (Fin a × Fin c) ℂ) :
    Matrix (Fin b × Fin d) (Fin b × Fin d) ℂ :=
body · 2 lines
  fun i j => ∑ x, ∑ y, ∑ z, ∑ w,
    U (x,z) (y,w) * L (matrixUnit x y) i.1 j.1 * M (matrixUnit z w) i.2 j.2
112
def outer {ι : Type v243_0} (u v : ι → ℂ) : Matrix ι ι ℂ :=
body · 1 line
  fun i j => u i * star (v j)
117
def Density {ι : Type v246_0} [Fintype ι] (R : Matrix ι ι ℂ) : Prop :=
body · 1 line
  R.PosSemidef ∧ Matrix.trace R = 1
122
def amplifyIndex {a b : ℕ} (L : Mat a →ₗ[ℂ] Mat b) {κ : Type v264_0}
    (U : Matrix (κ × Fin a) (κ × Fin a) ℂ) :
    Matrix (κ × Fin b) (κ × Fin b) ℂ :=
body · 1 line
  fun i j => L (fun x y => U (i.1,x) (j.1,y)) i.2 j.2
129
lemma cp_amplifyIndex {a b : ℕ} {L : Mat a →ₗ[ℂ] Mat b} (hL : CompletelyPositive L)
    {κ : Type v269_0} [Fintype κ] (U : Matrix (κ × Fin a) (κ × Fin a) ℂ)
    (hU : U.PosSemidef) : (amplifyIndex L U).PosSemidef := by
proof · 8 lines
  let e := Fintype.equivFin κ
  have h := hL (Fintype.card κ)
    (U.submatrix (fun i => (e.symm i.1, i.2)) (fun i => (e.symm i.1, i.2)))
    (hU.submatrix _)
  have h' := h.submatrix (fun i : κ × Fin b => (e i.1, i.2))
  convert h' using 1
  ext i j
  simp [amplifyIndex, amplify, Matrix.submatrix]
141
lemma matrix_sum_units {a : ℕ} (U : Mat a) :
    U = ∑ x, ∑ y, U x y • matrixUnit x y := by
proof · 2 lines
  ext i j
  simp [matrixUnit, Matrix.sum_apply, Matrix.smul_apply, ite_and]
146
lemma linearMap_matrix_units {a b : ℕ} (L : Mat a →ₗ[ℂ] Mat b) (U : Mat a) :
    L U = ∑ x, ∑ y, U x y • L (matrixUnit x y) := by
proof · 2 lines
  conv_lhs => rw [matrix_sum_units U]
  simp
151
lemma linearMap_matrix_units_apply {a b : ℕ} (L : Mat a →ₗ[ℂ] Mat b) (U : Mat a)
    (i j : Fin b) : L U i j = ∑ x, ∑ y, U x y * L (matrixUnit x y) i j := by
proof · 2 lines
  have h := congrArg (fun A : Mat b => A i j) (linearMap_matrix_units L U)
  simpa only [Matrix.sum_apply, Matrix.smul_apply, smul_eq_mul] using h
156
lemma tensorMap_apply {a b c d : ℕ} (L : Mat a →ₗ[ℂ] Mat b) (M : Mat c →ₗ[ℂ] Mat d)
    (U : Matrix (Fin a × Fin c) (Fin a × Fin c) ℂ) (i j : Fin b) (k l : Fin d) :
    tensorMap L M U (i,k) (j,l) =
      L (fun x y => M (fun z w => U (x,z) (y,w)) k l) i j := by
proof · 21 lines
  calc
    _ = ∑ x, ∑ y, (∑ z, ∑ w, U (x,z) (y,w) * M (matrixUnit z w) k l) *
        L (matrixUnit x y) i j := by
      simp only [tensorMap, Finset.sum_mul]
      apply Finset.sum_congr rfl
      intro x _
      apply Finset.sum_congr rfl
      intro y _
      apply Finset.sum_congr rfl
      intro z _
      apply Finset.sum_congr rfl
      intro w _
      ring
    _ = ∑ x, ∑ y, M (fun z w => U (x,z) (y,w)) k l * L (matrixUnit x y) i j := by
      apply Finset.sum_congr rfl
      intro x _
      apply Finset.sum_congr rfl
      intro y _
      exact congrArg (fun t => t * L (matrixUnit x y) i j)
        (linearMap_matrix_units_apply M (fun z w => U (x,z) (y,w)) k l).symm
    _ = _ := (linearMap_matrix_units_apply L _ i j).symm
182
lemma tensorMap_positive {a b c d : ℕ} {L : Mat a →ₗ[ℂ] Mat b}
    {M : Mat c →ₗ[ℂ] Mat d} (hL : CompletelyPositive L) (hM : CompletelyPositive M)
    {U : Matrix (Fin a × Fin c) (Fin a × Fin c) ℂ} (hU : U.PosSemidef) :
    (tensorMap L M U).PosSemidef := by
proof · 6 lines
  have h₁ := cp_amplifyIndex hM U hU
  have h₂ := cp_amplifyIndex hL _ (h₁.submatrix Prod.swap)
  have h₃ := h₂.submatrix Prod.swap
  convert h₃ using 1
  ext i j
  exact tensorMap_apply L M U i.1 j.1 i.2 j.2
195
lemma outer_posSemidef {ι : Type v375_0} [Fintype ι] (u : ι → ℂ) : (outer u u).PosSemidef := by
proof · 1 line
  exact Matrix.posSemidef_vecMulVec_self_star u
200
lemma cp_of_kraus_representation {a b : ℕ} {ν : Type v1019_0} [Fintype ν]
    (L : Mat a →ₗ[ℂ] Mat b) (K : ν → Matrix (Fin b) (Fin a) ℂ)
    (hK : ∀ U, L U = ∑ s, K s * U * (K s)ᴴ) : CompletelyPositive L := by
proof · 16 lines
  intro r U hU
  have he : amplify L r U = ∑ s, ((1 : Mat r) ⊗ₖ K s) * U * ((1 : Mat r) ⊗ₖ K s)ᴴ := by
    ext ⟨i,x⟩ ⟨j,y⟩
    change L (fun v w => U (i,v) (j,w)) x y = _
    erw [hK]
    simp only [Matrix.sum_apply]
    apply Finset.sum_congr rfl
    intro s _
    change (K s * U.submatrix (fun v => (i,v)) (fun w => (j,w)) * (K s)ᴴ) x y = _
    simp [Matrix.mul_apply, Matrix.conjTranspose_apply, Matrix.submatrix,
      Fintype.sum_prod_type, Matrix.one_apply, apply_ite]

  rw [he]
  apply Matrix.posSemidef_sum
  intro s _
  exact hU.mul_mul_conjTranspose_same _
233
def tensorPi (A : ∀ s, Matrix (ι s) (ι s) ℂ) : Matrix (∀ s, ι s) (∀ s, ι s) ℂ :=
body · 1 line
  fun i j => ∏ s, A s (i s) (j s)
237
lemma tensorPi_mul [∀ s, DecidableEq (ι s)] (A B : ∀ s, Matrix (ι s) (ι s) ℂ) :
    tensorPi (fun s => A s * B s) = tensorPi A * tensorPi B := by
proof · 6 lines
  ext i j
  simp only [tensorPi, Matrix.mul_apply]
  rw [Fintype.prod_sum]
  apply Finset.sum_congr rfl
  intro k _
  exact Finset.prod_mul_distrib
247
lemma tensorPi_one [DecidableEq σ] [∀ s, Fintype (ι s)] : tensorPi (fun s => (1 : Matrix (ι s) (ι s) ℂ)) = 1 := by
proof · 8 lines
  ext i j
  by_cases h : i=j
  · subst j
    simp [tensorPi]
  · obtain ⟨s,hs⟩ := Function.ne_iff.mp h
    simp only [tensorPi, Matrix.one_apply, ite_eq_right h]
    apply Finset.prod_eq_zero (Finset.mem_univ s)
    simp [hs]
258
lemma tensorPi_star [DecidableEq σ] [∀ s, Fintype (ι s)] [∀ s, DecidableEq (ι s)] (A : ∀ s, Matrix (ι s) (ι s) ℂ) :
    tensorPi (fun s => (A s)ᴴ) = (tensorPi A)ᴴ := by
proof · 2 lines
  ext i j
  simp [tensorPi, Matrix.conjTranspose_apply]
263
lemma tensorPi_projection (A : ∀ s, Matrix (ι s) (ι s) ℂ)
    (hA : ∀ s, IsStarProjection (A s)) : IsStarProjection (tensorPi A) := by
proof · 11 lines
  constructor
  · change tensorPi A * tensorPi A = tensorPi A
    rw [← tensorPi_mul]
    congr 1
    funext s
    exact (hA s).isIdempotentElem
  · change (tensorPi A)ᴴ = tensorPi A
    rw [← tensorPi_star]
    congr 1
    funext s
    exact (hA s).isSelfAdjoint
280
lemma tensorPi_sum [∀ s, Fintype (ι s)] [∀ s, DecidableEq (ι s)] {α : σ → Type v1163_0} [∀ s, Fintype (α s)]
    (A : ∀ s, α s → Matrix (ι s) (ι s) ℂ) :
    (∑ a : ∀ s, α s, tensorPi (fun s => A s (a s))) =
      tensorPi (fun s => ∑ a, A s a) := by
proof · 3 lines
  ext i j
  simp only [Matrix.sum_apply, tensorPi]
  exact (Fintype.prod_sum (fun s a => A s a (i s) (j s))).symm
294
abbrev p : ℕ := 257
296
abbrev k : ℕ := 1028
298
@[irreducible] def b : ℕ := Nat.choose 1027 513
300
@[irreducible] def Q : ℕ := b^3
302
@[irreducible] def K : ℕ := k^3
304
def dimensionFunction (q : ℕ) : ℕ := 4^(Nat.choose q 8)
306
lemma dimensionFunction_eq (q : ℕ) : dimensionFunction q = 4^(Nat.choose q 8) := rfl
308
@[irreducible] def D : ℕ := dimensionFunction Q
310
lemma D_eq : D = 4^(Nat.choose Q 8) := by
proof · 2 lines
  unfold D
  exact dimensionFunction_eq Q
314
def outputDimensionFunction (q d : ℕ) : ℕ := q*d
316
lemma outputDimensionFunction_eq (q d : ℕ) : outputDimensionFunction q d = q*d := rfl
318
@[irreducible] def m : ℕ := outputDimensionFunction Q D
320
lemma m_eq : m = Q*D := by
proof · 2 lines
  unfold m
  exact outputDimensionFunction_eq Q D
324
def Base := {A : Finset (Fin k) // A.card = 514 ∧ (0 : Fin k) ∈ A}
326
instance : Finite Base := by unfold Base; infer_instance
328
instance : Fintype Base := Fintype.ofFinite _
330
instance : DecidableEq Base := Classical.decEq _
332
instance : LinearOrder Base := LinearOrder.lift' (fun A : Base => A.1.sort (· ≤ ·)) (by
body · 3 lines
  intro A B h
  apply Subtype.ext
  simpa only [Finset.sort_toFinset] using congrArg List.toFinset h)
337
def Question := Lex (Fin 3 → Base)
339
instance : Finite Question := by unfold Question; infer_instance
341
instance : Fintype Question := Fintype.ofFinite _
343
instance : DecidableEq Question := Classical.decEq _
345
instance : LinearOrder Question := inferInstanceAs (LinearOrder (Lex (Fin 3 → Base)))
347
def baseVector (A : Base) (h : Fin k) : ℝ :=
body · 1 line
  (if h ∈ A.1 then 1 else -1) / Real.sqrt k
350
def vector (q : Question) (h : Fin 3 → Fin k) : ℝ :=
body · 1 line
  ∏ l, baseVector (ofLex q l) (h l)
353
def overlap (q r : Question) : ℝ := ∑ h, vector q h * vector r h
355
def EightSet := {S : Finset Question // S.card = 8}
357
instance : Finite EightSet := by unfold EightSet; infer_instance
359
instance : Fintype EightSet := Fintype.ofFinite _
361
instance : DecidableEq EightSet := Classical.decEq _
363
instance : LinearOrder EightSet := LinearOrder.lift'
body · 4 lines
  (fun S : EightSet => S.1.sort (· ≤ ·)) (by
    intro S T h
    apply Subtype.ext
    simpa only [Finset.sort_toFinset] using congrArg List.toFinset h)
369
def PairwiseOrthogonal (S : EightSet) : Prop :=
body · 1 line
  ∀ q ∈ S.1, ∀ r ∈ S.1, q ≠ r → overlap q r = 0
372
abbrev Sign := Fin 2
374
def signValue (s : Sign) : ℂ := if s=0 then -1 else 1
376
abbrev Outcome := Lex (Sign × Sign)
378
abbrev LocalBasis := Lex (Fin 2 × Fin 2)
380
def pauliX : Matrix (Fin 2) (Fin 2) ℂ := !![0,1;1,0]
382
def pauliY : Matrix (Fin 2) (Fin 2) ℂ := !![0,-Complex.I;Complex.I,0]
384
def pauliZ : Matrix (Fin 2) (Fin 2) ℂ := !![1,0;0,-1]
386
def tensor₂ (A B : Matrix (Fin 2) (Fin 2) ℂ) : Matrix LocalBasis LocalBasis ℂ :=
body · 1 line
  (A ⊗ₖ B).submatrix ofLex ofLex
389
def observable : Fin 3 → Fin 3 → Matrix LocalBasis LocalBasis ℂ
body · 9 lines
  | 0,0 => tensor₂ pauliX 1
  | 0,1 => tensor₂ 1 pauliX
  | 0,2 => tensor₂ pauliX pauliX
  | 1,0 => tensor₂ 1 pauliZ
  | 1,1 => tensor₂ pauliZ 1
  | 1,2 => tensor₂ pauliZ pauliZ
  | 2,0 => tensor₂ pauliX pauliZ
  | 2,1 => tensor₂ pauliZ pauliX
  | 2,2 => tensor₂ pauliY pauliY
400
def contextObservable (t : Fin 6) (j : Fin 3) : Matrix LocalBasis LocalBasis ℂ :=
body · 2 lines
  if h : t.val < 3 then observable ⟨t.val,h⟩ j
  else observable j ⟨t.val-3, by omega⟩
404
def contextProjection (t : Fin 6) (a : Outcome) : Matrix LocalBasis LocalBasis ℂ :=
body · 2 lines
  (1/4 : ℂ) • ((1+signValue (ofLex a).1 • contextObservable t 0) *
    (1+signValue (ofLex a).2 • contextObservable t 1))
408
def trivialProjection (a : Outcome) : Matrix LocalBasis LocalBasis ℂ :=
body · 1 line
  if ofLex a = (1,1) then 1 else 0
411
def localProjection (S : EightSet) (q : Question) (a : Outcome) :
    Matrix LocalBasis LocalBasis ℂ :=
body · 10 lines
  by
    classical
    exact
      if PairwiseOrthogonal S then
        if h : q ∈ S.1 then
          let rank := (S.1.orderIsoOfFin S.2).symm ⟨q,h⟩
          if hr : rank.val < 6 then contextProjection ⟨rank.val,hr⟩ a
          else trivialProjection a
        else trivialProjection a
      else trivialProjection a
424
def Basis := Lex (EightSet → LocalBasis)
426
instance : Finite Basis := by unfold Basis; infer_instance
428
instance : Fintype Basis := Fintype.ofFinite _
430
instance : DecidableEq Basis := Classical.decEq _
432
instance : LinearOrder Basis := inferInstanceAs (LinearOrder (Lex (EightSet → LocalBasis)))
434
def OutcomeString := Lex (EightSet → Outcome)
436
instance : Finite OutcomeString := by unfold OutcomeString; infer_instance
438
instance : Fintype OutcomeString := Fintype.ofFinite _
440
instance : DecidableEq OutcomeString := Classical.decEq _
442
instance : LinearOrder OutcomeString := inferInstanceAs (LinearOrder (Lex (EightSet → Outcome)))
444
def Label := Lex (Question × OutcomeString)
446
instance : Finite Label := by unfold Label; infer_instance
448
instance : Fintype Label := Fintype.ofFinite _
450
instance : DecidableEq Label := Classical.decEq _
452
instance : LinearOrder Label := inferInstanceAs (LinearOrder (Lex (Question × OutcomeString)))
454
def projection (i : Label) : Matrix Basis Basis ℂ := fun h l =>
body · 1 line
  ∏ S, localProjection S (ofLex i).1 (ofLex (ofLex i).2 S) (ofLex h S) (ofLex l S)
457
def block (i j : Label) : Matrix Basis Basis ℂ :=
body · 1 line
  (overlap (ofLex i).1 (ofLex j).1 : ℂ) • (projection i * projection j)
462
def traceBlockMap {α : Type v1312_0} {β : Type v1312_1} [Fintype α] [DecidableEq α]
    (H : β → β → Matrix α α ℂ) : Matrix α α ℂ →ₗ[ℂ] Matrix β β ℂ where
body · 10 lines
  toFun U i j := trace (H i j * U)
  map_add' U V := by
    ext i j
    change trace (H i j * (U+V)) = trace (H i j * U)+trace (H i j * V)
    rw [mul_add, Matrix.trace_add]
  map_smul' c U := by
    ext i j
    change trace (H i j * (c • U)) = c * trace (H i j * U)
    rw [Matrix.mul_smul, Matrix.trace_smul]
    rfl
475
def rawOutput : Matrix (Label × Label) (Label × Label) ℂ := fun i j =>
body · 1 line
  ∑ h, ∑ l, block i.1 j.1 l h * block i.2 j.2 l h
478
def rho : Matrix (Label × Label) (Label × Label) ℂ :=
body · 1 line
  ((Q^2*D : ℕ) : ℂ)⁻¹ • rawOutput
481
lemma tensor₂_apply (A B : Matrix (Fin 2) (Fin 2) ℂ) (i j : LocalBasis) :
    tensor₂ A B i j = A (ofLex i).1 (ofLex j).1 * B (ofLex i).2 (ofLex j).2 := rfl
484
lemma sum_localBasis (f : LocalBasis → ℂ) : (∑ i, f i) =
    f (toLex (0,0)) + f (toLex (0,1)) + (f (toLex (1,0)) + f (toLex (1,1))) := by
proof · 2 lines
  change (∑ i : Fin 2 × Fin 2, f (toLex i)) = _
  rw [Fintype.sum_prod_type, Fin.sum_univ_two, Fin.sum_univ_two, Fin.sum_univ_two]
489
lemma signValue_sq (a : Sign) : signValue a * signValue a = 1 := by
proof · 1 line
  fin_cases a <;> norm_num [signValue]
492
lemma signValue_conj (a : Sign) : star (signValue a) = signValue a := by
proof · 1 line
  fin_cases a <;> norm_num [signValue]
495
lemma localBasis_one_apply (i j : LocalBasis) :
    (1 : Matrix LocalBasis LocalBasis ℂ) i j = if ofLex i = ofLex j then 1 else 0 := by
proof · 1 line
  simp only [Matrix.one_apply, ofLex.injective.eq_iff]
499
lemma context_hermitian (t : Fin 6) (j : Fin 3) :
    (contextObservable t j).IsHermitian := by
proof · 11 lines
  fin_cases t <;> fin_cases j <;> change (tensor₂ _ _).IsHermitian
  all_goals
    rw [Matrix.IsHermitian]
    ext i l
    rw [Matrix.conjTranspose_apply, tensor₂_apply, tensor₂_apply]
    generalize hi : ofLex i = ii
    generalize hl : ofLex l = ll
    rcases ii with ⟨i,h⟩
    rcases ll with ⟨l,k⟩
    fin_cases i <;> fin_cases h <;> fin_cases l <;> fin_cases k <;>
      norm_num [pauliX, pauliY, pauliZ, Matrix.one_apply]
513
lemma context_real (t : Fin 6) (j : Fin 3) (i l : LocalBasis) :
    star (contextObservable t j i l) = contextObservable t j i l := by
proof · 9 lines
  fin_cases t <;> fin_cases j <;> change star (tensor₂ _ _ i l) = tensor₂ _ _ i l
  all_goals
    rw [tensor₂_apply]
    generalize hi : ofLex i = ii
    generalize hl : ofLex l = ll
    rcases ii with ⟨i,h⟩
    rcases ll with ⟨l,k⟩
    fin_cases i <;> fin_cases h <;> fin_cases l <;> fin_cases k <;>
      norm_num [pauliX, pauliY, pauliZ, Matrix.one_apply]
525
lemma context_involution (t : Fin 6) (j : Fin 3) :
    contextObservable t j * contextObservable t j = 1 := by
proof · 11 lines
  fin_cases t <;> fin_cases j <;> change tensor₂ _ _ * tensor₂ _ _ = (1 : Matrix LocalBasis LocalBasis ℂ)
  all_goals
    ext i l
    rw [Matrix.mul_apply, sum_localBasis, localBasis_one_apply]
    simp only [tensor₂_apply, ofLex_toLex]
    generalize hi : ofLex i = ii
    generalize hl : ofLex l = ll
    rcases ii with ⟨i,h⟩
    rcases ll with ⟨l,k⟩
    fin_cases i <;> fin_cases h <;> fin_cases l <;> fin_cases k <;>
      norm_num [pauliX, pauliY, pauliZ, Matrix.one_apply]
539
lemma context_commute (t : Fin 6) :
    Commute (contextObservable t 0) (contextObservable t 1) := by
proof · 11 lines
  change contextObservable t 0 * contextObservable t 1 = contextObservable t 1 * contextObservable t 0
  fin_cases t <;> change tensor₂ _ _ * tensor₂ _ _ = tensor₂ _ _ * tensor₂ _ _
  all_goals
    ext i l
    simp only [Matrix.mul_apply, sum_localBasis, tensor₂_apply, ofLex_toLex]
    generalize hi : ofLex i = ii
    generalize hl : ofLex l = ll
    rcases ii with ⟨i,h⟩
    rcases ll with ⟨l,k⟩
    fin_cases i <;> fin_cases h <;> fin_cases l <;> fin_cases k <;>
      norm_num [pauliX, pauliY, pauliZ, Matrix.one_apply]
553
def contextParity (t : Fin 6) : ℂ := if t=5 then -1 else 1
555
lemma context_third (t : Fin 6) : contextObservable t 2 =
    contextParity t • (contextObservable t 0 * contextObservable t 1) := by
proof · 10 lines
  fin_cases t <;> change tensor₂ _ _ = _ • (tensor₂ _ _ * tensor₂ _ _)
  all_goals
    ext i l
    simp only [Matrix.smul_apply, smul_eq_mul, Matrix.mul_apply, sum_localBasis, tensor₂_apply, ofLex_toLex]
    generalize hi : ofLex i = ii
    generalize hl : ofLex l = ll
    rcases ii with ⟨i,h⟩
    rcases ll with ⟨l,k⟩
    fin_cases i <;> fin_cases h <;> fin_cases l <;> fin_cases k <;>
      norm_num [contextParity, pauliX, pauliY, pauliZ, Matrix.one_apply, Fin.ext_iff]
568
def spectralSign (O : Matrix LocalBasis LocalBasis ℂ) (a : Sign) :=
body · 1 line
  (1/2 : ℂ) • (1 + signValue a • O)
571
lemma sign_eigen_aux (O : Matrix LocalBasis LocalBasis ℂ) (hO : O*O=1) (a : Sign) :
    O*(1+signValue a • O) = signValue a • (1+signValue a • O) := by
proof · 3 lines
  simp only [mul_add, mul_one, mul_smul_comm, hO, smul_add, smul_smul,
    signValue_sq, one_smul]
  exact add_comm _ _
577
lemma spectralSign_projection (O : Matrix LocalBasis LocalBasis ℂ)
    (hO : O.IsHermitian) (hO2 : O*O=1) (a : Sign) :
    IsStarProjection (spectralSign O a) := by
proof · 10 lines
  refine ⟨?_, ?_⟩
  · change spectralSign O a * spectralSign O a = spectralSign O a
    dsimp [spectralSign]
    rw [smul_mul_smul_comm, add_mul, one_mul, smul_mul_assoc, sign_eigen_aux O hO2]
    rw [smul_smul, signValue_sq, one_smul]
    module
  · change star (spectralSign O a) = spectralSign O a
    dsimp [spectralSign]
    simp only [star_smul, star_add, star_one, signValue_conj, hO.star_eq]
    norm_num
591
lemma spectralSign_commute (O T : Matrix LocalBasis LocalBasis ℂ)
    (h : Commute O T) (a b : Sign) : Commute (spectralSign O a) (spectralSign T b) := by
proof · 3 lines
  unfold spectralSign
  exact ((Commute.one_left _).add_left ((Commute.one_right _).add_right
    (h.smul_left _ |>.smul_right _))).smul_left _ |>.smul_right _
597
lemma contextProjection_factor (t : Fin 6) (a : Outcome) : contextProjection t a =
    spectralSign (contextObservable t 0) (ofLex a).1 *
      spectralSign (contextObservable t 1) (ofLex a).2 := by
proof · 2 lines
  simp only [contextProjection, spectralSign, smul_mul_smul_comm]
  norm_num
603
lemma contextProjection_projection (t : Fin 6) (a : Outcome) :
    IsStarProjection (contextProjection t a) := by
proof · 4 lines
  rw [contextProjection_factor]
  exact (spectralSign_projection _ (context_hermitian t 0) (context_involution t 0) _).mul
    (spectralSign_projection _ (context_hermitian t 1) (context_involution t 1) _)
    (spectralSign_commute _ _ (context_commute t) _ _)
610
lemma contextProjection_real (t : Fin 6) (a : Outcome) (i j : LocalBasis) :
    star (contextProjection t a i j) = contextProjection t a i j := by
proof · 4 lines
  simp only [contextProjection, Matrix.smul_apply, smul_eq_mul, Matrix.mul_apply,
    star_mul, star_sum, Matrix.add_apply, Matrix.smul_apply, smul_eq_mul,
    star_add, context_real, signValue_conj]
  simp [Matrix.one_apply, mul_comm]
617
lemma contextParity_conj (t : Fin 6) : star (contextParity t) = contextParity t := by
proof · 2 lines
  unfold contextParity
  split <;> norm_num
621
def contextEigenvalue (t : Fin 6) (a : Outcome) : Fin 3 → ℂ :=
body · 2 lines
  ![signValue (ofLex a).1, signValue (ofLex a).2,
    contextParity t * signValue (ofLex a).1 * signValue (ofLex a).2]
625
lemma context_eigen_first (t : Fin 6) (a : Outcome) :
    contextObservable t 0 * contextProjection t a =
      signValue (ofLex a).1 • contextProjection t a := by
proof · 3 lines
  unfold contextProjection
  rw [mul_smul_comm, ← mul_assoc, sign_eigen_aux _ (context_involution t 0),
    smul_mul_assoc, smul_comm]
632
lemma context_eigen_second (t : Fin 6) (a : Outcome) :
    contextObservable t 1 * contextProjection t a =
      signValue (ofLex a).2 • contextProjection t a := by
proof · 7 lines
  unfold contextProjection
  rw [mul_smul_comm]
  have hc : Commute (contextObservable t 1)
      (1+signValue (ofLex a).1 • contextObservable t 0) :=
    (Commute.one_right _).add_right ((context_commute t).symm.smul_right _)
  rw [← mul_assoc, hc, mul_assoc, sign_eigen_aux _ (context_involution t 1),
    mul_smul_comm, smul_comm]
643
lemma context_eigen (t : Fin 6) (a : Outcome) (j : Fin 3) :
    contextObservable t j * contextProjection t a =
      contextEigenvalue t a j • contextProjection t a := by
proof · 10 lines
  fin_cases j
  · exact context_eigen_first t a
  · exact context_eigen_second t a
  · change contextObservable t 2 * contextProjection t a =
      (contextParity t * signValue (ofLex a).1 * signValue (ofLex a).2) • _
    rw [context_third, smul_mul_assoc, mul_assoc, context_eigen_second,
      mul_smul_comm, context_eigen_first]
    simp only [smul_smul]
    congr 1
    ring
657
lemma contextEigenvalue_conj (t : Fin 6) (a : Outcome) (j : Fin 3) :
    star (contextEigenvalue t a j) = contextEigenvalue t a j := by
proof · 1 line
  (fin_cases j <;> simp [contextEigenvalue, star_mul, signValue_conj, contextParity_conj]); ring
661
lemma context_eigen_right (t : Fin 6) (a : Outcome) (j : Fin 3) :
    contextProjection t a * contextObservable t j =
      contextEigenvalue t a j • contextProjection t a := by
proof · 3 lines
  have h := congrArg star (context_eigen t a j)
  simpa only [star_mul, (contextProjection_projection t a).isSelfAdjoint.star_eq,
    (context_hermitian t j).star_eq, star_smul, contextEigenvalue_conj] using h
668
lemma spectralSign_sum (O : Matrix LocalBasis LocalBasis ℂ) :
    ∑ a, spectralSign O a = 1 := by
proof · 3 lines
  rw [Fin.sum_univ_two]
  norm_num [spectralSign, signValue]
  module
674
lemma contextProjection_sum (t : Fin 6) : ∑ a, contextProjection t a = 1 := by
proof · 5 lines
  simp_rw [contextProjection_factor]
  change (∑ a : Sign × Sign, spectralSign (contextObservable t 0) a.1 *
    spectralSign (contextObservable t 1) a.2) = _
  rw [Fintype.sum_prod_type]
  simp only [← Finset.mul_sum, ← Finset.sum_mul, spectralSign_sum, one_mul]
683
lemma eigenvalues_agree {ι : Type v1552_0} [Fintype ι] (E F O : Matrix ι ι ℂ)
    (a b : ℂ) (hE : E*O=a • E) (hF : O*F=b • F) (hEF : E*F≠0) : a=b := by
proof · 4 lines
  apply smul_left_injective ℂ hEF
  calc a • (E*F) = (E*O)*F := by rw [hE, smul_mul_assoc]
       _ = E*(O*F) := mul_assoc _ _ _
       _ = b • (E*F) := by rw [hF, mul_smul_comm]
690
lemma signValue_injective : Function.Injective signValue := by
proof · 2 lines
  intro a b hab
  fin_cases a <;> fin_cases b <;> first | rfl | norm_num [signValue] at hab
694
lemma contextProjection_orthogonal (t : Fin 6) (a b : Outcome) (hab : a≠b) :
    contextProjection t a * contextProjection t b = 0 := by
proof · 8 lines
  by_contra h
  have he (j : Fin 3) := eigenvalues_agree _ _ _ _ _
    (context_eigen_right t a j) (context_eigen t b j) h
  apply hab
  apply ofLex.injective
  apply Prod.ext
  · exact signValue_injective (he 0)
  · exact signValue_injective (he 1)
705
lemma trivialProjection_projection (a : Outcome) : IsStarProjection (trivialProjection a) := by
proof · 2 lines
  unfold trivialProjection
  split <;> simp only [IsStarProjection.one, IsStarProjection.zero]
709
lemma trivialProjection_sum : ∑ a, trivialProjection a = 1 := by
proof · 6 lines
  classical
  have h : ∀ a, trivialProjection a = if a = toLex (1,1) then 1 else 0 := by
    intro a
    simp [trivialProjection, ← ofLex.injective.eq_iff]
  simp only [h]
  simp
717
lemma trivialProjection_orthogonal (a b : Outcome) (hab : a≠b) :
    trivialProjection a * trivialProjection b = 0 := by
proof · 4 lines
  by_cases ha : ofLex a = (1,1)
  · have hb : ofLex b ≠ (1,1) := fun hb => hab (ofLex.injective (ha.trans hb.symm))
    simp [trivialProjection, hb]
  · simp [trivialProjection, ha]
724
lemma localProjection_type (S : EightSet) (q : Question) :
    (localProjection S q = trivialProjection) ∨
      (∃ t : Fin 6, localProjection S q = contextProjection t) := by
proof · 10 lines
  classical
  unfold localProjection
  split
  · split
    · dsimp only
      split
      · exact Or.inr ⟨_,rfl⟩
      · exact Or.inl rfl
    · exact Or.inl rfl
  · exact Or.inl rfl
738
lemma localProjection_projection (S : EightSet) (q : Question) (a : Outcome) :
    IsStarProjection (localProjection S q a) := by
proof · 3 lines
  rcases localProjection_type S q with h | ⟨t,h⟩
  · rw [h]; exact trivialProjection_projection a
  · rw [h]; exact contextProjection_projection t a
744
lemma localProjection_real (S : EightSet) (q : Question) (a : Outcome) (i j : LocalBasis) :
    star (localProjection S q a i j) = localProjection S q a i j := by
proof · 5 lines
  rcases localProjection_type S q with h | ⟨t,h⟩
  · rw [h]
    unfold trivialProjection
    split <;> simp [Matrix.one_apply]
  · rw [h]; exact contextProjection_real t a i j
752
lemma localProjection_sum (S : EightSet) (q : Question) :
    ∑ a, localProjection S q a = 1 := by
proof · 3 lines
  rcases localProjection_type S q with h | ⟨t,h⟩
  · rw [h]; exact trivialProjection_sum
  · rw [h]; exact contextProjection_sum t
758
lemma localProjection_orthogonal (S : EightSet) (q : Question) (a b : Outcome) (hab : a≠b) :
    localProjection S q a * localProjection S q b = 0 := by
proof · 3 lines
  rcases localProjection_type S q with h | ⟨t,h⟩
  · rw [h]; exact trivialProjection_orthogonal a b hab
  · rw [h]; exact contextProjection_orthogonal t a b hab
764
lemma projection_tensor (i : Label) : projection i =
    (tensorPi (fun S => localProjection S (ofLex i).1 (ofLex (ofLex i).2 S))).submatrix
      (show Basis ≃ (EightSet → LocalBasis) from Equiv.refl _) (show Basis ≃ (EightSet → LocalBasis) from Equiv.refl _) := rfl
768
lemma projection_projection (i : Label) : IsStarProjection (projection i) := by
proof · 10 lines
  let e : Basis ≃ (EightSet → LocalBasis) := Equiv.refl _
  let A := tensorPi (fun S => localProjection S (ofLex i).1 (ofLex (ofLex i).2 S))
  have hA : IsStarProjection A := tensorPi_projection _ (fun S => localProjection_projection _ _ _)
  rw [projection_tensor]
  change IsStarProjection (A.submatrix e e)
  constructor
  · change A.submatrix e e * A.submatrix e e = A.submatrix e e
    rw [Matrix.submatrix_mul_equiv, hA.isIdempotentElem]
  · change (A.submatrix e e)ᴴ = A.submatrix e e
    rw [Matrix.conjTranspose_submatrix, show Aᴴ=A from hA.isSelfAdjoint]
780
lemma projection_real (i : Label) (h l : Basis) : star (projection i h l) = projection i h l := by
proof · 4 lines
  change (starRingEnd ℂ) (∏ S, localProjection S (ofLex i).1 (ofLex (ofLex i).2 S) (ofLex h S) (ofLex l S)) = _
  rw [map_prod]
  simp only [starRingEnd_apply, localProjection_real]
  rfl
786
lemma projection_sum (q : Question) : ∑ a : OutcomeString, projection (toLex (q,a)) = 1 := by
proof · 13 lines
  ext h l
  simp only [Matrix.sum_apply, projection, ofLex_toLex]
  have he := congr_fun (congr_fun (tensorPi_sum (fun S a => localProjection S q a)) (ofLex h)) (ofLex l)
  have hr : tensorPi (fun S => ∑ a, localProjection S q a) = 1 := by
    simp only [localProjection_sum, tensorPi_one]
  rw [hr] at he
  calc
    _ = ∑ a : EightSet → Outcome, ∏ S, localProjection S q (a S) (ofLex h S) (ofLex l S) :=
      (Equiv.sum_comp (show (EightSet → Outcome) ≃ OutcomeString from Equiv.refl _)
        (fun a : OutcomeString => ∏ S, localProjection S q (ofLex a S) (ofLex h S) (ofLex l S))).symm
    _ = _ := by
      simpa only [tensorPi, Matrix.sum_apply, Matrix.one_apply,
        show (ofLex h = ofLex l) ↔ h=l from Iff.rfl] using he
801
lemma projection_mul_apply (i j : Label) (h l : Basis) :
    (projection i * projection j) h l = ∏ S,
      (localProjection S (ofLex i).1 (ofLex (ofLex i).2 S) *
        localProjection S (ofLex j).1 (ofLex (ofLex j).2 S)) (ofLex h S) (ofLex l S) := by
proof · 6 lines
  let e : Basis ≃ (EightSet → LocalBasis) := Equiv.refl _
  let A := tensorPi (fun S => localProjection S (ofLex i).1 (ofLex (ofLex i).2 S))
  let B := tensorPi (fun S => localProjection S (ofLex j).1 (ofLex (ofLex j).2 S))
  change (A.submatrix e e * B.submatrix e e) h l = _
  rw [Matrix.submatrix_mul_equiv]
  exact congr_fun (congr_fun (tensorPi_mul _ _).symm (ofLex h)) (ofLex l)
812
lemma trivialProjection_commute (a : Outcome) (A : Matrix LocalBasis LocalBasis ℂ) :
    Commute (trivialProjection a) A := by
proof · 2 lines
  unfold trivialProjection
  split <;> simp
817
lemma localProjection_self_commute (S : EightSet) (q : Question) (a b : Outcome) :
    Commute (localProjection S q a) (localProjection S q b) := by
proof · 5 lines
  by_cases h : a=b
  · subst b; exact Commute.refl _
  · change _ * _ = _ * _
    rw [localProjection_orthogonal _ _ _ _ h,
      localProjection_orthogonal _ _ _ _ (Ne.symm h)]
825
lemma localProjection_trivial_of_not_orthogonal (S : EightSet) (q : Question)
    (h : ¬PairwiseOrthogonal S) : localProjection S q = trivialProjection := by
proof · 2 lines
  funext a
  simp only [localProjection, ite_eq_right h]
830
lemma localProjection_trivial_of_not_mem (S : EightSet) (q : Question)
    (h : q∉S.1) : localProjection S q = trivialProjection := by
proof · 2 lines
  funext a
  simp only [localProjection, dite_eq_right h, ite_self]
835
lemma localProjection_commute (S : EightSet) (q r : Question) (a b : Outcome)
    (hqr : overlap q r ≠ 0) : Commute (localProjection S q a) (localProjection S r b) := by
proof · 11 lines
  by_cases he : q=r
  · subst r; exact localProjection_self_commute S q a b
  by_cases hS : PairwiseOrthogonal S
  · by_cases hq : q∈S.1
    · have hr : r∉S.1 := fun hr => hqr (hS q hq r hr he)
      rw [localProjection_trivial_of_not_mem S r hr]
      exact (trivialProjection_commute b _).symm
    · rw [localProjection_trivial_of_not_mem S q hq]
      exact trivialProjection_commute a _
  · rw [localProjection_trivial_of_not_orthogonal S q hS]
    exact trivialProjection_commute a _
849
lemma projection_commute (i j : Label) (hij : overlap (ofLex i).1 (ofLex j).1 ≠ 0) :
    Commute (projection i) (projection j) := by
proof · 6 lines
  change _ * _ = _ * _
  ext h l
  rw [projection_mul_apply, projection_mul_apply]
  apply Finset.prod_congr rfl
  intro S _
  rw [(localProjection_commute S _ _ _ _ hij).eq]
860
lemma card_subsets_containing {α : Type v1965_0} [Fintype α] [DecidableEq α]
    (a : α) (r : ℕ) (hr : 1 ≤ r)
    [Fintype {A : Finset α // A.card=r ∧ a ∈ A}] :
    Fintype.card {A : Finset α // A.card=r ∧ a ∈ A} =
      Nat.choose (Fintype.card α - 1) (r-1) := by
proof · 8 lines
  rw [Fintype.card_of_subtype
    (((Finset.univ : Finset α).powersetCard r).filter ({a} ⊆ ·)) (by
      intro A
      simp only [Finset.mem_filter, Finset.mem_powersetCard, Finset.subset_univ,
        true_and, Finset.singleton_subset_iff])]
  rw [Finset.card_filter_powersetCard_subset _ _ _ (Finset.subset_univ _)
    (by simpa only [Finset.card_singleton] using hr)]
  simp only [Finset.card_univ, Finset.card_singleton]
874
lemma card_base : Fintype.card Base = b := by
proof · 7 lines
  let : Fintype {A : Finset (Fin k) // A.card=514 ∧ (0 : Fin k) ∈ A} :=
    inferInstanceAs (Fintype Base)
  have h := card_subsets_containing (0 : Fin k) 514 (by decide)
  change Fintype.card Base = Nat.choose (Fintype.card (Fin k)-1) (514-1) at h
  rw [Fintype.card_fin] at h
  delta b
  exact h.trans (congrArg₂ Nat.choose (show k-1=1027 from rfl) (show 514-1=513 from rfl))
883
lemma card_question : Fintype.card Question = Q := by
proof · 4 lines
  rw [Fintype.card_congr (show Question ≃ (Fin 3 → Base) from Equiv.refl _)]
  rw [Fintype.card_fun, Fintype.card_fin, card_base]
  delta Q
  rfl
891
lemma signed_overlap {α : Type v1995_0} [Fintype α] [DecidableEq α] (A B : Finset α) :
    (∑ h, (if h ∈ A then (1:ℝ) else -1) * (if h ∈ B then 1 else -1)) =
      Fintype.card α - 2*(A.card:ℝ) - 2*(B.card:ℝ) + 4*(A ∩ B).card := by
proof · 10 lines
  have hp (h : α) :
      (if h ∈ A then (1:ℝ) else -1) * (if h ∈ B then 1 else -1) =
      1 - 2*(if h ∈ A then 1 else 0) - 2*(if h ∈ B then 1 else 0) +
        4*(if h ∈ A ∩ B then 1 else 0) := by
    by_cases ha : h ∈ A <;> by_cases hb : h ∈ B <;> simp [ha,hb] <;> norm_num
  simp only [hp, Finset.sum_add_distrib, Finset.sum_sub_distrib, ← Finset.mul_sum]
  simp
  congr 1
  ext x
  simp
905
lemma base_inner (A B : Base) :
    (∑ h, baseVector A h * baseVector B h) = (A.1 ∩ B.1).card / (p:ℝ) - 1 := by
proof · 6 lines
  simp only [baseVector, div_mul_div_comm, ← Finset.sum_div]
  rw [Real.mul_self_sqrt (by norm_num : (0:ℝ) ≤ k), signed_overlap]
  rw [A.2.1,B.2.1]
  simp only [Fintype.card_fin, k, p]
  push_cast
  ring
914
lemma base_unit (A : Base) : ∑ h, baseVector A h * baseVector A h = 1 := by
proof · 2 lines
  rw [base_inner, Finset.inter_self, A.2.1]
  norm_num [p]
918
lemma vector_unit (q : Question) : overlap q q = 1 := by
proof · 5 lines
  change Lex (Fin 3 → Base) at q
  unfold overlap vector
  simp only [← Finset.prod_mul_distrib]
  rw [← Fintype.prod_sum (fun l h => baseVector (ofLex q l) h * baseVector (ofLex q l) h)]
  simp only [base_unit, Finset.prod_const_one]
925
@[irreducible] def a₀ : ℕ := ∑ j ∈ Finset.range p, Nat.choose k j
929
lemma choose_mono_small {n i j : ℕ} (hij : i ≤ j) (hj : j ≤ n/2) :
    Nat.choose n i ≤ Nat.choose n j := by
proof · 4 lines
  induction hij with
  | refl => rfl
  | @step j hij ih =>
    exact (ih (by omega)).trans (Nat.choose_le_succ_of_lt_half_left (by omega))
936
lemma a₀_upper : a₀ ≤ 257 * Nat.choose 1028 257 := by
proof · 7 lines
  delta a₀
  calc
    (∑ j ∈ range 257, Nat.choose 1028 j) ≤ ∑ _j ∈ range 257, Nat.choose 1028 257 := by
      apply sum_le_sum
      intro j hj
      exact choose_mono_small (Nat.le_of_lt (mem_range.mp hj)) (by decide)
    _ = _ := by rw [sum_const, card_range, nsmul_eq_mul, Nat.cast_id]
945
lemma binomial_term_le (n j x y : ℕ) (hj : j ≤ n) :
    x^j*y^(n-j)*Nat.choose n j ≤ (x+y)^n := by
proof · 3 lines
  rw [add_pow]
  exact single_le_sum (f := fun m => x^m*y^(n-m)*Nat.choose n m)
    (fun _ _ => Nat.zero_le _) (show j ∈ range (n+1) from mem_range.mpr (by omega))
951
lemma choose_upper : Nat.choose 1028 257 < 12^257 := by
proof · 10 lines
  have ht := binomial_term_le 1028 257 1 3 (by decide)
  simp only [one_pow, one_mul, show 1028-257=771 from rfl, show 1+3=4 from rfl] at ht
  have hn : (4:ℕ)^1028 < 3^771*12^257 := by
    have hh := pow_lt_pow_left₀ (by norm_num : (4:ℕ)^4 < 3^3*12) (by positivity) (by decide : 257 ≠ 0)
    have hl : ((4:ℕ)^4)^257 = 4^1028 := by rw [← pow_mul]
    have hr : ((3:ℕ)^3*12)^257 = 3^771*12^257 :=
      by rw [mul_pow, ← pow_mul]
    exact (congrArg₂ (fun x y : ℕ => x<y) hl hr).mp hh
  have hh : 3^771 * Nat.choose 1028 257 < 3^771 * 12^257 := lt_of_le_of_lt ht hn
  exact Nat.lt_of_mul_lt_mul_left hh
963
lemma double_b : Nat.choose 1028 514 = 2*b := by
proof · 10 lines
  delta b
  have hs : Nat.choose 1027 514 = Nat.choose 1027 513 :=
    Nat.choose_symm_of_eq_add (show 1027=514+513 from rfl)
  calc
    Nat.choose 1028 514 = Nat.choose 1027 513 + Nat.choose 1027 514 :=
      (congrArg₂ Nat.choose (show 1028=Nat.succ 1027 from rfl)
        (show 514=Nat.succ 513 from rfl)).trans ((Nat.choose_succ_succ 1027 513).trans
        (congrArg (fun x => Nat.choose 1027 513 + x)
          (congrArg (Nat.choose 1027) (show Nat.succ 513=514 from rfl))))
    _ = 2 * Nat.choose 1027 513 := by rw [hs]; omega
975
lemma b_lower : 2^1028 ≤ 2058*b := by
proof · 5 lines
  have h : (∑ j ∈ range (1028+1), Nat.choose 1028 j) ≤
      ∑ _j ∈ range (1028+1), Nat.choose 1028 514 :=
    sum_le_sum (fun j _ => Nat.choose_le_middle j 1028)
  rw [Nat.sum_range_choose, sum_const, card_range, nsmul_eq_mul, double_b] at h
  simpa only [Nat.cast_id, show 1028+1=1029 from rfl, ← Nat.mul_assoc, show 1029*2=2058 from rfl] using h
982
lemma strict_parameter_bound : 3*a₀*K < b := by
proof · 18 lines
  have ha := a₀_upper
  have hc := choose_upper
  have hb := b_lower
  have ha' : a₀ < 257*12^257 := lt_of_le_of_lt ha (Nat.mul_lt_mul_of_pos_left hc (by decide))
  have hn : (2058:ℕ)*(3*(257*12^257)*1028^3) < 2^1028 := by
    calc
      (2058:ℕ)*(3*(257*12^257)*1028^3) < 16^257 := by
        rw [show 257=256+1 from rfl, pow_add, pow_add]
        norm_num
      _ = 2^1028 := by
        calc
          (16 : ℕ)^257 = ((2 : ℕ)^4)^257 := congrArg (fun value : ℕ => value^257) (show 16 = 2^4 from rfl)
          _ = 2^1028 := by rw [← pow_mul]
  delta K
  change 3*a₀*1028^3 < b
  apply Nat.lt_of_mul_lt_mul_left (a := 2058)
  exact lt_of_le_of_lt (Nat.mul_le_mul_left 2058
    (Nat.mul_le_mul_right (1028^3) (Nat.mul_le_mul_left 3 (Nat.le_of_lt ha')))) (hn.trans_le hb)
1002
lemma b_pos : 0 < b := lt_of_le_of_lt (Nat.zero_le _) strict_parameter_bound
1008
lemma card_subsets {α : Type v2431_0} [Fintype α] [DecidableEq α] (r : ℕ)
    [Fintype {S : Finset α // S.card=r}] :
    Fintype.card {S : Finset α // S.card=r} = Nat.choose (Fintype.card α) r := by
proof · 4 lines
  rw [Fintype.card_of_subtype ((Finset.univ : Finset α).powersetCard r) (by
    intro S
    simp only [Finset.mem_powersetCard, Finset.subset_univ, true_and])]
  rw [Finset.card_powersetCard, Finset.card_univ]
1016
lemma card_eightSet : Fintype.card EightSet = Nat.choose Q 8 := by
proof · 2 lines
  let : Fintype {S : Finset Question // S.card=8} := inferInstanceAs (Fintype EightSet)
  exact (card_subsets (α := Question) 8).trans (congrArg (fun n => Nat.choose n 8) card_question)
1020
lemma card_basis_aux : Fintype.card Basis = Fintype.card LocalBasis ^ Fintype.card EightSet := by
proof · 2 lines
  rw [← Nat.card_eq_fintype_card, ← Nat.card_eq_fintype_card, ← Nat.card_eq_fintype_card]
  exact (Nat.card_congr (show Basis ≃ (EightSet → LocalBasis) from Equiv.refl _)).trans Nat.card_fun
1024
lemma card_localBasis : Fintype.card LocalBasis = 4 := by
proof · 2 lines
  rw [Fintype.card_congr (show LocalBasis ≃ (Fin 2 × Fin 2) from Equiv.refl _),
    Fintype.card_prod, Fintype.card_fin]
1028
lemma card_basis : Fintype.card Basis = D := by
proof · 1 line
  exact (card_basis_aux.trans (congrArg₂ (fun a b : ℕ => a^b) card_localBasis card_eightSet)).trans D_eq.symm
1031
lemma card_outcomeString : Fintype.card OutcomeString = D :=
proof · 1 line
  (Fintype.card_congr (show OutcomeString ≃ Basis from Equiv.refl _)).trans card_basis
1034
lemma card_label : Fintype.card Label = m := by
proof · 3 lines
  rw [Fintype.card_congr (show Label ≃ (Question × OutcomeString) from Equiv.refl _)]
  rw [Fintype.card_prod, card_question, card_outcomeString]
  exact m_eq.symm
1039
def basisOrder : Fin D ≃o Basis := Fintype.orderIsoFinOfCardEq Basis card_basis
1041
def labelOrder : Fin m ≃o Label := Fintype.orderIsoFinOfCardEq Label (by
body · 62 lines
  have cardContaining {α : Type} [Fintype α] [DecidableEq α]
      (a : α) (r : ℕ) (hr : 1 ≤ r)
      [Fintype {A : Finset α // A.card = r ∧ a ∈ A}] :
      Fintype.card {A : Finset α // A.card = r ∧ a ∈ A} =
        Nat.choose (Fintype.card α - 1) (r - 1) := by
    as_aux_lemma =>
      rw [Fintype.card_of_subtype
        (((Finset.univ : Finset α).powersetCard r).filter ({a} ⊆ ·)) (by
          intro A
          simp only [Finset.mem_filter, Finset.mem_powersetCard, Finset.subset_univ,
            true_and, Finset.singleton_subset_iff])]
      rw [Finset.card_filter_powersetCard_subset _ _ _ (Finset.subset_univ _)
        (by simpa only [Finset.card_singleton] using hr)]
      simp only [Finset.card_univ, Finset.card_singleton]
  have hbase : Fintype.card Base = b := by
    as_aux_lemma =>
      let : Fintype {A : Finset (Fin k) // A.card = 514 ∧ (0 : Fin k) ∈ A} :=
        inferInstanceAs (Fintype Base)
      have h := cardContaining (0 : Fin k) 514 (by decide)
      change Fintype.card Base = Nat.choose (Fintype.card (Fin k) - 1) (514 - 1) at h
      rw [Fintype.card_fin] at h
      delta b
      exact h.trans (congrArg₂ Nat.choose (show k - 1 = 1027 from rfl)
        (show 514 - 1 = 513 from rfl))
  have hquestion : Fintype.card Question = Q := by
    calc
      Fintype.card Question = Fintype.card (Fin 3 → Base) :=
        Fintype.card_congr (show Question ≃ (Fin 3 → Base) from Equiv.refl _)
      _ = Fintype.card Base ^ Fintype.card (Fin 3) := Fintype.card_fun
      _ = b ^ 3 := congrArg₂ (fun x y : ℕ => x ^ y) hbase (Fintype.card_fin 3)
      _ = Q := Q.eq_def.symm

  have height : Fintype.card EightSet = Nat.choose Q 8 := by
    as_aux_lemma =>
      let : Fintype {S : Finset Question // S.card = 8} := inferInstanceAs (Fintype EightSet)
      change Fintype.card {S : Finset Question // S.card = 8} = _
      rw [Fintype.card_of_subtype ((Finset.univ : Finset Question).powersetCard 8) (by
        intro S
        simp only [Finset.mem_powersetCard, Finset.subset_univ, true_and])]
      rw [Finset.card_powersetCard, Finset.card_univ, hquestion]
  have hlocal : Fintype.card Outcome = 4 := by
    as_aux_lemma =>
      rw [Fintype.card_congr (show Outcome ≃ (Fin 2 × Fin 2) from Equiv.refl _),
        Fintype.card_prod, Fintype.card_fin]
  have houtcome : Fintype.card OutcomeString = D := by
    as_aux_lemma =>
      calc
        Fintype.card OutcomeString = Fintype.card Outcome ^ Fintype.card EightSet := by
          rw [Fintype.card_congr (show OutcomeString ≃ (EightSet → Outcome) from Equiv.refl _),
            Fintype.card_fun]
        _ = 4 ^ Nat.choose Q 8 := congrArg₂ (fun a b : ℕ => a ^ b) hlocal height
        _ = D := by
          have hd (q : ℕ) : dimensionFunction q = 4 ^ Nat.choose q 8 := rfl
          symm
          unfold D
          exact hd Q
  rw [Fintype.card_congr (show Label ≃ (Question × OutcomeString) from Equiv.refl _)]
  rw [Fintype.card_prod, hquestion, houtcome]
  have hm (q d : ℕ) : outputDimensionFunction q d = q * d := rfl
  symm
  unfold m
  exact hm Q D)
1105
def rhoFin : Matrix (Fin m × Fin m) (Fin m × Fin m) ℂ :=
body · 1 line
  rho.submatrix (Prod.map labelOrder labelOrder) (Prod.map labelOrder labelOrder)
1108
lemma overlap_symm (q r : Question) : overlap q r = overlap r q := by
proof · 2 lines
  unfold overlap
  exact Finset.sum_congr rfl (fun h _ => mul_comm _ _)
1112
lemma projection_transpose (i : Label) : (projection i)ᵀ = projection i := by
proof · 4 lines
  ext h l
  have hh := congrArg (fun A : Matrix Basis Basis ℂ => A h l)
    (show (projection i)ᴴ=projection i from (projection_projection i).isSelfAdjoint)
  exact (projection_real i l h).symm.trans hh
1118
lemma block_swap (i j : Label) : block i j = block j i := by
proof · 5 lines
  by_cases h : overlap (ofLex i).1 (ofLex j).1 = 0
  · simp only [block, ← overlap_symm (ofLex i).1 (ofLex j).1, h,
      Complex.ofReal_zero, zero_smul]
  · unfold block
    rw [overlap_symm, (projection_commute i j h).eq]
1125
lemma block_transpose (i j : Label) : (block i j)ᵀ = block i j := by
proof · 5 lines
  by_cases h : overlap (ofLex i).1 (ofLex j).1 = 0
  · simp only [block, h, Complex.ofReal_zero, zero_smul, Matrix.transpose_zero]
  · unfold block
    rw [Matrix.transpose_smul, Matrix.transpose_mul, projection_transpose, projection_transpose,
      (projection_commute i j h).eq]
1132
lemma block_diag (i : Label) : block i i = projection i := by
proof · 2 lines
  unfold block
  rw [vector_unit, Complex.ofReal_one, one_smul, (projection_projection i).isIdempotentElem]
1136
lemma rawOutput_trace (i j : Label × Label) :
    rawOutput i j = trace (block i.1 j.1 * block i.2 j.2) := by
proof · 2 lines
  rw [← block_transpose i.1 j.1]
  simp only [rawOutput, trace, Matrix.diag_apply, Matrix.mul_apply, Matrix.transpose_apply]
1141
lemma rawOutput_crossed_diagonal (i j : Label) :
    rawOutput (i,j) (i,j) = trace (projection i * projection j) := by
proof · 1 line
  rw [rawOutput_trace, block_diag, block_diag]
1145
def makeLabel (q : Question) (a : OutcomeString) : Label := toLex (q,a)
1147
lemma projection_sum_makeLabel (q : Question) : ∑ a, projection (makeLabel q a) = 1 := projection_sum q
1151
lemma sum_label {M : Type v2545_0} [AddCommMonoid M] (f : Label → M) :
    ∑ i, f i = ∑ q : Question, ∑ a : OutcomeString, f (makeLabel q a) := by
proof · 2 lines
  exact (Equiv.sum_comp (show (Question × OutcomeString) ≃ Label from Equiv.refl _) f).symm.trans
    (Fintype.sum_prod_type _)
1156
lemma sum_projection_pair_trace (q r : Question) :
    (∑ a : OutcomeString, ∑ b : OutcomeString,
      trace (projection (makeLabel q a) * projection (makeLabel r b))) = (D : ℂ) := by
proof · 6 lines
  have h : (∑ a : OutcomeString, ∑ b : OutcomeString,
      projection (makeLabel q a) * projection (makeLabel r b)) = 1 := by
    simp_rw [← Finset.mul_sum, projection_sum_makeLabel, mul_one]
    exact projection_sum_makeLabel q
  have hh := congrArg Matrix.trace h
  simpa only [trace_sum, trace_one, card_basis] using hh
1166
lemma rawOutput_trace_total : trace rawOutput = (Q^2*D : ℕ) := by
proof · 10 lines
  change (∑ i : Label × Label, rawOutput i i) = _
  rw [Fintype.sum_prod_type]
  simp_rw [rawOutput_crossed_diagonal, sum_label]
  conv_lhs =>
    arg 2; ext q
    rw [Finset.sum_comm]
  simp_rw [sum_projection_pair_trace]
  simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul, card_question,
    Nat.cast_mul, Nat.cast_pow]
  ring
1186
def Separable {ι : Type v2604_0} {κ : Type v2604_1} [Fintype ι] [Fintype κ]
    (R : Matrix (ι × κ) (ι × κ) ℂ) : Prop :=
body · 2 lines
  ∃ (n : ℕ) (A : Fin n → Matrix ι ι ℂ) (B : Fin n → Matrix κ κ ℂ),
    (∀ s, (A s).PosSemidef) ∧ (∀ s, (B s).PosSemidef) ∧ R = ∑ s, A s ⊗ₖ B s
1195
lemma Q_pos : 0 < Q := by unfold Q; exact pow_pos b_pos 3
1197
lemma D_pos : 0 < D := by rw [D_eq]; exact pow_pos (by decide) _
1211
def projectionKraus (u : β → γ → ℝ) (P : β → Matrix α α ℂ) :
    (γ × α) → Matrix β α ℂ := fun s i a => (u i s.1 : ℂ) * P i s.2 a
1215
lemma projectionKraus_coeff [Fintype β] (u : β → γ → ℝ) (P : β → Matrix α α ℂ)
    (hP : ∀ i, (P i).IsHermitian) (i j : β) (a b : α) :
    (∑ s, projectionKraus u P s i a * star (projectionKraus u P s j b)) =
      (∑ h, (u i h : ℂ) * (u j h : ℂ)) * (P j * P i) b a := by
proof · 10 lines
  simp only [projectionKraus, Fintype.sum_prod_type, star_mul, Complex.star_def,
    Complex.conj_ofReal]
  have hp (l : α) : star (P j l b) = P j b l := congrArg (fun A : Matrix α α ℂ => A b l) (hP j).eq
  simp only [← Complex.star_def, hp, Matrix.mul_apply, Finset.sum_mul, Finset.mul_sum]
  rw [Finset.sum_comm]
  apply Finset.sum_congr rfl
  intro h _
  apply Finset.sum_congr rfl
  intro l _
  ring
1230
lemma projectionKraus_formula (u : β → γ → ℝ) (P : β → Matrix α α ℂ)
    (hP : ∀ i, (P i).IsHermitian) (U : Matrix α α ℂ) (i j : β) :
    (∑ s, projectionKraus u P s * U * (projectionKraus u P s)ᴴ) i j =
      trace (((∑ h, (u i h : ℂ) * (u j h : ℂ)) • (P j * P i)) * U) := by
proof · 17 lines
  simp only [Matrix.sum_apply, Matrix.mul_apply, Matrix.conjTranspose_apply]
  simp only [Finset.sum_mul]
  calc
    _ = ∑ b, ∑ a, (∑ s, projectionKraus u P s i a * star (projectionKraus u P s j b)) * U a b := by
      simp only [Finset.sum_mul]
      rw [Finset.sum_comm]
      apply Finset.sum_congr rfl
      intro b _
      rw [Finset.sum_comm]
      apply Finset.sum_congr rfl
      intro a _
      apply Finset.sum_congr rfl
      intro s _
      ring
    _ = _ := by
      simp only [projectionKraus_coeff u P hP, trace, Matrix.diag_apply,
        Matrix.mul_apply, Matrix.smul_apply, smul_eq_mul]
1256
def questionFin (i : Fin m) : Question := (ofLex (labelOrder i)).1
1258
def projectionFin (i : Fin m) : Mat D := (projection (labelOrder i)).submatrix basisOrder basisOrder
1260
def blockFin (i j : Fin m) : Mat D := (block (labelOrder i) (labelOrder j)).submatrix basisOrder basisOrder
1262
def constructionMapFin : Mat D →ₗ[ℂ] Mat m := traceBlockMap blockFin
1264
lemma projectionFin_hermitian (i : Fin m) : (projectionFin i).IsHermitian :=
proof · 2 lines
  (show (projection (labelOrder i)).IsHermitian from
    (projection_projection (labelOrder i)).isSelfAdjoint).submatrix _
1268
lemma blockFin_swap (i j : Fin m) : blockFin i j = blockFin j i := by
proof · 2 lines
  unfold blockFin
  rw [block_swap]
1272
lemma blockFin_formula (i j : Fin m) : blockFin i j =
    (overlap (questionFin i) (questionFin j) : ℂ) •
      (projectionFin i * projectionFin j) := by
proof · 5 lines
  unfold blockFin block projectionFin
  change _ = (overlap (questionFin i) (questionFin j) : ℂ) • _
  have hh := (Matrix.submatrix_mul_equiv (projection (labelOrder i)) (projection (labelOrder j))
    basisOrder basisOrder.toEquiv basisOrder).symm
  exact congrArg (fun A : Mat D => (overlap (questionFin i) (questionFin j) : ℂ) • A) hh
1281
lemma constructionMapFin_kraus (U : Mat D) : constructionMapFin U =
    ∑ s, projectionKraus (fun i => vector (questionFin i)) projectionFin s * U *
      (projectionKraus (fun i => vector (questionFin i)) projectionFin s)ᴴ := by
proof · 10 lines
  ext i j
  change trace (blockFin i j * U) = _
  rw [projectionKraus_formula _ _ projectionFin_hermitian]
  rw [blockFin_swap i j, blockFin_formula]
  have hc : (overlap (questionFin j) (questionFin i) : ℂ) =
      ∑ h, (vector (questionFin i) h : ℂ) * (vector (questionFin j) h : ℂ) := by
    rw [overlap_symm]
    simp only [← Complex.ofReal_mul, ← Complex.ofReal_sum]
    rfl
  rw [hc]
1295
lemma constructionMapFin_cp : CompletelyPositive constructionMapFin :=
proof · 1 line
  cp_of_kraus_representation _ _ constructionMapFin_kraus
1298
lemma constructionMapFin_unit (x y : Fin D) (i j : Fin m) :
    constructionMapFin (matrixUnit x y) i j = blockFin i j y x := by
proof · 2 lines
  change trace (blockFin i j * matrixUnit x y) = _
  simp [trace, Matrix.diag_apply, Matrix.mul_apply, matrixUnit, ite_and]
1303
def omegaVector : Fin D × Fin D → ℂ := fun i => if i.1=i.2 then 1 else 0
1305
def rawOutputFin : Matrix (Fin m × Fin m) (Fin m × Fin m) ℂ :=
body · 1 line
  rawOutput.submatrix (Prod.map labelOrder labelOrder) (Prod.map labelOrder labelOrder)
1308
lemma rawOutputFin_tensor : rawOutputFin =
    tensorMap constructionMapFin constructionMapFin (outer omegaVector omegaVector) := by
proof · 13 lines
  ext ⟨i,a⟩ ⟨j,b⟩
  change rawOutput (labelOrder i, labelOrder a) (labelOrder j, labelOrder b) = _
  simp only [tensorMap, outer, omegaVector, constructionMapFin_unit, apply_ite,
    star_one, star_zero, ite_mul, one_mul, zero_mul, mul_zero]
  simp only [Finset.sum_ite_eq, Finset.mem_univ, ite_true]
  change (∑ h, ∑ l, block (labelOrder i) (labelOrder j) l h *
    block (labelOrder a) (labelOrder b) l h) =
    ∑ h : Fin D, ∑ l : Fin D, block (labelOrder i) (labelOrder j) (basisOrder l) (basisOrder h) *
    block (labelOrder a) (labelOrder b) (basisOrder l) (basisOrder h)
  rw [← Equiv.sum_comp basisOrder.toEquiv]
  apply Finset.sum_congr rfl
  intro h _
  exact (Equiv.sum_comp basisOrder.toEquiv _).symm
1324
lemma rawOutputFin_psd : rawOutputFin.PosSemidef := by
proof · 2 lines
  rw [rawOutputFin_tensor]
  exact tensorMap_positive constructionMapFin_cp constructionMapFin_cp (outer_posSemidef _)
1328
lemma rawOutputFin_trace : trace rawOutputFin = ((Q^2*D : ℕ) : ℂ) := by
proof · 4 lines
  rw [← rawOutput_trace_total]
  unfold trace
  exact Equiv.sum_comp (labelOrder.toEquiv.prodCongr labelOrder.toEquiv)
    (fun x : Label × Label => rawOutput x x)
1334
lemma rhoFin_eq : rhoFin = ((Q^2*D : ℕ) : ℂ)⁻¹ • rawOutputFin := rfl
1336
lemma symbolic_normalization_ne_zero (q d : ℕ) (hq : 0 < q) (hd : 0 < d) :
    ((q^2*d : ℕ) : ℂ) ≠ 0 := by
proof · 1 line
  exact Nat.cast_ne_zero.mpr (mul_pos (pow_pos hq 2) hd).ne'
1340
lemma normalization_ne_zero : ((Q^2*D : ℕ) : ℂ) ≠ 0 :=
proof · 1 line
  symbolic_normalization_ne_zero Q D Q_pos D_pos
1343
lemma rhoFin_density : Density rhoFin := by
proof · 4 lines
  rw [rhoFin_eq]
  constructor
  · exact rawOutputFin_psd.smul (inv_nonneg.mpr (Nat.cast_nonneg _))
  · rw [trace_smul, rawOutputFin_trace, smul_eq_mul, inv_mul_cancel₀ normalization_ne_zero]
1368
abbrev ReferenceFunctional (A : Type v3101_0) [AddCommGroup A] [Module ℂ A]
    (ι : Type v3101_1) := A →ₗ[ℂ] Matrix ι ι ℂ
1371
def PositiveFunctional [LE A] (S : ReferenceFunctional A ι) : Prop :=
body · 1 line
  ∀ x, 0 ≤ x → (S x).PosSemidef
1376
structure ObservableInstrument (O : Type v3140_0) [MeasurableSpace O]
    (A : Type v3140_1) (B : Type v3140_2) [AddCommGroup A] [Module ℂ A] [One A] [LE A]
    [AddCommGroup B] [Module ℂ B] [One B] [LE B] where
body · 3 lines
  operation : Set O → B →ₗ[ℂ] A
  positive : ∀ E, MeasurableSet E → ∀ x, 0 ≤ x → 0 ≤ operation E x
  total_unit : operation Set.univ 1 = 1
1388
structure InstrumentRun where
body · 8 lines
  law : Measure O
  probability : IsProbabilityMeasure law
  posterior : O → ReferenceFunctional B ι
  posterior_positive : ∀ o, PositiveFunctional (posterior o)
  posterior_trace : ∀ o, trace (posterior o 1) = 1
  integrable : ∀ x i j, Integrable (fun o => posterior o x i j) law
  update : ∀ E, MeasurableSet E → ∀ x i j,
    (∫ o in E, posterior o x i j ∂law) = S (J.operation E x) i j
1403
theorem reference_mean [DecidableEq ι] (i j : ι) :
    (∫ o, r.posterior o 1 i j ∂r.law) = S 1 i j := by
proof · 2 lines
  simpa only [Measure.restrict_univ, J.total_unit] using
    r.update Set.univ MeasurableSet.univ 1 i j
1424
theorem sample_instrument_kernel {S : Type v3216_0} {O : Type v3216_1} [MeasurableSpace S]
    [MeasurableSpace O] [StandardBorelSpace O] [Nonempty O]
    (κ : Kernel S O) [IsMarkovKernel κ] :
    ∃ f : S → I → O, Measurable (uncurry f) ∧
      (∀ s, volume.map (f s) = κ s) ∧
      ∀ (μ : Measure S) [IsFiniteMeasure μ],
        (μ.prod (volume : Measure I)).map (fun p => (p.1, f p.1 p.2)) = μ ⊗ₘ κ := by
proof · 12 lines
  obtain ⟨f,hf,hfLaw⟩ := κ.exists_measurable_map_eq_unitInterval
  change Measurable (fun p : S × I => f p.1 p.2) at hf
  refine ⟨f,hf,hfLaw,?_⟩
  intro μ hμ
  ext B hB
  rw [Measure.map_apply (measurable_fst.prodMk hf) hB,
    Measure.prod_apply (hB.preimage (measurable_fst.prodMk hf)),
    Measure.compProd_apply hB]
  congr 1
  funext s
  rw [← hfLaw s, Measure.map_apply hf.of_uncurry_left (measurable_prodMk_left hB)]
  rfl
1444
abbrev UniformTape := ℕ → I
1446
def uniformTapeLaw : Measure UniformTape :=
body · 1 line
  Measure.infinitePi (fun _ : ℕ => (volume : Measure I))
1449
instance : IsProbabilityMeasure uniformTapeLaw := by
body · 2 lines
  unfold uniformTapeLaw
  infer_instance
1466
lemma indicator_one_mul (s : Set Ω) (f : Ω → ℝ) :
    s.indicator (fun _ => (1:ℝ)) * f = s.indicator f := by
proof · 2 lines
  ext x
  by_cases hx : x∈s <;> simp [hx]
1471
lemma indicator_inter_one (s t : Set Ω) :
    (s∩t).indicator (fun _ => (1:ℝ)) =
      s.indicator (t.indicator (fun _ => (1:ℝ))) := by
proof · 2 lines
  ext x
  by_cases hs : x∈s <;> by_cases ht : x∈t <;> simp [hs,ht]
1477
lemma condIndep_condExp_right {C A B : MeasurableSpace Ω}
    (hC : C ≤ mΩ) (hA : A ≤ mΩ) (hB : B ≤ mΩ) (hCA : C ≤ A)
    (h : CondIndep C A B hC μ) {t : Set Ω} (ht : MeasurableSet[B] t) :
    μ[t.indicator (fun _ => (1:ℝ)) | A] =ᵐ[μ]
      μ[t.indicator (fun _ => (1:ℝ)) | C] := by
proof · 29 lines
  let f := t.indicator (fun _ : Ω => (1:ℝ))
  have hf : Integrable f μ := (integrable_const (1:ℝ)).indicator (hB t ht)
  symm
  apply ae_eq_condExp_of_forall_setIntegral_eq hA hf
  · intro s hs hμs
    exact integrable_condExp.integrableOn
  · intro s hs hμs
    have hs' : MeasurableSet[mΩ] s := hA s hs
    have hsi : Integrable (s.indicator (fun _ : Ω => (1:ℝ))) μ :=
      (integrable_const (1:ℝ)).indicator hs'
    have hprod : Integrable (s.indicator (fun _ : Ω => (1:ℝ)) * μ[f | C]) μ := by
      rw [indicator_one_mul]
      exact integrable_condExp.indicator hs'
    have hp := condExp_mul_of_stronglyMeasurable_right
      (stronglyMeasurable_condExp (μ := μ) (m := C) (f := f)) hprod hsi
    rw [indicator_one_mul] at hp
    have hh := ((condIndep_iff C A B hC hA hB μ).mp h) s t hs ht
    change μ[(s∩t).indicator (fun _ => (1:ℝ)) | C] =ᵐ[μ]
      μ[s.indicator (fun _ => (1:ℝ)) | C] * μ[f | C] at hh
    calc
      (∫ x in s, μ[f | C] x ∂μ) = ∫ x, s.indicator (μ[f | C]) x ∂μ :=
        (integral_indicator hs').symm
      _ = ∫ x, μ[s.indicator (μ[f | C]) | C] x ∂μ := (integral_condExp hC).symm
      _ = ∫ x, μ[(s∩t).indicator (fun _ => (1:ℝ)) | C] x ∂μ :=
        integral_congr_ae (hp.trans hh.symm)
      _ = ∫ x, (s∩t).indicator (fun _ => (1:ℝ)) x ∂μ := integral_condExp hC
      _ = ∫ x in s, f x ∂μ := by
        rw [indicator_inter_one, integral_indicator hs']
  · exact (stronglyMeasurable_condExp.mono hCA).aestronglyMeasurable
1512
lemma condIndep_local_refinement {C N A B : MeasurableSpace Ω}
    (hC : C ≤ mΩ) (hA : A ≤ mΩ) (hB : B ≤ mΩ)
    (hCA : C ≤ A) (hCN : C ≤ N) (hNA : N ≤ A)
    (h : CondIndep C A B hC μ) :
    CondIndep N A B (hNA.trans hA) μ := by
proof · 35 lines
  have hN := hNA.trans hA
  rw [condIndep_iff N A B hN hA hB]
  intro s t hs ht
  let f := t.indicator (fun _ : Ω => (1:ℝ))
  have hf : Integrable f μ := (integrable_const (1:ℝ)).indicator (hB t ht)
  have hp : μ[f | A] =ᵐ[μ] μ[f | C] := condIndep_condExp_right hC hA hB hCA h ht
  have hcN : μ[μ[f | C] | N] = μ[f | C] :=
    condExp_of_stronglyMeasurable hN (stronglyMeasurable_condExp.mono hCN) integrable_condExp
  have htN : μ[f | N] =ᵐ[μ] μ[f | C] := by
    calc
      _ =ᵐ[μ] μ[μ[f | A] | N] := (condExp_condExp_of_le hNA hA).symm
      _ =ᵐ[μ] μ[μ[f | C] | N] := condExp_congr_ae hp
      _ =ᵐ[μ] μ[f | C] := by rw [hcN]
  have hsep : μ[s.indicator f | A] =ᵐ[μ] s.indicator (μ[f | C]) := by
    apply (condExp_indicator hf hs).trans
    filter_upwards [hp] with x hx
    by_cases hxs : x∈s <;> simp [hxs,hx]
  have hsi : Integrable (s.indicator (fun _ : Ω => (1:ℝ))) μ :=
    (integrable_const (1:ℝ)).indicator (hA s hs)
  have hprod : Integrable (s.indicator (fun _ : Ω => (1:ℝ)) * μ[f | C]) μ := by
    rw [indicator_one_mul]
    exact integrable_condExp.indicator (hA s hs)
  have hpull := condExp_mul_of_stronglyMeasurable_right
    (stronglyMeasurable_condExp.mono hCN) hprod hsi
  rw [indicator_one_mul] at hpull
  change μ[(s∩t).indicator (fun _ => (1:ℝ)) | N] =ᵐ[μ]
    μ[s.indicator (fun _ => (1:ℝ)) | N] * μ[f | N]
  rw [indicator_inter_one]
  calc
    _ =ᵐ[μ] μ[μ[s.indicator f | A] | N] := (condExp_condExp_of_le hNA hA).symm
    _ =ᵐ[μ] μ[s.indicator (μ[f | C]) | N] := condExp_congr_ae hsep
    _ =ᵐ[μ] μ[s.indicator (fun _ => (1:ℝ)) | N] * μ[f | C] := hpull
    _ =ᵐ[μ] _ := by
      filter_upwards [htN] with x hx
      simp only [Pi.mul_apply, hx]
1553
def recordInterFamily (B C : MeasurableSpace Ω) : Set (Set Ω) :=
body · 1 line
  {s | ∃ b n, MeasurableSet[B] b ∧ MeasurableSet[C] n ∧ s = b∩n}
1556
lemma recordInterFamily_pi (B C : MeasurableSpace Ω) : IsPiSystem (recordInterFamily B C) := by
proof · 5 lines
  rintro s ⟨b,n,hb,hn,rfl⟩ t ⟨b',n',hb',hn',rfl⟩ _
  refine ⟨b∩b',n∩n',hb.inter hb',hn.inter hn',?_⟩
  ext x
  simp only [Set.mem_inter_iff]
  tauto
1563
lemma recordInterFamily_generate (B C : MeasurableSpace Ω) :
    MeasurableSpace.generateFrom (recordInterFamily B C) = B ⊔ C := by
proof · 12 lines
  apply le_antisymm
  · apply MeasurableSpace.generateFrom_le
    rintro s ⟨b,n,hb,hn,rfl⟩
    exact ((show B ≤ B ⊔ C from le_sup_left) b hb).inter
      ((show C ≤ B ⊔ C from le_sup_right) n hn)
  · apply sup_le
    · intro s hs
      apply MeasurableSpace.measurableSet_generateFrom
      exact ⟨s,Set.univ,hs,MeasurableSet.univ,(Set.inter_univ s).symm⟩
    · intro s hs
      apply MeasurableSpace.measurableSet_generateFrom
      exact ⟨Set.univ,s,MeasurableSet.univ,hs,(Set.univ_inter s).symm⟩
1578
lemma condIndep_join_public_right {C A B : MeasurableSpace Ω}
    (hC : C ≤ mΩ) (hA : A ≤ mΩ) (hB : B ≤ mΩ)
    (h : CondIndep C A B hC μ) : CondIndep C A (B ⊔ C) hC μ := by
proof · 39 lines
  let p : Set (Set Ω) := {s | MeasurableSet[A] s}
  have hp : IsPiSystem p := by
    intro s hs t ht _
    exact hs.inter ht
  have hpg : A = MeasurableSpace.generateFrom p :=
    (@MeasurableSpace.generateFrom_measurableSet Ω A).symm
  have hpM : ∀ s ∈ p, MeasurableSet[mΩ] s := hA
  have hrM : ∀ s ∈ recordInterFamily B C, MeasurableSet[mΩ] s := by
    rintro s ⟨b,n,hb,hn,rfl⟩
    exact (hB b hb).inter (hC n hn)
  apply CondIndepSets.condIndep hA (sup_le hB hC) hp (recordInterFamily_pi B C)
    hpg (recordInterFamily_generate B C).symm
  rw [condIndepSets_iff C hC p (recordInterFamily B C) hpM hrM μ]
  rintro s _ hs ⟨b,n,hb,hn,rfl⟩
  have hsb : Integrable ((s∩b).indicator (fun _ : Ω => (1:ℝ))) μ :=
    (integrable_const (1:ℝ)).indicator ((hA s hs).inter (hB b hb))
  have hbI : Integrable (b.indicator (fun _ : Ω => (1:ℝ))) μ :=
    (integrable_const (1:ℝ)).indicator (hB b hb)
  have hleft := condExp_indicator hsb hn
  have hright := condExp_indicator hbI hn
  have he1 : (s∩(b∩n)).indicator (fun _ : Ω => (1:ℝ)) =
      n.indicator ((s∩b).indicator (fun _ : Ω => (1:ℝ))) := by
    ext x
    by_cases hx : x∈n <;> by_cases hsx : x∈s <;> by_cases hbx : x∈b <;> simp [hx,hsx,hbx]
  have he2 : (b∩n).indicator (fun _ : Ω => (1:ℝ)) =
      n.indicator (b.indicator (fun _ : Ω => (1:ℝ))) := by
    ext x
    by_cases hx : x∈n <;> by_cases hbx : x∈b <;> simp [hx,hbx]
  have hfactor := (condIndep_iff C A B hC hA hB μ).mp h s b hs hb
  change μ[(s∩(b∩n)).indicator (fun _ => (1:ℝ)) | C] =ᵐ[μ]
    μ[s.indicator (fun _ => (1:ℝ)) | C] * μ[(b∩n).indicator (fun _ => (1:ℝ)) | C]
  rw [he1,he2]
  filter_upwards [hleft,hright,hfactor] with x hx hy hz
  change μ[n.indicator ((s∩b).indicator (fun _ => (1:ℝ))) | C] x =
    μ[s.indicator (fun _ => (1:ℝ)) | C] x *
      μ[n.indicator (b.indicator (fun _ => (1:ℝ))) | C] x
  rw [hx,hy]
  by_cases hxn : x∈n <;> simp [hxn,Pi.mul_apply] at hz ⊢
  exact hz
1621
lemma condIndep_local_message {C N A B : MeasurableSpace Ω}
    (hC : C ≤ mΩ) (hA : A ≤ mΩ) (hB : B ≤ mΩ) (hCN : C ≤ N)
    (hNA : N ≤ A ⊔ C) (h : CondIndep C A B hC μ) :
    CondIndep N A B (hNA.trans (sup_le hA hC)) μ := by
proof · 5 lines
  have hjoin : CondIndep C (A ⊔ C) B hC μ :=
    (condIndep_join_public_right hC hB hA h.symm).symm
  have href := condIndep_local_refinement hC (sup_le hA hC) hB
    le_sup_right hCN hNA hjoin
  exact condIndep_of_condIndep_of_le_left href le_sup_left
1635
lemma condFactor_full_transcript {Ω : Type v3643_0} {mΩ : MeasurableSpace Ω} {μ : Measure Ω}
    [inst3643_0 : IsFiniteMeasure μ]
    (ℱ : Filtration ℕ mΩ) (f g : Ω → ℝ)
    (h : ∀ n, μ[f*g | ℱ n] =ᵐ[μ] μ[f | ℱ n] * μ[g | ℱ n]) :
    μ[f*g | ⨆ n, ℱ n] =ᵐ[μ] μ[f | ⨆ n, ℱ n] * μ[g | ⨆ n, ℱ n] := by
proof · 5 lines
  have he : ∀ᵐ x ∂μ, ∀ n, μ[f*g | ℱ n] x = μ[f | ℱ n] x * μ[g | ℱ n] x :=
    ae_all_iff.mpr h
  filter_upwards [he, tendsto_ae_condExp (ℱ := ℱ) (f*g),
    tendsto_ae_condExp (ℱ := ℱ) f, tendsto_ae_condExp (ℱ := ℱ) g] with x hx hfg hf hg
  exact tendsto_nhds_unique hfg ((hf.mul hg).congr (fun n => (hx n).symm))
1646
lemma condIndep_full_transcript {Ω : Type v3643_0} {mΩ : MeasurableSpace Ω} {μ : Measure Ω}
    [inst3643_0 : IsFiniteMeasure μ]
    [StandardBorelSpace Ω]
    (ℱ : Filtration ℕ mΩ) (mA mB : MeasurableSpace Ω) (hA : mA ≤ mΩ) (hB : mB ≤ mΩ)
    (h : ∀ n, CondIndep (ℱ n) mA mB (ℱ.le n) μ) :
    CondIndep (⨆ n, ℱ n) mA mB (iSup_le fun n => ℱ.le n) μ := by
proof · 9 lines
  rw [condIndep_iff _ _ _ _ hA hB]
  intro s t hs ht
  have hh (n : ℕ) := (condIndep_iff _ _ _ _ hA hB μ).mp (h n) s t hs ht
  have he : (s ∩ t).indicator (fun _ : Ω => (1:ℝ)) =
      s.indicator (fun _ : Ω => (1:ℝ)) * t.indicator (fun _ : Ω => (1:ℝ)) := by
    ext x
    by_cases hxs : x∈s <;> by_cases hxt : x∈t <;> simp [hxs,hxt]
  simpa only [he] using condFactor_full_transcript ℱ (s.indicator (fun _ => (1:ℝ)))
    (t.indicator (fun _ => (1:ℝ))) (by simpa only [he] using hh)
1668
lemma indep_condIndep_bot {Ω : Type v3698_0} {mΩ : MeasurableSpace Ω}
    [StandardBorelSpace Ω] {P : Measure Ω} [IsProbabilityMeasure P]
    {A B : MeasurableSpace Ω} (hA : A ≤ mΩ) (hB : B ≤ mΩ) (h : Indep A B P) :
    CondIndep ⊥ A B bot_le P := by
proof · 8 lines
  rw [condIndep_iff _ _ _ _ hA hB]
  intro s t hs ht
  have hmul := (h.indepSet_of_measurableSet hs ht).measure_inter_eq_mul
  simp only [condExp_bot, integral_indicator (hA s hs), integral_indicator (hB t ht),
    integral_indicator ((hA s hs).inter (hB t ht)), integral_const,
    smul_eq_mul, mul_one]
  exact Filter.Eventually.of_forall fun _ => by
    simpa only [Measure.real, Measure.restrict_apply_univ, Pi.mul_apply, ENNReal.toReal_mul] using congrArg ENNReal.toReal hmul
1683
structure LocalTapeProgram (U : Type v3715_0) (V : Type v3715_1) (M : Type v3715_2) [MeasurableSpace U]
    [MeasurableSpace V] [MeasurableSpace M] where
body · 5 lines
  alice : ℕ → Bool
  sendA : (n : ℕ) → U × (Fin n → M) → M
  sendB : (n : ℕ) → V × (Fin n → M) → M
  measA : ∀ n, Measurable (sendA n)
  measB : ∀ n, Measurable (sendB n)
1698
def publicPrefix : (n : ℕ) → U × V → (Fin n → M)
body · 3 lines
  | 0, _ => Fin.elim0
  | n+1, ω => Fin.snoc (publicPrefix n ω)
      (if p.alice n then p.sendA n (ω.1,publicPrefix n ω) else p.sendB n (ω.2,publicPrefix n ω))
1711
structure LocalOutcomeSampler (O : Type v3943_0) (M : Type v3943_1) [MeasurableSpace O] [MeasurableSpace M] where
body · 9 lines
  alice : ℕ → Bool
  sampleA : (n : ℕ) → ((Fin n → (O ⊕ Unit)) × (Fin n → M)) × I → O
  sampleB : (n : ℕ) → ((Fin n → (O ⊕ Unit)) × (Fin n → M)) × I → O
  sendA : (n : ℕ) → ((Fin n → (O ⊕ Unit)) × (Fin n → M)) × O → M
  sendB : (n : ℕ) → ((Fin n → (O ⊕ Unit)) × (Fin n → M)) × O → M
  sampleA_meas : ∀ n, Measurable (sampleA n)
  sampleB_meas : ∀ n, Measurable (sampleB n)
  sendA_meas : ∀ n, Measurable (sendA n)
  sendB_meas : ∀ n, Measurable (sendB n)
1729
def privatePrefix (a : Bool) : (n : ℕ) → UniformTape × (Fin n → M) → (Fin n → (O ⊕ Unit))
body · 7 lines
  | 0, _ => Fin.elim0
  | n+1, pair =>
    let old := privatePrefix a n (pair.1,fun i => pair.2 i.castSucc)
    let pubPast := fun i : Fin n => pair.2 i.castSucc
    Fin.snoc old (if p.alice n = a then Sum.inl
      (if a then p.sampleA n ((old,pubPast),pair.1 n)
       else p.sampleB n ((old,pubPast),pair.1 n)) else Sum.inr ())
1738
def extractPrivate (a : Bool) (n : ℕ) (x : Fin n → O) : Fin n → (O ⊕ Unit) :=
body · 1 line
  fun i => if p.alice i = a then Sum.inl (x i) else Sum.inr ()
1741
def publicFrom : (n : ℕ) → (Fin n → O) → (Fin n → M)
body · 7 lines
  | 0, _ => Fin.elim0
  | n+1, x =>
    let old := fun i : Fin n => x i.castSucc
    let pubPast := publicFrom n old
    Fin.snoc pubPast (if p.alice n then
      p.sendA n ((p.extractPrivate true n old,pubPast),x (Fin.last n)) else
      p.sendB n ((p.extractPrivate false n old,pubPast),x (Fin.last n)))
1763
instance matrixMeasurableSpace {m : Type v4350_0} {n : Type v4350_1} [Fintype m] [Fintype n] :
    MeasurableSpace (Matrix m n ℂ) := borel _
1769
instance matrixBorelSpace {m : Type v4353_0} {n : Type v4353_1} [Fintype m] [Fintype n] :
    BorelSpace (Matrix m n ℂ) := ⟨rfl⟩
1778
structure PositiveMatrixMeasure (Ω : Type v4361_0) (ι : Type v4361_1) [MeasurableSpace Ω] [Fintype ι] where
body · 2 lines
  entry : ι → ι → ComplexMeasure Ω
  positive : ∀ S, MeasurableSet S → Matrix.PosSemidef (fun i j => entry i j S)
1784
def value (W : PositiveMatrixMeasure Ω ι) (S : Set Ω) : Matrix ι ι ℂ := fun i j => W.entry i j S
1786
def density (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) : Ω → Matrix ι ι ℂ :=
body · 1 line
  fun t i j => (W.entry i j).rnDeriv μ t
1790
lemma measurable_density [DecidableEq ι] (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) :
    Measurable (W.density μ) := by
proof · 12 lines
  have hh : Measurable (fun t i j => (W.entry i j).rnDeriv μ t) := by
    apply Measurable.of_eval; intro i
    apply Measurable.of_eval; intro j
    unfold ComplexMeasure.rnDeriv
    simp only [Complex.mk_eq_add_mul_I]
    apply Measurable.add
    · exact Complex.measurable_ofReal.comp ((W.entry i j).re.measurable_rnDeriv μ)
    · exact (Complex.measurable_ofReal.comp ((W.entry i j).im.measurable_rnDeriv μ)).mul_const Complex.I

  have hc : Continuous (Matrix.of : (ι → ι → ℂ) → Matrix ι ι ℂ) := by
    exact continuous_matrix (fun i j => continuous_apply_apply i j)
  exact hc.measurable.comp hh
1806
lemma integrable_entry [DecidableEq ι] (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) (i j : ι) :
    Integrable (fun t => W.density μ t i j) μ := (W.entry i j).integrable_rnDeriv μ
1810
lemma entry_absolutelyContinuous [DecidableEq ι] (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω)
    (hAC : ∀ S, MeasurableSet S → μ S = 0 → W.value S = 0) (i j : ι) :
    W.entry i j ≪ᵥ μ.toENNRealVectorMeasure := by
proof · 5 lines
  intro S hS
  by_cases hs : MeasurableSet S
  · have hm : μ S = 0 := by simpa [Measure.toENNRealVectorMeasure_apply,hs] using hS
    exact congrArg (fun M : Matrix ι ι ℂ => M i j) (hAC S hs hm)
  · exact (W.entry i j).not_measurable hs
1819
lemma integral_density_entry (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) [SigmaFinite μ]
    (hAC : ∀ S, MeasurableSet S → μ S = 0 → W.value S = 0)
    (S : Set Ω) (hS : MeasurableSet S) (i j : ι) :
    (∫ t in S, W.density μ t i j ∂μ) = W.value S i j := by
proof · 15 lines
  have h := (ComplexMeasure.absolutelyContinuous_ennreal_iff _ _).mp
    (W.entry_absolutelyContinuous μ hAC i j)
  apply Complex.ext
  · have hh := congrArg (fun s : SignedMeasure Ω => s S)
      (SignedMeasure.withDensityᵥ_rnDeriv_eq (W.entry i j).re μ h.1)
    rw [withDensityᵥ_apply (SignedMeasure.integrable_rnDeriv _ _) hS] at hh
    change RCLike.re (∫ t in S, W.density μ t i j ∂μ) = _
    rw [← integral_re ((W.integrable_entry μ i j).integrableOn)]
    exact hh
  · have hh := congrArg (fun s : SignedMeasure Ω => s S)
      (SignedMeasure.withDensityᵥ_rnDeriv_eq (W.entry i j).im μ h.2)
    rw [withDensityᵥ_apply (SignedMeasure.integrable_rnDeriv _ _) hS] at hh
    change RCLike.im (∫ t in S, W.density μ t i j ∂μ) = _
    rw [← integral_im ((W.integrable_entry μ i j).integrableOn)]
    exact hh
1841
lemma ae_complex_nonneg_of_integral_nonneg {μ : Measure Ω} {f : Ω → ℂ}
    (hf : Integrable f μ)
    (h : ∀ S, MeasurableSet S → μ S < ⊤ → 0 ≤ ∫ t in S, f t ∂μ) :
    ∀ᵐ t ∂μ, 0 ≤ f t := by
proof · 13 lines
  have hre : ∀ᵐ t ∂μ, 0 ≤ (f t).re := by
    apply ae_nonneg_of_forall_setIntegral_nonneg hf.re
    intro S hs hm
    rw [integral_re hf.integrableOn]
    exact (RCLike.nonneg_iff.mp (h S hs hm)).1
  have him : (fun t => (f t).im) =ᵐ[μ] fun _ => (0:ℝ) := by
    apply Integrable.ae_eq_of_forall_setIntegral_eq _ _ hf.im (integrable_zero _ _ _)
    intro S hs hm
    rw [integral_im hf.integrableOn]
    simp only [Pi.zero_apply,integral_zero]
    exact (RCLike.nonneg_iff.mp (h S hs hm)).2
  filter_upwards [hre,him] with t ht hi
  exact RCLike.nonneg_iff.mpr ⟨ht,hi⟩
1859
def quadraticValue (v : ι → ℂ) (M : Matrix ι ι ℂ) : ℂ :=
body · 1 line
  ∑ i, ∑ j, star (v i) * M i j * v j
1863
lemma quadraticValue_eq [DecidableEq ι] (v : ι → ℂ) (M : Matrix ι ι ℂ) :
    quadraticValue v M = star v ⬝ᵥ (M *ᵥ v) := by
proof · 1 line
  simp [quadraticValue,dotProduct,Matrix.mulVec,Finset.mul_sum,mul_assoc]
1868
lemma integrable_quadraticValue [DecidableEq ι] {μ : Measure Ω} {F : Ω → Matrix ι ι ℂ}
    (hF : ∀ i j, Integrable (fun t => F t i j) μ) (v : ι → ℂ) :
    Integrable (fun t => quadraticValue v (F t)) μ := by
proof · 3 lines
  apply integrable_finsetSum; intro i _
  apply integrable_finsetSum; intro j _
  exact ((hF i j).const_mul _).mul_const _
1876
lemma integral_quadraticValue [DecidableEq ι] {μ : Measure Ω} {F : Ω → Matrix ι ι ℂ}
    (hF : ∀ i j, Integrable (fun t => F t i j) μ) (v : ι → ℂ) :
    (∫ t, quadraticValue v (F t) ∂μ) = quadraticValue v (fun i j => ∫ t, F t i j ∂μ) := by
proof · 7 lines
  unfold quadraticValue
  rw [integral_finsetSum _ (fun i _ => integrable_finsetSum _
    (fun j _ => ((hF i j).const_mul _).mul_const _))]
  apply Finset.sum_congr rfl; intro i _
  rw [integral_finsetSum _ (fun j _ => ((hF i j).const_mul _).mul_const _)]
  apply Finset.sum_congr rfl; intro j _
  rw [integral_mul_const,integral_const_mul]
1887
lemma density_posSemidef_ae (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) [SigmaFinite μ]
    (hAC : ∀ S, MeasurableSet S → μ S = 0 → W.value S = 0) :
    ∀ᵐ t ∂μ, (W.density μ t).PosSemidef := by
proof · 33 lines
  have hherm : ∀ᵐ t ∂μ, (W.density μ t).IsHermitian := by
    have hij (i j : ι) : (fun t => star (W.density μ t j i)) =ᵐ[μ]
        (fun t => W.density μ t i j) := by
      apply Integrable.ae_eq_of_forall_setIntegral_eq _ _
        ((Complex.conjCLE.integrable_comp_iff).mpr (W.integrable_entry μ j i)) (W.integrable_entry μ i j)
      intro S hs hm
      simp only [Complex.conjCLE_apply,integral_conj,W.integral_density_entry μ hAC S hs]
      exact congrArg (fun M : Matrix ι ι ℂ => M i j) (W.positive S hs).isHermitian.eq
    have hh : ∀ᵐ t ∂μ, ∀ i j, star (W.density μ t j i)=W.density μ t i j :=
      ae_all_iff.mpr (fun i => ae_all_iff.mpr (hij i))
    filter_upwards [hh] with t ht
    exact Matrix.IsHermitian.ext ht
  have hq (v : ι → ℂ) : ∀ᵐ t ∂μ, 0 ≤ quadraticValue v (W.density μ t) := by
    apply ae_complex_nonneg_of_integral_nonneg
      (integrable_quadraticValue (W.integrable_entry μ) v)
    intro S hs hm
    rw [integral_quadraticValue (fun i j => (W.integrable_entry μ i j).integrableOn)]
    simp only [W.integral_density_entry μ hAC S hs]
    change 0 ≤ quadraticValue v (W.value S)
    rw [quadraticValue_eq]
    exact (W.positive S hs).dotProduct_mulVec_nonneg v
  obtain ⟨s,hs,hd⟩ := TopologicalSpace.exists_countable_dense (ι → ℂ)
  let : Countable s := hs.to_subtype
  have hqs : ∀ᵐ t ∂μ, ∀ v : s, 0 ≤ quadraticValue v (W.density μ t) :=
    ae_all_iff.mpr (fun v => hq v)
  filter_upwards [hherm,hqs] with t ht hqt
  apply Matrix.posSemidef_iff_dotProduct_mulVec.mpr
  refine ⟨ht,fun v => ?_⟩
  rw [← quadraticValue_eq]
  refine hd.denseRange_val.induction_on (p := fun v => 0 ≤ quadraticValue v (W.density μ t)) v ?_ hqt
  · apply isClosed_le continuous_const
    unfold quadraticValue
    fun_prop
1934
lemma psd_entry_norm_le_trace {ι : Type v4526_0} [inst4526_0 : Fintype ι]
    [inst4526_1 : DecidableEq ι]
    {M : Matrix ι ι ℂ} (hM : M.PosSemidef) (i j : ι) :
    ‖M i j‖ ≤ (trace M).re := by
proof · 21 lines
  let S := CFC.sqrt M
  have hS : S.IsHermitian := (CFC.sqrt_nonneg M).isSelfAdjoint.isHermitian
  have hSS : S * Sᴴ = M := by rw [hS.eq]; exact CFC.sqrt_mul_sqrt_self M hM.nonneg
  let v (i : ι) : EuclideanSpace ℂ ι := WithLp.toLp 2 (fun k => S i k)
  have he (i j : ι) : M i j = inner ℂ (v j) (v i) := by
    rw [← hSS]
    simp [v,Matrix.mul_apply,EuclideanSpace.inner_toLp_toLp,dotProduct]
  have hv (i : ι) : ‖v i‖ = Real.sqrt (M i i).re := by
    rw [he]; exact norm_eq_sqrt_re_inner (𝕜 := ℂ) (v i)
  have hdiag (i : ι) : (M i i).re ≤ (trace M).re := by
    change (M i i).re ≤ (∑ k, M k k).re
    rw [Complex.re_sum]
    exact Finset.single_le_sum (fun k _ => (RCLike.nonneg_iff.mp (hM.diag_nonneg (i:=k))).1) (Finset.mem_univ i)
  have ht : 0 ≤ (trace M).re := (RCLike.nonneg_iff.mp hM.trace_nonneg).1
  calc
    ‖M i j‖ ≤ ‖v j‖ * ‖v i‖ := by rw [he]; exact norm_inner_le_norm _ _
    _ ≤ Real.sqrt (trace M).re * Real.sqrt (trace M).re := by
      rw [hv,hv]
      exact mul_le_mul (Real.sqrt_le_sqrt (hdiag j)) (Real.sqrt_le_sqrt (hdiag i))
        (Real.sqrt_nonneg _) (Real.sqrt_nonneg _)
    _ = (trace M).re := Real.mul_self_sqrt ht
1962
lemma uniformIntegrable_of_ae_norm_le_one {Ω : Type v4553_0} {mΩ : MeasurableSpace Ω}
    {μ : Measure Ω} [inst4553_0 : IsFiniteMeasure μ]
    {f : ℕ → Ω → ℝ}
    (hf : ∀ n, AEStronglyMeasurable (f n) μ) (hb : ∀ n, ∀ᵐ ω ∂μ, ‖f n ω‖ ≤ 1) :
    UniformIntegrable f 1 μ := by
proof · 12 lines
  apply uniformIntegrable_of le_rfl ENNReal.one_ne_top hf
  intro ε hε
  refine ⟨2, fun n => ?_⟩
  have hz : ({ω | (2:ℝ≥0) ≤ ‖f n ω‖₊}.indicator (f n)) =ᵐ[μ] 0 := by
    filter_upwards [hb n] with ω hω
    rw [Set.indicator_of_notMem]
    · rfl
    · simp only [Set.mem_ofPred_eq,not_le]
      have : ‖f n ω‖₊ ≤ (1:ℝ≥0) := hω
      exact lt_of_le_of_lt this (by norm_num)
  rw [eLpNorm_congr_ae hz,eLpNorm_zero]
  exact bot_le
1980
lemma bounded_martingale_limit {Ω : Type v4553_0} {mΩ : MeasurableSpace Ω} {μ : Measure Ω}
    [inst4553_0 : IsFiniteMeasure μ] {F : Filtration ℕ mΩ}
    {f : ℕ → Ω → ℝ} (hf : Martingale f F μ)
    (hb : ∀ n, ∀ᵐ ω ∂μ, ‖f n ω‖ ≤ 1) :
    ∃ g : Ω → ℝ, Integrable g μ ∧
      (∀ᵐ ω ∂μ, Tendsto (fun n => f n ω) atTop (𝓝 (g ω))) ∧
      (∀ n, f n =ᵐ[μ] μ[g | F n]) := by
proof · 6 lines
  have hui := uniformIntegrable_of_ae_norm_le_one
    (fun n => ((hf.stronglyMeasurable n).mono (F.le n)).aestronglyMeasurable) hb
  refine ⟨F.limitProcess f μ, ?_, hf.submartingale.ae_tendsto_limitProcess_of_uniformIntegrable hui,
    hf.ae_eq_condExp_limitProcess hui⟩
  obtain ⟨R,hR⟩ := hui.2
  exact (hf.submartingale.memLp_limitProcess hR).integrable le_rfl
1999
lemma integrable_matrix_of_entries (hG : ∀ i j, Integrable (fun ω => G ω i j) μ) :
    Integrable G μ := by
proof · 5 lines
  have h : Integrable (fun ω i j => G ω i j) μ :=
    integrable_pi_iff.mpr (fun i => integrable_pi_iff.mpr (hG i))
  let L : (ι → ι → ℂ) →L[ℂ] Matrix ι ι ℂ :=
    LinearMap.toContinuousLinearMap (Matrix.ofLinearEquiv ℂ).toLinearMap
  exact L.integrable_comp h
2007
lemma integral_matrix_entry (hG : Integrable G μ) (i j : ι) :
    (∫ ω, G ω ∂μ) i j = ∫ ω, G ω i j ∂μ := by
proof · 2 lines
  let L : Matrix ι ι ℂ →L[ℂ] ℂ := (Matrix.entryLinearMap ℂ ℂ i j).toContinuousLinearMap
  exact (L.integral_comp_comm hG).symm
2012
def positiveMatrixMeasureOfDensity (hG : ∀ i j, Integrable (fun ω => G ω i j) μ)
    (hp : ∀ᵐ ω ∂μ, (G ω).PosSemidef) : PositiveMatrixMeasure Ω ι where
body · 8 lines
  entry i j := μ.withDensityᵥ (fun ω => G ω i j)
  positive S hS := by
    have he : (fun i j => μ.withDensityᵥ (fun ω => G ω i j) S) = ∫ ω in S, G ω ∂μ := by
      ext i j
      rw [withDensityᵥ_apply (hG i j) hS,
        integral_matrix_entry (integrable_matrix_of_entries hG).integrableOn]
    rw [he]
    exact (integral_nonneg_of_ae ((ae_restrict_of_ae hp).mono (fun _ h => h.nonneg))).posSemidef
2023
lemma positiveMatrixMeasureOfDensity_value
    (hG : ∀ i j, Integrable (fun ω => G ω i j) μ)
    (hp : ∀ᵐ ω ∂μ, (G ω).PosSemidef) (S : Set Ω) (hS : MeasurableSet S) :
    (positiveMatrixMeasureOfDensity hG hp).value S = ∫ ω in S, G ω ∂μ := by
proof · 4 lines
  ext i j
  change μ.withDensityᵥ (fun ω => G ω i j) S = _
  rw [withDensityᵥ_apply (hG i j) hS,
    integral_matrix_entry (integrable_matrix_of_entries hG).integrableOn]
2045
def keepReference (U : Matrix (ι × η) (ι × η) ℂ) : Matrix η η ℂ :=
body · 1 line
  fun x y => ∑ i, U (i,x) (i,y)
2048
def referenceFilter (K : Matrix κ η ℂ) (U : Matrix (ι × η) (ι × η) ℂ) :
    Matrix (ι × κ) (ι × κ) ℂ :=
body · 1 line
  ((1 : Matrix ι ι ℂ) ⊗ₖ K) * U * ((1 : Matrix ι ι ℂ) ⊗ₖ K)ᴴ
2053
lemma referenceFilter_apply [Fintype κ] [DecidableEq κ] [DecidableEq η] (K : Matrix κ η ℂ) (U : Matrix (ι × η) (ι × η) ℂ)
    (i j : ι) (x y : κ) :
    referenceFilter K U (i,x) (j,y) = ∑ s, ∑ t, K x s * U (i,s) (j,t) * star (K y t) := by
proof · 4 lines
  simp only [referenceFilter,Matrix.conjTranspose_kronecker,Matrix.conjTranspose_one]
  simp [Matrix.mul_apply,Fintype.sum_prod_type,Matrix.one_apply,
    Matrix.conjTranspose_apply,Finset.sum_mul]
  rw [Finset.sum_comm]
2061
lemma keepReference_filter (K : Matrix κ η ℂ) (U : Matrix (ι × η) (ι × η) ℂ) :
    keepReference (referenceFilter K U) = K * keepReference U * Kᴴ := by
proof · 7 lines
  ext x y
  simp only [keepReference,referenceFilter_apply,Matrix.mul_apply,Matrix.conjTranspose_apply]
  simp_rw [Finset.mul_sum,Finset.sum_mul]
  conv_rhs => rw [Finset.sum_comm]
  rw [Finset.sum_comm]
  apply Finset.sum_congr rfl; intro s _
  rw [Finset.sum_comm]
2071
def vectorize (S : Matrix ι η ℂ) : ι × η → ℂ := fun p => S p.1 p.2
2074
lemma keepReference_vectorize [Fintype η] [DecidableEq ι] [DecidableEq η] (S : Matrix ι η ℂ) :
    keepReference (outer (vectorize S) (vectorize S)) = (Sᴴ*S)ᵀ := by
proof · 4 lines
  ext x y
  simp only [keepReference,outer,vectorize,Matrix.transpose_apply,Matrix.mul_apply,
    Matrix.conjTranspose_apply]
  apply Finset.sum_congr rfl; intro i _; ring
2081
lemma referenceFilter_vectorize (K : Matrix κ η ℂ) (S : Matrix ι η ℂ) :
    referenceFilter K (outer (vectorize S) (vectorize S)) =
      outer (vectorize (S*Kᵀ)) (vectorize (S*Kᵀ)) := by
proof · 8 lines
  ext ⟨i,x⟩ ⟨j,y⟩
  rw [referenceFilter_apply]
  simp only [outer,vectorize,Matrix.mul_apply,Matrix.transpose_apply,star_sum,star_mul]
  simp_rw [Finset.mul_sum,Finset.sum_mul]
  conv_rhs => rw [Finset.sum_comm]
  apply Finset.sum_congr rfl; intro s _
  apply Finset.sum_congr rfl; intro t _
  ring
2093
def localProbe : ι × ι → ℂ :=
body · 1 line
  vectorize (((Real.sqrt (Fintype.card ι : ℝ))⁻¹ : ℂ) • (1 : Matrix ι ι ℂ))
2096
def canonicalPurification (R : Matrix ι ι ℂ) : ι × ι → ℂ := vectorize (CFC.sqrt R)
2098
def preparationFilter (R : Matrix ι ι ℂ) : Matrix ι ι ℂ :=
body · 1 line
  (Real.sqrt (Fintype.card ι : ℝ) : ℂ) • (CFC.sqrt R)ᵀ
2101
lemma preparation_filter_identity [Nonempty ι] (R : Matrix ι ι ℂ) :
    referenceFilter (preparationFilter R) (outer (localProbe (ι := ι)) localProbe) =
      outer (canonicalPurification R) (canonicalPurification R) := by
proof · 6 lines
  unfold localProbe canonicalPurification preparationFilter
  rw [referenceFilter_vectorize,Matrix.transpose_smul,Matrix.transpose_transpose,
    Matrix.smul_mul,Matrix.mul_smul,one_mul,smul_smul]
  have hc : (Real.sqrt (Fintype.card ι : ℝ) : ℂ) ≠ 0 := by
    exact Complex.ofReal_ne_zero.mpr (Real.sqrt_pos.mpr (by exact_mod_cast Fintype.card_pos)).ne'
  rw [inv_mul_cancel₀ hc,one_smul]
2111
lemma canonical_reference (R : Matrix ι ι ℂ) (hR : R.PosSemidef) :
    keepReference (outer (canonicalPurification R) (canonicalPurification R)) = Rᵀ := by
proof · 2 lines
  rw [canonicalPurification,keepReference_vectorize,
    (CFC.sqrt_nonneg R).posSemidef.isHermitian.eq,CFC.sqrt_mul_sqrt_self R hR.nonneg]
2130
def traceSigned (W : PositiveMatrixMeasure Ω ι) : SignedMeasure Ω :=
body · 1 line
  ∑ i, (W.entry i i).re
2134
lemma traceSigned_apply [DecidableEq ι] (W : PositiveMatrixMeasure Ω ι) (S : Set Ω) :
    W.traceSigned S = (trace (W.value S)).re := by
proof · 2 lines
  simp [traceSigned,trace,value,ComplexMeasure.re_apply]
  apply Finset.sum_congr rfl; intro i _; rfl
2139
lemma traceSigned_nonneg (W : PositiveMatrixMeasure Ω ι) :
    (0 : SignedMeasure Ω) ≤[Set.univ] W.traceSigned := by
proof · 6 lines
  simpa only [VectorMeasure.restrict_univ] using (show (0 : SignedMeasure Ω) ≤ W.traceSigned from by
    intro S hS
    change 0 ≤ W.traceSigned S
    rw [traceSigned_apply]
    have hh : (0:ℂ) ≤ trace (W.value S) := (W.positive S hS).trace_nonneg
    exact (RCLike.nonneg_iff.mp hh).1)
2148
def traceMeasure (W : PositiveMatrixMeasure Ω ι) : Measure Ω :=
body · 12 lines
  W.traceSigned.toMeasureOfZeroLE Set.univ MeasurableSet.univ (by
    simpa only [VectorMeasure.restrict_univ] using
      (show (0 : SignedMeasure Ω) ≤ W.traceSigned from by
        intro S hS
        have htrace : W.traceSigned S = (trace (W.value S)).re := by
          simp [traceSigned, trace, value, ComplexMeasure.re_apply]
          apply Finset.sum_congr rfl
          intro i _
          rfl
        change 0 ≤ W.traceSigned S
        rw [htrace]
        exact (RCLike.nonneg_iff.mp (W.positive S hS).trace_nonneg).1))
2162
instance traceMeasure_finite (W : PositiveMatrixMeasure Ω ι) : IsFiniteMeasure W.traceMeasure :=
body · 2 lines
  inferInstanceAs (IsFiniteMeasure (W.traceSigned.toMeasureOfZeroLE Set.univ
    MeasurableSet.univ W.traceSigned_nonneg))
2166
lemma integrable_trace_density (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) :
    Integrable (fun t => trace (W.density μ t)) μ := by
proof · 1 line
  exact integrable_finsetSum _ (fun i _ => W.integrable_entry μ i i)
2170
lemma integral_trace_density (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) [SigmaFinite μ]
    (hAC : ∀ S, MeasurableSet S → μ S=0 → W.value S=0)
    (S : Set Ω) (hS : MeasurableSet S) :
    (∫ t in S, trace (W.density μ t) ∂μ) = trace (W.value S) := by
proof · 4 lines
  unfold trace diag
  rw [integral_finsetSum _ (fun i _ => (W.integrable_entry μ i i).integrableOn)]
  apply Finset.sum_congr rfl; intro i _
  exact W.integral_density_entry μ hAC S hS i i
2181
def filter {κ : Type v4989_0} [Fintype κ] (W : PositiveMatrixMeasure Ω ι) (K : Matrix κ ι ℂ) :
    PositiveMatrixMeasure Ω κ where
body · 14 lines
  entry x y := ∑ i, ∑ j, (K x i * star (K y j)) • W.entry i j
  positive S hS := by
    have he : (fun x y => (∑ i, ∑ j, (K x i * star (K y j)) • W.entry i j) S) =
        K * W.value S * Kᴴ := by
      ext x y
      simp only [_root_.sum_apply,_root_.smul_apply,Matrix.mul_apply,
        Matrix.conjTranspose_apply,smul_eq_mul,Finset.sum_mul]
      rw [Finset.sum_comm]
      apply Finset.sum_congr rfl; intro i _
      apply Finset.sum_congr rfl; intro j _
      dsimp [value]
      ring
    rw [he]
    exact (W.positive S hS).mul_mul_conjTranspose_same K
2201
lemma filter_value [DecidableEq ι] {κ : Type v5006_0} [Fintype κ] (W : PositiveMatrixMeasure Ω ι)
    (K : Matrix κ ι ℂ) (S : Set Ω) : (W.filter K).value S = K * W.value S * Kᴴ := by
proof · 7 lines
  ext x y
  simp only [filter,value,_root_.sum_apply,_root_.smul_apply,Matrix.mul_apply,
    Matrix.conjTranspose_apply,smul_eq_mul,Finset.sum_mul]
  rw [Finset.sum_comm]
  apply Finset.sum_congr rfl; intro i _
  apply Finset.sum_congr rfl; intro j _
  ring
2224
def pushforward {T : Type v5098_0} [MeasurableSpace T] (W : PositiveMatrixMeasure O η)
    (f : O → T) (hf : Measurable f) : PositiveMatrixMeasure T η where
body · 3 lines
  entry i j := (W.entry i j).map f
  positive S hS := by
    simpa only [VectorMeasure.map_apply _ hf hS] using W.positive (f ⁻¹' S) (hf hS)
2233
lemma pushforward_value [DecidableEq η] {T : Type v5104_0} [MeasurableSpace T] (W : PositiveMatrixMeasure O η)
    (f : O → T) (hf : Measurable f) (S : Set T) (hS : MeasurableSet S) :
    (W.pushforward f hf).value S = W.value (f ⁻¹' S) := by
proof · 1 line
  ext i j; exact VectorMeasure.map_apply _ hf hS
2250
def historyPrefix (n : ℕ) (x : ℕ → O) : Fin n → O := fun i => x i
2252
lemma measurable_historyPrefix (n : ℕ) : Measurable (historyPrefix (O := O) n) := by
proof · 2 lines
  unfold historyPrefix
  fun_prop
2256
def historyFiltration : Filtration ℕ (inferInstance : MeasurableSpace (ℕ → O)) where
body · 11 lines
  seq n := MeasurableSpace.comap (historyPrefix n) inferInstance
  mono' := by
    intro n k hnk
    apply MeasurableSpace.comap_le_iff_le_map.mpr
    intro S hS
    change MeasurableSet[MeasurableSpace.comap (historyPrefix k) inferInstance]
      (historyPrefix n ⁻¹' S)
    refine ⟨(fun s : Fin k → O => fun i : Fin n => s (Fin.castLE hnk i)) ⁻¹' S,
      (show Measurable (fun s : Fin k → O => fun i : Fin n => s (Fin.castLE hnk i)) by fun_prop) hS,?_⟩
    rfl
  le' n := (show Measurable (historyPrefix (O := O) n) by unfold historyPrefix; fun_prop).comap_le
2269
lemma historyPrefix_adapted (n : ℕ) :
    Measurable[(historyFiltration (O := O)) n] (historyPrefix (O := O) n) :=
proof · 1 line
  Measurable.of_comap_le le_rfl
2273
lemma historyFiltration_generates :
    (inferInstance : MeasurableSpace (ℕ → O)) = ⨆ n, historyFiltration (O := O) n := by
proof · 9 lines
  apply le_antisymm
  · change (⨆ k : ℕ, MeasurableSpace.comap (fun x : ℕ → O => x k) inferInstance) ≤ _
    apply iSup_le
    intro k
    apply le_iSup_of_le (k+1)
    have hk := (measurable_pi_apply (⟨k,Nat.lt_succ_self k⟩ : Fin (k+1))).comp
      (historyPrefix_adapted (O := O) (k+1))
    exact hk.comap_le
  · exact iSup_le (fun n => (historyFiltration (O := O)).le n)
2291
lemma density_entry_bound {Ω : Type v5313_0} [MeasurableSpace Ω]
    (U : Ω → Matrix ι ι ℂ) (hU : ∀ x, Density (U x)) (i j : ι) (x : Ω) :
    ‖U x i j‖ ≤ 1 := by
proof · 2 lines
  have h := psd_entry_norm_le_trace (hU x).1 i j
  simpa only [(hU x).2,Complex.one_re] using h
2299
lemma integrable_density_entry {Ω : Type v5319_0} [MeasurableSpace Ω]
    (U : Ω → Matrix ι ι ℂ) (hU : ∀ x, Density (U x)) (hm : Measurable U)
    (μ : Measure Ω) [IsFiniteMeasure μ] (i j : ι) :
    Integrable (fun x => U x i j) μ := by
proof · 3 lines
  let L : Matrix ι ι ℂ →L[ℂ] ℂ := (Matrix.entryLinearMap ℂ ℂ i j).toContinuousLinearMap
  exact Integrable.of_bound (L.continuous.measurable.comp hm).aestronglyMeasurable 1
    (Filter.Eventually.of_forall (density_entry_bound U hU i j))
2318
lemma reference_history_step_integral (n : ℕ) (S : Set (ℕ → O))
    (hS : MeasurableSet[historyFiltration n] S) (i j : ι) :
    (∫ x in S, U (n+1) (historyPrefix (n+1) x) i j ∂P) =
      ∫ x in S, U n (historyPrefix n x) i j ∂P := by
proof · 50 lines
  classical
  obtain ⟨A,hA,rfl⟩ := hS
  let r : (Fin (n+1) → O) → (Fin n → O) := fun s i => s i.castSucc
  have hr : Measurable r := by fun_prop
  let snoc : (Fin n → O) × O → (Fin (n+1) → O) := fun s => Fin.snoc s.1 s.2
  have hs : Measurable snoc := by
    apply Measurable.of_eval
    intro i
    refine Fin.lastCases ?_ (fun j => ?_) i
    · simpa [snoc] using measurable_snd
    · simpa only [snoc, Fin.snoc_castSucc, Function.comp_def] using ((measurable_pi_apply j).comp measurable_fst)
  let f : (Fin (n+1) → O) → ℂ := (r ⁻¹' A).indicator (fun s => U (n+1) s i j)
  let g : (Fin n → O) → ℂ := A.indicator (fun s => U n s i j)
  let L : Matrix ι ι ℂ →L[ℂ] ℂ := (Matrix.entryLinearMap ℂ ℂ i j).toContinuousLinearMap
  have hf : Measurable f := (L.continuous.measurable.comp (hm (n+1))).indicator (hr hA)
  have hg : Measurable g := (L.continuous.measurable.comp (hm n)).indicator hA
  have hfi : Integrable (fun s => f (snoc s)) ((P.map (historyPrefix n)) ⊗ₘ K n) := by
    apply Integrable.of_bound (hf.comp hs).aestronglyMeasurable 1
    apply Filter.Eventually.of_forall
    intro s
    exact (norm_indicator_le_norm_self (s := r ⁻¹' A) (fun t => U (n+1) t i j) (snoc s)).trans (density_entry_bound (U (n+1)) (hU (n+1)) i j _)
  have he (s : Fin n → O) : (∫ o, f (snoc (s,o)) ∂K n s) = g s := by
    have hh (o : O) : f (snoc (s,o)) =
        if s ∈ A then U (n+1) (Fin.snoc s o) i j else 0 := by
      simp only [f,Set.indicator,Set.mem_preimage,r,snoc]
      have ht : (fun i : Fin n => Fin.snoc (α := fun _ : Fin (n+1) => O) s o i.castSucc) = s := by
        ext i; simp
      rw [ht]
    simp_rw [hh]
    by_cases h : s ∈ A
    · simp only [h,ite_true,g,Set.indicator_of_mem]
      exact hmean n s i j
    · simp [h,g]
  calc
    _ = ∫ x, f (historyPrefix (n+1) x) ∂P := by
      rw [← integral_indicator ((measurable_historyPrefix n) hA)]
      congr 1
    _ = ∫ s, f s ∂P.map (historyPrefix (n+1)) :=
      (integral_map (measurable_historyPrefix _).aemeasurable hf.aestronglyMeasurable).symm
    _ = ∫ s, f s ∂(((P.map (historyPrefix n)) ⊗ₘ K n).map snoc) := by rw [hP n]
    _ = ∫ s, f (snoc s) ∂((P.map (historyPrefix n)) ⊗ₘ K n) :=
      integral_map hs.aemeasurable hf.aestronglyMeasurable
    _ = ∫ s, ∫ o, f (snoc (s,o)) ∂K n s ∂P.map (historyPrefix n) :=
      MeasureTheory.Measure.integral_compProd hfi
    _ = ∫ s, g s ∂P.map (historyPrefix n) := by simp_rw [he]
    _ = ∫ x, g (historyPrefix n x) ∂P :=
      integral_map (measurable_historyPrefix n).aemeasurable hg.aestronglyMeasurable
    _ = _ := by
      rw [← integral_indicator ((measurable_historyPrefix n) hA)]
      rfl
2374
lemma reference_history_integral (n k : ℕ) (hnk : n ≤ k) (S : Set (ℕ → O))
    (hS : MeasurableSet[historyFiltration n] S) (i j : ι) :
    (∫ x in S, U k (historyPrefix k x) i j ∂P) =
      ∫ x in S, U n (historyPrefix n x) i j ∂P := by
proof · 6 lines
  induction k, hnk using Nat.le_induction with
  | base => rfl
  | succ k hnk ih =>
    rw [reference_history_step_integral P K hP U hU hm hmean k S
      (((historyFiltration (O := O)).mono hnk) S hS) i j]
    exact ih
2386
lemma reference_history_measurable [DecidableEq ι] (n : ℕ) (i j : ι) (hm : ∀ n, Measurable (U n)) :
    Measurable[historyFiltration n] (fun x => U n (historyPrefix n x) i j) := by
proof · 2 lines
  let L : Matrix ι ι ℂ →L[ℂ] ℂ := (Matrix.entryLinearMap ℂ ℂ i j).toContinuousLinearMap
  exact L.continuous.measurable.comp ((hm n).comp (historyPrefix_adapted n))
2391
lemma reference_history_integrable (n : ℕ) (i j : ι) (hU : ∀ n s, Density (U n s))
    (hm : ∀ n, Measurable (U n)) :
    Integrable (fun x => U n (historyPrefix n x) i j) P := by
proof · 2 lines
  exact integrable_density_entry (fun x => U n (historyPrefix n x))
    (fun x => hU n _) ((hm n).comp (measurable_historyPrefix n)) P i j
2398
lemma reference_history_martingale_re (i j : ι) :
    Martingale (fun n x => (U n (historyPrefix n x) i j).re) historyFiltration P := by
proof · 13 lines
  refine ⟨fun n => ((reference_history_measurable U n i j hm).re).stronglyMeasurable, ?_⟩
  intro n k hnk
  symm
  apply ae_eq_condExp_of_forall_setIntegral_eq (historyFiltration.le n)
    ((reference_history_integrable P U k i j hU hm).re)
  · intro S _ _
    exact (reference_history_integrable P U n i j hU hm).re.integrableOn
  · intro S hS _
    change (∫ x in S, RCLike.re (U n (historyPrefix n x) i j) ∂P) = _
    rw [integral_re (reference_history_integrable P U n i j hU hm).integrableOn,
      integral_re (reference_history_integrable P U k i j hU hm).integrableOn,
      reference_history_integral P K hP U hU hm hmean n k hnk S hS i j]
  · exact ((reference_history_measurable U n i j hm).re).stronglyMeasurable.aestronglyMeasurable
2415
lemma reference_history_martingale_im (i j : ι) :
    Martingale (fun n x => (U n (historyPrefix n x) i j).im) historyFiltration P := by
proof · 13 lines
  refine ⟨fun n => ((reference_history_measurable U n i j hm).im).stronglyMeasurable, ?_⟩
  intro n k hnk
  symm
  apply ae_eq_condExp_of_forall_setIntegral_eq (historyFiltration.le n)
    ((reference_history_integrable P U k i j hU hm).im)
  · intro S _ _
    exact (reference_history_integrable P U n i j hU hm).im.integrableOn
  · intro S hS _
    change (∫ x in S, RCLike.im (U n (historyPrefix n x) i j) ∂P) = _
    rw [integral_im (reference_history_integrable P U n i j hU hm).integrableOn,
      integral_im (reference_history_integrable P U k i j hU hm).integrableOn,
      reference_history_integral P K hP U hU hm hmean n k hnk S hS i j]
  · exact ((reference_history_measurable U n i j hm).im).stronglyMeasurable.aestronglyMeasurable
2443
theorem local_reference_limit
    (W : ℕ → Ω → Matrix ι ι ℂ)
    (hW : ∀ n x, Density (W n x))
    (hlocal : ∀ n, Measurable[L] (W n))
    (hL : L ≤ mΩ)
    (hr : ∀ i j, Martingale (fun n x => (W n x i j).re) F μ)
    (hi : ∀ i j, Martingale (fun n x => (W n x i j).im) F μ) :
    ∃ G : Ω → Matrix ι ι ℂ,
      Measurable[L] G ∧ (∀ x, Density (G x)) ∧
      (∀ i j, Integrable (fun x => G x i j) μ) ∧
      (∀ᵐ x ∂μ, Tendsto (fun n => W n x) atTop (𝓝 (G x))) ∧
      (∀ n S, MeasurableSet[F n] S →
        (∫ x in S, G x ∂μ) = ∫ x in S, W n x ∂μ) := by
proof · 90 lines
  classical
  have hbound (n : ℕ) (i j : ι) (x : Ω) : ‖W n x i j‖ ≤ 1 := by
    simpa only [(hW n x).2, Complex.one_re] using
      psd_entry_norm_le_trace (hW n x).1 i j
  have hbr (i j : ι) (n : ℕ) : ∀ᵐ x ∂μ, ‖(W n x i j).re‖ ≤ 1 :=
    Filter.Eventually.of_forall (fun x =>
      (show ‖(W n x i j).re‖ ≤ ‖W n x i j‖ from by
        simpa only [Real.norm_eq_abs] using Complex.abs_re_le_norm (W n x i j)).trans
      (hbound n i j x))
  have hbi (i j : ι) (n : ℕ) : ∀ᵐ x ∂μ, ‖(W n x i j).im‖ ≤ 1 :=
    Filter.Eventually.of_forall (fun x =>
      (show ‖(W n x i j).im‖ ≤ ‖W n x i j‖ from by
        simpa only [Real.norm_eq_abs] using Complex.abs_im_le_norm (W n x i j)).trans
      (hbound n i j x))
  choose gr hgr htr hcr using (fun i j => bounded_martingale_limit (hr i j) (hbr i j))
  choose gi hgi hti hci using (fun i j => bounded_martingale_limit (hi i j) (hbi i j))
  let H : Ω → Matrix ι ι ℂ := fun x i j => (gr i j x : ℂ) + (gi i j x : ℂ)*Complex.I
  have hH (i j : ι) : Integrable (fun x => H x i j) μ :=
    ((hgr i j).ofReal).add (((hgi i j).ofReal).mul_const Complex.I)
  have hlimH : ∀ᵐ x ∂μ, Tendsto (fun n => W n x) atTop (𝓝 (H x)) := by
    have htr' := ae_all_iff.mpr (fun i => ae_all_iff.mpr (htr i))
    have hti' := ae_all_iff.mpr (fun i => ae_all_iff.mpr (hti i))
    filter_upwards [htr', hti'] with x hx hy
    have hc : Tendsto (fun n i j => W n x i j) atTop (𝓝 (fun i j => H x i j)) := by
      apply tendsto_pi_nhds.mpr; intro i
      apply tendsto_pi_nhds.mpr; intro j
      have hh := (Complex.continuous_ofReal.continuousAt.tendsto.comp (hx i j)).add
        ((Complex.continuous_ofReal.continuousAt.tendsto.comp (hy i j)).mul_const Complex.I)
      simpa only [Function.comp_apply, Pi.add_apply, Complex.re_add_im] using hh
    have hcM : Continuous (Matrix.of : (ι → ι → ℂ) → Matrix ι ι ℂ) :=
      continuous_matrix (fun i j => continuous_apply_apply i j)
    exact hcM.continuousAt.tendsto.comp hc
  let raw : Ω → Matrix ι ι ℂ := fun x => limUnder atTop (fun n => W n x)
  have hraw : Measurable[L] raw :=
    (@StronglyMeasurable.limUnder ℕ Ω (Matrix ι ι ℂ) L _ _ atTop _ _ _ _
      (fun n => (hlocal n).stronglyMeasurable)).measurable
  have hgood : MeasurableSet[L] {x | 0 ≤ raw x ∧ trace (raw x) = 1} := by
    exact (measurableSet_le measurable_const hraw).inter
      (measurableSet_eq_fun ((Matrix.traceLinearMap ι ℂ ℂ).toContinuousLinearMap.continuous.measurable.comp hraw) measurable_const)
  let G : Ω → Matrix ι ι ℂ := fun x =>
    if 0 ≤ raw x ∧ trace (raw x) = 1 then raw x else W 0 x
  have hGm : Measurable[L] G := hraw.ite hgood (hlocal 0)
  have hGp (x : Ω) : Density (G x) := by
    dsimp [G]
    split
    · rename_i h
      exact ⟨h.1.posSemidef, h.2⟩
    · exact hW 0 x
  have hGH : G =ᵐ[μ] H := by
    filter_upwards [hlimH] with x hx
    have hrx : raw x = H x := hx.limUnder_eq
    have hp : 0 ≤ H x :=
      isClosed_Ici.mem_of_tendsto hx (Eventually.of_forall (fun n => (hW n x).1.nonneg))
    have ht : trace (H x) = 1 := by
      have htend := (Matrix.traceLinearMap ι ℂ ℂ).toContinuousLinearMap.continuous.continuousAt.tendsto.comp hx
      have he : (fun n => trace (W n x)) = fun _ => (1 : ℂ) := funext (fun n => (hW n x).2)
      change Tendsto (fun n => trace (W n x)) atTop (𝓝 (trace (H x))) at htend
      rw [he] at htend
      exact tendsto_nhds_unique htend tendsto_const_nhds
    simp only [G, hrx, hp, ht, and_self, ↓reduceIte]
  have hGi (i j : ι) : Integrable (fun x => G x i j) μ :=
    (hH i j).congr (hGH.mono (fun x hx => congrArg (fun M => M i j) hx.symm))
  refine ⟨G,hGm,hGp,hGi,?_,?_⟩
  · filter_upwards [hlimH,hGH] with x hx he
    simpa only [he] using hx
  · intro n S hS
    have hc (i j : ι) : Integrable (fun x => W n x i j) μ :=
      Integrable.of_bound (((Matrix.entryLinearMap ℂ ℂ i j).toContinuousLinearMap).measurable.comp
        ((hlocal n).mono hL le_rfl)).aestronglyMeasurable 1
        (Eventually.of_forall (hbound n i j))
    ext i j
    rw [integral_matrix_entry (integrable_matrix_of_entries hGi).integrableOn,
      integral_matrix_entry (integrable_matrix_of_entries hc).integrableOn]
    calc
      _ = ∫ x in S, H x i j ∂μ := integral_congr_ae
        (ae_restrict_of_ae (hGH.mono (fun x hx => congrArg (fun M => M i j) hx)))
      _ = ∫ x in S, W n x i j ∂μ := by
        apply Complex.ext
        · change RCLike.re (∫ x in S, H x i j ∂μ) = RCLike.re (∫ x in S, W n x i j ∂μ)
          rw [← integral_re (hH i j).integrableOn, ← integral_re (hc i j).integrableOn]
          have heH : (fun x => RCLike.re (H x i j)) = gr i j := by
            funext x; simp [H]
          rw [heH, ← setIntegral_condExp (F.le n) (hgr i j) hS]
          exact integral_congr_ae (ae_restrict_of_ae (hcr i j n).symm)
        · change RCLike.im (∫ x in S, H x i j ∂μ) = RCLike.im (∫ x in S, W n x i j ∂μ)
          rw [← integral_im (hH i j).integrableOn, ← integral_im (hc i j).integrableOn]
          have heH : (fun x => RCLike.im (H x i j)) = gi i j := by
            funext x; simp [H]
          rw [heH, ← setIntegral_condExp (F.le n) (hgi i j) hS]
          exact integral_congr_ae (ae_restrict_of_ae (hci i j n).symm)
2557
abbrev LocalHistory (O : Type v5616_0) (M : Type v5616_1) (n : ℕ) :=
body · 1 line
  (Fin n → (O ⊕ Unit)) × (Fin n → M)
2562
def localActive {O : Type v5619_0} {M : Type v5619_1} {n : ℕ} (h : LocalHistory O M n) (o : O) (m : M) :
    LocalHistory O M (n+1) :=
body · 1 line
  (Fin.snoc h.1 (Sum.inl o), Fin.snoc h.2 m)
2568
def localPassive {O : Type v5623_0} {M : Type v5623_1} {n : ℕ} (h : LocalHistory O M n) (m : M) :
    LocalHistory O M (n+1) :=
body · 1 line
  (Fin.snoc h.1 (Sum.inr ()), Fin.snoc h.2 m)
2581
structure LocalObservableExecution where
body · 15 lines
  state : (n : ℕ) → LocalHistory O M n → ReferenceFunctional (A n) ι
  positive_state : ∀ n h, PositiveFunctional (state n h)
  positive_unit : ∀ n, (0 : A n) ≤ 1
  normalized : ∀ n h, trace (state n h 1) = 1
  measurable_reference : ∀ n, Measurable (fun h => state n h 1)
  instrument : (n : ℕ) → (h : LocalHistory O M n) → ObservableInstrument O (A n) (A (n+1))
  run : ∀ n h, InstrumentRun (instrument n h) (state n h)
  run_law : ∀ n h, (run n h).law = κ n h
  active_update : ∀ n h,
    (fun o => state (n+1) (localActive h o (send n (h,o)))) =ᵐ[κ n h] (run n h).posterior
  receive : (n : ℕ) → LocalHistory O M n → M → (A (n+1) →ₗ[ℂ] A n)
  receive_positive : ∀ n h m x, 0 ≤ x → 0 ≤ receive n h m x
  receive_unit : ∀ n h m, receive n h m 1 = 1
  passive_update : ∀ n h m,
    state (n+1) (localPassive h m) = (state n h).comp (receive n h m)
2602
def reference (n : ℕ) (h : LocalHistory O M n) : Matrix ι ι ℂ := e.state n h 1
2605
lemma reference_density [DecidableEq ι] (n : ℕ) (h : LocalHistory O M n) :
    ZeroKey.Density (e.reference n h) :=
proof · 1 line
  ⟨e.positive_state n h 1 (e.positive_unit n), e.normalized n h⟩
2610
lemma reference_measurable [DecidableEq ι] (n : ℕ) : Measurable (e.reference n) := e.measurable_reference n
2612
lemma active_mean (n : ℕ) (h : LocalHistory O M n) (i j : ι) :
    (∫ o, e.reference (n+1) (localActive h o (send n (h,o))) i j ∂κ n h) =
      e.reference n h i j := by
proof · 8 lines
  calc
    _ = ∫ o, (e.run n h).posterior o 1 i j ∂κ n h := by
      apply integral_congr_ae
      filter_upwards [e.active_update n h] with o ho
      exact congrArg (fun S : ReferenceFunctional (A (n+1)) ι => S 1 i j) ho
    _ = _ := by
      rw [← e.run_law]
      exact InstrumentRun.reference_mean (e.instrument n h) (e.state n h) (e.run n h) i j
2625
lemma passive_reference [DecidableEq ι] (n : ℕ) (h : LocalHistory O M n) (m : M) :
    e.reference (n+1) (localPassive h m) = e.reference n h := by
proof · 1 line
  simp only [reference,e.passive_update,LinearMap.comp_apply,e.receive_unit]
2641
theorem condIndep_adaptive_public_message {Ω : Type v5828_0} {M : Type v5828_1}
    {mΩ : MeasurableSpace Ω} [StandardBorelSpace Ω] [MeasurableSpace M]
    {P : Measure Ω} [IsFiniteMeasure P]
    {C A B : MeasurableSpace Ω}
    (hC : C ≤ mΩ) (hA : A ≤ mΩ) (hB : B ≤ mΩ)
    (h : CondIndep C A B hC P)
    (q : Ω → Bool) (a b : Ω → M) (d : M)
    (hq : @Measurable Ω Bool C _ q)
    (ha : @Measurable Ω M (A ⊔ C) _ a)
    (hb : @Measurable Ω M (B ⊔ C) _ b) :
    let message := fun x => if q x then a x else b x
proof · 51 lines
    let N := C ⊔ MeasurableSpace.comap message inferInstance
    CondIndep N A B (sup_le hC ((ha.mono (sup_le hA hC) le_rfl).ite
      ((measurableSet_singleton true).preimage (hq.mono hC le_rfl))
      (hb.mono (sup_le hB hC) le_rfl)).comap_le) P := by
  let left := fun x => if q x then a x else d
  let right := fun x => if q x then d else b x
  let NA := C ⊔ MeasurableSpace.comap left inferInstance
  let NB := NA ⊔ MeasurableSpace.comap right inferInstance
  let g := fun x => if q x then a x else b x
  let N := C ⊔ MeasurableSpace.comap g inferInstance
  have hqset : MeasurableSet[C] {x | q x = true} := hq (measurableSet_singleton true)
  have hleft : @Measurable Ω M (A ⊔ C) _ left :=
    ha.ite ((show C ≤ A ⊔ C from le_sup_right) _ hqset) measurable_const
  have hCA : C ≤ NA := le_sup_left
  have hNAA : NA ≤ A ⊔ C := sup_le le_sup_right hleft.comap_le
  have hNAM : NA ≤ mΩ := hNAA.trans (sup_le hA hC)
  have h₁ : CondIndep NA A B hNAM P := condIndep_local_message hC hA hB hCA hNAA h
  have hright : @Measurable Ω M (B ⊔ NA) _ right :=
    measurable_const.ite ((hCA.trans le_sup_right) _ hqset)
      (hb.mono (sup_le_sup_left hCA B) le_rfl)
  have hNBB : NB ≤ B ⊔ NA := sup_le le_sup_right hright.comap_le
  have hNBM : NB ≤ mΩ := hNBB.trans (sup_le hB hNAM)
  have h₂ : CondIndep NB A B hNBM P :=
    (condIndep_local_message hNAM hB hA le_sup_left hNBB h₁.symm).symm
  have hCN : C ≤ N := le_sup_left
  have hgN : @Measurable Ω M N _ g := Measurable.of_comap_le le_sup_right
  have hlN : @Measurable Ω M N _ left := by
    have hh : @Measurable Ω M N _ (fun x => if q x then g x else d) :=
      hgN.ite (hCN _ hqset) measurable_const
    convert hh using 1
    ext x
    cases hqx : q x <;> simp [left,g,hqx]
  have hrN : @Measurable Ω M N _ right := by
    have hh : @Measurable Ω M N _ (fun x => if q x then d else g x) :=
      measurable_const.ite (hCN _ hqset) hgN
    convert hh using 1
    ext x
    cases hqx : q x <;> simp [right,g,hqx]
  have hNBN : NB ≤ N := sup_le (sup_le hCN hlN.comap_le) hrN.comap_le
  have hCNB : C ≤ NB := le_sup_left.trans le_sup_left
  have hlNB : @Measurable Ω M NB _ left :=
    Measurable.of_comap_le (le_sup_right.trans le_sup_left)
  have hrNB : @Measurable Ω M NB _ right := Measurable.of_comap_le le_sup_right
  have hgNB : @Measurable Ω M NB _ g := by
    have hh : @Measurable Ω M NB _ (fun x => if q x then left x else right x) :=
      hlNB.ite (hCNB _ hqset) hrNB
    convert hh using 1
    ext x
    cases hqx : q x <;> simp [left,right,g,hqx]
  have he : NB = N := le_antisymm hNBN (sup_le hCNB hgNB.comap_le)
  simpa only [he] using h₂
2708
theorem adaptive_tapes_given_full_transcript {Ω : Type v5898_0} {M : Type v5898_1}
    {mΩ : MeasurableSpace Ω} [StandardBorelSpace Ω] [MeasurableSpace M]
    {P : Measure Ω} [IsFiniteMeasure P]
    (F : Filtration ℕ mΩ) (A B : MeasurableSpace Ω)
    (hA : A ≤ mΩ) (hB : B ≤ mΩ)
    (hinitial : CondIndep (F 0) A B (F.le 0) P)
    (q : ℕ → Ω → Bool) (a b : ℕ → Ω → M) (d : M)
    (hq : ∀ n, @Measurable Ω Bool (F n) _ (q n))
    (ha : ∀ n, @Measurable Ω M (A ⊔ F n) _ (a n))
    (hb : ∀ n, @Measurable Ω M (B ⊔ F n) _ (b n))
    (hF : ∀ n, F (n+1) = F n ⊔ MeasurableSpace.comap
      (fun x => if q n x then a n x else b n x) inferInstance) :
    CondIndep (⨆ n, F n) (A ⊔ ⨆ n, F n) (B ⊔ ⨆ n, F n)
      (iSup_le F.le) P := by
proof · 12 lines
  have hfinite : ∀ n, CondIndep (F n) A B (F.le n) P := by
    intro n
    induction n with
    | zero => exact hinitial
    | succ n ih =>
      have h := condIndep_adaptive_public_message (F.le n) hA hB ih
        (q n) (a n) (b n) d (hq n) (ha n) (hb n)
      simpa only [hF n] using h
  have h := condIndep_full_transcript F A B hA hB hfinite
  have hC := iSup_le F.le
  have h1 := (condIndep_join_public_right hC hB hA h.symm).symm
  exact condIndep_join_public_right hC (sup_le hA hC) hB h1
2737
structure AdaptiveTapeProgram (U : Type v5935_0) (V : Type v5935_1) (M : Type v5935_2) [MeasurableSpace U]
    [MeasurableSpace V] [MeasurableSpace M] where
body · 6 lines
  alice : (n : ℕ) → (Fin n → M) → Bool
  measAlice : ∀ n, Measurable (alice n)
  sendA : (n : ℕ) → U × (Fin n → M) → M
  sendB : (n : ℕ) → V × (Fin n → M) → M
  measA : ∀ n, Measurable (sendA n)
  measB : ∀ n, Measurable (sendB n)
2753
def publicPrefix : (n : ℕ) → U × V → (Fin n → M)
body · 3 lines
  | 0, _ => Fin.elim0
  | n+1, ω => Fin.snoc (publicPrefix n ω)
      (if p.alice n (publicPrefix n ω) then p.sendA n (ω.1,publicPrefix n ω) else p.sendB n (ω.2,publicPrefix n ω))
2758
def message (n : ℕ) (ω : U × V) : M :=
body · 1 line
  if p.alice n (p.publicPrefix n ω) then p.sendA n (ω.1,p.publicPrefix n ω) else p.sendB n (ω.2,p.publicPrefix n ω)
2761
lemma prefix_succ (n : ℕ) (ω : U × V) :
    p.publicPrefix (n+1) ω = Fin.snoc (p.publicPrefix n ω) (p.message n ω) := rfl
2764
lemma measurable_prefix : ∀ n, Measurable (p.publicPrefix n) := by
proof · 12 lines
  intro n
  induction n with
  | zero => exact Measurable.of_eval (fun i => Fin.elim0 i)
  | succ n ih =>
    apply Measurable.of_eval
    intro i
    refine Fin.lastCases ?_ (fun j => ?_) i
    · simp only [publicPrefix, Fin.snoc_last]
      exact ((p.measA n).comp (measurable_fst.prodMk ih)).ite
        ((measurableSet_singleton true).preimage ((p.measAlice n).comp ih))
        ((p.measB n).comp (measurable_snd.prodMk ih))
    · simpa only [publicPrefix, Fin.snoc_castSucc] using ih.eval (a := j)
2778
lemma measurable_message (n : ℕ) : Measurable (p.message n) := by
proof · 3 lines
  exact ((p.measA n).comp (measurable_fst.prodMk (p.measurable_prefix n))).ite
    ((measurableSet_singleton true).preimage ((p.measAlice n).comp (p.measurable_prefix n)))
    ((p.measB n).comp (measurable_snd.prodMk (p.measurable_prefix n)))
2783
abbrev field (n : ℕ) : MeasurableSpace (U × V) :=
body · 1 line
  MeasurableSpace.comap (p.publicPrefix n) inferInstance
2786
lemma field_le (n : ℕ) : p.field n ≤ (inferInstance : MeasurableSpace (U × V)) := (p.measurable_prefix n).comap_le
2788
lemma field_mono : Monotone p.field := by
proof · 10 lines
  apply monotone_nat_of_le_succ
  intro n
  have h : @Measurable (U × V) (Fin n → M) (p.field (n+1)) _ (p.publicPrefix n) := by
    let : MeasurableSpace (U × V) := p.field (n+1)
    apply Measurable.of_eval
    intro i
    have hh := (@Measurable.of_comap_le (U × V) (Fin (n+1) → M)
      (p.field (n+1)) _ (p.publicPrefix (n+1)) le_rfl).eval (a := i.castSucc)
    simpa only [publicPrefix, Fin.snoc_castSucc] using hh
  exact h.comap_le
2800
def filtration : Filtration ℕ (inferInstance : MeasurableSpace (U × V)) where
body · 3 lines
  seq := p.field
  mono' := p.field_mono
  le' := p.field_le
2805
lemma field_zero : p.field 0 = ⊥ := by
proof · 1 line
  exact MeasurableSpace.comap_const Fin.elim0
2808
lemma field_succ (n : ℕ) :
    p.field (n+1) = p.field n ⊔ MeasurableSpace.comap (p.message n) inferInstance := by
proof · 17 lines
  apply le_antisymm
  · let : MeasurableSpace (U × V) := p.field n ⊔ MeasurableSpace.comap (p.message n) inferInstance
    apply Measurable.comap_le
    apply Measurable.of_eval
    intro i
    refine Fin.lastCases ?_ (fun j => ?_) i
    · simpa only [prefix_succ,Fin.snoc_last] using
        (Measurable.of_comap_le (f := p.message n) le_sup_right)
    · simpa only [prefix_succ,Fin.snoc_castSucc,field] using
        (Measurable.of_comap_le (f := p.publicPrefix n) le_sup_left).eval (a := j)
  · apply sup_le (p.field_mono (Nat.le_succ n))
    let : MeasurableSpace (U × V) := p.field (n+1)
    have h := (@Measurable.of_comap_le (U × V) (Fin (n+1) → M)
      (p.field (n+1)) _ (p.publicPrefix (n+1)) le_rfl).eval (a := Fin.last n)
    have hh : Measurable[p.field (n+1)] (p.message n) := by
      simpa only [prefix_succ,Fin.snoc_last] using h
    exact hh.comap_le
2828
theorem tapes_conditionally_independent [StandardBorelSpace U] [StandardBorelSpace V]
    [Nonempty M]
    (μ : Measure U) (ν : Measure V) [IsProbabilityMeasure μ] [IsProbabilityMeasure ν] :
    CondIndep (⨆ n, p.field n)
      (MeasurableSpace.comap Prod.fst ‹MeasurableSpace U› ⊔ ⨆ n, p.field n)
      (MeasurableSpace.comap Prod.snd ‹MeasurableSpace V› ⊔ ⨆ n, p.field n)
      (iSup_le p.field_le) (μ.prod ν) := by
proof · 18 lines
  refine adaptive_tapes_given_full_transcript (P := μ.prod ν) p.filtration _ _
    measurable_fst.comap_le measurable_snd.comap_le ?_
    (fun n ω => p.alice n (p.publicPrefix n ω))
    (fun n ω => p.sendA n (ω.1,p.publicPrefix n ω))
    (fun n ω => p.sendB n (ω.2,p.publicPrefix n ω)) (Classical.arbitrary M) ?_ ?_ ?_ ?_
  · change CondIndep (p.field 0) _ _ _ _
    have h0 := indep_condIndep_bot measurable_fst.comap_le measurable_snd.comap_le
      (P := μ.prod ν) (indepFun_prod (μ := μ) (ν := ν) measurable_id measurable_id)
    simpa only [p.field_zero] using h0
  · intro n
    exact (p.measAlice n).comp (Measurable.of_comap_le le_rfl)
  · intro n
    exact (p.measA n).comp ((Measurable.of_comap_le le_sup_left).prodMk
      (Measurable.of_comap_le le_sup_right))
  · intro n
    exact (p.measB n).comp ((Measurable.of_comap_le le_sup_left).prodMk
      (Measurable.of_comap_le le_sup_right))
  · exact p.field_succ
2854
def record (ω : U × V) (n : ℕ) : M := p.message n ω
2856
lemma measurable_record : Measurable p.record :=
proof · 1 line
  Measurable.of_eval p.measurable_message
2859
lemma prefix_eq_record (n : ℕ) (ω : U × V) (i : Fin n) :
    p.publicPrefix n ω i = p.record ω i := by
proof · 6 lines
  induction n with
  | zero => exact Fin.elim0 i
  | succ n ih =>
    refine Fin.lastCases ?_ (fun j => ?_) i
    · simp only [publicPrefix, Fin.snoc_last, record, message, Fin.val_last]
    · simpa only [publicPrefix, Fin.snoc_castSucc, Fin.val_castSucc] using ih j
2868
lemma full_record_field :
    MeasurableSpace.comap p.record inferInstance = ⨆ n, p.field n := by
proof · 16 lines
  apply le_antisymm
  · let : MeasurableSpace (U × V) := ⨆ n, p.field n
    apply Measurable.comap_le
    apply Measurable.of_eval
    intro n
    have h : @Measurable (U × V) (Fin (n+1) → M) (⨆ n, p.field n) _
        (p.publicPrefix (n+1)) := Measurable.of_comap_le (le_iSup p.field (n+1))
    simpa only [publicPrefix, Fin.snoc_last, record, message] using h.eval (a := Fin.last n)
  · apply iSup_le
    intro n
    let : MeasurableSpace (U × V) := MeasurableSpace.comap p.record inferInstance
    apply Measurable.comap_le
    apply Measurable.of_eval
    intro i
    simp_rw [prefix_eq_record]
    exact (Measurable.of_comap_le (f := p.record) le_rfl).eval
2889
theorem final_records_conditionally_independent [Nonempty M]
    [StandardBorelSpace U] [StandardBorelSpace V]
    (μ : Measure U) (ν : Measure V) [IsProbabilityMeasure μ] [IsProbabilityMeasure ν]
    {A : Type v6086_0} {B : Type v6086_1} [MeasurableSpace A] [MeasurableSpace B]
    (readA : U × (ℕ → M) → A) (readB : V × (ℕ → M) → B)
    (ha : Measurable readA) (hb : Measurable readB) :
    CondIndepFun (MeasurableSpace.comap p.record inferInstance)
      p.measurable_record.comap_le
      (fun ω => readA (ω.1,p.record ω))
      (fun ω => readB (ω.2,p.record ω)) (μ.prod ν) := by
proof · 15 lines
  have h := p.tapes_conditionally_independent μ ν
  rw [condIndepFun_iff_condIndep]
  have hA : MeasurableSpace.comap (fun ω => readA (ω.1,p.record ω)) inferInstance ≤
      MeasurableSpace.comap Prod.fst ‹MeasurableSpace U› ⊔ ⨆ n, p.field n := by
    apply Measurable.comap_le
    exact ha.comp ((Measurable.of_comap_le le_sup_left).prodMk
      (Measurable.of_comap_le (p.full_record_field ▸ le_sup_right)))
  have hB : MeasurableSpace.comap (fun ω => readB (ω.2,p.record ω)) inferInstance ≤
      MeasurableSpace.comap Prod.snd ‹MeasurableSpace V› ⊔ ⨆ n, p.field n := by
    apply Measurable.comap_le
    exact hb.comp ((Measurable.of_comap_le le_sup_left).prodMk
      (Measurable.of_comap_le (p.full_record_field ▸ le_sup_right)))
  have h' := condIndep_of_condIndep_of_le_right
    (condIndep_of_condIndep_of_le_left h hA) hB
  simpa only [p.full_record_field] using h'
2929
def noiseTapeLaw : Measure (ℕ → N) := Measure.infinitePi (fun _ => ν)
2931
instance noiseTapeLaw_probability : IsProbabilityMeasure (noiseTapeLaw ν) := by
body · 2 lines
  unfold noiseTapeLaw
  infer_instance
2935
lemma noise_prefix_fresh_independent (n : ℕ) :
    IndepFun (fun u : (ℕ → N) => fun i : Fin n => u i) (fun u => u n) (noiseTapeLaw ν) := by
proof · 9 lines
  have h := iIndepFun_infinitePi (P := fun _ : ℕ => ν)
    (X := fun _ x => x) (fun _ => measurable_id)
  have hd : Disjoint (Finset.range n) {n} := by simp
  have hh := h.indepFun_finset (Finset.range n) {n} hd (fun i => measurable_pi_apply i)
  have hh' := hh.comp
    (show Measurable (fun x : (i : Finset.range n) → N => fun j : Fin n =>
      x ⟨j,Finset.mem_range.mpr j.isLt⟩) from by fun_prop)
    (show Measurable (fun x : (i : ({n} : Finset ℕ)) → N => x ⟨n,by simp⟩) from by fun_prop)
  exact hh'
2947
lemma noise_tape_eval (n : ℕ) : (noiseTapeLaw ν).map (fun u => u n) = ν :=
proof · 1 line
  Measure.infinitePi_map_eval _ _
2952
structure NoiseSampler (N : Type v6146_0) (O : Type v6146_1) [MeasurableSpace N] [MeasurableSpace O] where
body · 2 lines
  step : (n : ℕ) → (Fin n → O) × N → O
  measurable_step : ∀ n, Measurable (step n)
2964
def finiteHistory : (n : ℕ) → (Fin n → N) → (Fin n → O)
body · 3 lines
  | 0, _ => Fin.elim0
  | n+1, u => Fin.snoc (finiteHistory n (fun i => u i.castSucc))
      (f.step n (finiteHistory n (fun i => u i.castSucc), u (Fin.last n)))
2969
lemma measurable_finiteHistory : ∀ n, Measurable (f.finiteHistory n) := by
proof · 11 lines
  intro n
  induction n with
  | zero => exact Measurable.of_eval (fun i => Fin.elim0 i)
  | succ n ih =>
    have hr : Measurable (fun u : Fin (n+1) → N => fun i : Fin n => u i.castSucc) := by fun_prop
    apply Measurable.of_eval
    intro i
    refine Fin.lastCases ?_ (fun j => ?_) i
    · simpa only [finiteHistory, Fin.snoc_last,Function.comp_def] using
        (f.measurable_step n).comp ((ih.comp hr).prodMk (measurable_pi_apply (Fin.last n)))
    · simpa only [finiteHistory, Fin.snoc_castSucc,Function.comp_def] using (ih.comp hr).eval (a := j)
2982
def history (n : ℕ) (u : (ℕ → N)) : Fin n → O :=
body · 1 line
  f.finiteHistory n (fun i => u i)
2985
lemma measurable_prefix (n : ℕ) : Measurable (f.history n) :=
proof · 1 line
  (f.measurable_finiteHistory n).comp (by fun_prop)
2988
lemma prefix_succ (n : ℕ) (u : (ℕ → N)) :
    f.history (n+1) u = Fin.snoc (f.history n u) (f.step n (f.history n u,u n)) := rfl
2991
lemma prefix_fresh_independent (n : ℕ) :
    IndepFun (f.history n) (fun u => u n) (noiseTapeLaw ν) :=
proof · 1 line
  (noise_prefix_fresh_independent ν n).comp (f.measurable_finiteHistory n) measurable_id
2995
lemma prefix_fresh_law (n : ℕ) :
    (noiseTapeLaw ν).map (fun u => (f.history n u,u n)) =
      ((noiseTapeLaw ν).map (f.history n)).prod ν := by
proof · 3 lines
  rw [(f.prefix_fresh_independent (ν := ν) n).map_prod_eq_prod_map_map
    (f.measurable_prefix n).aemeasurable (measurable_pi_apply n).aemeasurable,
    noise_tape_eval]
3002
theorem prefix_law_succ (κ : (n : ℕ) → Kernel (Fin n → O) O)
    [∀ n, IsMarkovKernel (κ n)]
    (hlaw : ∀ n s, ν.map (fun u => f.step n (s,u)) = κ n s)
    (n : ℕ) :
    (noiseTapeLaw ν).map (f.history (n+1)) =
      (((noiseTapeLaw ν).map (f.history n)) ⊗ₘ κ n).map (fun p : (Fin n → O) × O => Fin.snoc (α := fun _ : Fin (n+1) => O) p.1 p.2) := by
proof · 26 lines
  have hfresh := f.prefix_fresh_law (ν := ν) n
  have hsample : (((noiseTapeLaw ν).map (f.history n)).prod ν).map
      (fun p => (p.1, f.step n p)) = ((noiseTapeLaw ν).map (f.history n)) ⊗ₘ κ n := by
    ext B hB
    rw [Measure.map_apply (measurable_fst.prodMk (f.measurable_step n)) hB,
      Measure.prod_apply (hB.preimage (measurable_fst.prodMk (f.measurable_step n))),
      Measure.compProd_apply hB]
    congr 1
    funext s
    have hm : Measurable (fun u : N => f.step n (s,u)) := by
      simpa only [Function.comp_def, id_eq] using
        ((f.measurable_step n).comp ((measurable_const (a := s)).prodMk measurable_id))
    rw [← hlaw n s, Measure.map_apply hm (measurable_prodMk_left hB)]
    rfl
  have hs : Measurable (fun p : (Fin n → O) × O => Fin.snoc (α := fun _ : Fin (n+1) => O) p.1 p.2) := by
    apply Measurable.of_eval
    intro i
    refine Fin.lastCases ?_ (fun j => ?_) i
    · simpa only [Fin.snoc_last] using measurable_snd
    · simpa only [Fin.snoc_castSucc, Function.comp_def] using (measurable_pi_apply j).comp measurable_fst
  rw [← hsample, ← hfresh,
    Measure.map_map (measurable_fst.prodMk (f.measurable_step n))
      ((f.measurable_prefix n).prodMk (measurable_pi_apply n)),
    Measure.map_map hs ((measurable_fst.prodMk (f.measurable_step n)).comp
      ((f.measurable_prefix n).prodMk (measurable_pi_apply n)))]
  rfl
3035
def record (u : (ℕ → N)) (n : ℕ) : O := f.step n (f.history n u,u n)
3037
lemma measurable_record : Measurable f.record :=
proof · 2 lines
  Measurable.of_eval (fun n =>
    (f.measurable_step n).comp ((f.measurable_prefix n).prodMk (measurable_pi_apply n)))
3041
lemma prefix_eq_record (n : ℕ) (u : (ℕ → N)) (i : Fin n) :
    f.history n u i = f.record u i := by
proof · 7 lines
  induction n with
  | zero => exact Fin.elim0 i
  | succ n ih =>
    rw [f.prefix_succ]
    refine Fin.lastCases ?_ (fun j => ?_) i
    · simp only [Fin.snoc_last,record, Fin.val_last]
    · simpa only [Fin.snoc_castSucc, Fin.val_castSucc] using ih j
3051
theorem finite_law_unique (κ : (n : ℕ) → Kernel (Fin n → O) O)
    [∀ n, IsMarkovKernel (κ n)]
    (hlaw : ∀ n s, ν.map (fun u => f.step n (s,u)) = κ n s)
    (P : Measure (ℕ → O)) [IsProbabilityMeasure P]
    (hP : ∀ n, P.map (fun x => fun i : Fin (n+1) => x i) =
      ((P.map (fun x => fun i : Fin n => x i)) ⊗ₘ κ n).map
        (fun p : (Fin n → O) × O => Fin.snoc (α := fun _ : Fin (n+1) => O) p.1 p.2)) :
    ∀ n, P.map (fun x => fun i : Fin n => x i) = (noiseTapeLaw ν).map (f.history n) := by
proof · 11 lines
  intro n
  induction n with
  | zero =>
    have ht : (fun x : ℕ → O => fun i : Fin 0 => x i) = fun _ => Fin.elim0 := by
      funext x i
      exact Fin.elim0 i
    rw [ht]
    change P.map (fun _ : ℕ → O => Fin.elim0) =
      (noiseTapeLaw ν).map (fun _ : (ℕ → N) => Fin.elim0)
    simp
  | succ n ih => rw [hP n, ih, f.prefix_law_succ κ hlaw]
3071
theorem record_law_unique (κ : (n : ℕ) → Kernel (Fin n → O) O)
    [∀ n, IsMarkovKernel (κ n)]
    (hlaw : ∀ n s, ν.map (fun u => f.step n (s,u)) = κ n s)
    (P : Measure (ℕ → O)) [IsProbabilityMeasure P]
    (hP : ∀ n, P.map (fun x => fun i : Fin (n+1) => x i) =
      ((P.map (fun x => fun i : Fin n => x i)) ⊗ₘ κ n).map
        (fun p : (Fin n → O) × O => Fin.snoc (α := fun _ : Fin (n+1) => O) p.1 p.2)) :
    P = (noiseTapeLaw ν).map f.record := by
proof · 19 lines
  let Q := (noiseTapeLaw ν).map f.record
  have hQ : IsProbabilityMeasure Q := inferInstance
  apply IsProjectiveLimit.unique (P := fun J : Finset ℕ => Q.map J.restrict) ?_
    (show IsProjectiveLimit Q (fun J : Finset ℕ => Q.map J.restrict) from fun _ => rfl)
  intro J
  let n := J.sup id + 1
  let index : J → Fin n := fun j => ⟨j.1,Nat.lt_succ_of_le (Finset.le_sup (f := id) j.2)⟩
  let cut : (Fin n → O) → (J → O) := fun x j => x (index j)
  have hc : Measurable cut := by fun_prop
  have h := congrArg (fun ν : Measure (Fin n → O) => ν.map cut)
    (f.finite_law_unique κ hlaw P hP n)
  rw [Measure.map_map hc (by fun_prop), Measure.map_map hc (f.measurable_prefix n)] at h
  change P.map J.restrict = ((noiseTapeLaw ν).map f.record).map J.restrict
  rw [Measure.map_map (Finset.measurable_restrict J) f.measurable_record]
  have heL : cut ∘ (fun x : ℕ → O => fun i : Fin n => x i) = J.restrict := rfl
  have heR : cut ∘ f.history n = J.restrict ∘ f.record := by
    funext u j
    exact f.prefix_eq_record n u (index j)
  rwa [heL,heR] at h
3105
structure AdaptiveOutcomeSampler (O : Type v6315_0) (M : Type v6315_1) [MeasurableSpace O] [MeasurableSpace M] where
body · 10 lines
  alice : (n : ℕ) → (Fin n → M) → Bool
  alice_meas : ∀ n, Measurable (alice n)
  sampleA : (n : ℕ) → ((Fin n → (O ⊕ Unit)) × (Fin n → M)) × I → O
  sampleB : (n : ℕ) → ((Fin n → (O ⊕ Unit)) × (Fin n → M)) × I → O
  sendA : (n : ℕ) → ((Fin n → (O ⊕ Unit)) × (Fin n → M)) × O → M
  sendB : (n : ℕ) → ((Fin n → (O ⊕ Unit)) × (Fin n → M)) × O → M
  sampleA_meas : ∀ n, Measurable (sampleA n)
  sampleB_meas : ∀ n, Measurable (sampleB n)
  sendA_meas : ∀ n, Measurable (sendA n)
  sendB_meas : ∀ n, Measurable (sendB n)
3126
def privatePrefix (a : Bool) : (n : ℕ) → UniformTape × (Fin n → M) → (Fin n → (O ⊕ Unit))
body · 7 lines
  | 0, _ => Fin.elim0
  | n+1, pair =>
    let old := privatePrefix a n (pair.1,fun i => pair.2 i.castSucc)
    let pubPast := fun i : Fin n => pair.2 i.castSucc
    Fin.snoc old (if p.alice n pubPast = a then Sum.inl
      (if a then p.sampleA n ((old,pubPast),pair.1 n)
       else p.sampleB n ((old,pubPast),pair.1 n)) else Sum.inr ())
3135
lemma measurable_privatePrefix (a : Bool) : ∀ n, Measurable (p.privatePrefix a n) := by
proof · 21 lines
  intro n
  induction n with
  | zero => exact Measurable.of_eval (fun i => Fin.elim0 i)
  | succ n ih =>
    have hr : Measurable (fun pair : UniformTape × (Fin (n+1) → M) =>
      (pair.1,fun i : Fin n => pair.2 i.castSucc)) := by fun_prop
    have hv : Measurable (fun pair : UniformTape × (Fin (n+1) → M) =>
      fun i : Fin n => pair.2 i.castSucc) := by fun_prop
    apply Measurable.of_eval
    intro i
    refine Fin.lastCases ?_ (fun j => ?_) i
    · simp only [privatePrefix,Fin.snoc_last]
      apply Measurable.ite ((measurableSet_singleton a).preimage ((p.alice_meas n).comp hv)) _ measurable_const
      apply measurable_inl.comp
      cases a
      · exact (p.sampleB_meas n).comp (((ih.comp hr).prodMk hv).prodMk
          ((measurable_pi_apply n).comp measurable_fst))
      · exact (p.sampleA_meas n).comp (((ih.comp hr).prodMk hv).prodMk
          ((measurable_pi_apply n).comp measurable_fst))
    · simpa only [privatePrefix,Fin.snoc_castSucc,Function.comp_def] using
        (ih.comp hr).eval (a := j)
3158
def tapeProgram : AdaptiveTapeProgram UniformTape UniformTape M where
body · 14 lines
  alice := p.alice
  measAlice := p.alice_meas
  sendA n pair := p.sendA n ((p.privatePrefix true n pair,pair.2),
    p.sampleA n ((p.privatePrefix true n pair,pair.2),pair.1 n))
  sendB n pair := p.sendB n ((p.privatePrefix false n pair,pair.2),
    p.sampleB n ((p.privatePrefix false n pair,pair.2),pair.1 n))
  measA n := (p.sendA_meas n).comp
    (((p.measurable_privatePrefix true n).prodMk measurable_snd).prodMk
      ((p.sampleA_meas n).comp (((p.measurable_privatePrefix true n).prodMk measurable_snd).prodMk
        ((measurable_pi_apply n).comp measurable_fst))))
  measB n := (p.sendB_meas n).comp
    (((p.measurable_privatePrefix false n).prodMk measurable_snd).prodMk
      ((p.sampleB_meas n).comp (((p.measurable_privatePrefix false n).prodMk measurable_snd).prodMk
        ((measurable_pi_apply n).comp measurable_fst))))
3174
def replay : (n : ℕ) → (Fin n → O) →
    ((Fin n → (O ⊕ Unit)) × (Fin n → (O ⊕ Unit))) × (Fin n → M) :=
body · 7 lines
  Nat.rec (fun _ => ((Fin.elim0,Fin.elim0),Fin.elim0)) (fun n previous x =>
    let r := previous (fun i => x i.castSucc)
    let o := x (Fin.last n)
    let q := p.alice n r.2
    ((Fin.snoc r.1.1 (if q then Sum.inl o else Sum.inr ()),
      Fin.snoc r.1.2 (if q then Sum.inr () else Sum.inl o)),
     Fin.snoc r.2 (if q then p.sendA n ((r.1.1,r.2),o) else p.sendB n ((r.1.2,r.2),o))))
3186
lemma measurable_snoc {Z : Type v6393_0} {X : Type v6393_1} [MeasurableSpace Z] [MeasurableSpace X]
    {n : ℕ} (f : Z → (Fin n → X)) (g : Z → X)
    (hf : Measurable f) (hg : Measurable g) :
    Measurable (fun x => Fin.snoc (α := fun _ : Fin (n+1) => X) (f x) (g x)) := by
proof · 5 lines
  apply Measurable.of_eval
  intro i
  refine Fin.lastCases ?_ (fun j => ?_) i
  · simpa only [Fin.snoc_last] using hg
  · simpa only [Fin.snoc_castSucc] using hf.eval (a := j)
3196
lemma measurable_replay : ∀ n, Measurable (p.replay n) := by
proof · 12 lines
  intro n
  induction n with
  | zero => exact measurable_const
  | succ n ih =>
    have hr : Measurable (fun x : Fin (n+1) → O => fun i : Fin n => x i.castSucc) := by fun_prop
    have hR := ih.comp hr
    have hq := (p.alice_meas n).comp hR.snd
    have hqs := (measurableSet_singleton true).preimage hq
    exact ((measurable_snoc _ _ hR.fst.fst ((measurable_inl.comp (measurable_pi_apply (Fin.last n))).ite hqs measurable_const)).prodMk
      (measurable_snoc _ _ hR.fst.snd (measurable_const.ite hqs (measurable_inl.comp (measurable_pi_apply (Fin.last n)))))).prodMk
      (measurable_snoc _ _ hR.snd (((p.sendA_meas n).comp ((hR.fst.fst.prodMk hR.snd).prodMk (measurable_pi_apply (Fin.last n)))).ite hqs
        ((p.sendB_meas n).comp ((hR.fst.snd.prodMk hR.snd).prodMk (measurable_pi_apply (Fin.last n))))))
3210
def extractPrivate (a : Bool) (n : ℕ) (x : Fin n → O) : Fin n → O ⊕ Unit :=
body · 1 line
  if a then (p.replay n x).1.1 else (p.replay n x).1.2
3213
def publicFrom (n : ℕ) (x : Fin n → O) : Fin n → M := (p.replay n x).2
3215
lemma measurable_extractPrivate (a : Bool) (n : ℕ) : Measurable (p.extractPrivate a n) := by
proof · 3 lines
  cases a
  · exact (p.measurable_replay n).fst.snd
  · exact (p.measurable_replay n).fst.fst
3220
lemma measurable_publicFrom (n : ℕ) : Measurable (p.publicFrom n) := (p.measurable_replay n).snd
3222
lemma extractPrivate_snoc (a : Bool) (n : ℕ) (x : Fin n → O) (z : O) :
    p.extractPrivate a (n+1) (Fin.snoc (α := fun _ => O) x z) =
      Fin.snoc (p.extractPrivate a n x) (if p.alice n (p.publicFrom n x) = a then Sum.inl z else Sum.inr ()) := by
proof · 2 lines
  cases a <;> cases hq : p.alice n (p.publicFrom n x) <;>
    simp only [extractPrivate,replay,Fin.snoc_castSucc,Fin.snoc_last,publicFrom] at hq ⊢ <;> simp [hq]
3228
lemma publicFrom_snoc (n : ℕ) (x : Fin n → O) (z : O) :
    p.publicFrom (n+1) (Fin.snoc (α := fun _ => O) x z) =
      Fin.snoc (p.publicFrom n x) (if p.alice n (p.publicFrom n x) then
        p.sendA n ((p.extractPrivate true n x,p.publicFrom n x),z) else
        p.sendB n ((p.extractPrivate false n x,p.publicFrom n x),z)) := by
proof · 1 line
  simp only [publicFrom,replay,extractPrivate,Fin.snoc_castSucc,Fin.snoc_last,Bool.false_eq_true,↓reduceIte]
3235
lemma privatePrefix_snoc (a : Bool) (n : ℕ) (u : UniformTape) (h : Fin n → M) (z : M) :
    p.privatePrefix a (n+1) (u,Fin.snoc (α := fun _ => M) h z) =
      Fin.snoc (p.privatePrefix a n (u,h)) (if p.alice n h = a then Sum.inl
        (if a then p.sampleA n ((p.privatePrefix a n (u,h),h),u n)
        else p.sampleB n ((p.privatePrefix a n (u,h),h),u n)) else Sum.inr ()) := by
proof · 1 line
  simp only [privatePrefix,Fin.snoc_castSucc]

The sampled outcome history, without a bundled measurability certificate.

3243
def causalStep (n : ℕ) (pair : (Fin n → O) × (I × I)) : O :=
body · 3 lines
  if p.alice n (p.publicFrom n pair.1) then
    p.sampleA n ((p.extractPrivate true n pair.1, p.publicFrom n pair.1), pair.2.1)
  else p.sampleB n ((p.extractPrivate false n pair.1, p.publicFrom n pair.1), pair.2.2)
3248
def causalHistory : (n : ℕ) → (Fin n → I × I) → (Fin n → O)
body · 3 lines
  | 0, _ => Fin.elim0
  | n+1, u => Fin.snoc (causalHistory n (fun i => u i.castSucc))
      (p.causalStep n (causalHistory n (fun i => u i.castSucc), u (Fin.last n)))
3253
def causalRecord (u : ℕ → I × I) (n : ℕ) : O :=
body · 1 line
  p.causalStep n (p.causalHistory n (fun i => u i), u n)
3256
def causal : NoiseSampler (I × I) O where
body · 9 lines
  step n pair := if p.alice n (p.publicFrom n pair.1) then
    p.sampleA n ((p.extractPrivate true n pair.1,p.publicFrom n pair.1),pair.2.1) else
    p.sampleB n ((p.extractPrivate false n pair.1,p.publicFrom n pair.1),pair.2.2)
  measurable_step n :=
    ((p.sampleA_meas n).comp ((((p.measurable_extractPrivate true n).comp measurable_fst).prodMk
        ((p.measurable_publicFrom n).comp measurable_fst)).prodMk measurable_snd.fst)).ite
      ((measurableSet_singleton true).preimage ((p.alice_meas n).comp ((p.measurable_publicFrom n).comp measurable_fst)))
      ((p.sampleB_meas n).comp ((((p.measurable_extractPrivate false n).comp measurable_fst).prodMk
        ((p.measurable_publicFrom n).comp measurable_fst)).prodMk measurable_snd.snd))
3267
lemma causalRecord_eq : p.causalRecord = p.causal.record := by
proof · 12 lines
  have h (n : ℕ) : p.causalHistory n = p.causal.finiteHistory n := by
    induction n with
    | zero => rfl
    | succ n ih =>
      funext u
      simp only [causalHistory, NoiseSampler.finiteHistory, ih]
      rfl
  funext u n
  change p.causalStep n (p.causalHistory n (fun i => u i), u n) =
    p.causal.step n (p.causal.finiteHistory n (fun i => u i), u n)
  rw [h n]
  rfl
3281
def pairedTape (uv : UniformTape × UniformTape) (n : ℕ) : I × I := (uv.1 n,uv.2 n)
3283
lemma measurable_pairedTape : Measurable pairedTape := by unfold pairedTape; fun_prop
3285
theorem replay_matches (n : ℕ) (uv : UniformTape × UniformTape) :
    p.publicFrom n (p.causal.history n (pairedTape uv)) = p.tapeProgram.publicPrefix n uv ∧
    (∀ a, p.extractPrivate a n (p.causal.history n (pairedTape uv)) =
      p.privatePrefix a n ((if a then uv.1 else uv.2),p.tapeProgram.publicPrefix n uv)) := by
proof · 28 lines
  induction n with
  | zero => exact ⟨rfl,fun a => by cases a <;> rfl⟩
  | succ n ih =>
    have hs : p.causal.step n (p.causal.history n (pairedTape uv),pairedTape uv n) =
      if p.alice n (p.tapeProgram.publicPrefix n uv) then
        p.sampleA n ((p.privatePrefix true n (uv.1,p.tapeProgram.publicPrefix n uv),
          p.tapeProgram.publicPrefix n uv),uv.1 n) else
        p.sampleB n ((p.privatePrefix false n (uv.2,p.tapeProgram.publicPrefix n uv),
          p.tapeProgram.publicPrefix n uv),uv.2 n) := by
      change (if p.alice n (p.publicFrom n (p.causal.history n (pairedTape uv))) then
        p.sampleA n ((p.extractPrivate true n (p.causal.history n (pairedTape uv)),
          p.publicFrom n (p.causal.history n (pairedTape uv))),uv.1 n)
        else p.sampleB n ((p.extractPrivate false n (p.causal.history n (pairedTape uv)),
          p.publicFrom n (p.causal.history n (pairedTape uv))),uv.2 n)) = _
      rw [ih.1,ih.2 true,ih.2 false]
      simp only [Bool.false_eq_true,↓reduceIte]
    rw [p.causal.prefix_succ,p.publicFrom_snoc,hs,ih.1,ih.2 true,ih.2 false]
    simp only [Bool.false_eq_true,↓reduceIte]
    constructor
    · rw [p.tapeProgram.prefix_succ]
      congr 1
      unfold AdaptiveTapeProgram.message tapeProgram
      split <;> simp_all only []
    · intro a
      rw [p.extractPrivate_snoc,ih.2 a,ih.1,p.tapeProgram.prefix_succ,p.privatePrefix_snoc]
      congr 1
      cases a <;> cases hrole : p.alice n (p.tapeProgram.publicPrefix n uv) <;>
        simp only [Bool.false_eq_true,Bool.true_eq_false,↓reduceIte]
3325
def instrumentKernel
    (κA κB : (n : ℕ) → Kernel ((Fin n → (O ⊕ Unit)) × (Fin n → M)) O)
    (n : ℕ) : Kernel (Fin n → O) O := by
body · 7 lines
  classical
  exact Kernel.piecewise
    ((measurableSet_singleton true).preimage ((p.alice_meas n).comp (p.measurable_publicFrom n)))
    ((κA n).comap (fun x => (p.extractPrivate true n x,p.publicFrom n x))
      ((p.measurable_extractPrivate true n).prodMk (p.measurable_publicFrom n)))
    ((κB n).comap (fun x => (p.extractPrivate false n x,p.publicFrom n x))
      ((p.measurable_extractPrivate false n).prodMk (p.measurable_publicFrom n)))
3336
lemma instrumentKernel_apply
    (κA κB : (n : ℕ) → Kernel ((Fin n → (O ⊕ Unit)) × (Fin n → M)) O)
    (n : ℕ) (s : Fin n → O) :
    p.instrumentKernel κA κB n s = if p.alice n (p.publicFrom n s) then
      κA n (p.extractPrivate true n s,p.publicFrom n s) else
      κB n (p.extractPrivate false n s,p.publicFrom n s) := by
proof · 3 lines
  classical
  simp only [instrumentKernel,Kernel.piecewise_apply,Set.mem_preimage,Set.mem_singleton_iff,
    Function.comp_def,Kernel.comap_apply]
3346
instance instrumentKernel_markov
    (κA κB : (n : ℕ) → Kernel ((Fin n → (O ⊕ Unit)) × (Fin n → M)) O)
    [∀ n, IsMarkovKernel (κA n)] [∀ n, IsMarkovKernel (κB n)] (n : ℕ) :
    IsMarkovKernel (p.instrumentKernel κA κB n) := by
body · 2 lines
  unfold instrumentKernel
  infer_instance
3353
lemma causal_step_law
    (κA κB : (n : ℕ) → Kernel ((Fin n → (O ⊕ Unit)) × (Fin n → M)) O)
    (ha : ∀ n s, volume.map (fun u => p.sampleA n (s,u)) = κA n s)
    (hb : ∀ n s, volume.map (fun u => p.sampleB n (s,u)) = κB n s) :
    ∀ n s, (volume.prod volume).map (fun u => p.causal.step n (s,u)) =
      p.instrumentKernel κA κB n s := by
proof · 18 lines
  intro n s
  rw [p.instrumentKernel_apply κA κB n s]
  change (volume.prod volume).map (fun u => if p.alice n (p.publicFrom n s) then
    p.sampleA n ((p.extractPrivate true n s,p.publicFrom n s),u.1) else
    p.sampleB n ((p.extractPrivate false n s,p.publicFrom n s),u.2)) = _
  split
  · have hm : Measurable (fun u : I => p.sampleA n ((p.extractPrivate true n s,p.publicFrom n s),u)) :=
      (p.sampleA_meas n).comp (measurable_const.prodMk measurable_id)
    have hh := Measure.map_map (μ := (volume : Measure I).prod volume) hm
      (measurable_fst : Measurable (Prod.fst : I × I → I))
    simp only [Measure.map_fst_prod, measure_univ, one_smul] at hh
    exact hh.symm.trans (ha n _)
  · have hm : Measurable (fun u : I => p.sampleB n ((p.extractPrivate false n s,p.publicFrom n s),u)) :=
      (p.sampleB_meas n).comp (measurable_const.prodMk measurable_id)
    have hh := Measure.map_map (μ := (volume : Measure I).prod volume) hm
      (measurable_snd : Measurable (Prod.snd : I × I → I))
    simp only [Measure.map_snd_prod, measure_univ, one_smul] at hh
    exact hh.symm.trans (hb n _)
3403
def globalLocalReferenceA (n : ℕ) (s : Fin n → O) : Matrix ι ι ℂ :=
body · 1 line
  eA.reference n (p.extractPrivate true n s,p.publicFrom n s)
3406
def globalLocalReferenceB (n : ℕ) (s : Fin n → O) : Matrix η η ℂ :=
body · 1 line
  eB.reference n (p.extractPrivate false n s,p.publicFrom n s)
3410
lemma globalLocalReferenceA_measurable [∀ n, IsMarkovKernel (κA n)] (n : ℕ) :
    Measurable (globalLocalReferenceA p κA eA n) :=
proof · 2 lines
  (eA.reference_measurable n).comp
    ((p.measurable_extractPrivate true n).prodMk (p.measurable_publicFrom n))
3416
lemma globalLocalReferenceB_measurable [∀ n, IsMarkovKernel (κB n)] (n : ℕ) :
    Measurable (globalLocalReferenceB p κB eB n) :=
proof · 2 lines
  (eB.reference_measurable n).comp
    ((p.measurable_extractPrivate false n).prodMk (p.measurable_publicFrom n))
3422
lemma globalLocalReferenceA_mean [∀ n, IsMarkovKernel (κA n)] (n : ℕ) (s : Fin n → O) (i j : ι) :
    (∫ o, globalLocalReferenceA p κA eA (n+1) (Fin.snoc s o) i j
      ∂p.instrumentKernel κA κB n s) = globalLocalReferenceA p κA eA n s i j := by
proof · 15 lines
  unfold globalLocalReferenceA
  simp_rw [p.extractPrivate_snoc, p.publicFrom_snoc]
  rw [p.instrumentKernel_apply]
  cases hrole : p.alice n (p.publicFrom n s) with
  | true =>
    simp only [↓reduceIte]
    exact eA.active_mean n _ i j
  | false =>
    simp only [Bool.false_eq_true,↓reduceIte]
    change (∫ o, eA.reference (n+1) (localPassive
      (p.extractPrivate true n s,p.publicFrom n s)
      (p.sendB n ((p.extractPrivate false n s,p.publicFrom n s),o))) i j
      ∂κB n (p.extractPrivate false n s,p.publicFrom n s)) = _
    simp only [eA.passive_reference]
    simp
3442
lemma globalLocalReferenceB_mean [∀ n, IsMarkovKernel (κB n)] (n : ℕ) (s : Fin n → O) (i j : η) :
    (∫ o, globalLocalReferenceB p κB eB (n+1) (Fin.snoc s o) i j
      ∂p.instrumentKernel κA κB n s) = globalLocalReferenceB p κB eB n s i j := by
proof · 15 lines
  unfold globalLocalReferenceB
  simp_rw [p.extractPrivate_snoc, p.publicFrom_snoc]
  rw [p.instrumentKernel_apply]
  cases hrole : p.alice n (p.publicFrom n s) with
  | false =>
    simp only [↓reduceIte]
    exact eB.active_mean n _ i j
  | true =>
    simp only [Bool.true_eq_false,↓reduceIte]
    change (∫ o, eB.reference (n+1) (localPassive
      (p.extractPrivate false n s,p.publicFrom n s)
      (p.sendA n ((p.extractPrivate true n s,p.publicFrom n s),o))) i j
      ∂κA n (p.extractPrivate true n s,p.publicFrom n s)) = _
    simp only [eB.passive_reference]
    simp
3461
def jointReference (n : ℕ) (s : Fin n → O) : Matrix (ι × η) (ι × η) ℂ :=
body · 1 line
  globalLocalReferenceA p κA eA n s ⊗ₖ globalLocalReferenceB p κB eB n s
3465
lemma jointReference_density [∀ n, IsMarkovKernel (κA n)] [∀ n, IsMarkovKernel (κB n)] (n : ℕ) (s : Fin n → O) :
    Density (jointReference p κA κB eA eB n s) := by
proof · 3 lines
  refine ⟨(eA.reference_density n _).1.kronecker (eB.reference_density n _).1,?_⟩
  simp only [jointReference, Matrix.trace_kronecker,globalLocalReferenceA,
    globalLocalReferenceB,(eA.reference_density n _).2,(eB.reference_density n _).2,one_mul]
3471
lemma jointReference_measurable (n : ℕ) :
    Measurable (jointReference p κA κB eA eB n) := by
proof · 10 lines
  have hm : Measurable (fun s i j => jointReference p κA κB eA eB n s i j) := by
    apply Measurable.of_eval; intro i
    apply Measurable.of_eval; intro j
    exact (((Matrix.entryLinearMap ℂ ℂ i.1 j.1).toContinuousLinearMap).measurable.comp
      (globalLocalReferenceA_measurable p κA eA n)).mul
      (((Matrix.entryLinearMap ℂ ℂ i.2 j.2).toContinuousLinearMap).measurable.comp
      (globalLocalReferenceB_measurable p κB eB n))
  have hM : Continuous (Matrix.of : ((ι × η) → (ι × η) → ℂ) → Matrix (ι × η) (ι × η) ℂ) :=
    continuous_matrix (fun i j => continuous_apply_apply i j)
  exact hM.measurable.comp hm
3485
lemma jointReference_mean [∀ n, IsMarkovKernel (κA n)] [∀ n, IsMarkovKernel (κB n)] (n : ℕ) (s : Fin n → O) (i j : ι × η) :
    (∫ o, jointReference p κA κB eA eB (n+1) (Fin.snoc s o) i j
      ∂p.instrumentKernel κA κB n s) = jointReference p κA κB eA eB n s i j := by
proof · 28 lines
  change (∫ o, globalLocalReferenceA p κA eA (n+1) (Fin.snoc s o) i.1 j.1 *
    globalLocalReferenceB p κB eB (n+1) (Fin.snoc s o) i.2 j.2
    ∂p.instrumentKernel κA κB n s) =
    globalLocalReferenceA p κA eA n s i.1 j.1 * globalLocalReferenceB p κB eB n s i.2 j.2
  simp only [globalLocalReferenceA,globalLocalReferenceB]
  simp_rw [p.extractPrivate_snoc,p.publicFrom_snoc]
  rw [p.instrumentKernel_apply]
  cases hrole : p.alice n (p.publicFrom n s) with
  | true =>
    simp only [Bool.true_eq_false,↓reduceIte]
    change (∫ o, eA.reference (n+1) (localActive
      (p.extractPrivate true n s,p.publicFrom n s) o
      (p.sendA n ((p.extractPrivate true n s,p.publicFrom n s),o))) i.1 j.1 *
      eB.reference (n+1) (localPassive (p.extractPrivate false n s,p.publicFrom n s)
      (p.sendA n ((p.extractPrivate true n s,p.publicFrom n s),o))) i.2 j.2
      ∂κA n (p.extractPrivate true n s,p.publicFrom n s)) = _
    simp_rw [eB.passive_reference]
    rw [integral_mul_const,eA.active_mean]
  | false =>
    simp only [Bool.false_eq_true,↓reduceIte]
    change (∫ o, eA.reference (n+1) (localPassive
      (p.extractPrivate true n s,p.publicFrom n s)
      (p.sendB n ((p.extractPrivate false n s,p.publicFrom n s),o))) i.1 j.1 *
      eB.reference (n+1) (localActive (p.extractPrivate false n s,p.publicFrom n s) o
      (p.sendB n ((p.extractPrivate false n s,p.publicFrom n s),o))) i.2 j.2
      ∂κB n (p.extractPrivate false n s,p.publicFrom n s)) = _
    simp_rw [eA.passive_reference]
    rw [integral_const_mul,eB.active_mean]
3523
theorem paired_tapes_law :
    (uniformTapeLaw.prod uniformTapeLaw).map
      (fun uv : UniformTape × UniformTape => fun n => (uv.1 n,uv.2 n)) =
      noiseTapeLaw ((volume : Measure I).prod volume) := by
proof · 26 lines
  let ν : Measure ((Fin 2 × ℕ) → I) := Measure.infinitePi (fun _ => (volume : Measure I))
  let split : ((Fin 2 × ℕ) → I) → UniformTape × UniformTape :=
    fun u => (fun n => u (0,n),fun n => u (1,n))
  have hs : Measurable split := by fun_prop
  have hsplit : ν.map split = uniformTapeLaw.prod uniformTapeLaw := by
    have hc := Measure.infinitePi_map_curry (fun (_ : Fin 2) (_ : ℕ) => (volume : Measure I))
    change ν.map (MeasurableEquiv.curry (Fin 2) ℕ I) =
      Measure.infinitePi (fun _ : Fin 2 => uniformTapeLaw) at hc
    rw [Measure.infinitePi_eq_pi (fun _ : Fin 2 => uniformTapeLaw)] at hc
    have h := congrArg (fun P : Measure (Fin 2 → UniformTape) => P.map MeasurableEquiv.finTwoArrow) hc
    rw [Measure.map_map (MeasurableEquiv.measurable _) (MeasurableEquiv.measurable _),
      (measurePreserving_finTwoArrow uniformTapeLaw).map_eq] at h
    exact h
  have hswap := Measure.map_infinitePi_infinitePi_of_inj
    (P := fun _ : Fin 2 × ℕ => (volume : Measure I))
    (f := fun p : ℕ × Fin 2 => (p.2,p.1)) (by intro a b h; cases a; cases b; simpa only [Prod.mk.injEq,and_comm] using h)
  have hc := Measure.infinitePi_map_curry (fun (_ : ℕ) (_ : Fin 2) => (volume : Measure I))
  have hp := Measure.infinitePi_map_pi (fun _ : ℕ => Measure.infinitePi (fun _ : Fin 2 => (volume : Measure I)))
    (f := fun _ => MeasurableEquiv.finTwoArrow) (fun _ => MeasurableEquiv.measurable _)
  simp only [Measure.infinitePi_eq_pi, (measurePreserving_finTwoArrow (volume : Measure I)).map_eq] at hp
  rw [← hsplit, Measure.map_map (by fun_prop) hs]
  unfold noiseTapeLaw
  rw [Measure.infinitePi_eq_pi (fun _ : Fin 2 => (volume : Measure I))] at hc
  rw [← hp, ← hc, ← hswap]
  rw [Measure.map_map (by fun_prop) (by fun_prop),Measure.map_map (by fun_prop) (by fun_prop)]
  rfl
3562
theorem condIndepFun_of_joint_law {Ω : Type v6786_0} {Ω' : Type v6786_1} {T : Type v6786_2} {A : Type v6786_3} {B : Type v6786_4}
    [MeasurableSpace Ω] [StandardBorelSpace Ω]
    [MeasurableSpace Ω'] [StandardBorelSpace Ω']
    [MeasurableSpace T] [MeasurableSpace A] [StandardBorelSpace A] [Nonempty A]
    [MeasurableSpace B] [StandardBorelSpace B] [Nonempty B]
    {P : Measure Ω} {Q : Measure Ω'} [IsFiniteMeasure P] [IsFiniteMeasure Q]
    {t : Ω → T} {a : Ω → A} {b : Ω → B}
    {t' : Ω' → T} {a' : Ω' → A} {b' : Ω' → B}
    (ht : Measurable t) (ha : Measurable a) (hb : Measurable b)
    (ht' : Measurable t') (ha' : Measurable a') (hb' : Measurable b')
    (hlaw : P.map (fun x => (t x,a x,b x)) = Q.map (fun x => (t' x,a' x,b' x)))
    (hind : CondIndepFun (MeasurableSpace.comap t' inferInstance) ht'.comap_le a' b' Q) :
    CondIndepFun (MeasurableSpace.comap t inferInstance) ht.comap_le a b P := by
proof · 24 lines
  have hm : Measurable (fun x => (t x,a x,b x)) := ht.prodMk (ha.prodMk hb)
  have hm' : Measurable (fun x => (t' x,a' x,b' x)) := ht'.prodMk (ha'.prodMk hb')
  have hT : P.map t = Q.map t' := by
    have h := congrArg (fun ν : Measure (T × A × B) => ν.map Prod.fst) hlaw
    simpa only [Measure.map_map measurable_fst hm,
      Measure.map_map measurable_fst hm', Function.comp_def] using h
  have hA : P.map (fun x => (t x,a x)) = Q.map (fun x => (t' x,a' x)) := by
    have hp : Measurable (fun x : T × A × B => (x.1,x.2.1)) := by fun_prop
    have h := congrArg (fun ν : Measure (T × A × B) => ν.map (fun x => (x.1,x.2.1))) hlaw
    simpa only [Measure.map_map hp hm, Measure.map_map hp hm', Function.comp_def] using h
  have hB : P.map (fun x => (t x,b x)) = Q.map (fun x => (t' x,b' x)) := by
    have hp : Measurable (fun x : T × A × B => (x.1,x.2.2)) := by fun_prop
    have h := congrArg (fun ν : Measure (T × A × B) => ν.map (fun x => (x.1,x.2.2))) hlaw
    simpa only [Measure.map_map hp hm, Measure.map_map hp hm', Function.comp_def] using h
  rw [condIndepFun_iff_map_prod_eq_prod_condDistrib_prod_condDistrib ha hb ht]
  rw [condIndepFun_iff_map_prod_eq_prod_condDistrib_prod_condDistrib ha' hb' ht'] at hind
  have hca : condDistrib a t P = condDistrib a' t' Q := by
    rw [condDistrib, condDistrib]
    congr 1
  have hcb : condDistrib b t P = condDistrib b' t' Q := by
    rw [condDistrib, condDistrib]
    congr 1
  rw [hlaw,hca,hcb,hT]
  exact hind
3611
def privateRecord (a : Bool) (pair : UniformTape × (ℕ → M)) (n : ℕ) : O ⊕ Unit :=
body · 1 line
  p.privatePrefix a (n+1) (pair.1,fun i => pair.2 i) (Fin.last n)
3614
lemma measurable_privateRecord (a : Bool) : Measurable (p.privateRecord a) := by
proof · 3 lines
  apply Measurable.of_eval
  intro n
  exact ((p.measurable_privatePrefix a (n+1)).comp (by fun_prop)).eval (a := Fin.last n)
3621
theorem final_records_independent [Nonempty M] {A : Type v6846_0} {B : Type v6846_1} [MeasurableSpace A] [MeasurableSpace B]
    (readA : (ℕ → (O ⊕ Unit)) × (ℕ → M) → A)
    (readB : (ℕ → (O ⊕ Unit)) × (ℕ → M) → B)
    (ha : Measurable readA) (hb : Measurable readB) :
    CondIndepFun (MeasurableSpace.comap p.tapeProgram.record inferInstance)
      p.tapeProgram.measurable_record.comap_le
      (fun uv => readA (p.privateRecord true (uv.1,p.tapeProgram.record uv),p.tapeProgram.record uv))
      (fun uv => readB (p.privateRecord false (uv.2,p.tapeProgram.record uv),p.tapeProgram.record uv))
      (uniformTapeLaw.prod uniformTapeLaw) := by
proof · 5 lines
  exact p.tapeProgram.final_records_conditionally_independent uniformTapeLaw uniformTapeLaw
    (fun pair => readA (p.privateRecord true pair,pair.2))
    (fun pair => readB (p.privateRecord false pair,pair.2))
    (ha.comp ((p.measurable_privateRecord true).prodMk measurable_snd))
    (hb.comp ((p.measurable_privateRecord false).prodMk measurable_snd))
3636
def outcomeRecord (uv : UniformTape × UniformTape) : ℕ → O :=
body · 1 line
  p.causal.record (pairedTape uv)
3639
lemma measurable_outcomeRecord : Measurable p.outcomeRecord :=
proof · 1 line
  p.causal.measurable_record.comp measurable_pairedTape
3642
lemma outcomeRecord_law (κ : (n : ℕ) → Kernel (Fin n → O) O)
    [∀ n, IsMarkovKernel (κ n)]
    (hlaw : ∀ n s, (volume.prod volume).map (fun u => p.causal.step n (s,u)) = κ n s)
    (P : Measure (ℕ → O)) [IsProbabilityMeasure P]
    (hP : ∀ n, P.map (fun x => fun i : Fin (n+1) => x i) =
      ((P.map (fun x => fun i : Fin n => x i)) ⊗ₘ κ n).map
        (fun pair : (Fin n → O) × O => Fin.snoc (α := fun _ : Fin (n+1) => O) pair.1 pair.2)) :
    (uniformTapeLaw.prod uniformTapeLaw).map p.outcomeRecord = P := by
proof · 5 lines
  rw [p.causal.record_law_unique κ hlaw P hP]
  change (uniformTapeLaw.prod uniformTapeLaw).map (p.causal.record ∘ pairedTape) = _
  rw [← Measure.map_map p.causal.measurable_record measurable_pairedTape]
  congr 1
  exact paired_tapes_law
3656
def publicRecord (x : ℕ → O) (n : ℕ) : M :=
body · 1 line
  p.publicFrom (n+1) (fun i => x i) (Fin.last n)
3659
def localRecord (a : Bool) (x : ℕ → O) (n : ℕ) : O ⊕ Unit :=
body · 1 line
  p.extractPrivate a (n+1) (fun i => x i) (Fin.last n)
3662
lemma measurable_publicRecord : Measurable p.publicRecord := by
proof · 3 lines
  apply Measurable.of_eval
  intro n
  exact ((p.measurable_publicFrom (n+1)).comp (by fun_prop)).eval (a := Fin.last n)
3667
lemma measurable_localRecord (a : Bool) : Measurable (p.localRecord a) := by
proof · 3 lines
  apply Measurable.of_eval
  intro n
  exact ((p.measurable_extractPrivate a (n+1)).comp (by fun_prop)).eval (a := Fin.last n)
3672
lemma publicRecord_replay (uv : UniformTape × UniformTape) :
    p.publicRecord (p.outcomeRecord uv) = p.tapeProgram.record uv := by
proof · 9 lines
  funext n
  have h := congrFun (p.replay_matches (n+1) uv).1 (Fin.last n)
  have he : (fun i : Fin (n+1) => p.outcomeRecord uv i) =
      p.causal.history (n+1) (pairedTape uv) := by
    funext i
    exact (p.causal.prefix_eq_record (n+1) (pairedTape uv) i).symm
  change p.publicFrom (n+1) (fun i => p.outcomeRecord uv i) (Fin.last n) = _
  rw [he,h,p.tapeProgram.prefix_eq_record]
  rfl
3684
lemma localRecord_replay (a : Bool) (uv : UniformTape × UniformTape) :
    p.localRecord a (p.outcomeRecord uv) =
      p.privateRecord a ((if a then uv.1 else uv.2),p.tapeProgram.record uv) := by
proof · 11 lines
  funext n
  have h := congrFun ((p.replay_matches (n+1) uv).2 a) (Fin.last n)
  have he : (fun i : Fin (n+1) => p.outcomeRecord uv i) =
      p.causal.history (n+1) (pairedTape uv) := by
    funext i
    exact (p.causal.prefix_eq_record (n+1) (pairedTape uv) i).symm
  change p.extractPrivate a (n+1) (fun i => p.outcomeRecord uv i) (Fin.last n) = _
  rw [he,h]
  congr 2
  funext i
  exact p.tapeProgram.prefix_eq_record (n+1) uv i
3701
theorem full_readout_law {A : Type v6928_0} {B : Type v6928_1} [MeasurableSpace A] [MeasurableSpace B]
    (κ : (n : ℕ) → Kernel (Fin n → O) O) [∀ n, IsMarkovKernel (κ n)]
    (hlaw : ∀ n s, (volume.prod volume).map (fun u => p.causal.step n (s,u)) = κ n s)
    (P : Measure (ℕ → O)) [IsProbabilityMeasure P]
    (hP : ∀ n, P.map (fun x => fun i : Fin (n+1) => x i) =
      ((P.map (fun x => fun i : Fin n => x i)) ⊗ₘ κ n).map
        (fun pair : (Fin n → O) × O => Fin.snoc (α := fun _ : Fin (n+1) => O) pair.1 pair.2))
    (readA : (ℕ → (O ⊕ Unit)) × (ℕ → M) → A)
    (readB : (ℕ → (O ⊕ Unit)) × (ℕ → M) → B)
    (ha : Measurable readA) (hb : Measurable readB) :
    P.map (fun x => (p.publicRecord x,
      readA (p.localRecord true x,p.publicRecord x),
      readB (p.localRecord false x,p.publicRecord x))) =
    (uniformTapeLaw.prod uniformTapeLaw).map (fun uv => (p.tapeProgram.record uv,
      readA (p.privateRecord true (uv.1,p.tapeProgram.record uv),p.tapeProgram.record uv),
      readB (p.privateRecord false (uv.2,p.tapeProgram.record uv),p.tapeProgram.record uv))) := by
proof · 12 lines
  rw [← p.outcomeRecord_law κ hlaw P hP]
  have hm : Measurable (fun x => (p.publicRecord x,
      readA (p.localRecord true x,p.publicRecord x),
      readB (p.localRecord false x,p.publicRecord x))) := by
    exact p.measurable_publicRecord.prodMk
      ((ha.comp ((p.measurable_localRecord true).prodMk p.measurable_publicRecord)).prodMk
        (hb.comp ((p.measurable_localRecord false).prodMk p.measurable_publicRecord)))
  rw [Measure.map_map hm p.measurable_outcomeRecord]
  congr 1
  funext uv
  simp only [Function.comp_def,p.publicRecord_replay,p.localRecord_replay,
    Bool.false_eq_true,↓reduceIte]
3732
theorem actual_records_independent [StandardBorelSpace O] [Nonempty M] {A : Type v6957_0} {B : Type v6957_1}
    [MeasurableSpace A] [StandardBorelSpace A] [Nonempty A]
    [MeasurableSpace B] [StandardBorelSpace B] [Nonempty B]
    (κ : (n : ℕ) → Kernel (Fin n → O) O) [∀ n, IsMarkovKernel (κ n)]
    (hlaw : ∀ n s, (volume.prod volume).map (fun u => p.causal.step n (s,u)) = κ n s)
    (P : Measure (ℕ → O)) [IsProbabilityMeasure P]
    (hP : ∀ n, P.map (fun x => fun i : Fin (n+1) => x i) =
      ((P.map (fun x => fun i : Fin n => x i)) ⊗ₘ κ n).map
        (fun pair : (Fin n → O) × O => Fin.snoc (α := fun _ : Fin (n+1) => O) pair.1 pair.2))
    (readA : (ℕ → (O ⊕ Unit)) × (ℕ → M) → A)
    (readB : (ℕ → (O ⊕ Unit)) × (ℕ → M) → B)
    (ha : Measurable readA) (hb : Measurable readB) :
    CondIndepFun (MeasurableSpace.comap p.publicRecord inferInstance)
      p.measurable_publicRecord.comap_le
      (fun x => readA (p.localRecord true x,p.publicRecord x))
      (fun x => readB (p.localRecord false x,p.publicRecord x)) P := by
proof · 12 lines
  apply condIndepFun_of_joint_law p.measurable_publicRecord
    (ha.comp ((p.measurable_localRecord true).prodMk p.measurable_publicRecord))
    (hb.comp ((p.measurable_localRecord false).prodMk p.measurable_publicRecord))
    p.tapeProgram.measurable_record
    (ha.comp (((p.measurable_privateRecord true).comp
      (measurable_fst.prodMk p.tapeProgram.measurable_record)).prodMk
        p.tapeProgram.measurable_record))
    (hb.comp (((p.measurable_privateRecord false).comp
      (measurable_snd.prodMk p.tapeProgram.measurable_record)).prodMk
        p.tapeProgram.measurable_record))
    (p.full_readout_law κ hlaw P hP readA readB ha hb)
    (p.final_records_independent readA readB ha hb)
3774
lemma continuous_matrix_kronecker [DecidableEq ι] [DecidableEq η] : Continuous
    (fun z : Matrix ι ι ℂ × Matrix η η ℂ => z.1 ⊗ₖ z.2) := by
proof · 6 lines
  apply continuous_matrix
  intro i j
  exact (((Matrix.entryLinearMap ℂ ℂ i.1 j.1).toContinuousLinearMap).continuous.comp
    continuous_fst).mul
    (((Matrix.entryLinearMap ℂ ℂ i.2 j.2).toContinuousLinearMap).continuous.comp
    continuous_snd)
3785
lemma measurable_matrix_kronecker {Ω : Type v7015_0} [MeasurableSpace Ω]
    {U : Ω → Matrix ι ι ℂ} {V : Ω → Matrix η η ℂ}
    (hU : Measurable U) (hV : Measurable V) :
    Measurable (fun x => U x ⊗ₖ V x) :=
proof · 1 line
  continuous_matrix_kronecker.measurable.comp (hU.prodMk hV)
3792
lemma density_kronecker [DecidableEq ι] [DecidableEq η] {U : Matrix ι ι ℂ} {V : Matrix η η ℂ}
    (hU : Density U) (hV : Density V) : Density (U ⊗ₖ V) := by
proof · 2 lines
  refine ⟨hU.1.kronecker hV.1,?_⟩
  rw [Matrix.trace_kronecker,hU.2,hV.2,one_mul]
3802
theorem local_product_reference_extension
    (U : ℕ → Ω → Matrix ι ι ℂ) (V : ℕ → Ω → Matrix η η ℂ)
    (hU : ∀ n x, Density (U n x)) (hV : ∀ n x, Density (V n x))
    (hLA : LA ≤ mΩ) (hLB : LB ≤ mΩ)
    (hmU : ∀ n, Measurable[LA] (U n)) (hmV : ∀ n, Measurable[LB] (V n))
    (hrU : ∀ i j, Martingale (fun n x => (U n x i j).re) F μ)
    (hiU : ∀ i j, Martingale (fun n x => (U n x i j).im) F μ)
    (hrV : ∀ i j, Martingale (fun n x => (V n x i j).re) F μ)
    (hiV : ∀ i j, Martingale (fun n x => (V n x i j).im) F μ)
    (hrUV : ∀ i j, Martingale (fun n x => ((U n x ⊗ₖ V n x) i j).re) F μ)
    (hiUV : ∀ i j, Martingale (fun n x => ((U n x ⊗ₖ V n x) i j).im) F μ) :
    ∃ G : Ω → Matrix ι ι ℂ, ∃ H : Ω → Matrix η η ℂ,
      Measurable[LA] G ∧ Measurable[LB] H ∧
      (∀ x, Density (G x)) ∧ (∀ x, Density (H x)) ∧
      (∀ᵐ x ∂μ, Tendsto (fun n => U n x) atTop (𝓝 (G x))) ∧
      (∀ᵐ x ∂μ, Tendsto (fun n => V n x) atTop (𝓝 (H x))) ∧
      (∀ n S, MeasurableSet[F n] S →
        (∫ x in S, G x ∂μ) = ∫ x in S, U n x ∂μ) ∧
      (∀ n S, MeasurableSet[F n] S →
        (∫ x in S, H x ∂μ) = ∫ x in S, V n x ∂μ) ∧
      ∃ W : PositiveMatrixMeasure Ω (ι × η),
        (∀ S, MeasurableSet S → W.value S = ∫ x in S, G x ⊗ₖ H x ∂μ) ∧
        (∀ n S, MeasurableSet[F n] S →
          W.value S = ∫ x in S, U n x ⊗ₖ V n x ∂μ) := by
proof · 21 lines
  obtain ⟨G,hGm,hG,hGi,hGt,hGc⟩ := local_reference_limit U hU hmU hLA hrU hiU
  obtain ⟨H,hHm,hH,hHi,hHt,hHc⟩ := local_reference_limit V hV hmV hLB hrV hiV
  obtain ⟨J,hJm,hJ,hJi,hJt,hJc⟩ := local_reference_limit
    (fun n x => U n x ⊗ₖ V n x)
    (fun n x => density_kronecker (hU n x) (hV n x))
    (fun n => measurable_matrix_kronecker ((hmU n).mono hLA le_rfl)
      ((hmV n).mono hLB le_rfl)) le_rfl hrUV hiUV
  have hprod : (fun x => G x ⊗ₖ H x) =ᵐ[μ] J := by
    filter_upwards [hGt,hHt,hJt] with x hx hy hz
    have ht : Tendsto (fun n => U n x ⊗ₖ V n x) atTop (𝓝 (G x ⊗ₖ H x)) :=
      continuous_matrix_kronecker.continuousAt.tendsto.comp (hx.prodMk_nhds hy)
    exact tendsto_nhds_unique ht hz
  let W := positiveMatrixMeasureOfDensity hJi
    (Eventually.of_forall (fun x => (hJ x).1))
  refine ⟨G,H,hGm,hHm,hG,hH,hGt,hHt,hGc,hHc,W,?_,?_⟩
  · intro S hS
    rw [positiveMatrixMeasureOfDensity_value hJi _ S hS]
    exact integral_congr_ae (ae_restrict_of_ae hprod.symm)
  · intro n S hS
    rw [positiveMatrixMeasureOfDensity_value hJi _ S (F.le n S hS)]
    exact hJc n S hS
3863
lemma publicFrom_eq_record (n : ℕ) (x : ℕ → O) (i : Fin n) :
    p.publicFrom n (historyPrefix n x) i = p.publicRecord x i := by
proof · 7 lines
  induction n with
  | zero => exact Fin.elim0 i
  | succ n ih =>
    refine Fin.lastCases ?_ (fun j => ?_) i
    · rfl
    · simp only [publicFrom,replay,Fin.snoc_castSucc]
      exact ih j
3873
lemma extractPrivate_eq_record (a : Bool) (n : ℕ) (x : ℕ → O) (i : Fin n) :
    p.extractPrivate a n (historyPrefix n x) i = p.localRecord a x i := by
proof · 7 lines
  induction n with
  | zero => exact Fin.elim0 i
  | succ n ih =>
    refine Fin.lastCases ?_ (fun j => ?_) i
    · rfl
    · cases a <;> simp only [extractPrivate,replay,Bool.false_eq_true,↓reduceIte,
          Fin.snoc_castSucc] <;> exact ih j
3883
abbrev localField (a : Bool) : MeasurableSpace (ℕ → O) :=
body · 1 line
  MeasurableSpace.comap (fun x => (p.localRecord a x,p.publicRecord x)) inferInstance
3886
lemma localField_le (a : Bool) : p.localField a ≤ (inferInstance : MeasurableSpace (ℕ → O)) :=
proof · 1 line
  ((p.measurable_localRecord a).prodMk p.measurable_publicRecord).comap_le
3889
lemma local_history_measurable (a : Bool) (n : ℕ) :
    Measurable[p.localField a] (fun x =>
      (p.extractPrivate a n (historyPrefix n x),p.publicFrom n (historyPrefix n x))) := by
proof · 12 lines
  have he : (fun x => (p.extractPrivate a n (historyPrefix n x),p.publicFrom n (historyPrefix n x))) =
      fun x => ((fun i : Fin n => p.localRecord a x i),fun i : Fin n => p.publicRecord x i) := by
    funext x
    congr 1 <;> funext i
    · exact p.extractPrivate_eq_record a n x i
    · exact p.publicFrom_eq_record n x i
  rw [he]
  let : MeasurableSpace (ℕ → O) := p.localField a
  have hm : Measurable (fun x : ℕ → O => (p.localRecord a x,p.publicRecord x)) :=
    Measurable.of_comap_le le_rfl
  exact (Measurable.of_eval (fun i => hm.fst.eval)).prodMk
    (Measurable.of_eval (fun i => hm.snd.eval))
3905
lemma localFields_independent [StandardBorelSpace O] [Nonempty O]
    [StandardBorelSpace M] [Nonempty M]
    (κ : (n : ℕ) → Kernel (Fin n → O) O) [∀ n, IsMarkovKernel (κ n)]
    (hlaw : ∀ n s, ((volume : Measure I).prod volume).map
      (fun u => p.causal.step n (s,u)) = κ n s)
    (P : Measure (ℕ → O)) [IsProbabilityMeasure P]
    (hP : ∀ n, P.map (historyPrefix (n+1)) =
      ((P.map (historyPrefix n)) ⊗ₘ κ n).map
        (fun pair : (Fin n → O) × O => Fin.snoc (α := fun _ : Fin (n+1) => O) pair.1 pair.2)) :
    CondIndep (MeasurableSpace.comap p.publicRecord inferInstance)
      (p.localField true) (p.localField false) p.measurable_publicRecord.comap_le P := by
proof · 21 lines
  classical
  rw [condIndep_iff _ _ _ _ (p.localField_le true) (p.localField_le false)]
  intro SA SB hSA hSB
  obtain ⟨EA,hEA,rfl⟩ := hSA
  obtain ⟨EB,hEB,rfl⟩ := hSB
  let fa : (ℕ → O ⊕ Unit) × (ℕ → M) → Bool :=
    fun x => if x ∈ EA then true else false
  let fb : (ℕ → O ⊕ Unit) × (ℕ → M) → Bool :=
    fun x => if x ∈ EB then true else false
  have hfa : Measurable fa := measurable_const.ite hEA measurable_const
  have hfb : Measurable fb := measurable_const.ite hEB measurable_const
  have hi := p.actual_records_independent κ hlaw P hP fa fb hfa hfb
  apply (condIndepFun_iff _ _ _ _
    (hfa.comp ((p.measurable_localRecord true).prodMk p.measurable_publicRecord))
    (hfb.comp ((p.measurable_localRecord false).prodMk p.measurable_publicRecord)) P).mp hi
  · refine ⟨{true},measurableSet_singleton true,?_⟩
    ext x
    simp [fa]
  · refine ⟨{true},measurableSet_singleton true,?_⟩
    ext x
    simp [fb]
3957
theorem full_reference_extension
    (P : Measure (ℕ → O)) [IsProbabilityMeasure P]
    (hP : ∀ n, P.map (historyPrefix (n+1)) =
      ((P.map (historyPrefix n)) ⊗ₘ p.instrumentKernel κA κB n).map
        (fun pair : (Fin n → O) × O => Fin.snoc (α := fun _ : Fin (n+1) => O) pair.1 pair.2)) :
    ∃ G : (ℕ → O) → Matrix ι ι ℂ, ∃ H : (ℕ → O) → Matrix η η ℂ,
      Measurable[p.localField true] G ∧ Measurable[p.localField false] H ∧
      (∀ x, Density (G x)) ∧ (∀ x, Density (H x)) ∧
      (∀ n S, MeasurableSet[historyFiltration n] S →
        (∫ x in S, G x ∂P) = ∫ x in S, globalLocalReferenceA p κA eA n (historyPrefix n x) ∂P) ∧
      (∀ n S, MeasurableSet[historyFiltration n] S →
        (∫ x in S, H x ∂P) = ∫ x in S, globalLocalReferenceB p κB eB n (historyPrefix n x) ∂P) ∧
      ∃ W : PositiveMatrixMeasure (ℕ → O) (ι × η),
        (∀ S, MeasurableSet S → W.value S = ∫ x in S, G x ⊗ₖ H x ∂P) ∧
        (∀ n S, MeasurableSet[historyFiltration n] S →
          W.value S = ∫ x in S, jointReference p κA κB eA eB n (historyPrefix n x) ∂P) := by
proof · 26 lines
  let U := globalLocalReferenceA p κA eA
  let V := globalLocalReferenceB p κB eB
  have hU (n : ℕ) (s : Fin n → O) : Density (U n s) := eA.reference_density n _
  have hV (n : ℕ) (s : Fin n → O) : Density (V n s) := eB.reference_density n _
  have hmU := globalLocalReferenceA_measurable p κA eA
  have hmV := globalLocalReferenceB_measurable p κB eB
  have hmeanU := globalLocalReferenceA_mean p κA κB eA
  have hmeanV := globalLocalReferenceB_mean p κA κB eB
  have hrU := reference_history_martingale_re P (p.instrumentKernel κA κB) hP U hU hmU hmeanU
  have hiU := reference_history_martingale_im P (p.instrumentKernel κA κB) hP U hU hmU hmeanU
  have hrV := reference_history_martingale_re P (p.instrumentKernel κA κB) hP V hV hmV hmeanV
  have hiV := reference_history_martingale_im P (p.instrumentKernel κA κB) hP V hV hmV hmeanV
  have hrUV := reference_history_martingale_re P (p.instrumentKernel κA κB) hP
    (jointReference p κA κB eA eB) (jointReference_density p κA κB eA eB)
    (jointReference_measurable p κA κB eA eB) (jointReference_mean p κA κB eA eB)
  have hiUV := reference_history_martingale_im P (p.instrumentKernel κA κB) hP
    (jointReference p κA κB eA eB) (jointReference_density p κA κB eA eB)
    (jointReference_measurable p κA κB eA eB) (jointReference_mean p κA κB eA eB)
  obtain ⟨G,H,hGm,hHm,hG,hH,_,_,hGc,hHc,W,hW,hWc⟩ :=
    local_product_reference_extension
      (fun n x => U n (historyPrefix n x)) (fun n x => V n (historyPrefix n x))
      (fun n x => hU n _) (fun n x => hV n _) (p.localField_le true) (p.localField_le false)
      (fun n => (eA.reference_measurable n).comp (p.local_history_measurable true n))
      (fun n => (eB.reference_measurable n).comp (p.local_history_measurable false n))
      hrU hiU hrV hiV hrUV hiUV
  exact ⟨G,H,hGm,hHm,hG,hH,hGc,hHc,W,hW,hWc⟩
4012
lemma condIndepFun_ae_indep_complex {Ω : Type v7253_0} {C : MeasurableSpace Ω}
    {mΩ : MeasurableSpace Ω} [inst7253_0 : StandardBorelSpace Ω] {μ : Measure Ω}
    [inst7253_1 : IsFiniteMeasure μ] (hC : C ≤ mΩ)
    {f g : Ω → ℂ}
    (hf : Measurable f) (hg : Measurable g)
    (h : CondIndepFun C hC f g μ) :
    ∀ᵐ x ∂μ.trim hC, IndepFun f g (condExpKernel μ C x) := by
proof · 5 lines
  have he := (condIndepFun_iff_map_prod_eq_prod_map_map hf hg).mp h
  filter_upwards [he] with x hx
  apply (indepFun_iff_map_prod_eq_prod_map_map hf.aemeasurable hg.aemeasurable).mpr
  simpa only [Kernel.prod_apply, Kernel.map_apply _ (hf.prodMk hg),
    Kernel.map_apply _ hf, Kernel.map_apply _ hg] using hx
4025
lemma condIndepFun_kernel_integral_mul {Ω : Type v7253_0} {C : MeasurableSpace Ω}
    {mΩ : MeasurableSpace Ω} [inst7253_0 : StandardBorelSpace Ω] {μ : Measure Ω}
    [inst7253_1 : IsFiniteMeasure μ] (hC : C ≤ mΩ)
    {f g : Ω → ℂ}
    (hf : Measurable f) (hg : Measurable g)
    (h : CondIndepFun C hC f g μ) :
    ∀ᵐ x ∂μ.trim hC,
      (∫ y, f y * g y ∂condExpKernel μ C x) =
        (∫ y, f y ∂condExpKernel μ C x) *
        (∫ y, g y ∂condExpKernel μ C x) := by
proof · 3 lines
  filter_upwards [condIndepFun_ae_indep_complex hC hf hg h] with x hx
  exact hx.integral_fun_mul_eq_mul_integral hf.stronglyMeasurable.aestronglyMeasurable
    hg.stronglyMeasurable.aestronglyMeasurable
4045
def publicMatrix (C : MeasurableSpace Ω) {mΩ : MeasurableSpace Ω}
    [StandardBorelSpace Ω] (μ : Measure Ω) [IsFiniteMeasure μ]
    (W : Ω → Matrix ι ι ℂ) (x : Ω) : Matrix ι ι ℂ :=
body · 1 line
  ∫ y, W y ∂condExpKernel μ C x
4050
lemma publicMatrix_measurable (_hC : C ≤ mΩ) {W : Ω → Matrix ι ι ℂ}
    (hW : Measurable W) : Measurable[C] (publicMatrix C μ W) := by
proof · 1 line
  exact hW.stronglyMeasurable.integral_condExpKernel.measurable
4054
lemma publicMatrix_entry (W : Ω → Matrix ι ι ℂ)
    (hWi : ∀ x, Integrable W (condExpKernel μ C x)) (x : Ω) (i j : ι) :
    publicMatrix C μ W x i j = ∫ y, W y i j ∂condExpKernel μ C x :=
proof · 1 line
  integral_matrix_entry (hWi x) i j
4059
theorem condIndep_publicMatrix_kronecker
    (hC : C ≤ mΩ) (hA : A ≤ mΩ) (hB : B ≤ mΩ)
    (h : CondIndep C A B hC μ)
    (U : Ω → Matrix ι ι ℂ) (V : Ω → Matrix κ κ ℂ)
    (hUm : Measurable[A] U) (hVm : Measurable[B] V)
    (hUi : ∀ x, Integrable U (condExpKernel μ C x))
    (hVi : ∀ x, Integrable V (condExpKernel μ C x))
    (hUVi : ∀ x, Integrable (fun y => U y ⊗ₖ V y) (condExpKernel μ C x)) :
    publicMatrix C μ (fun y => U y ⊗ₖ V y) =ᵐ[μ.trim hC]
      fun x => publicMatrix C μ U x ⊗ₖ publicMatrix C μ V x := by
proof · 21 lines
  have he (i j : ι) (k l : κ) :
      ∀ᵐ x ∂μ.trim hC,
        (∫ y, U y i j * V y k l ∂condExpKernel μ C x) =
          (∫ y, U y i j ∂condExpKernel μ C x) *
          (∫ y, V y k l ∂condExpKernel μ C x) := by
    have huf : Measurable[A] (fun y => U y i j) :=
      ((Matrix.entryLinearMap ℂ ℂ i j).toContinuousLinearMap).measurable.comp hUm
    have hvf : Measurable[B] (fun y => V y k l) :=
      ((Matrix.entryLinearMap ℂ ℂ k l).toContinuousLinearMap).measurable.comp hVm
    have hfg : CondIndepFun C hC (fun y => U y i j) (fun y => V y k l) μ := by
      exact condIndep_of_condIndep_of_le_right
        (condIndep_of_condIndep_of_le_left h huf.comap_le) hvf.comap_le
    exact condIndepFun_kernel_integral_mul hC (huf.mono hA le_rfl)
      (hvf.mono hB le_rfl) hfg
  have he' := ae_all_iff.mpr (fun i => ae_all_iff.mpr (fun j =>
    ae_all_iff.mpr (fun k => ae_all_iff.mpr (he i j k))))
  filter_upwards [he'] with x hx
  ext ⟨i,k⟩ ⟨j,l⟩
  rw [publicMatrix_entry _ hUVi, Matrix.kronecker_apply,
    publicMatrix_entry _ hUi, publicMatrix_entry _ hVi]
  exact hx i j k l
4103
def Subdensity (U : Matrix ι ι ℂ) : Prop := U.PosSemidef ∧ (trace U).re ≤ 1
4106
lemma subdensity_zero [DecidableEq ι] : Subdensity (0 : Matrix ι ι ℂ) := by
proof · 1 line
  exact ⟨Matrix.PosSemidef.zero, by simp⟩
4110
lemma subdensity_kronecker [DecidableEq ι] [DecidableEq η] {U : Matrix ι ι ℂ} {V : Matrix η η ℂ}
    (hU : Subdensity U) (hV : Subdensity V) : Subdensity (U ⊗ₖ V) := by
proof · 5 lines
  refine ⟨hU.1.kronecker hV.1,?_⟩
  have hUi : (trace U).im = 0 := (RCLike.nonneg_iff.mp hU.1.trace_nonneg).2
  have hVi : (trace V).im = 0 := (RCLike.nonneg_iff.mp hV.1.trace_nonneg).2
  rw [Matrix.trace_kronecker, Complex.mul_re,hUi,hVi,mul_zero,sub_zero]
  exact (mul_le_of_le_one_left ((RCLike.nonneg_iff.mp hV.1.trace_nonneg).1) hU.2).trans hV.2
4120
lemma integrable_subdensity {Ω : Type v7376_0} [MeasurableSpace Ω]
    (U : Ω → Matrix ι ι ℂ) (hU : ∀ x, Subdensity (U x)) (hm : Measurable U)
    (μ : Measure Ω) [IsFiniteMeasure μ] : Integrable U μ := by
proof · 6 lines
  apply integrable_matrix_of_entries
  intro i j
  apply Integrable.of_bound
    (((Matrix.entryLinearMap ℂ ℂ i j).toContinuousLinearMap).measurable.comp hm).aestronglyMeasurable 1
  exact Filter.Eventually.of_forall (fun x =>
    (psd_entry_norm_le_trace (hU x).1 i j).trans (hU x).2)
4132
lemma integral_subdensity {Ω : Type v7386_0} [MeasurableSpace Ω]
    (U : Ω → Matrix ι ι ℂ) (hU : ∀ x, Subdensity (U x)) (hm : Measurable U)
    (μ : Measure Ω) [IsProbabilityMeasure μ] : Subdensity (∫ x, U x ∂μ) := by
proof · 12 lines
  have hi := integrable_subdensity U hU hm μ
  refine ⟨(integral_nonneg_of_ae (Filter.Eventually.of_forall
    (fun x => (hU x).1.nonneg))).posSemidef,?_⟩
  let L : Matrix ι ι ℂ →L[ℂ] ℂ := (Matrix.traceLinearMap ι ℂ ℂ).toContinuousLinearMap
  have htrace : trace (∫ x, U x ∂μ) = ∫ x, trace (U x) ∂μ := (L.integral_comp_comm hi).symm
  rw [htrace]
  change RCLike.re (∫ x, L (U x) ∂μ) ≤ 1
  rw [← integral_re (L.integrable_comp hi)]
  calc
    _ ≤ ∫ _ : Ω, (1 : ℝ) ∂μ := integral_mono ((L.integrable_comp hi).re)
      (integrable_const 1) (fun x => (hU x).2)
    _ = 1 := by simp
4150
lemma measurable_positive_factor {Ω : Type v7402_0} {T : Type v7402_1} [MeasurableSpace Ω]
    [MeasurableSpace T] (r : Ω → T)
    (U : Ω → Matrix ι ι ℂ) (hU : ∀ x, Subdensity (U x))
    (hm : Measurable[MeasurableSpace.comap r inferInstance] U) :
    ∃ V : T → Matrix ι ι ℂ, Measurable V ∧ (∀ t, Subdensity (V t)) ∧ U = V ∘ r := by
proof · 20 lines
  classical
  obtain ⟨V,hVm,hV⟩ := hm.exists_eq_measurable_comp
  let good : Set T := {t | 0 ≤ V t ∧ (trace (V t)).re ≤ 1}
  have hg : MeasurableSet good :=
    (measurableSet_le measurable_const hVm).inter
      (measurableSet_le (((Matrix.traceLinearMap ι ℂ ℂ).toContinuousLinearMap).measurable.comp hVm).re measurable_const)
  let W : T → Matrix ι ι ℂ := good.piecewise V (fun _ => 0)
  refine ⟨W,hVm.piecewise hg measurable_const,?_,?_⟩
  · intro t
    by_cases ht : t ∈ good
    · simpa only [W,Set.piecewise_eq_of_mem good V _ ht] using
        (show Subdensity (V t) from ⟨ht.1.posSemidef,ht.2⟩)
    · simpa only [W,Set.piecewise_eq_of_notMem good V _ ht] using subdensity_zero (ι := ι)
  · funext x
    have he : U x = V (r x) := congrFun hV x
    have hgx : r x ∈ good := by
      change 0 ≤ V (r x) ∧ (trace (V (r x))).re ≤ 1
      rw [← he]
      exact ⟨(hU x).1.nonneg,(hU x).2⟩
    simpa only [Function.comp_apply,W,Set.piecewise_eq_of_mem good V _ hgx] using he
4183
theorem terminal_public_factors
    (hA : A ≤ mΩ) (hB : B ≤ mΩ)
    (h : CondIndep (MeasurableSpace.comap r inferInstance) A B hr.comap_le μ)
    (U : Bool → Ω → Matrix ι ι ℂ) (V : Bool → Ω → Matrix η η ℂ)
    (hU : ∀ i x, Subdensity (U i x)) (hV : ∀ j x, Subdensity (V j x))
    (hmU : ∀ i, Measurable[A] (U i)) (hmV : ∀ j, Measurable[B] (V j)) :
    ∃ X : Bool → T → Matrix ι ι ℂ, ∃ Y : Bool → T → Matrix η η ℂ,
      (∀ i, Measurable (X i)) ∧ (∀ j, Measurable (Y j)) ∧
      (∀ i t, Subdensity (X i t)) ∧ (∀ j t, Subdensity (Y j t)) ∧
      (∀ i j, publicMatrix (MeasurableSpace.comap r inferInstance) μ
        (fun x => U i x ⊗ₖ V j x) =ᵐ[μ]
        fun x => X i (r x) ⊗ₖ Y j (r x)) := by
proof · 22 lines
  have hUm (i : Bool) := (hmU i).mono hA le_rfl
  have hVm (j : Bool) := (hmV j).mono hB le_rfl
  have hpU (i : Bool) : ∀ x, Subdensity (publicMatrix (MeasurableSpace.comap r inferInstance) μ (U i) x) :=
    fun x => integral_subdensity (U i) (hU i) (hUm i) (condExpKernel μ (MeasurableSpace.comap r inferInstance) x)
  have hpV (j : Bool) : ∀ x, Subdensity (publicMatrix (MeasurableSpace.comap r inferInstance) μ (V j) x) :=
    fun x => integral_subdensity (V j) (hV j) (hVm j) (condExpKernel μ (MeasurableSpace.comap r inferInstance) x)
  choose X hXm hXp hX using fun i => measurable_positive_factor r
    (publicMatrix (MeasurableSpace.comap r inferInstance) μ (U i)) (hpU i) (publicMatrix_measurable hr.comap_le (hUm i))
  choose Y hYm hYp hY using fun j => measurable_positive_factor r
    (publicMatrix (MeasurableSpace.comap r inferInstance) μ (V j)) (hpV j) (publicMatrix_measurable hr.comap_le (hVm j))
  refine ⟨X,Y,hXm,hYm,hXp,hYp,?_⟩
  intro i j
  have he := condIndep_publicMatrix_kronecker hr.comap_le hA hB h
    (U i) (V j) (hmU i) (hmV j)
    (fun x => integrable_subdensity (U i) (hU i) (hUm i) (condExpKernel μ (MeasurableSpace.comap r inferInstance) x))
    (fun x => integrable_subdensity (V j) (hV j) (hVm j) (condExpKernel μ (MeasurableSpace.comap r inferInstance) x))
    (fun x => integrable_subdensity (fun y => U i y ⊗ₖ V j y)
      (fun y => subdensity_kronecker (hU i y) (hV j y))
      (measurable_matrix_kronecker (hUm i) (hVm j)) (condExpKernel μ (MeasurableSpace.comap r inferInstance) x))
  have he' := ae_of_ae_trim hr.comap_le he
  filter_upwards [he'] with x hx
  exact hx.trans (by rw [congrFun (hX i) x,congrFun (hY j) x]; rfl)
4231
lemma ext_on_generating_piSystem [DecidableEq ι] (W V : PositiveMatrixMeasure Ω ι) (C : Set (Set Ω))
    (hC : IsPiSystem C) (hm : mΩ = MeasurableSpace.generateFrom C)
    (h : ∀ S ∈ C, W.value S = V.value S) (hu : W.value Set.univ = V.value Set.univ) : W = V := by
proof · 10 lines
  have he : W.entry = V.entry := by
    funext i j
    refine VectorMeasure.ext_of_generateFrom C ?_ hm hC ?_
    · intro S hS
      exact congrArg (fun M : Matrix ι ι ℂ => M i j) (h S hS)
    · exact congrArg (fun M : Matrix ι ι ℂ => M i j) hu
  cases W
  cases V
  cases he
  rfl
4245
lemma ext_on_filtration (W V : PositiveMatrixMeasure Ω ι) (F : Filtration ℕ mΩ)
    (hm : mΩ = ⨆ n, F n)
    (h : ∀ n S, MeasurableSet[F n] S → W.value S = V.value S) : W = V := by
proof · 16 lines
  let C : Set (Set Ω) := {S | ∃ n, MeasurableSet[F n] S}
  have hc : IsPiSystem C := by
    intro S hS T hT _
    obtain ⟨n,hn⟩ := hS
    obtain ⟨k,hk⟩ := hT
    exact ⟨max n k, ((F.mono (le_max_left n k)) S hn).inter ((F.mono (le_max_right n k)) T hk)⟩
  have hgen : mΩ = MeasurableSpace.generateFrom C := by
    rw [hm]
    apply le_antisymm
    · apply iSup_le; intro n S hS
      exact MeasurableSpace.measurableSet_generateFrom ⟨n,hS⟩
    · apply MeasurableSpace.generateFrom_le
      rintro S ⟨n,hn⟩
      exact (le_iSup (fun n => F n) n) S hn
  exact W.ext_on_generating_piSystem V C hc hgen (fun S ⟨n,hn⟩ => h n S hn)
    (h 0 Set.univ MeasurableSet.univ)
4283
def bitReference (i : Bool) : Matrix ι ι ℂ := S (J.operation {i} 1)
4286
lemma bitReference_positive [Fintype ι] [DecidableEq ι] (hS : PositiveFunctional S) (h1 : (0 : B) ≤ 1)
    (i : Bool) : (bitReference J S i).PosSemidef :=
proof · 1 line
  hS _ (J.positive {i} (measurableSet_singleton i) 1 h1)
4291
lemma bitReference_entry [DecidableEq ι] (i : Bool) (k l : ι) :
    bitReference J S i k l = ∫ b in {i}, r.posterior b 1 k l ∂r.law :=
proof · 1 line
  (r.update {i} (measurableSet_singleton i) 1 k l).symm
4296
theorem bitReference_sum : bitReference J S false + bitReference J S true = S 1 := by
proof · 10 lines
  ext k l
  rw [Matrix.add_apply, bitReference_entry J S r false,
    bitReference_entry J S r true]
  have hd : Disjoint ({false} : Set Bool) {true} := by simp
  have hu : ({false} : Set Bool) ∪ {true} = Set.univ := by
    ext b; cases b <;> simp
  rw [← setIntegral_union hd (measurableSet_singleton true)
    (r.integrable 1 k l).integrableOn (r.integrable 1 k l).integrableOn, hu,
    Measure.restrict_univ]
  exact InstrumentRun.reference_mean J S r k l
4325
lemma density_subdensity [DecidableEq ι] {U : Matrix ι ι ℂ} (hU : Density U) : Subdensity U :=
proof · 1 line
  ⟨hU.1,by rw [hU.2]; norm_num⟩
4328
theorem reference_density_unique_on_filtration (F : Filtration ℕ mΩ)
    (hgen : mΩ = ⨆ n, F n)
    (U V : Ω → Matrix ι ι ℂ)
    (hU : ∀ x, Subdensity (U x)) (hV : ∀ x, Subdensity (V x))
    (hmU : Measurable U) (hmV : Measurable V)
    (hEq : ∀ n S, MeasurableSet[F n] S →
      (∫ x in S, U x ∂μ) = ∫ x in S, V x ∂μ) : U =ᵐ[μ] V := by
proof · 23 lines
  have hUi := integrable_subdensity U hU hmU μ
  have hVi := integrable_subdensity V hV hmV μ
  have hUe (i j : ι) : Integrable (fun x => U x i j) μ :=
    ((Matrix.entryLinearMap ℂ ℂ i j).toContinuousLinearMap).integrable_comp hUi
  have hVe (i j : ι) : Integrable (fun x => V x i j) μ :=
    ((Matrix.entryLinearMap ℂ ℂ i j).toContinuousLinearMap).integrable_comp hVi
  let P := positiveMatrixMeasureOfDensity hUe
    (Filter.Eventually.of_forall (fun x => (hU x).1))
  let Q := positiveMatrixMeasureOfDensity hVe
    (Filter.Eventually.of_forall (fun x => (hV x).1))
  have hpq : P = Q := by
    apply PositiveMatrixMeasure.ext_on_filtration P Q F hgen
    intro n S hS
    dsimp only [P,Q]
    rw [positiveMatrixMeasureOfDensity_value hUe _ S (F.le n S hS),
      positiveMatrixMeasureOfDensity_value hVe _ S (F.le n S hS)]
    exact hEq n S hS
  apply Integrable.ae_eq_of_forall_setIntegral_eq U V hUi hVi
  intro S hS _
  have he := congrArg (fun P : PositiveMatrixMeasure Ω ι => P.value S) hpq
  dsimp only [P,Q] at he
  simpa only [positiveMatrixMeasureOfDensity_value hUe _ S hS,
    positiveMatrixMeasureOfDensity_value hVe _ S hS] using he
4372
structure IncomingObservableProcess where
body · 7 lines
  state : Ω → ReferenceFunctional A ι
  positive : ∀ x, PositiveFunctional (state x)
  positive_unit : (0 : A) ≤ 1
  normalized : ∀ x, trace (state x 1) = 1
  local_measurable : Measurable[L] (fun x => state x 1)
  causality : ∀ n S, MeasurableSet[F n] S →
    (∫ x in S, state x 1 ∂μ) = ∫ x in S, U n x ∂μ
4385
lemma reference_density (x : Ω) : ZeroKey.Density (e.state x 1) :=
proof · 1 line
  ⟨e.positive x 1 e.positive_unit,e.normalized x⟩
4388
structure BitReadout where
body · 4 lines
  instrument : Ω → ObservableInstrument Bool A B
  run : ∀ x, InstrumentRun (instrument x) (e.state x)
  positive_unit : (0 : B) ≤ 1
  local_measurable : ∀ i, Measurable[L] (fun x => bitReference (instrument x) (e.state x) i)
4400
def value (i : Bool) (x : Ω) : Matrix ι ι ℂ := bitReference (b.instrument x) (e.state x) i
4402
lemma positive (i : Bool) (x : Ω) : (b.value i x).PosSemidef :=
proof · 1 line
  bitReference_positive (b.instrument x) (e.state x) (e.positive x) b.positive_unit i
4405
lemma sum (x : Ω) : b.value false x + b.value true x = e.state x 1 :=
proof · 1 line
  bitReference_sum (b.instrument x) (e.state x) (b.run x)
4408
lemma subdensity (i : Bool) (x : Ω) : ZeroKey.Subdensity (b.value i x) := by
proof · 8 lines
  refine ⟨b.positive i x,?_⟩
  have hsum := congrArg (fun M : Matrix ι ι ℂ => (trace M).re) (b.sum x)
  rw [Matrix.trace_add,Complex.add_re,e.normalized x,Complex.one_re] at hsum
  have h0 := (RCLike.nonneg_iff.mp (b.positive false x).trace_nonneg).1
  have h1 := (RCLike.nonneg_iff.mp (b.positive true x).trace_nonneg).1
  change 0 ≤ (trace (b.value false x)).re at h0
  change 0 ≤ (trace (b.value true x)).re at h1
  cases i <;> linarith
4418
lemma measurable (i : Bool) : Measurable[L] (b.value i) := b.local_measurable i
4443
lemma setIntegral_publicMatrix (hL : L ≤ mΩ) (U : Ω → Matrix ι ι ℂ)
    (hU : Integrable U P) (S : Set Ω) (hS : MeasurableSet[L] S) :
    (∫ x in S, publicMatrix L P U x ∂P) = ∫ x in S, U x ∂P := by
proof · 4 lines
  calc
    _ = ∫ x in S, P[U | L] x ∂P := integral_congr_ae
      (ae_restrict_of_ae (condExp_ae_eq_integral_condExpKernel hL hU).symm)
    _ = _ := setIntegral_condExp hL hU hS
4451
lemma public_factor_event (r : Ω → T) (hr : Measurable r)
    (U : Ω → Matrix ι ι ℂ) (V : T → Matrix ι ι ℂ)
    (hU : Integrable U P) (hV : Measurable V)
    (he : publicMatrix (MeasurableSpace.comap r inferInstance) P U =ᵐ[P] V ∘ r)
    (S : Set T) (hS : MeasurableSet S) :
    (∫ t in S, V t ∂P.map r) = ∫ x in r ⁻¹' S, U x ∂P := by
proof · 5 lines
  rw [setIntegral_map hS hV.aestronglyMeasurable hr.aemeasurable]
  calc
    _ = ∫ x in r ⁻¹' S, publicMatrix (MeasurableSpace.comap r inferInstance) P U x ∂P :=
      integral_congr_ae (ae_restrict_of_ae he.symm)
    _ = _ := setIntegral_publicMatrix hr.comap_le U hU _ ⟨S,hS,rfl⟩
4506
theorem incoming_joint_reference_consistency [StandardBorelSpace O] [Nonempty O] [StandardBorelSpace M] [Nonempty M] :
    ∀ n S, MeasurableSet[historyFiltration n] S →
      (∫ x in S, (eTA.state x 1) ⊗ₖ (eTB.state x 1) ∂P) =
        ∫ x in S, jointReference p κA κB eA eB n (historyPrefix n x) ∂P := by
proof · 23 lines
  obtain ⟨G,H,hGm,hHm,hG,hH,hGc,hHc,W,hW,hWc⟩ :=
    full_reference_extension p κA κB eA eB P hP
  have hGa : (fun x => eTA.state x 1) =ᵐ[P] G :=
    reference_density_unique_on_filtration historyFiltration historyFiltration_generates
      (fun x => eTA.state x 1) G
      (fun x => density_subdensity (eTA.reference_density x))
      (fun x => density_subdensity (hG x))
      (eTA.local_measurable.mono (p.localField_le true) le_rfl)
      (hGm.mono (p.localField_le true) le_rfl)
      (fun n S hS => (eTA.causality n S hS).trans (hGc n S hS).symm)
  have hHa : (fun x => eTB.state x 1) =ᵐ[P] H :=
    reference_density_unique_on_filtration historyFiltration historyFiltration_generates
      (fun x => eTB.state x 1) H
      (fun x => density_subdensity (eTB.reference_density x))
      (fun x => density_subdensity (hH x))
      (eTB.local_measurable.mono (p.localField_le false) le_rfl)
      (hHm.mono (p.localField_le false) le_rfl)
      (fun n S hS => (eTB.causality n S hS).trans (hHc n S hS).symm)
  intro n S hS
  rw [← hWc n S hS,hW S (historyFiltration.le n S hS)]
  apply integral_congr_ae
  filter_upwards [ae_restrict_of_ae hGa,ae_restrict_of_ae hHa] with x hx hy
  rw [hx,hy]
4534
theorem genuine_terminal_public_factors
    (hlaw : ∀ n s, ((volume : Measure I).prod volume).map
      (fun u => p.causal.step n (s,u)) = p.instrumentKernel κA κB n s) :
    ∃ X : Bool → (ℕ → M) → Matrix ι ι ℂ,
    ∃ Y : Bool → (ℕ → M) → Matrix η η ℂ,
      (∀ i, Measurable (X i)) ∧ (∀ j, Measurable (Y j)) ∧
      (∀ i t, Subdensity (X i t)) ∧ (∀ j t, Subdensity (Y j t)) ∧
      ∀ i j S, MeasurableSet S →
        (∫ t in S, X i t ⊗ₖ Y j t ∂P.map p.publicRecord) =
          ∫ x in p.publicRecord ⁻¹' S, bA.value i x ⊗ₖ bB.value j x ∂P := by
proof · 12 lines
  obtain ⟨X,Y,hXm,hYm,hXp,hYp,hXY⟩ := terminal_public_factors p.publicRecord
    p.measurable_publicRecord (p.localField_le true) (p.localField_le false)
    (p.localFields_independent (p.instrumentKernel κA κB) hlaw P hP)
    bA.value bB.value bA.subdensity bB.subdensity bA.measurable bB.measurable
  refine ⟨X,Y,hXm,hYm,hXp,hYp,?_⟩
  intro i j S hS
  exact public_factor_event p.publicRecord p.measurable_publicRecord
    (fun x => bA.value i x ⊗ₖ bB.value j x) (fun t => X i t ⊗ₖ Y j t)
    (integrable_subdensity _ (fun x => subdensity_kronecker (bA.subdensity i x) (bB.subdensity j x))
      (measurable_matrix_kronecker ((bA.measurable i).mono (p.localField_le true) le_rfl)
        ((bB.measurable j).mono (p.localField_le false) le_rfl)) P)
    (measurable_matrix_kronecker (hXm i) (hYm j)) (hXY i j) S hS
4591
theorem terminal_reference_total :
    (∫ x, bA.value false x ⊗ₖ bB.value false x ∂P) +
    (∫ x, bA.value false x ⊗ₖ bB.value true x ∂P) +
    (∫ x, bA.value true x ⊗ₖ bB.value false x ∂P) +
    (∫ x, bA.value true x ⊗ₖ bB.value true x ∂P) =
      jointReference p κA κB eA eB 0 Fin.elim0 := by
proof · 21 lines
  have hi (i j : Bool) : Integrable (fun x => bA.value i x ⊗ₖ bB.value j x) P :=
    integrable_subdensity _ (fun x => subdensity_kronecker (bA.subdensity i x) (bB.subdensity j x))
      (measurable_matrix_kronecker ((bA.measurable i).mono (p.localField_le true) le_rfl)
        ((bB.measurable j).mono (p.localField_le false) le_rfl)) P
  have h2 := (integral_add ((hi false false).add (hi false true)) (hi true false)).symm
  have h3 := (integral_add (((hi false false).add (hi false true)).add (hi true false)) (hi true true)).symm
  simp only [Pi.add_apply] at h2 h3
  rw [← integral_add (hi false false) (hi false true),h2,h3]
  have hs (x : ℕ → O) : bA.value false x ⊗ₖ bB.value false x +
      bA.value false x ⊗ₖ bB.value true x + bA.value true x ⊗ₖ bB.value false x +
      bA.value true x ⊗ₖ bB.value true x = eTA.state x 1 ⊗ₖ eTB.state x 1 := by
    rw [← bA.sum x,← bB.sum x,Matrix.add_kronecker,Matrix.kronecker_add,Matrix.kronecker_add]
    abel
  simp_rw [hs]
  have hc := incoming_joint_reference_consistency p κA κB eA eB P hP eTA eTB
    0 Set.univ MeasurableSet.univ
  simp only [Measure.restrict_univ] at hc
  rw [hc]
  have he (x : ℕ → O) : historyPrefix 0 x = Fin.elim0 := Subsingleton.elim _ _
  simp_rw [he]
  simp
4619
theorem terminal_public_normalization
    (hlaw : ∀ n s, ((volume : Measure I).prod volume).map
      (fun u => p.causal.step n (s,u)) = p.instrumentKernel κA κB n s) :
    ∃ X : Bool → (ℕ → M) → Matrix ι ι ℂ,
    ∃ Y : Bool → (ℕ → M) → Matrix η η ℂ,
      (∀ i, Measurable (X i)) ∧ (∀ j, Measurable (Y j)) ∧
      (∀ i t, Subdensity (X i t)) ∧ (∀ j t, Subdensity (Y j t)) ∧
      (∀ i j S, MeasurableSet S →
        (∫ t in S, X i t ⊗ₖ Y j t ∂P.map p.publicRecord) =
          ∫ x in p.publicRecord ⁻¹' S, bA.value i x ⊗ₖ bB.value j x ∂P) ∧
      (∫ t, X false t ⊗ₖ Y false t ∂P.map p.publicRecord) +
      (∫ t, X false t ⊗ₖ Y true t ∂P.map p.publicRecord) +
      (∫ t, X true t ⊗ₖ Y false t ∂P.map p.publicRecord) +
      (∫ t, X true t ⊗ₖ Y true t ∂P.map p.publicRecord) =
        jointReference p κA κB eA eB 0 Fin.elim0 := by
proof · 7 lines
  obtain ⟨X,Y,hXm,hYm,hX,hY,hXY⟩ := genuine_terminal_public_factors
    p κA κB eA eB P hP eTA eTB bA bB hlaw
  refine ⟨X,Y,hXm,hYm,hX,hY,hXY,?_⟩
  have ht (i j : Bool) := hXY i j Set.univ MeasurableSet.univ
  simp only [Measure.restrict_univ,Set.preimage_univ] at ht
  simp_rw [ht]
  exact terminal_reference_total p κA κB eA eB P hP eTA eTB bA bB
4654
lemma localProbe_reference :
    keepReference (outer (localProbe (ι := ι)) localProbe) =
      (Fintype.card ι : ℂ)⁻¹ • (1 : Matrix ι ι ℂ) := by
proof · 9 lines
  rw [localProbe,keepReference_vectorize]
  simp only [Matrix.conjTranspose_smul,Matrix.conjTranspose_one,
    Matrix.smul_mul,Matrix.mul_smul,one_mul,Matrix.transpose_smul,Matrix.transpose_one]
  have hc : star (((Real.sqrt (Fintype.card ι : ℝ))⁻¹ : ℂ)) *
      ((Real.sqrt (Fintype.card ι : ℝ))⁻¹ : ℂ) = (Fintype.card ι : ℂ)⁻¹ := by
    simp only [map_inv₀,Complex.star_def,Complex.conj_ofReal]
    rw [← mul_inv,← Complex.ofReal_mul,Real.mul_self_sqrt (Nat.cast_nonneg _)]
    simp
  rw [smul_smul,mul_comm,hc]
4667
lemma localProbe_reference_product :
    keepReference (outer (localProbe (ι := ι)) localProbe) ⊗ₖ
      keepReference (outer (localProbe (ι := η)) localProbe) =
    keepReference (outer (localProbe (ι := ι × η)) localProbe) := by
proof · 3 lines
  simp only [localProbe_reference,Matrix.smul_kronecker,Matrix.kronecker_smul,
    Matrix.one_kronecker_one,smul_smul,Fintype.card_prod,Nat.cast_mul,mul_inv]
  rw [mul_comm]
4687
structure Discussion (O : Type v8064_0) (M : Type v8064_1) [MeasurableSpace O] [MeasurableSpace M] where
body · 10 lines
  alice : (n : ℕ) → (Fin n → M) → Bool
  alice_measurable : ∀ n, Measurable (alice n)
  kernelA : (n : ℕ) → Kernel (LocalHistory O M n) O
  kernelB : (n : ℕ) → Kernel (LocalHistory O M n) O
  markovA : ∀ n, IsMarkovKernel (kernelA n)
  markovB : ∀ n, IsMarkovKernel (kernelB n)
  sendA : (n : ℕ) → LocalHistory O M n × O → M
  sendB : (n : ℕ) → LocalHistory O M n × O → M
  sendA_measurable : ∀ n, Measurable (sendA n)
  sendB_measurable : ∀ n, Measurable (sendB n)
4708
def sampler : AdaptiveOutcomeSampler O M := by
body · 28 lines
  have sample (n : ℕ) (κ : Kernel (LocalHistory O M n) O) [IsMarkovKernel κ] :
      ∃ f : LocalHistory O M n → I → O, Measurable (uncurry f) ∧
        (∀ h, volume.map (f h) = κ h) ∧
        ∀ (μ : Measure (LocalHistory O M n)) [IsFiniteMeasure μ],
          (μ.prod (volume : Measure I)).map (fun p => (p.1, f p.1 p.2)) = μ ⊗ₘ κ := by
    obtain ⟨f, hf, hfLaw⟩ := κ.exists_measurable_map_eq_unitInterval
    change Measurable (fun pair : LocalHistory O M n × I => f pair.1 pair.2) at hf
    refine ⟨f, hf, hfLaw, ?_⟩
    intro μ hμ
    ext B hB
    rw [Measure.map_apply (measurable_fst.prodMk hf) hB,
      Measure.prod_apply (hB.preimage (measurable_fst.prodMk hf)),
      Measure.compProd_apply hB]
    congr 1
    funext h
    rw [← hfLaw h, Measure.map_apply hf.of_uncurry_left (measurable_prodMk_left hB)]
    rfl
  exact {
    alice := d.alice
    alice_meas := d.alice_measurable
    sampleA n := uncurry (Classical.choose (sample n (d.kernelA n)))
    sampleB n := uncurry (Classical.choose (sample n (d.kernelB n)))
    sendA := d.sendA
    sendB := d.sendB
    sampleA_meas n := (Classical.choose_spec (sample n (d.kernelA n))).1
    sampleB_meas n := (Classical.choose_spec (sample n (d.kernelB n))).1
    sendA_meas := d.sendA_measurable
    sendB_meas := d.sendB_measurable }
4738
lemma sampler_law : ∀ n s, ((volume : Measure I).prod volume).map
    (fun u => d.sampler.causal.step n (s,u)) =
      d.sampler.instrumentKernel d.kernelA d.kernelB n s := by
proof · 5 lines
  apply d.sampler.causal_step_law
  · intro n s
    exact (Classical.choose_spec (sample_instrument_kernel (d.kernelA n))).2.1 s
  · intro n s
    exact (Classical.choose_spec (sample_instrument_kernel (d.kernelB n))).2.1 s
4747
def probeLaw : Measure (ℕ → O) :=
body · 1 line
  (noiseTapeLaw ((volume : Measure I).prod volume)).map d.sampler.causalRecord
4750
instance probeLaw_probability : IsProbabilityMeasure d.probeLaw :=
body · 1 line
  inferInstanceAs (IsProbabilityMeasure ((noiseTapeLaw ((volume : Measure I).prod volume)).map d.sampler.causal.record))
4753
lemma probeLaw_prefix (n : ℕ) : d.probeLaw.map (historyPrefix (n+1)) =
    ((d.probeLaw.map (historyPrefix n)) ⊗ₘ
      d.sampler.instrumentKernel d.kernelA d.kernelB n).map
      (fun pair : (Fin n → O) × O => Fin.snoc (α := fun _ : Fin (n+1) => O) pair.1 pair.2) := by
proof · 6 lines
  have he (k : ℕ) : historyPrefix k ∘ d.sampler.causal.record =
      d.sampler.causal.history k := by
    funext u i
    exact (d.sampler.causal.prefix_eq_record k u i).symm
  simp only [probeLaw, AdaptiveOutcomeSampler.causalRecord_eq, Measure.map_map (measurable_historyPrefix _) d.sampler.causal.measurable_record,he]
  exact d.sampler.causal.prefix_law_succ _ d.sampler_law n
4764
def transcriptLaw : Measure (ℕ → M) := d.probeLaw.map d.sampler.publicRecord
4766
instance transcriptLaw_probability : IsProbabilityMeasure d.transcriptLaw :=
body · 1 line
  inferInstanceAs (IsProbabilityMeasure (d.probeLaw.map d.sampler.publicRecord))
4783
lemma preparationFilter_product (R : Matrix (ι × κ) (ι × κ) ℂ)
    (U : Matrix ι ι ℂ) (V : Matrix κ κ ℂ) :
    preparationFilter R * (U ⊗ₖ V) * (preparationFilter R)ᴴ =
      (CFC.sqrt R * (((Fintype.card ι : ℂ) • Uᵀ) ⊗ₖ ((Fintype.card κ : ℂ) • Vᵀ)) * CFC.sqrt R)ᵀ := by
proof · 18 lines
  have hs : (CFC.sqrt R)ᴴ = CFC.sqrt R := (CFC.sqrt_nonneg R).posSemidef.isHermitian.eq
  have ht : ((CFC.sqrt R)ᵀ)ᴴ = (CFC.sqrt R)ᵀ := by
    rw [Matrix.conjTranspose_transpose_eq_transpose_conjTranspose,hs]
  have hc : (Real.sqrt (Fintype.card (ι × κ) : ℝ) : ℂ) *
      (Real.sqrt (Fintype.card (ι × κ) : ℝ) : ℂ) =
      (Fintype.card ι : ℂ)*(Fintype.card κ : ℂ) := by
    rw [← Complex.ofReal_mul,Real.mul_self_sqrt (Nat.cast_nonneg _),
      Fintype.card_prod,Nat.cast_mul]
    simp
  unfold preparationFilter
  simp only [Matrix.conjTranspose_smul,Complex.star_def,Complex.conj_ofReal,ht,
    smul_mul_assoc,mul_smul_comm,smul_smul,Matrix.transpose_mul,
    ← Matrix.kroneckerMap_transpose,Matrix.transpose_smul,Matrix.transpose_transpose,
    Matrix.smul_kronecker,Matrix.kronecker_smul]
  rw [hc]
  congr 1
  · ring
  · exact Matrix.mul_assoc _ _ _
4816
def eveBlock {ι : Type v8360_0} [inst8360_0 : Fintype ι] [inst8360_1 : DecidableEq ι]
    (R D : Matrix ι ι ℂ) : Matrix ι ι ℂ := (CFC.sqrt R * D * CFC.sqrt R)ᵀ
4829
structure BitDensity (μ : Measure Ω) (ι : Type v8637_0) [Fintype ι] where
body · 8 lines
  block : Bool → Bool → Ω → Matrix ι ι ℂ
  positive : ∀ i j t, (block i j t).PosSemidef
  measurable : ∀ i j, Measurable (block i j)
  integrable : ∀ i j, Integrable (fun t => (trace (block i j t)).re) μ
  normalized : (∫ t, (trace (block false false t)).re ∂μ) +
    (∫ t, (trace (block false true t)).re ∂μ) +
    (∫ t, (trace (block true false t)).re ∂μ) +
    (∫ t, (trace (block true true t)).re ∂μ) = 1
4839
def BitDensity.mass {μ : Measure Ω} (τ : BitDensity μ ι) (i j : Bool) : ℝ :=
body · 1 line
  ∫ t, (trace (τ.block i j t)).re ∂μ
4842
def BitDensity.idealBitError {μ : Measure Ω} (τ : BitDensity μ ι)
    (σ : Ω → Matrix ι ι ℂ) : ℝ :=
body · 3 lines
  τ.mass false true + τ.mass true false +
  (∫ t, traceNorm (τ.block false false t - (1/2 : ℝ) • σ t) ∂μ) +
  (∫ t, traceNorm (τ.block true true t - (1/2 : ℝ) • σ t) ∂μ)
4863
def matrixFilterLinear (K : Matrix η ι ℂ) : Matrix ι ι ℂ →ₗ[ℂ] Matrix η η ℂ where
body · 3 lines
  toFun U := K * U * Kᴴ
  map_add' U V := by rw [Matrix.mul_add,Matrix.add_mul]
  map_smul' c U := by simp only [Matrix.mul_smul,Matrix.smul_mul,RingHom.id_apply]
4868
lemma integral_filter (K : Matrix η ι ℂ) {U : Ω → Matrix ι ι ℂ}
    (hi : Integrable U μ) :
    (∫ x, K * U x * Kᴴ ∂μ) = K * (∫ x, U x ∂μ) * Kᴴ :=
proof · 1 line
  (matrixFilterLinear K).toContinuousLinearMap.integral_comp_comm hi
4873
lemma integrable_filter (K : Matrix η ι ℂ) {U : Ω → Matrix ι ι ℂ}
    (hi : Integrable U μ) : Integrable (fun x => K * U x * Kᴴ) μ :=
proof · 1 line
  (matrixFilterLinear K).toContinuousLinearMap.integrable_comp hi
4877
lemma integral_realTrace {U : Ω → Matrix ι ι ℂ} (hi : Integrable U μ) :
    (∫ x, (trace (U x)).re ∂μ) = (trace (∫ x, U x ∂μ)).re := by
proof · 4 lines
  let L : Matrix ι ι ℂ →L[ℂ] ℂ := (Matrix.traceLinearMap ι ℂ ℂ).toContinuousLinearMap
  change (∫ x, RCLike.re (L (U x)) ∂μ) = (L (∫ x,U x ∂μ)).re
  rw [integral_re (L.integrable_comp hi)]
  exact congrArg Complex.re (L.integral_comp_comm hi)
4884
lemma preparationFilter_probe_reference [Nonempty ι]
    (R : Matrix ι ι ℂ) (hR : R.PosSemidef) :
    preparationFilter R * keepReference (outer (localProbe (ι := ι)) localProbe) *
      (preparationFilter R)ᴴ = Rᵀ := by
proof · 1 line
  rw [← keepReference_filter,preparation_filter_identity,canonical_reference R hR]
4904
lemma public_product_integrable [Nonempty ι] [Nonempty η] (i j : Bool) : Integrable (fun t => X i t ⊗ₖ Y j t) μ :=
proof · 2 lines
  integrable_subdensity _ (fun t => subdensity_kronecker (hX i t) (hY j t))
    (measurable_matrix_kronecker (hXm i) (hYm j)) μ
4908
def filteredPublicDensity
    (hInit : (∫ t, X false t ⊗ₖ Y false t ∂μ) +
      (∫ t, X false t ⊗ₖ Y true t ∂μ) +
      (∫ t, X true t ⊗ₖ Y false t ∂μ) +
      (∫ t, X true t ⊗ₖ Y true t ∂μ) =
        keepReference (outer (localProbe (ι := ι × η)) localProbe)) : BitDensity μ (ι × η) where
body · 20 lines
  block i j t := preparationFilter R * (X i t ⊗ₖ Y j t) * (preparationFilter R)ᴴ
  positive i j t := ((hX i t).1.kronecker (hY j t).1).mul_mul_conjTranspose_same _
  measurable i j := (matrixFilterLinear (preparationFilter R)).toContinuousLinearMap.measurable.comp
    (measurable_matrix_kronecker (hXm i) (hYm j))
  integrable i j := by
    let L : Matrix (ι × η) (ι × η) ℂ →L[ℂ] ℂ :=
      (Matrix.traceLinearMap (ι × η) ℂ ℂ).toContinuousLinearMap
    exact (L.integrable_comp (integrable_filter _ (public_product_integrable μ X Y hXm hYm hX hY i j))).re
  normalized := by
    have hi (i j : Bool) := integrable_filter (preparationFilter R)
      (public_product_integrable μ X Y hXm hYm hX hY i j)
    rw [integral_realTrace (hi false false),integral_realTrace (hi false true),
      integral_realTrace (hi true false),integral_realTrace (hi true true)]
    simp_rw [integral_filter _ (public_product_integrable μ X Y hXm hYm hX hY _ _)]
    rw [← Complex.add_re,← Matrix.trace_add,← Complex.add_re,← Matrix.trace_add,
      ← Complex.add_re,← Matrix.trace_add]
    rw [← Matrix.add_mul,← Matrix.mul_add,← Matrix.add_mul,← Matrix.mul_add,
      ← Matrix.add_mul,← Matrix.mul_add,hInit,preparationFilter_probe_reference R hR.1,
      Matrix.trace_transpose,hR.2]
    rfl
4935
lemma filteredPublicDensity_common_factors
    (hInit : (∫ t, X false t ⊗ₖ Y false t ∂μ) +
      (∫ t, X false t ⊗ₖ Y true t ∂μ) +
      (∫ t, X true t ⊗ₖ Y false t ∂μ) +
      (∫ t, X true t ⊗ₖ Y true t ∂μ) =
        keepReference (outer (localProbe (ι := ι × η)) localProbe)) (i j : Bool) (t : T) :
    (filteredPublicDensity μ X Y hXm hYm hX hY R hR hInit).block i j t =
      eveBlock R (((Fintype.card ι : ℂ) • (X i t)ᵀ) ⊗ₖ ((Fintype.card η : ℂ) • (Y j t)ᵀ)) :=
proof · 1 line
  preparationFilter_product R (X i t) (Y j t)
4970
structure BitProtocol where
body · 14 lines
  executionA : LocalObservableExecution (ι := ι) A d.kernelA d.sendA
  executionB : LocalObservableExecution (ι := η) B d.kernelB d.sendB
  initialA : executionA.reference 0 (Fin.elim0,Fin.elim0) =
    keepReference (outer (localProbe (ι := ι)) localProbe)
  initialB : executionB.reference 0 (Fin.elim0,Fin.elim0) =
    keepReference (outer (localProbe (ι := η)) localProbe)
  incomingA : IncomingObservableProcess (L := d.sampler.localField true) (A := TA)
    d.probeLaw historyFiltration
    (fun n x => AdaptiveQuantum.globalLocalReferenceA d.sampler d.kernelA executionA n (historyPrefix n x))
  incomingB : IncomingObservableProcess (L := d.sampler.localField false) (A := TB)
    d.probeLaw historyFiltration
    (fun n x => AdaptiveQuantum.globalLocalReferenceB d.sampler d.kernelB executionB n (historyPrefix n x))
  readoutA : incomingA.BitReadout (B := CA)
  readoutB : incomingB.BitReadout (B := CB)
4992
def completeReference (i j : Bool) (x : ℕ → O) : Matrix (ι × η) (ι × η) ℂ :=
body · 1 line
  p.readoutA.value i x ⊗ₖ p.readoutB.value j x
4996
lemma completeReference_positive [StandardBorelSpace M] [Nonempty M] (i j : Bool) (x : ℕ → O) :
    (p.completeReference i j x).PosSemidef :=
proof · 1 line
  (p.readoutA.positive i x).kronecker (p.readoutB.positive j x)
5001
lemma completeReference_measurable [StandardBorelSpace M] [Nonempty M] (i j : Bool) : Measurable (p.completeReference i j) :=
proof · 3 lines
  measurable_matrix_kronecker
    ((p.readoutA.measurable i).mono (d.sampler.localField_le true) le_rfl)
    ((p.readoutB.measurable j).mono (d.sampler.localField_le false) le_rfl)
5006
lemma completeReference_integrable (i j : Bool) : Integrable (p.completeReference i j) d.probeLaw :=
proof · 3 lines
  integrable_subdensity _
    (fun x => subdensity_kronecker (p.readoutA.subdensity i x) (p.readoutB.subdensity j x))
    (p.completeReference_measurable i j) d.probeLaw
5011
lemma completeReference_entry_integrable (i j : Bool) (k l : ι × η) :
    Integrable (fun x => p.completeReference i j x k l) d.probeLaw :=
proof · 2 lines
  ((Matrix.entryLinearMap ℂ ℂ k l).toContinuousLinearMap).integrable_comp
    (p.completeReference_integrable i j)
5016
def probeOutput (i j : Bool) : PositiveMatrixMeasure (ℕ → M) (ι × η) :=
body · 3 lines
  (positiveMatrixMeasureOfDensity (p.completeReference_entry_integrable i j)
    (Eventually.of_forall (p.completeReference_positive i j))).pushforward
    d.sampler.publicRecord d.sampler.measurable_publicRecord
5021
lemma probeOutput_event (i j : Bool) (S : Set (ℕ → M)) (hS : MeasurableSet S) :
    (p.probeOutput i j).value S =
      ∫ x in d.sampler.publicRecord ⁻¹' S, p.completeReference i j x ∂d.probeLaw := by
proof · 2 lines
  rw [probeOutput,PositiveMatrixMeasure.pushforward_value _ _ _ S hS,
    positiveMatrixMeasureOfDensity_value _ _ _ (d.sampler.measurable_publicRecord hS)]
5028
lemma initial_joint_reference [StandardBorelSpace M] [Nonempty M] :
    AdaptiveQuantum.jointReference d.sampler d.kernelA d.kernelB p.executionA p.executionB
      0 Fin.elim0 = keepReference (outer (localProbe (ι := ι × η)) localProbe) := by
proof · 10 lines
  unfold AdaptiveQuantum.jointReference AdaptiveQuantum.globalLocalReferenceA
    AdaptiveQuantum.globalLocalReferenceB
  have ha : (d.sampler.extractPrivate true 0 Fin.elim0,d.sampler.publicFrom 0 Fin.elim0) =
      (Fin.elim0,Fin.elim0) := Subsingleton.elim _ _
  have hb : (d.sampler.extractPrivate false 0 Fin.elim0,d.sampler.publicFrom 0 Fin.elim0) =
      (Fin.elim0,Fin.elim0) := Subsingleton.elim _ _
  rw [ha,hb]
  change p.executionA.reference 0 (Fin.elim0,Fin.elim0) ⊗ₖ
    p.executionB.reference 0 (Fin.elim0,Fin.elim0) = _
  rw [p.initialA,p.initialB,localProbe_reference_product]
5042
theorem public_factors :
    ∃ X : Bool → (ℕ → M) → Matrix ι ι ℂ,
    ∃ Y : Bool → (ℕ → M) → Matrix η η ℂ,
      (∀ i, Measurable (X i)) ∧ (∀ j, Measurable (Y j)) ∧
      (∀ i t, Subdensity (X i t)) ∧ (∀ j t, Subdensity (Y j t)) ∧
      (∀ i j S, MeasurableSet S → (p.probeOutput i j).value S =
        ∫ t in S, X i t ⊗ₖ Y j t ∂d.transcriptLaw) ∧
      (∫ t, X false t ⊗ₖ Y false t ∂d.transcriptLaw) +
      (∫ t, X false t ⊗ₖ Y true t ∂d.transcriptLaw) +
      (∫ t, X true t ⊗ₖ Y false t ∂d.transcriptLaw) +
      (∫ t, X true t ⊗ₖ Y true t ∂d.transcriptLaw) =
        keepReference (outer (localProbe (ι := ι × η)) localProbe) := by
proof · 6 lines
  obtain ⟨X,Y,hXm,hYm,hX,hY,hXY,hn⟩ := AdaptiveQuantum.terminal_public_normalization
    d.sampler d.kernelA d.kernelB p.executionA p.executionB d.probeLaw d.probeLaw_prefix
    p.incomingA p.incomingB p.readoutA p.readoutB d.sampler_law
  refine ⟨X,Y,hXm,hYm,hX,hY,?_,hn.trans p.initial_joint_reference⟩
  intro i j S hS
  exact (p.probeOutput_event i j S hS).trans (hXY i j S hS).symm
5061
def canonicalOutput (R : Matrix (ι × η) (ι × η) ℂ) (i j : Bool) :
    PositiveMatrixMeasure (ℕ → M) (ι × η) :=
body · 1 line
  (p.probeOutput i j).filter (preparationFilter R)
5079
def positiveDensity (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) (t : Ω) : Matrix ι ι ℂ :=
body · 1 line
  by classical exact if (W.density μ t).PosSemidef then W.density μ t else 0
5083
lemma positiveDensity_pos [DecidableEq ι] (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) (t : Ω) :
    (W.positiveDensity μ t).PosSemidef := by
proof · 4 lines
  unfold positiveDensity
  split
  · assumption
  · exact Matrix.PosSemidef.zero
5090
lemma measurable_positiveDensity (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) :
    Measurable (W.positiveDensity μ) := by
proof · 7 lines
  have hm : MeasurableSet {t | (W.density μ t).PosSemidef} := by
    have he : {t | (W.density μ t).PosSemidef} = (W.density μ) ⁻¹' Set.Ici 0 := by
      ext t
      exact Matrix.nonneg_iff_posSemidef.symm
    rw [he]
    exact (W.measurable_density μ) measurableSet_Ici
  exact (W.measurable_density μ).ite hm measurable_const
5100
lemma positiveDensity_ae (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω) [SigmaFinite μ]
    (hAC : ∀ S, MeasurableSet S → μ S=0 → W.value S=0) :
    W.positiveDensity μ =ᵐ[μ] W.density μ := by
proof · 2 lines
  filter_upwards [density_posSemidef_ae W μ hAC] with t ht
  exact ite_eq_left ht
5106
lemma integrable_positiveDensity_trace (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω)
    [SigmaFinite μ] (hAC : ∀ S, MeasurableSet S → μ S=0 → W.value S=0) :
    Integrable (fun t => (trace (W.positiveDensity μ t)).re) μ := by
proof · 4 lines
  apply (W.integrable_trace_density μ).re.congr
  filter_upwards [W.positiveDensity_ae μ hAC] with t ht
  rw [ht]
  rfl
5114
lemma integral_positiveDensity_trace (W : PositiveMatrixMeasure Ω ι) (μ : Measure Ω)
    [SigmaFinite μ] (hAC : ∀ S, MeasurableSet S → μ S=0 → W.value S=0) :
    (∫ t, (trace (W.positiveDensity μ t)).re ∂μ) = (trace (W.value Set.univ)).re := by
proof · 11 lines
  calc
    _ = ∫ t, (trace (W.density μ t)).re ∂μ := by
      apply integral_congr_ae
      filter_upwards [W.positiveDensity_ae μ hAC] with t ht
      rw [ht]
    _ = (trace (W.value Set.univ)).re := by
      change (∫ t, RCLike.re (trace (W.density μ t)) ∂μ) = _
      rw [integral_re (W.integrable_trace_density μ)]
      have he := W.integral_trace_density μ hAC Set.univ MeasurableSet.univ
      change (∫ t, trace (W.density μ t) ∂μ).re = _
      simpa only [Measure.restrict_univ] using congrArg Complex.re he
5129
def toBitDensity (W : Bool → Bool → PositiveMatrixMeasure Ω ι)
    (μ : Measure Ω) [SigmaFinite μ]
    (hAC : ∀ i j S, MeasurableSet S → μ S=0 → (W i j).value S=0)
    (hn : (trace ((W false false).value Set.univ)).re +
      (trace ((W false true).value Set.univ)).re +
      (trace ((W true false).value Set.univ)).re +
      (trace ((W true true).value Set.univ)).re = 1) : BitDensity μ ι where
body · 7 lines
  block i j := (W i j).positiveDensity μ
  positive i j := (W i j).positiveDensity_pos μ
  measurable i j := (W i j).measurable_positiveDensity μ
  integrable i j := (W i j).integrable_positiveDensity_trace μ (hAC i j)
  normalized := by
    simp only [integral_positiveDensity_trace _ μ (hAC _ _)]
    exact hn
5176
theorem canonical_density_representation :
    ∃ τ : BitDensity d.transcriptLaw (ι × η),
    ∃ X : Bool → (ℕ → M) → Matrix ι ι ℂ,
    ∃ Y : Bool → (ℕ → M) → Matrix η η ℂ,
      (∀ i t, (X i t).PosSemidef) ∧ (∀ j t, (Y j t).PosSemidef) ∧
      (∀ i j t, τ.block i j t = eveBlock R (X i t ⊗ₖ Y j t)) ∧
      (∀ i j, Integrable (τ.block i j) d.transcriptLaw) ∧
      (∀ i j S, MeasurableSet S → (p.canonicalOutput R i j).value S =
        ∫ t in S, τ.block i j t ∂d.transcriptLaw) := by
proof · 15 lines
  obtain ⟨X,Y,hXm,hYm,hX,hY,hXY,hn⟩ := p.public_factors
  let τ := filteredPublicDensity d.transcriptLaw X Y hXm hYm hX hY R hR hn
  refine ⟨τ,fun i t => (Fintype.card ι : ℂ) • (X i t)ᵀ,
    fun j t => (Fintype.card η : ℂ) • (Y j t)ᵀ,?_,?_,?_,?_,?_⟩
  · intro i t
    exact (hX i t).1.transpose.smul (by exact_mod_cast Nat.zero_le (Fintype.card ι))
  · intro j t
    exact (hY j t).1.transpose.smul (by exact_mod_cast Nat.zero_le (Fintype.card η))
  · exact filteredPublicDensity_common_factors d.transcriptLaw X Y hXm hYm hX hY R hR hn
  · intro i j
    exact integrable_filter _ (public_product_integrable d.transcriptLaw X Y hXm hYm hX hY i j)
  · intro i j S hS
    rw [canonicalOutput,PositiveMatrixMeasure.filter_value,hXY i j S hS]
    exact (integral_filter _
      ((public_product_integrable d.transcriptLaw X Y hXm hYm hX hY i j).integrableOn)).symm
5201
lemma canonicalOutput_normalized :
    (trace ((p.canonicalOutput R false false).value Set.univ)).re +
    (trace ((p.canonicalOutput R false true).value Set.univ)).re +
    (trace ((p.canonicalOutput R true false).value Set.univ)).re +
    (trace ((p.canonicalOutput R true true).value Set.univ)).re = 1 := by
proof · 3 lines
  obtain ⟨τ,X,Y,hX,hY,hτ,hi,he⟩ := p.canonical_density_representation R hR
  simp_rw [he _ _ Set.univ MeasurableSet.univ,Measure.restrict_univ,← integral_realTrace (hi _ _)]
  exact τ.normalized
5225
def cqDistance (W V : K → K → Ω → Matrix ι ι ℂ) (μ : Measure Ω) : ℝ :=
body · 1 line
  ∑ a, ∑ b, ∫ t, traceNorm (W a b t - V a b t) ∂μ
5228
def idealFiniteBlocks (sigma : Matrix ι ι ℂ) (a b : K) : Matrix ι ι ℂ :=
body · 1 line
  if a = b then (1/(Fintype.card K : ℝ)) • sigma else 0
5243
def compressReference (D : Matrix ι ι ℂ) :
    Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ →ₗ[ℂ] Matrix ι ι ℂ where
body · 3 lines
  toFun Z := Z.submatrix Sum.inr Sum.inr + trace (Z.submatrix Sum.inl Sum.inl) • D
  map_add' Z W := by simp [Matrix.submatrix_add,Matrix.trace_add,add_smul]; abel
  map_smul' c Z := by simp [Matrix.submatrix_smul,Matrix.trace_smul,smul_smul,smul_add]
5250
lemma compressReference_pos [Fintype ι] [DecidableEq ι] [DecidableEq κ] (D : Matrix ι ι ℂ) (hD : D.PosSemidef)
    {Z : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ} (hZ : Z.PosSemidef) :
    (compressReference D Z).PosSemidef :=
proof · 1 line
  (hZ.submatrix Sum.inr).add (hD.smul (hZ.submatrix Sum.inl).trace_nonneg)
5256
lemma compressReference_trace [DecidableEq ι] [DecidableEq κ] (D : Matrix ι ι ℂ) (hD : trace D = 1)
    (Z : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ) : trace (compressReference D Z) = trace Z := by
proof · 4 lines
  change trace (Z.submatrix Sum.inr Sum.inr + trace (Z.submatrix Sum.inl Sum.inl) • D) = _
  rw [Matrix.trace_add,Matrix.trace_smul,hD,smul_eq_mul,mul_one]
  simp only [trace,diag,Matrix.submatrix_apply,Fintype.sum_sum_type]
  exact add_comm _ _
5264
lemma compressReference_right [Fintype ι] [DecidableEq ι] [DecidableEq κ] (D Z : Matrix ι ι ℂ) :
    compressReference (κ := κ) D (fromBlocks 0 0 0 Z) = Z := by
proof · 2 lines
  ext i j
  simp [compressReference,Matrix.submatrix,Matrix.trace,Matrix.diag]
5269
lemma padded_purification_unitary (M : Matrix ι κ ℂ) (S : Matrix ι ι ℂ)
    (hS : S.IsHermitian) (hgram : M * Mᴴ = S * S) :
    ∃ U : Matrix.unitaryGroup (κ ⊕ ι) ℂ,
      fromBlocks (0 : Matrix κ κ ℂ) Mᴴ 0 0 =
        (U : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ) * fromBlocks 0 0 0 S := by
proof · 2 lines
  apply exists_unitary_mul_of_gram_eq
  simp [Matrix.fromBlocks_conjTranspose,Matrix.fromBlocks_multiply,hS.eq,hgram]
5277
def embedReference : Matrix κ κ ℂ →ₗ[ℂ] Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ where
body · 3 lines
  toFun Z := fromBlocks Z 0 0 0
  map_add' _ _ := by ext i j; cases i <;> cases j <;> simp
  map_smul' _ _ := by ext i j; cases i <;> cases j <;> simp
5283
lemma embedReference_pos [DecidableEq ι] {Z : Matrix κ κ ℂ} (hZ : Z.PosSemidef) :
    (embedReference (ι := ι) Z).PosSemidef := by
proof · 4 lines
  have hh := hZ.mul_mul_conjTranspose_same (Matrix.fromRows (1 : Matrix κ κ ℂ) (0 : Matrix ι κ ℂ))
  rw [Matrix.conjTranspose_fromRows_eq_fromCols_conjTranspose,Matrix.fromRows_mul,
    Matrix.fromRows_mul_fromCols] at hh
  simpa [embedReference] using hh
5291
lemma embedReference_trace [DecidableEq ι] [DecidableEq κ] (Z : Matrix κ κ ℂ) :
    trace (embedReference (ι := ι) Z) = trace Z := by
proof · 1 line
  simp [embedReference,trace,diag,Fintype.sum_sum_type]
5295
def refSandwich (K : Matrix ι κ ℂ) : Matrix κ κ ℂ →ₗ[ℂ] Matrix ι ι ℂ where
body · 3 lines
  toFun Z := K * Z * Kᴴ
  map_add' _ _ := by simp [Matrix.mul_add,Matrix.add_mul]
  map_smul' _ _ := by simp [Matrix.mul_smul,Matrix.smul_mul]
5300
lemma padded_purification_blocks (M : Matrix ι κ ℂ) (S : Matrix ι ι ℂ)
    (hS : S.IsHermitian) (U : Matrix.unitaryGroup (κ ⊕ ι) ℂ)
    (hU : fromBlocks (0 : Matrix κ κ ℂ) Mᴴ 0 0 =
      (U : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ) * fromBlocks 0 0 0 S)
    (X : Matrix ι ι ℂ) :
    embedReference (ι := ι) (Mᴴ * X * M) =
      (U : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ) * fromBlocks 0 0 0 (S * X * S) *
      (U : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ)ᴴ := by
proof · 13 lines
  let A : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ := fromBlocks 0 Mᴴ 0 0
  let B : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ := fromBlocks 0 0 0 S
  let Y : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ := fromBlocks 0 0 0 X
  have he : embedReference (ι := ι) (Mᴴ * X * M) = A * Y * Aᴴ := by
    simp [embedReference,A,Y,Matrix.fromBlocks_conjTranspose,Matrix.fromBlocks_multiply]
  have hb : B * Y * Bᴴ = fromBlocks 0 0 0 (S*X*S) := by
    simp [B,Y,Matrix.fromBlocks_conjTranspose,Matrix.fromBlocks_multiply,hS.eq]
  change A = (U : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ)*B at hU
  rw [he,hU,Matrix.conjTranspose_mul]
  calc
    (U : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ)*B*Y*(Bᴴ*(U : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ)ᴴ) =
      (U : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ)*(B*Y*Bᴴ)*(U : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ)ᴴ := by noncomm_ring
    _ = _ := by rw [hb]
5322
theorem purification_recovery (R : Matrix ι ι ℂ) (hR : Density R)
    (M : Matrix ι κ ℂ) (hM : M * Mᴴ = R) :
    ∃ L : Matrix κ κ ℂ →ₗ[ℂ] Matrix ι ι ℂ,
      (∀ Z, Z.PosSemidef → (L Z).PosSemidef) ∧
      (∀ Z, trace (L Z) = trace Z) ∧
      ∀ X, L (Mᴴ * X * M) = CFC.sqrt R * X * CFC.sqrt R := by
proof · 22 lines
  let S := CFC.sqrt R
  have hS : S.IsHermitian := (CFC.sqrt_nonneg R).posSemidef.isHermitian
  have hgram : M*Mᴴ = S*S := by rw [hM]; exact (CFC.sqrt_mul_sqrt_self R hR.1.nonneg).symm
  obtain ⟨U,hU⟩ := padded_purification_unitary M S hS hgram
  let V : Matrix (κ ⊕ ι) (κ ⊕ ι) ℂ := U
  let L := (compressReference (κ := κ) R).comp ((refSandwich Vᴴ).comp embedReference)
  refine ⟨L,?_,?_,?_⟩
  · intro Z hZ
    exact compressReference_pos R hR.1 ((embedReference_pos hZ).mul_mul_conjTranspose_same Vᴴ)
  · intro Z
    change trace (compressReference R (Vᴴ * embedReference Z * Vᴴᴴ)) = _
    rw [compressReference_trace R hR.2,Matrix.conjTranspose_conjTranspose,
      Matrix.trace_mul_cycle,show V*Vᴴ = 1 from U.prop.2,one_mul,embedReference_trace]
  · intro X
    change compressReference R (Vᴴ * embedReference (Mᴴ*X*M) * Vᴴᴴ) = _
    rw [padded_purification_blocks M S hS U hU X,Matrix.conjTranspose_conjTranspose]
    have hv : Vᴴ * (V * fromBlocks 0 0 0 (S*X*S) * Vᴴ) * V =
        fromBlocks 0 0 0 (S*X*S) := by
      rw [mul_assoc V _ _,← mul_assoc Vᴴ V _,show Vᴴ*V=1 from U.prop.1,one_mul,
        mul_assoc,show Vᴴ*V=1 from U.prop.1,mul_one]
    change compressReference R (Vᴴ*(V*fromBlocks 0 0 0 (S*X*S)*Vᴴ)*V) = _
    rw [hv,compressReference_right]
5361
theorem purification_recovery_transpose {ι : Type v10053_0} {κ : Type v10053_1}
    [Fintype ι] [Fintype κ] [DecidableEq ι] [DecidableEq κ]
    (R : Matrix ι ι ℂ) (hR : Density R)
    (M : Matrix ι κ ℂ) (hM : M * Mᴴ = R) :
    ∃ L : Matrix κ κ ℂ →ₗ[ℂ] Matrix ι ι ℂ,
      (∀ Z, Z.PosSemidef → (L Z).PosSemidef) ∧
      (∀ Z, trace (L Z) = trace Z) ∧
      ∀ X, L ((Mᴴ * X * M)ᵀ) = (CFC.sqrt R * X * CFC.sqrt R)ᵀ := by
proof · 14 lines
  obtain ⟨L,hpos,htr,hrec⟩ := purification_recovery R hR M hM
  let J : Matrix κ κ ℂ →ₗ[ℂ] Matrix ι ι ℂ :=
    { toFun Z := (L Zᵀ)ᵀ
      map_add' Z W := by simp only [Matrix.transpose_add,map_add]
      map_smul' c Z := by simp only [Matrix.transpose_smul,map_smul,RingHom.id_apply] }
  refine ⟨J,?_,?_,?_⟩
  · intro Z hZ
    exact (hpos Zᵀ hZ.transpose).transpose
  · intro Z
    change trace (L Zᵀ)ᵀ = trace Z
    rw [Matrix.trace_transpose,htr,Matrix.trace_transpose]
  · intro X
    change (L ((Mᴴ*X*M)ᵀ)ᵀ)ᵀ = _
    rw [Matrix.transpose_transpose,hrec]
5396
def purificationFilter (P : Matrix ι κ ℂ) : Matrix κ ι ℂ :=
body · 1 line
  (Real.sqrt (Fintype.card ι : ℝ) : ℂ) • Pᵀ

Matrix-valued output entries after purification and publication of the transcript.

5402
def publicPurifiedEntries {Ω T K : Type*} [MeasurableSpace Ω] [MeasurableSpace T]
    (μ : Measure Ω) (record : Ω → T) (W : K → K → Ω → Matrix ι ι ℂ)
    (P : Matrix ι κ ℂ) (a b : K) (x y : κ) : ComplexMeasure T :=
body · 2 lines
  ∑ i, ∑ j, (purificationFilter P x i * star (purificationFilter P y j)) •
    (μ.withDensityᵥ (fun ω => W a b ω i j)).map record

The positive representative of the entrywise Radon–Nikodym derivative.

5409
def positiveEntryDensity {Ω : Type*} [MeasurableSpace Ω]
    (W : ι → ι → ComplexMeasure Ω) (μ : Measure Ω) (t : Ω) : Matrix ι ι ℂ := by
body · 3 lines
  classical
  let D : Matrix ι ι ℂ := fun i j => (W i j).rnDeriv μ t
  exact if D.PosSemidef then D else 0
5416
lemma purificationFilter_block [Fintype κ] [DecidableEq ι] [DecidableEq κ] (P : Matrix ι κ ℂ) (U : Matrix ι ι ℂ) :
    purificationFilter P * U * (purificationFilter P)ᴴ =
      (Pᴴ * ((Fintype.card ι : ℂ) • Uᵀ) * P)ᵀ := by
proof · 9 lines
  have hc : (Real.sqrt (Fintype.card ι : ℝ) : ℂ) *
      (Real.sqrt (Fintype.card ι : ℝ) : ℂ) = (Fintype.card ι : ℂ) := by
    rw [← Complex.ofReal_mul,Real.mul_self_sqrt (Nat.cast_nonneg _)]
    norm_cast
  simp only [purificationFilter,Matrix.conjTranspose_smul,Complex.star_def,
    Complex.conj_ofReal,Matrix.smul_mul,Matrix.mul_smul,smul_smul,
    Matrix.transpose_mul,Matrix.transpose_smul,Matrix.transpose_transpose,
    Matrix.conjTranspose_transpose_eq_transpose_conjTranspose]
  rw [hc,Matrix.mul_assoc]
5429
lemma preparationFilter_eq_purificationFilter (R : Matrix ι ι ℂ) :
    preparationFilter R = purificationFilter (CFC.sqrt R) := rfl
5432
theorem purification_filter_recovery (R : Matrix ι ι ℂ) (hR : Density R)
    (P : Matrix ι κ ℂ) (hP : P * Pᴴ = R) :
    ∃ L : Matrix κ κ ℂ →ₗ[ℂ] Matrix ι ι ℂ,
      (∀ Z, Z.PosSemidef → (L Z).PosSemidef) ∧
      (∀ Z, trace (L Z) = trace Z) ∧
      ∀ U, L (purificationFilter P * U * (purificationFilter P)ᴴ) =
        preparationFilter R * U * (preparationFilter R)ᴴ := by
proof · 5 lines
  obtain ⟨L,hpos,htr,he⟩ := purification_recovery_transpose R hR P hP
  refine ⟨L,hpos,htr,?_⟩
  intro U
  rw [purificationFilter_block,he,preparationFilter_eq_purificationFilter,
    purificationFilter_block,(CFC.sqrt_nonneg R).posSemidef.isHermitian.eq]
5475
def purifiedOutput (i j : Bool) : PositiveMatrixMeasure (ℕ → M) κ :=
body · 1 line
  (p.probeOutput i j).filter (purificationFilter P)
5479
lemma purifiedOutput_ac [Nonempty ι] [Nonempty η] [DecidableEq κ] (i j : Bool) (S : Set (ℕ → M)) (hS : MeasurableSet S)
    (hnull : d.transcriptLaw S = 0) : (p.purifiedOutput P i j).value S = 0 := by
proof · 3 lines
  obtain ⟨X,Y,hXm,hYm,hX,hY,he,hn⟩ := p.public_factors
  rw [purifiedOutput,PositiveMatrixMeasure.filter_value,he i j S hS,
    Measure.restrict_eq_zero.mpr hnull,integral_zero_measure,Matrix.mul_zero,Matrix.zero_mul]
5488
lemma purifiedOutput_recovery [Nonempty ι] [Nonempty η] :
    ∃ L : Matrix κ κ ℂ →ₗ[ℂ] Matrix (ι × η) (ι × η) ℂ,
      (∀ Z, Z.PosSemidef → (L Z).PosSemidef) ∧
      (∀ Z, trace (L Z) = trace Z) ∧
      ∀ i j S, L ((p.purifiedOutput P i j).value S) = (p.canonicalOutput R i j).value S := by
proof · 5 lines
  obtain ⟨L,hpos,htr,he⟩ := purification_filter_recovery R hR P hP
  refine ⟨L,hpos,htr,?_⟩
  intro i j S
  simpa only [purifiedOutput,canonicalOutput,PositiveMatrixMeasure.filter_value] using
    he ((p.probeOutput i j).value S)
5499
lemma purifiedOutput_normalized :
    (trace ((p.purifiedOutput P false false).value Set.univ)).re +
    (trace ((p.purifiedOutput P false true).value Set.univ)).re +
    (trace ((p.purifiedOutput P true false).value Set.univ)).re +
    (trace ((p.purifiedOutput P true true).value Set.univ)).re = 1 := by
proof · 5 lines
  obtain ⟨L,hpos,htr,he⟩ := p.purifiedOutput_recovery R hR P hP
  have hh i j : trace ((p.purifiedOutput P i j).value Set.univ) =
      trace ((p.canonicalOutput R i j).value Set.univ) := by rw [← he,htr]
  simp_rw [hh]
  exact p.canonicalOutput_normalized R hR
5510
def purifiedDensity (ν : Measure (ℕ → M)) [SigmaFinite ν]
    (hμν : d.transcriptLaw ≪ ν) : BitDensity ν κ :=
body · 3 lines
  PositiveMatrixMeasure.toBitDensity (p.purifiedOutput P) ν
    (fun i j S hS hz => p.purifiedOutput_ac P i j S hS (hμν hz))
    (p.purifiedOutput_normalized R hR P hP)

The operational bit error written directly in terms of the output entries.

5517
def rawPurifiedSecretBitError (P : Matrix (ι × η) κ ℂ)
    (sigma : PositiveMatrixMeasure (ℕ → M) κ) : ℝ :=
body · 8 lines
  let ν := d.transcriptLaw + sigma.traceMeasure
  let τ := fun i j => positiveEntryDensity
    (publicPurifiedEntries d.probeLaw d.sampler.publicRecord p.completeReference P i j) ν
  let σ := sigma.positiveDensity ν
  (∫ t, (trace (τ false true t)).re ∂ν) +
    (∫ t, (trace (τ true false t)).re ∂ν) +
    (∫ t, traceNorm (τ false false t - (1 / ((2 : ℕ) : ℝ)) • σ t) ∂ν) +
    (∫ t, traceNorm (τ true true t - (1 / ((2 : ℕ) : ℝ)) • σ t) ∂ν)
5528
def purifiedSecretBitError (sigma : PositiveMatrixMeasure (ℕ → M) κ) : ℝ :=
body · 3 lines
  (p.purifiedDensity R hR P hP (d.transcriptLaw + sigma.traceMeasure)
    (Measure.AbsolutelyContinuous.rfl.add_right _)).idealBitError
      (sigma.positiveDensity (d.transcriptLaw + sigma.traceMeasure))
5533
lemma rawPurifiedSecretBitError_eq (sigma : PositiveMatrixMeasure (ℕ → M) κ) :
    p.rawPurifiedSecretBitError P sigma = p.purifiedSecretBitError R hR P hP sigma := rfl
5556
def postprocess (J : ObservableInstrument O A B) (f : O → Bool) (hf : Measurable f) :
    ObservableInstrument Bool A B where
body · 3 lines
  operation E := J.operation (f ⁻¹' E)
  positive E hE x hx := J.positive _ (hf hE) x hx
  total_unit := by simpa only [Set.preimage_univ] using J.total_unit
5570
lemma event_trace [Nonempty O] [DecidableEq ι] (E : Set O) (hE : MeasurableSet E) :
    trace (S (J.operation E 1)) = (r.law.real E : ℂ) := by
proof · 8 lines
  calc
    _ = ∫ o in E, trace (r.posterior o 1) ∂r.law := by
      simp only [trace,diag]
      rw [integral_finsetSum _ (fun i _ => (r.integrable 1 i i).integrableOn)]
      apply Finset.sum_congr rfl
      intro i _
      exact (r.update E hE 1 i i).symm
    _ = _ := by simp [r.posterior_trace,integral_const,Complex.real_smul,Measure.real]
5582
lemma event_zero [Nonempty O] [DecidableEq ι] (E : Set O) (hE : MeasurableSet E) (hz : r.law E = 0) (x : B) :
    S (J.operation E x) = 0 := by
proof · 3 lines
  ext i j
  rw [← r.update E hE x i j,Measure.restrict_eq_zero.mpr hz,integral_zero_measure]
  rfl
5590
def coarseMass (i : Bool) : ℝ := r.law.real (f ⁻¹' {i})
5592
def coarsePosterior (i : Bool) : ReferenceFunctional B ι :=
body · 2 lines
  if r.coarseMass f i = 0 then r.posterior (Classical.choice ‹Nonempty O›)
  else ((r.coarseMass f i)⁻¹ : ℝ) • (S.comp (J.operation (f ⁻¹' {i})))
5599
lemma coarsePosterior_positive [DecidableEq ι] (hS : PositiveFunctional S) (i : Bool) :
    PositiveFunctional (r.coarsePosterior f i) := by
proof · 6 lines
  unfold coarsePosterior
  split
  · exact r.posterior_positive _
  · intro x hx
    exact (hS _ (J.positive _ (hf (measurableSet_singleton i)) x hx)).smul
      (inv_nonneg.mpr (ENNReal.toReal_nonneg))
5608
lemma coarsePosterior_trace (_hS : PositiveFunctional S) (i : Bool) :
    trace (r.coarsePosterior f i 1) = 1 := by
proof · 8 lines
  unfold coarsePosterior
  split
  · exact r.posterior_trace _
  · rename_i hn
    simp only [LinearMap.smul_apply,LinearMap.comp_apply,Matrix.trace_smul]
    rw [r.event_trace _ (hf (measurableSet_singleton i))]
    change ((r.coarseMass f i)⁻¹ : ℝ) • ((r.coarseMass f i : ℝ) : ℂ) = 1
    simp only [Complex.real_smul,Complex.ofReal_inv,inv_mul_cancel₀ (Complex.ofReal_ne_zero.mpr hn)]
5619
lemma coarsePosterior_weighted (i : Bool) (x : B) :
    r.coarseMass f i • r.coarsePosterior f i x = S (J.operation (f ⁻¹' {i}) x) := by
proof · 10 lines
  let : IsProbabilityMeasure r.law := r.probability
  unfold coarsePosterior
  split
  · rename_i hz
    rw [hz,zero_smul]
    symm
    apply r.event_zero _ (hf (measurableSet_singleton i))
    exact (ENNReal.toReal_eq_zero_iff _).mp hz |>.resolve_right (measure_ne_top _ _)
  · rename_i hn
    simp only [LinearMap.smul_apply,LinearMap.comp_apply,smul_smul,mul_inv_cancel₀ hn,one_smul]
5632
lemma coarsePosterior_singleton (i : Bool) (x : B) (k l : ι) :
    (∫ b in {i}, r.coarsePosterior f b x k l ∂(r.law.map f)) =
      S (J.operation (f ⁻¹' {i}) x) k l := by
proof · 6 lines
  rw [integral_singleton]
  have hm : (r.law.map f).real {i} = r.coarseMass f i := by
    rw [Measure.real,Measure.map_apply hf (measurableSet_singleton i)]
    rfl
  rw [hm]
  exact congrArg (fun U : Matrix ι ι ℂ => U k l) (r.coarsePosterior_weighted f hf i x)
5642
lemma coarsePosterior_integral (E : Set Bool) (x : B) (k l : ι) :
    (∫ b in E, r.coarsePosterior f b x k l ∂(r.law.map f)) =
      S (J.operation (f ⁻¹' E) x) k l := by
proof · 33 lines
  let : IsProbabilityMeasure r.law := r.probability
  let : IsProbabilityMeasure (r.law.map f) := inferInstance
  have hpart : E = ∅ ∨ E = {false} ∨ E = {true} ∨ E = Set.univ := by
    by_cases h0 : false ∈ E <;> by_cases h1 : true ∈ E
    · right; right; right; ext b; cases b <;> simp_all
    · right; left; ext b; cases b <;> simp_all
    · right; right; left; ext b; cases b <;> simp_all
    · left; ext b; cases b <;> simp_all
  rcases hpart with rfl | rfl | rfl | rfl
  · simp only [setIntegral_empty,Set.preimage_empty]
    exact (congrArg (fun U : Matrix ι ι ℂ => U k l)
      (r.event_zero ∅ MeasurableSet.empty (measure_empty) x)).symm
  · exact r.coarsePosterior_singleton f hf false x k l
  · exact r.coarsePosterior_singleton f hf true x k l
  · have hd : Disjoint (f ⁻¹' {false}) (f ⁻¹' {true}) := by
      apply Set.disjoint_left.mpr
      intro o h0 h1
      simp only [Set.mem_preimage,Set.mem_singleton_iff] at h0 h1
      rw [h0] at h1
      contradiction
    have hu : (f ⁻¹' {false}) ∪ (f ⁻¹' {true}) = Set.univ := by
      ext o; cases f o <;> simp
    have hb : ({false} : Set Bool) ∪ {true} = Set.univ := by ext b; cases b <;> simp
    rw [← hb,setIntegral_union (by simp) (measurableSet_singleton true)
      Integrable.of_finite.integrableOn Integrable.of_finite.integrableOn,
      r.coarsePosterior_singleton f hf false x k l,
      r.coarsePosterior_singleton f hf true x k l]
    rw [← r.update _ (hf (measurableSet_singleton false)) x k l,
      ← r.update _ (hf (measurableSet_singleton true)) x k l]
    rw [← setIntegral_union hd (hf (measurableSet_singleton true))
      (r.integrable x k l).integrableOn (r.integrable x k l).integrableOn,hu]
    simpa only [Set.preimage_union,Set.preimage_univ,hu] using
      r.update Set.univ MeasurableSet.univ x k l
5679
def postprocess (hS : PositiveFunctional S) :
    InstrumentRun (J.postprocess f hf) S where
body · 12 lines
  law := r.law.map f
  probability := by
    let : IsProbabilityMeasure r.law := r.probability
    exact inferInstance
  posterior := r.coarsePosterior f
  posterior_positive := r.coarsePosterior_positive f hf hS
  posterior_trace := r.coarsePosterior_trace f hf hS
  integrable x k l := by
    let : IsProbabilityMeasure r.law := r.probability
    let : IsProbabilityMeasure (r.law.map f) := inferInstance
    exact Integrable.of_finite
  update E hE x k l := r.coarsePosterior_integral f hf E x k l
5706
lemma InstrumentRun.event_finset [Fintype K] [DecidableEq K] [Nonempty K] [DecidableEq ι] {J : ObservableInstrument K A B}
    {S : ReferenceFunctional A ι} (r : InstrumentRun J S) (s : Finset K) (x : B) :
    S (J.operation s x) = ∑ k ∈ s, S (J.operation {k} x) := by
proof · 7 lines
  ext i j
  simp only [Matrix.sum_apply]
  rw [← r.update s (s.finite_toSet.measurableSet) x i j,
    setIntegral_finset s (r.integrable x i j).integrableOn]
  apply Finset.sum_congr rfl
  intro k hk
  rw [← r.update {k} (measurableSet_singleton k) x i j,integral_singleton]
5717
lemma InstrumentRun.event_total {J : ObservableInstrument K A B}
    {S : ReferenceFunctional A ι} (r : InstrumentRun J S) :
    (∑ k, S (J.operation {k} 1)) = S 1 := by
proof · 1 line
  simpa only [Finset.coe_univ,J.total_unit] using (r.event_finset Finset.univ 1).symm
5728
structure FiniteReadout where
body · 4 lines
  instrument : Ω → ObservableInstrument K A B
  run : ∀ x, InstrumentRun (instrument x) (e.state x)
  positive_unit : (0 : B) ≤ 1
  local_measurable : ∀ k, Measurable[L] (fun x => e.state x ((instrument x).operation {k} 1))
5738
def value (k : K) (x : Ω) : Matrix ι ι ℂ := e.state x ((b.instrument x).operation {k} 1)
5741
lemma measurable [MeasurableSingletonClass K] [Fintype K] [DecidableEq K] [Nonempty K] (k : K) : Measurable[L] (b.value k) := b.local_measurable k
5744
lemma positive [Fintype K] [DecidableEq K] [Nonempty K] (k : K) (x : Ω) : (b.value k x).PosSemidef :=
proof · 1 line
  e.positive x _ ((b.instrument x).positive {k} (measurableSet_singleton k) 1 b.positive_unit)
5747
lemma sum (x : Ω) : (∑ k, b.value k x) = e.state x 1 := (b.run x).event_total
5749
lemma subdensity (k : K) (x : Ω) : ZeroKey.Subdensity (b.value k x) := by
proof · 6 lines
  refine ⟨b.positive k x,?_⟩
  have hsum := congrArg (fun M : Matrix ι ι ℂ => (trace M).re) (b.sum x)
  simp only [Matrix.trace_sum,Complex.re_sum,e.normalized x,Complex.one_re] at hsum
  rw [← hsum]
  exact Finset.single_le_sum (fun l _ => (RCLike.nonneg_iff.mp (b.positive l x).trace_nonneg).1)
    (Finset.mem_univ k)
5757
lemma event_finset (s : Finset K) (x : Ω) :
    e.state x ((b.instrument x).operation s 1) = ∑ k ∈ s, b.value k x :=
proof · 1 line
  (b.run x).event_finset s 1
5761
lemma eventMeasurable (s : Finset K) :
    Measurable[L] (fun x => e.state x ((b.instrument x).operation s 1)) := by
proof · 2 lines
  simp_rw [b.event_finset s]
  exact Finset.measurable_sum s (fun k _ => b.local_measurable k)
5766
def postprocess (f : K → Bool) : e.BitReadout (B := B) where
body · 9 lines
  instrument x := (b.instrument x).postprocess f (measurable_of_finite f)
  run x := (b.run x).postprocess f (measurable_of_finite f) (e.positive x)
  positive_unit := b.positive_unit
  local_measurable i := by
    let s := Finset.univ.filter (fun k => f k = i)
    have hs : (s : Set K) = f ⁻¹' {i} := by ext k; simp [s]
    change Measurable[L] (fun x => e.state x ((b.instrument x).operation (f ⁻¹' {i}) 1))
    rw [← hs]
    exact b.eventMeasurable s
5812
structure FiniteProtocol where
body · 14 lines
  executionA : LocalObservableExecution (ι := ι) A d.kernelA d.sendA
  executionB : LocalObservableExecution (ι := η) B d.kernelB d.sendB
  initialA : executionA.reference 0 (Fin.elim0,Fin.elim0) =
    keepReference (outer (localProbe (ι := ι)) localProbe)
  initialB : executionB.reference 0 (Fin.elim0,Fin.elim0) =
    keepReference (outer (localProbe (ι := η)) localProbe)
  incomingA : IncomingObservableProcess (L := d.sampler.localField true) (A := TA)
    d.probeLaw historyFiltration
    (fun n x => AdaptiveQuantum.globalLocalReferenceA d.sampler d.kernelA executionA n (historyPrefix n x))
  incomingB : IncomingObservableProcess (L := d.sampler.localField false) (A := TB)
    d.probeLaw historyFiltration
    (fun n x => AdaptiveQuantum.globalLocalReferenceB d.sampler d.kernelB executionB n (historyPrefix n x))
  readoutA : incomingA.FiniteReadout (B := CA) (K := K)
  readoutB : incomingB.FiniteReadout (B := CB) (K := K)
5834
def toBit (f : K → Bool) : BitProtocol (ι := ι) (η := η) d A B TA TB CA CB where
body · 8 lines
  executionA := p.executionA
  executionB := p.executionB
  initialA := p.initialA
  initialB := p.initialB
  incomingA := p.incomingA
  incomingB := p.incomingB
  readoutA := p.readoutA.postprocess f
  readoutB := p.readoutB.postprocess f
5844
def completeReference (i j : K) (x : ℕ → O) : Matrix (ι × η) (ι × η) ℂ :=
body · 1 line
  p.readoutA.value i x ⊗ₖ p.readoutB.value j x
5848
lemma completeReference_positive [StandardBorelSpace M] [Nonempty M] (i j : K) (x : ℕ → O) :
    (p.completeReference i j x).PosSemidef :=
proof · 1 line
  (p.readoutA.positive i x).kronecker (p.readoutB.positive j x)
5853
lemma completeReference_measurable [StandardBorelSpace M] [Nonempty M] (i j : K) : Measurable (p.completeReference i j) :=
proof · 3 lines
  measurable_matrix_kronecker
    ((p.readoutA.measurable i).mono (d.sampler.localField_le true) le_rfl)
    ((p.readoutB.measurable j).mono (d.sampler.localField_le false) le_rfl)
5858
lemma completeReference_integrable (i j : K) : Integrable (p.completeReference i j) d.probeLaw :=
proof · 3 lines
  integrable_subdensity _
    (fun x => subdensity_kronecker (p.readoutA.subdensity i x) (p.readoutB.subdensity j x))
    (p.completeReference_measurable i j) d.probeLaw
5863
lemma completeReference_entry_integrable (i j : K) (k l : ι × η) :
    Integrable (fun x => p.completeReference i j x k l) d.probeLaw :=
proof · 2 lines
  ((Matrix.entryLinearMap ℂ ℂ k l).toContinuousLinearMap).integrable_comp
    (p.completeReference_integrable i j)
5868
def probeOutput (i j : K) : PositiveMatrixMeasure (ℕ → M) (ι × η) :=
body · 3 lines
  (positiveMatrixMeasureOfDensity (p.completeReference_entry_integrable i j)
    (Eventually.of_forall (p.completeReference_positive i j))).pushforward
    d.sampler.publicRecord d.sampler.measurable_publicRecord
5873
lemma probeOutput_event (i j : K) (S : Set (ℕ → M)) (hS : MeasurableSet S) :
    (p.probeOutput i j).value S =
      ∫ x in d.sampler.publicRecord ⁻¹' S, p.completeReference i j x ∂d.probeLaw := by
proof · 2 lines
  rw [probeOutput,PositiveMatrixMeasure.pushforward_value _ _ _ S hS,
    positiveMatrixMeasureOfDensity_value _ _ _ (d.sampler.measurable_publicRecord hS)]
5879
def canonicalOutput (R : Matrix (ι × η) (ι × η) ℂ) (i j : K) :
    PositiveMatrixMeasure (ℕ → M) (ι × η) :=
body · 1 line
  (p.probeOutput i j).filter (preparationFilter R)
5883
lemma canonicalOutput_event (R : Matrix (ι × η) (ι × η) ℂ)
    (i j : K) (S : Set (ℕ → M)) (hS : MeasurableSet S) :
    (p.canonicalOutput R i j).value S =
      ∫ x in d.sampler.publicRecord ⁻¹' S,
        preparationFilter R * p.completeReference i j x * (preparationFilter R)ᴴ ∂d.probeLaw := by
proof · 2 lines
  rw [canonicalOutput,PositiveMatrixMeasure.filter_value,p.probeOutput_event i j S hS,
    integral_filter _ (p.completeReference_integrable i j).integrableOn]
5922
lemma completeReference_sum [StandardBorelSpace M] [Nonempty M] [Nonempty ι] [Nonempty η] (x : ℕ → O) :
    (∑ i, ∑ j, p.completeReference i j x) =
      p.incomingA.state x 1 ⊗ₖ p.incomingB.state x 1 := by
proof · 5 lines
  rw [← p.readoutA.sum x,← p.readoutB.sum x]
  ext a b
  simp only [completeReference,Matrix.sum_apply,Matrix.kroneckerMap_apply,
    Finset.sum_mul,Finset.mul_sum]
  rw [Finset.sum_comm]
5931
lemma completeReference_total :
    (∑ i, ∑ j, ∫ x, p.completeReference i j x ∂d.probeLaw) =
      keepReference (outer (localProbe (ι := ι × η)) localProbe) := by
proof · 16 lines

  simp_rw [← integral_finsetSum _ (fun j _ => p.completeReference_integrable _ j)]
  rw [← integral_finsetSum _ (fun i _ => integrable_finsetSum _
    (fun j _ => p.completeReference_integrable i j))]
  simp_rw [p.completeReference_sum]
  have hc := AdaptiveQuantum.incoming_joint_reference_consistency
    d.sampler d.kernelA d.kernelB p.executionA p.executionB d.probeLaw d.probeLaw_prefix
    p.incomingA p.incomingB 0 Set.univ MeasurableSet.univ
  simp only [Measure.restrict_univ] at hc
  rw [hc]
  have hpre (x : ℕ → O) : historyPrefix 0 x = Fin.elim0 := Subsingleton.elim _ _
  simp_rw [hpre]
  have hi := (p.toBit (fun _ => false)).initial_joint_reference
  change AdaptiveQuantum.jointReference d.sampler d.kernelA d.kernelB p.executionA p.executionB
    0 Fin.elim0 = _ at hi
  simp [hi]
5951
lemma canonicalOutput_sum (R : Matrix (ι × η) (ι × η) ℂ) (hR : Density R) :
    (∑ i, ∑ j, (p.canonicalOutput R i j).value Set.univ) = Rᵀ := by
proof · 6 lines
  simp_rw [p.canonicalOutput_event R _ _ Set.univ MeasurableSet.univ,
    Set.preimage_univ,Measure.restrict_univ]
  simp_rw [integral_filter _ (p.completeReference_integrable _ _)]
  simp only [← Matrix.sum_mul,← Matrix.mul_sum]
  rw [p.completeReference_total,← keepReference_filter,preparation_filter_identity,
    canonical_reference R hR.1]
5960
lemma canonicalOutput_normalized (R : Matrix (ι × η) (ι × η) ℂ) (hR : Density R) :
    (∑ i, ∑ j, (trace ((p.canonicalOutput R i j).value Set.univ)).re) = 1 := by
proof · 3 lines
  have h := congrArg (fun X : Matrix (ι × η) (ι × η) ℂ => (trace X).re)
    (p.canonicalOutput_sum R hR)
  simpa only [Matrix.trace_sum,Complex.re_sum,Matrix.trace_transpose,hR.2,Complex.one_re] using h
5999
def purifiedOutput (P : Matrix (ι × η) κ ℂ) (i j : K) : PositiveMatrixMeasure (ℕ → M) κ :=
body · 1 line
  (p.probeOutput i j).filter (purificationFilter P)
6003
lemma purifiedOutput_event [Nonempty ι] [Nonempty η] (P : Matrix (ι × η) κ ℂ)
    (i j : K) (S : Set (ℕ → M)) (hS : MeasurableSet S) :
    (p.purifiedOutput P i j).value S =
      ∫ x in d.sampler.publicRecord ⁻¹' S,
        purificationFilter P * p.completeReference i j x * (purificationFilter P)ᴴ ∂d.probeLaw := by
proof · 2 lines
  rw [purifiedOutput,PositiveMatrixMeasure.filter_value,p.probeOutput_event i j S hS,
    integral_filter _ (p.completeReference_integrable i j).integrableOn]
6011
lemma purifiedOutput_ac (P : Matrix (ι × η) κ ℂ) (i j : K)
    (S : Set (ℕ → M)) (hS : MeasurableSet S) (hz : d.transcriptLaw S = 0) :
    (p.purifiedOutput P i j).value S = 0 := by
proof · 3 lines
  have hp : d.probeLaw (d.sampler.publicRecord ⁻¹' S) = 0 := by
    simpa only [Discussion.transcriptLaw,Measure.map_apply d.sampler.measurable_publicRecord hS] using hz
  rw [p.purifiedOutput_event P i j S hS,Measure.restrict_eq_zero.mpr hp,integral_zero_measure]
6018
lemma purifiedOutput_normalized (R : Matrix (ι × η) (ι × η) ℂ) (hR : Density R)
    (P : Matrix (ι × η) κ ℂ) (hP : P * Pᴴ = R) :
    (trace (∑ i, ∑ j, (p.purifiedOutput P i j).value Set.univ)).re = 1 := by
proof · 8 lines
  obtain ⟨L,hpos,htr,he⟩ := purification_filter_recovery R hR P hP
  have hh i j : trace ((p.purifiedOutput P i j).value Set.univ) =
      trace ((p.canonicalOutput R i j).value Set.univ) := by
    rw [← htr]
    simpa only [purifiedOutput, canonicalOutput, PositiveMatrixMeasure.filter_value] using
      congrArg trace (he ((p.probeOutput i j).value Set.univ))
  simp_rw [Matrix.trace_sum,Complex.re_sum,hh]
  simpa only [Matrix.trace_sum,Complex.re_sum] using p.canonicalOutput_normalized R hR
6030
def purifiedOutputDensity (P : Matrix (ι × η) κ ℂ) (ν : Measure (ℕ → M)) (i j : K) :
    (ℕ → M) → Matrix κ κ ℂ := (p.purifiedOutput P i j).positiveDensity ν
6050
structure KeyDensity (μ : Measure Ω) (K : Type v11171_0) (ι : Type v11171_1) [Fintype K] [Fintype ι] [DecidableEq ι] where
body · 5 lines
  block : K → K → Ω → Matrix ι ι ℂ
  measurable : ∀ a b, Measurable (block a b)
  positive : ∀ a b t, (block a b t).PosSemidef
  integrable : ∀ a b, Integrable (fun t => (trace (block a b t)).re) μ
  normalized : (∑ a, ∑ b, ∫ t, (trace (block a b t)).re ∂μ) = 1
6059
structure EveDensity (μ : Measure Ω) (ι : Type v11179_0) [Fintype ι] [DecidableEq ι] where
body · 5 lines
  block : Ω → Matrix ι ι ℂ
  measurable : Measurable block
  positive : ∀ t, (block t).PosSemidef
  integrable : Integrable (fun t => (trace (block t)).re) μ
  normalized : (∫ t, (trace (block t)).re ∂μ) = 1
6070
def idealError : ℝ := cqDistance τ.block (fun a b t => idealFiniteBlocks (sigma.block t) a b) μ
6072
def distance : ℝ := cqDistance τ.block υ.block μ
6103
def FiniteProtocol.purifiedKeyDensity
    (p : FiniteProtocol (ι := ι) (η := η) (K := K) d A B TA TB CA CB)
    (R : Matrix (ι × η) (ι × η) ℂ) (hR : Density R)
    (P : Matrix (ι × η) κ ℂ) (hP : P * Pᴴ = R)
    (ν : Measure (ℕ → M)) [SigmaFinite ν] (hμν : d.transcriptLaw ≪ ν) : KeyDensity ν K κ where
body · 11 lines
  block := p.purifiedOutputDensity P ν
  measurable := fun i j => (p.purifiedOutput P i j).measurable_positiveDensity ν
  positive := fun i j => (p.purifiedOutput P i j).positiveDensity_pos ν
  integrable := fun i j => (p.purifiedOutput P i j).integrable_positiveDensity_trace ν
    (fun S hS hz => p.purifiedOutput_ac P i j S hS (hμν hz))
  normalized := by
    have he i j := (p.purifiedOutput P i j).integral_positiveDensity_trace ν
      (fun S hS hz => p.purifiedOutput_ac P i j S hS (hμν hz))
    change (∑ i, ∑ j, ∫ t, (trace ((p.purifiedOutput P i j).positiveDensity ν t)).re ∂ν) = 1
    simp_rw [he]
    simpa only [Matrix.trace_sum,Complex.re_sum] using p.purifiedOutput_normalized R hR P hP
6130
def KeyWord : ℕ → Type
body · 2 lines
  | 0 => Unit
  | n+1 => Bool × KeyWord n
6134
instance keyWordFintype (n : ℕ) : Fintype (KeyWord n) := by
body · 3 lines
  induction n with
  | zero => exact inferInstanceAs (Fintype Unit)
  | succ n ih => letI := ih; exact inferInstanceAs (Fintype (Bool × KeyWord n))
6139
instance keyWordDecidableEq (n : ℕ) : DecidableEq (KeyWord n) := by
body · 3 lines
  induction n with
  | zero => exact inferInstanceAs (DecidableEq Unit)
  | succ n ih => letI := ih; exact inferInstanceAs (DecidableEq (Bool × KeyWord n))
6144
instance keyWordNonempty (n : ℕ) : Nonempty (KeyWord n) := by
body · 3 lines
  induction n with
  | zero => exact inferInstanceAs (Nonempty Unit)
  | succ n ih => let := ih; exact inferInstanceAs (Nonempty (Bool × KeyWord n))
6149
instance keyWordMeasurableSpace (n : ℕ) : MeasurableSpace (KeyWord n) := by
body · 3 lines
  induction n with
  | zero => exact inferInstanceAs (MeasurableSpace Unit)
  | succ n ih => letI := ih; exact inferInstanceAs (MeasurableSpace (Bool × KeyWord n))
6154
instance keyWordMeasurableSingletonClass (n : ℕ) : MeasurableSingletonClass (KeyWord n) := by
body · 3 lines
  induction n with
  | zero => exact inferInstanceAs (MeasurableSingletonClass Unit)
  | succ n ih => let := ih; exact inferInstanceAs (MeasurableSingletonClass (Bool × KeyWord n))
6159
structure QuantumMemory where
body · 5 lines
  Carrier : Type u
  additive : AddCommGroup Carrier
  complexModule : Module ℂ Carrier
  unit : One Carrier
  order : LE Carrier
6168
instance : CoeSort QuantumMemory (Type u) := ⟨QuantumMemory.Carrier⟩
6170
structure ClassicalRecordSpace where
body · 4 lines
  Carrier : Type u
  measurableSpace : MeasurableSpace Carrier
  standardBorel : StandardBorelSpace Carrier
  nonempty : Nonempty Carrier
6178
instance : CoeSort ClassicalRecordSpace (Type u) := ⟨ClassicalRecordSpace.Carrier⟩
6188
structure KeyImplementation (a b ell : ℕ) (messages : ClassicalRecordSpace.{u}) where
body · 11 lines
  outcomes : ClassicalRecordSpace.{u}
  discussion : Discussion outcomes messages
  memoryA : ℕ → QuantumMemory.{u}
  memoryB : ℕ → QuantumMemory.{u}
  incomingMemoryA : QuantumMemory.{u}
  incomingMemoryB : QuantumMemory.{u}
  outgoingMemoryA : QuantumMemory.{u}
  outgoingMemoryB : QuantumMemory.{u}
  protocol : FiniteProtocol (ι := Fin a) (η := Fin b) (K := KeyWord ell) discussion
    (fun n => memoryA n) (fun n => memoryB n)
    incomingMemoryA incomingMemoryB outgoingMemoryA outgoingMemoryB
6210
def output : KeyDensity ν (KeyWord ell) (Fin e) :=
body · 1 line
  p.protocol.purifiedKeyDensity R hR P hP ν hμν
6215
def IsCompletedKeyOutput {a b ell e : ℕ} [Nonempty (Fin a)] [Nonempty (Fin b)]
    (R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) (_hR : Density R)
    (P : Matrix (Fin a × Fin b) (Fin e) ℂ) (_hP : P * Pᴴ = R)
    (messages : ClassicalRecordSpace.{u}) (ν : Measure (ℕ → messages)) [SigmaFinite ν]
    (τ : KeyDensity ν (KeyWord ell) (Fin e)) : Prop :=
body · 7 lines
  ∃ p : ℕ → KeyImplementation a b ell messages,
    ∃ _hAC : ∀ j, (p j).discussion.transcriptLaw ≪ ν,
      Tendsto (fun j => cqDistance
        (fun a b => positiveEntryDensity
          (publicPurifiedEntries (p j).discussion.probeLaw
            (p j).discussion.sampler.publicRecord (p j).protocol.completeReference P a b) ν)
        τ.block ν) atTop (nhds 0)
6228
theorem isCompletedKeyOutput_iff_original {a b ell e : ℕ}
    [Nonempty (Fin a)] [Nonempty (Fin b)]
    (R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) (hR : Density R)
    (P : Matrix (Fin a × Fin b) (Fin e) ℂ) (hP : P * Pᴴ = R)
    (messages : ClassicalRecordSpace.{u}) (ν : Measure (ℕ → messages)) [SigmaFinite ν]
    (τ : KeyDensity ν (KeyWord ell) (Fin e)) :
    IsCompletedKeyOutput R hR P hP messages ν τ ↔
      ∃ p : ℕ → KeyImplementation a b ell messages,
        ∃ hAC : ∀ j, (p j).discussion.transcriptLaw ≪ ν,
          Tendsto (fun j => ((p j).output R hR P hP ν (hAC j)).distance τ) atTop (nhds 0) :=
proof · 1 line
  Iff.rfl
6240
structure CompletedKeyTrial {a b : ℕ} [Nonempty (Fin a)] [Nonempty (Fin b)]
    (R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) (hR : Density R) (ell : ℕ) where
body · 10 lines
  messages : ClassicalRecordSpace.{u}
  eveDim : ℕ
  purification : Matrix (Fin a × Fin b) (Fin eveDim) ℂ
  prepares : purification * purificationᴴ = R
  referenceMeasure : Measure (ℕ → messages)
  sigmaFinite : SigmaFinite referenceMeasure
  actual : KeyDensity referenceMeasure (KeyWord ell) (Fin eveDim)
  completed : @IsCompletedKeyOutput _ _ _ _ _ _ R hR purification prepares
    messages referenceMeasure sigmaFinite actual
  ideal : EveDensity referenceMeasure (Fin eveDim)
6261
def error : ℝ := p.actual.idealError p.ideal
6273
def groupIndex (a b c d : ℕ) :
    (Fin (a*c) × Fin (b*d)) ≃ ((Fin a × Fin b) × (Fin c × Fin d)) where
body · 6 lines
  toFun i := (((finProdFinEquiv.symm i.1).1,(finProdFinEquiv.symm i.2).1),
    ((finProdFinEquiv.symm i.1).2,(finProdFinEquiv.symm i.2).2))
  invFun i := (finProdFinEquiv (i.1.1,i.2.1),finProdFinEquiv (i.1.2,i.2.2))
  left_inv i := by
    apply Prod.ext <;> exact finProdFinEquiv.apply_symm_apply _
  right_inv i := by simp
6282
def groupTensor {a b c d : ℕ}
    (R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ)
    (S : Matrix (Fin c × Fin d) (Fin c × Fin d) ℂ) :
    Matrix (Fin (a*c) × Fin (b*d)) (Fin (a*c) × Fin (b*d)) ℂ :=
body · 1 line
  (R ⊗ₖ S).submatrix (groupIndex a b c d) (groupIndex a b c d)
6288
lemma trace_groupTensor {a b c d : ℕ}
    (R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ)
    (S : Matrix (Fin c × Fin d) (Fin c × Fin d) ℂ) :
    trace (groupTensor R S) = trace R * trace S := by
proof · 2 lines
  rw [← trace_kronecker]
  exact Equiv.sum_comp (groupIndex a b c d) (fun x => (R ⊗ₖ S) x x)
6295
lemma Density.groupTensor {a b c d : ℕ}
    {R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ}
    {S : Matrix (Fin c × Fin d) (Fin c × Fin d) ℂ}
    (hR : Density R) (hS : Density S) : Density (groupTensor R S) := by
proof · 2 lines
  refine ⟨(hR.1.kronecker hS.1).submatrix _,?_⟩
  rw [trace_groupTensor,hR.2,hS.2,one_mul]
6302
def copyDim (a : ℕ) : ℕ → ℕ
body · 2 lines
  | 0 => a
  | k+1 => copyDim a k * a
6306
lemma copyDim_eq (a k : ℕ) : copyDim a k = a^(k+1) := by
proof · 3 lines
  induction k with
  | zero => simp [copyDim]
  | succ k ih => simp only [copyDim,ih,pow_succ]
6311
def positiveCopies {a b : ℕ} (R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) :
    (k : ℕ) → Matrix (Fin (copyDim a k) × Fin (copyDim b k))
      (Fin (copyDim a k) × Fin (copyDim b k)) ℂ
body · 2 lines
  | 0 => R
  | k+1 => groupTensor (positiveCopies R k) R
6327
lemma Density.positiveCopies {a b : ℕ}
    {R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ} (hR : Density R) (k : ℕ) :
    Density (positiveCopies R k) := by
proof · 3 lines
  induction k with
  | zero => exact hR
  | succ k ih => exact ih.groupTensor hR
6334
instance positiveCopy_nonempty (a k : ℕ) [Nonempty (Fin a)] : Nonempty (Fin (copyDim a k)) := by
body · 5 lines
  have ha : 0 < copyDim a k := by
    induction k with
    | zero => exact (show 0 < a from Fin.size_positive')
    | succ k ih => exact Nat.mul_pos ih (show 0 < a from Fin.size_positive')
  exact ⟨⟨0,ha⟩⟩
6341
structure CompletedKeyScheme {a b : ℕ} [Nonempty (Fin a)] [Nonempty (Fin b)]
    (R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) (hR : Density R) where
body · 11 lines
  length : ℕ → ℕ
  trial : (k : ℕ) → CompletedKeyTrial.{u} (positiveCopies R k) (by
      induction k with
      | zero => exact hR
      | succ k ih =>
        refine ⟨(ih.1.kronecker hR.1).submatrix _, ?_⟩
        change trace (groupTensor (positiveCopies R k) R) = 1
        have ht := Equiv.sum_comp (groupIndex (copyDim a k) (copyDim b k) a b)
          (fun x => (Matrix.kronecker (positiveCopies R k) R) x x)
        change trace (groupTensor (positiveCopies R k) R) = trace (Matrix.kronecker (positiveCopies R k) R) at ht
        rw [ht, Matrix.kronecker, trace_kronecker, ih.2, hR.2, one_mul]) (length k)
6361
def error (k : ℕ) : ℝ := (s.trial k).error
6363
def Secure : Prop := Tendsto s.error atTop (nhds 0)
6365
def lowerRate : ℝ≥0∞ := liminf (fun k : ℕ => (s.length k : ℝ≥0∞) / (k+1 : ℕ)) atTop
6369
def distillableSecretKey {a b : ℕ} [Nonempty (Fin a)] [Nonempty (Fin b)]
    (R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) (hR : Density R) : ℝ≥0∞ :=
body · 1 line
  sSup {r | r = 0 ∨ ∃ s : CompletedKeyScheme.{u} R hR, s.Secure ∧ r = s.lowerRate}
6381
def UniformProtocolGap {a b : ℕ} [Nonempty (Fin a)] [Nonempty (Fin b)]
    (R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) (_hR : Density R) : Prop :=
body · 16 lines
  ∀ (k : ℕ) (O M : Type u) [MeasurableSpace O] [MeasurableSpace M]
    [StandardBorelSpace O] [Nonempty O] [StandardBorelSpace M] [Nonempty M]
    (d : Discussion O M) (A B : ℕ → Type u)
    [∀ n, AddCommGroup (A n)] [∀ n, Module ℂ (A n)] [∀ n, One (A n)] [∀ n, LE (A n)]
    [∀ n, AddCommGroup (B n)] [∀ n, Module ℂ (B n)] [∀ n, One (B n)] [∀ n, LE (B n)]
    (TA TB CA CB : Type u)
    [AddCommGroup TA] [Module ℂ TA] [One TA] [LE TA]
    [AddCommGroup TB] [Module ℂ TB] [One TB] [LE TB]
    [AddCommGroup CA] [Module ℂ CA] [One CA] [LE CA]
    [AddCommGroup CB] [Module ℂ CB] [One CB] [LE CB]
    (p : BitProtocol (ι := Fin (copyDim a k)) (η := Fin (copyDim b k)) d A B TA TB CA CB)
    (e : ℕ) (P : Matrix (Fin (copyDim a k) × Fin (copyDim b k)) (Fin e) ℂ)
    (_hP : P * Pᴴ = positiveCopies R k)
    (sigma : PositiveMatrixMeasure (ℕ → M) (Fin e)),
    (trace (sigma.value Set.univ)).re = 1 →
      1/5 ≤ p.rawPurifiedSecretBitError P sigma
6400
def UniformCompletedGap {a b : ℕ} [Nonempty (Fin a)] [Nonempty (Fin b)]
    (R : Matrix (Fin a × Fin b) (Fin a × Fin b) ℂ) (hR : Density R) : Prop :=
body · 12 lines
  ∀ (k ell : ℕ), 0 < ell →
    ∀ p : CompletedKeyTrial.{u} (positiveCopies R k) (by
      induction k with
      | zero => exact hR
      | succ k ih =>
        refine ⟨(ih.1.kronecker hR.1).submatrix _, ?_⟩
        change trace (groupTensor (positiveCopies R k) R) = 1
        have ht := Equiv.sum_comp (groupIndex (copyDim a k) (copyDim b k) a b)
          (fun x => (Matrix.kronecker (positiveCopies R k) R) x x)
        change trace (groupTensor (positiveCopies R k) R) = trace (Matrix.kronecker (positiveCopies R k) R) at ht
        rw [ht, Matrix.kronecker, trace_kronecker, ih.2, hR.2, one_mul]) ell,
      1/5 ≤ p.error
6417
lemma m_pos : 0 < m := by rw [m_eq]; exact Nat.mul_pos Q_pos D_pos
6419
instance output_nonempty : Nonempty (Fin m) := by
body · 9 lines
  have hb : 0 < b := by
    unfold b
    exact Nat.choose_pos (by decide)
  have hq : 0 < Q := by unfold Q; exact pow_pos hb 3
  have hd (q : ℕ) : 0 < dimensionFunction q := pow_pos (by decide) _
  have hD : 0 < D := by unfold D; exact hd Q
  have hm (q d : ℕ) (hq : 0 < q) (hd : 0 < d) :
      0 < outputDimensionFunction q d := Nat.mul_pos hq hd
  exact ⟨⟨0, by unfold m; exact hm Q D hq hD⟩⟩
6430
def MainClaim : Prop :=
body · 4 lines
  ∃ hR : Density rhoFin, ¬ Separable rhoFin ∧
    distillableSecretKey.{u} rhoFin hR = 0 ∧
    UniformProtocolGap.{u} rhoFin hR ∧
    UniformCompletedGap.{u} rhoFin hR