roseking's picture
Add lean source and binary files (part 84)
cdec422 verified
Raw History Blame Contribute Delete
39.8 kB
19
def deterministicInstrument (k : K) : ObservableInstrument K A A := by
body · 10 lines
  classical
  exact {
    operation := fun E => if k ∈ E then LinearMap.id else 0
    positive := by
      intro E hE x hx
      by_cases h : k ∈ E
      · simpa only [h, ite_eq_left, LinearMap.id_apply] using hx
      · simp only [h]; exact le_rfl
    total_unit := by simp
  }
31
def deterministicRun (k : K) (S : ReferenceFunctional A ι)
    (hS : PositiveFunctional S) (hn : trace (S 1) = 1) :
    InstrumentRun (deterministicInstrument (A := A) k) S where
body · 10 lines
  law := Measure.dirac k
  probability := inferInstance
  posterior _ := S
  posterior_positive _ := hS
  posterior_trace _ := hn
  integrable _ _ _ := integrable_const _
  update E hE x i j := by
    by_cases h : k ∈ E
    · simp [deterministicInstrument,h,Measure.real, Measure.dirac_apply', hE]
    · simp [deterministicInstrument,h,Measure.real, Measure.dirac_apply', hE]
46
lemma deterministic_event [MeasurableSingletonClass K] [Fintype ι] [DecidableEq ι] (k l : K) (S : ReferenceFunctional A ι) :
    S ((deterministicInstrument (A := A) k).operation {l} 1) =
      if k = l then S 1 else 0 := by
proof · 1 line
  by_cases h : k = l <;> simp [deterministicInstrument,h]
56
def scalarReference (G : Matrix ι ι ℂ) : ReferenceFunctional ℂ ι where
body · 3 lines
  toFun z := z • G
  map_add' z w := add_smul z w G
  map_smul' z w := by simp [smul_smul]
62
lemma scalarReference_positive [Fintype ι] [DecidableEq ι] {G : Matrix ι ι ℂ} (hG : G.PosSemidef) :
    PositiveFunctional (scalarReference G) := by
proof · 2 lines
  intro z hz
  exact hG.smul hz
68
@[simp] lemma scalarReference_one [Fintype ι] [DecidableEq ι] (G : Matrix ι ι ℂ) : scalarReference G 1 = G := one_smul _ _
70
def scalarIncoming (G : Ω → Matrix ι ι ℂ)
    (hG : ∀ x, ZeroKey.Density (G x)) (hm : Measurable[L] G)
    (hc : ∀ n E, MeasurableSet[F n] E →
      (∫ x in E, G x ∂μ) = ∫ x in E, U n x ∂μ) :
    IncomingObservableProcess (L := L) (A := ℂ) μ F U where
body · 6 lines
  state x := scalarReference (G x)
  positive x := scalarReference_positive (hG x).1
  positive_unit := zero_le_one
  normalized x := by simpa using (hG x).2
  local_measurable := by simpa using hm
  causality := by simpa using hc
87
def classicalReadout (f : Ω → K) (hf : Measurable[L] f) : e.FiniteReadout (K := K) (B := A) where
body · 6 lines
  instrument x := deterministicInstrument (f x)
  run x := deterministicRun (f x) (e.state x) (e.positive x) (e.normalized x)
  positive_unit := e.positive_unit
  local_measurable k := by
    simp_rw [deterministic_event]
    exact e.local_measurable.ite (measurableSet_eq_fun hf measurable_const) measurable_const
96
lemma classicalReadout_value [Nonempty K] (f : Ω → K) (hf : Measurable[L] f) (k : K) (x : Ω) :
    (e.classicalReadout f hf).value k x = if f x = k then e.state x 1 else 0 :=
proof · 1 line
  deterministic_event _ _ _
117
structure ConsistentReferenceHistory where
body · 5 lines
  finiteMeasure : (n : ℕ) → @PositiveMatrixMeasure Ω ι (F n) _
  traceLaw : ∀ n, @PositiveMatrixMeasure.traceMeasure Ω ι (F n) _ (finiteMeasure n) = μ.trim (F.le n)
  consistency : ∀ n k, n ≤ k → ∀ S, MeasurableSet[F n] S →
    @PositiveMatrixMeasure.value Ω ι (F n) _ (finiteMeasure n) S =
      @PositiveMatrixMeasure.value Ω ι (F k) _ (finiteMeasure k) S
130
def finiteValue (n : ℕ) (S : Set Ω) : Matrix ι ι ℂ :=
body · 1 line
  @PositiveMatrixMeasure.value Ω ι (F n) _ (H.finiteMeasure n) S
133
def density (n : ℕ) : Ω → Matrix ι ι ℂ :=
body · 1 line
  @PositiveMatrixMeasure.density Ω ι (F n) _ (H.finiteMeasure n) (μ.trim (F.le n))
137
lemma measurable_density [IsFiniteMeasure μ] (n : ℕ) : Measurable[F n] (H.density n) :=
proof · 1 line
  @PositiveMatrixMeasure.measurable_density Ω ι (F n) _ _ (H.finiteMeasure n) _
140
lemma measurable_entry (n : ℕ) (i j : ι) : Measurable[F n] (fun ω => H.density n ω i j) := by
proof · 2 lines
  let L : Matrix ι ι ℂ →L[ℂ] ℂ := (Matrix.entryLinearMap ℂ ℂ i j).toContinuousLinearMap
  exact L.continuous.measurable.comp (H.measurable_density n)
145
lemma integrable_entry [IsFiniteMeasure μ] (n : ℕ) (i j : ι) : Integrable (fun ω => H.density n ω i j) μ :=
proof · 2 lines
  integrable_of_integrable_trim (F.le n)
    (@PositiveMatrixMeasure.integrable_entry Ω ι (F n) _ _ (H.finiteMeasure n) _ i j)
149
lemma integral_entry (n : ℕ) (S : Set Ω) (hS : MeasurableSet[F n] S) (i j : ι) :
    (∫ ω in S, H.density n ω i j ∂μ) = H.finiteValue n S i j := by
proof · 6 lines
  rw [setIntegral_trim (F.le n) (H.measurable_entry n i j).stronglyMeasurable hS]
  let : MeasurableSpace Ω := F n
  apply (H.finiteMeasure n).integral_density_entry _ _ S hS i j
  intro T hT hμ
  apply (H.finiteMeasure n).value_zero_of_traceMeasure_zero T hT
  rwa [H.traceLaw n]
158
lemma density_pos (n : ℕ) : ∀ᵐ ω ∂μ, (H.density n ω).PosSemidef := by
proof · 6 lines
  apply ae_of_ae_trim (F.le n)
  let : MeasurableSpace Ω := F n
  apply density_posSemidef_ae (H.finiteMeasure n)
  intro S hS hs
  apply (H.finiteMeasure n).value_zero_of_traceMeasure_zero S hS
  rwa [H.traceLaw n]
166
lemma density_trace (n : ℕ) : ∀ᵐ ω ∂μ, (trace (H.density n ω)).re = 1 := by
proof · 19 lines
  apply ae_of_ae_trim (F.le n)
  let : MeasurableSpace Ω := F n
  let W := H.finiteMeasure n
  let ν := μ.trim (F.le n)
  have hi := (W.integrable_trace_density ν).re
  apply Integrable.ae_eq_of_forall_setIntegral_eq _ _ hi (integrable_const (1 : ℝ))
  intro S hS _
  change (∫ ω in S, RCLike.re (trace (W.density ν ω)) ∂ν) = _
  rw [integral_re (W.integrable_trace_density ν).integrableOn]
  have hAC : ∀ T, MeasurableSet[F n] T → ν T = 0 → W.value T = 0 := by
    intro T hT hμ
    apply W.value_zero_of_traceMeasure_zero T hT
    rwa [H.traceLaw n]
  rw [W.integral_trace_density ν hAC S hS,integral_const]
  simp only [smul_eq_mul,mul_one]
  change (trace (W.value S)).re = (ν.restrict S).real Set.univ
  rw [← W.traceMeasure_real S hS,H.traceLaw n]
  simp only [Measure.real,Measure.restrict_apply_univ]
  rfl
187
lemma density_entry_martingale_re (i j : ι) :
    Martingale (fun n ω => (H.density n ω i j).re) F μ := by
proof · 15 lines
  refine ⟨fun n => ((H.measurable_entry n i j).re).stronglyMeasurable, ?_⟩
  intro n k hnk
  symm
  apply ae_eq_condExp_of_forall_setIntegral_eq (F.le n) ((H.integrable_entry k i j).re)
  · intro S _ _
    exact (H.integrable_entry n i j).re.integrableOn
  · intro S hS _
    change (∫ ω in S, RCLike.re (H.density n ω i j) ∂μ) =
      ∫ ω in S, RCLike.re (H.density k ω i j) ∂μ
    rw [integral_re (H.integrable_entry n i j).integrableOn,
      integral_re (H.integrable_entry k i j).integrableOn,
      H.integral_entry n S hS i j,
      H.integral_entry k S ((F.mono hnk) S hS) i j]
    exact congrArg (fun Z : Matrix ι ι ℂ => (Z i j).re) (H.consistency n k hnk S hS)
  · exact ((H.measurable_entry n i j).re).stronglyMeasurable.aestronglyMeasurable
205
lemma density_entry_martingale_im (i j : ι) :
    Martingale (fun n ω => (H.density n ω i j).im) F μ := by
proof · 15 lines
  refine ⟨fun n => ((H.measurable_entry n i j).im).stronglyMeasurable, ?_⟩
  intro n k hnk
  symm
  apply ae_eq_condExp_of_forall_setIntegral_eq (F.le n) ((H.integrable_entry k i j).im)
  · intro S _ _
    exact (H.integrable_entry n i j).im.integrableOn
  · intro S hS _
    change (∫ ω in S, RCLike.im (H.density n ω i j) ∂μ) =
      ∫ ω in S, RCLike.im (H.density k ω i j) ∂μ
    rw [integral_im (H.integrable_entry n i j).integrableOn,
      integral_im (H.integrable_entry k i j).integrableOn,
      H.integral_entry n S hS i j,
      H.integral_entry k S ((F.mono hnk) S hS) i j]
    exact congrArg (fun Z : Matrix ι ι ℂ => (Z i j).im) (H.consistency n k hnk S hS)
  · exact ((H.measurable_entry n i j).im).stronglyMeasurable.aestronglyMeasurable
223
theorem full_reference_measure : ∃ V : PositiveMatrixMeasure Ω ι,
    ∀ n S, MeasurableSet[F n] S → V.value S = H.finiteValue n S := by
proof · 7 lines
  obtain ⟨V,hV⟩ := positive_matrix_martingale_extension (H.density)
    H.density_entry_martingale_re H.density_entry_martingale_im H.density_pos
    (fun n => (H.density_trace n).mono (fun _ h => le_of_eq h))
  refine ⟨V,fun n S hS => ?_⟩
  rw [hV n S hS]
  ext i j
  exact H.integral_entry n S hS i j
252
def restrictEvent (W : PositiveMatrixMeasure Ω ι) (A : Set Ω) (hA : MeasurableSet A) :
    PositiveMatrixMeasure Ω ι where
body · 3 lines
  entry i j := (W.entry i j).restrict A
  positive S hS := by
    simpa only [VectorMeasure.restrict_apply _ hA hS] using W.positive (S ∩ A) (hS.inter hA)
259
lemma restrictEvent_value [DecidableEq ι] (W : PositiveMatrixMeasure Ω ι) (A : Set Ω)
    (hA : MeasurableSet A) (S : Set Ω) (hS : MeasurableSet S) :
    (W.restrictEvent A hA).value S = W.value (S ∩ A) := by
proof · 2 lines
  ext i j
  exact VectorMeasure.restrict_apply _ hA hS
266
lemma eq_of_value [DecidableEq ι] (W V : PositiveMatrixMeasure Ω ι)
    (h : ∀ S, MeasurableSet S → W.value S = V.value S) : W = V := by
proof · 9 lines
  have he : W.entry = V.entry := by
    funext i j
    apply VectorMeasure.ext
    intro S hS
    exact congrArg (fun X : Matrix ι ι ℂ => X i j) (h S hS)
  cases W
  cases V
  cases he
  rfl
280
lemma restrictEvent_filter {κ : Type v12080_0} [Fintype κ] [DecidableEq κ]
    (W : PositiveMatrixMeasure Ω ι) (A : Set Ω) (hA : MeasurableSet A)
    (K : Matrix κ ι ℂ) :
    (W.filter K).restrictEvent A hA = (W.restrictEvent A hA).filter K := by
proof · 4 lines
  apply eq_of_value
  intro S hS
  rw [restrictEvent_value _ _ _ _ hS,filter_value,filter_value,
    restrictEvent_value _ _ _ _ hS]
289
def testMeasure (W : PositiveMatrixMeasure Ω ι) (E : Matrix ι ι ℂ) : Measure Ω :=
body · 1 line
  (W.filter (CFC.sqrt E)).traceMeasure
292
instance testMeasure_finite (W : PositiveMatrixMeasure Ω ι) (E : Matrix ι ι ℂ) :
    IsFiniteMeasure (W.testMeasure E) := inferInstanceAs (IsFiniteMeasure (W.filter (CFC.sqrt E)).traceMeasure)
295
lemma testMeasure_real (W : PositiveMatrixMeasure Ω ι) (E : Matrix ι ι ℂ)
    (hE : E.PosSemidef) (S : Set Ω) (hS : MeasurableSet S) :
    (W.testMeasure E).real S = (trace (E * W.value S)).re := by
proof · 4 lines
  rw [testMeasure,traceMeasure_real _ S hS,filter_value]
  have hs : (CFC.sqrt E)ᴴ = CFC.sqrt E := (CFC.sqrt_nonneg E).isSelfAdjoint.isHermitian.eq
  rw [hs,Matrix.trace_mul_cycle]
  rw [CFC.sqrt_mul_sqrt_self E hE.nonneg]
303
def bitReadout (W : PositiveMatrixMeasure Ω ι) (record : Ω → T) (hr : Measurable record)
    (a b : Ω → Bool) (ha : Measurable a) (hb : Measurable b) (i j : Bool) :
    PositiveMatrixMeasure T ι :=
body · 2 lines
  (W.restrictEvent ((a ⁻¹' {i}) ∩ (b ⁻¹' {j}))
    ((ha (measurableSet_singleton i)).inter (hb (measurableSet_singleton j)))).pushforward record hr
309
lemma bitReadout_value (W : PositiveMatrixMeasure Ω ι) (record : Ω → T) (hr : Measurable record)
    (a b : Ω → Bool) (ha : Measurable a) (hb : Measurable b) (i j : Bool)
    (S : Set T) (hS : MeasurableSet S) :
    (W.bitReadout record hr a b ha hb i j).value S =
      W.value ((record ⁻¹' S) ∩ ((a ⁻¹' {i}) ∩ (b ⁻¹' {j}))) := by
proof · 2 lines
  rw [bitReadout,pushforward_value _ _ _ S hS,
    restrictEvent_value _ _ _ _ (hr hS)]
319
lemma bitReadout_filter {κ : Type v12122_0} [Fintype κ] [DecidableEq κ]
    (W : PositiveMatrixMeasure Ω ι) (record : Ω → T) (hr : Measurable record)
    (a b : Ω → Bool) (ha : Measurable a) (hb : Measurable b) (i j : Bool)
    (K : Matrix κ ι ℂ) (S : Set T) (hS : MeasurableSet S) :
    ((W.filter K).bitReadout record hr a b ha hb i j).value S =
      ((W.bitReadout record hr a b ha hb i j).filter K).value S := by
proof · 2 lines
  rw [bitReadout_value _ _ _ _ _ _ _ _ _ _ hS,filter_value,filter_value,
    bitReadout_value _ _ _ _ _ _ _ _ _ _ hS]
334
lemma scalar_measure_ext_on_filtration {Ω : Type v12136_0} {mΩ : MeasurableSpace Ω}
    (μ ν : Measure Ω) [IsFiniteMeasure μ] (F : Filtration ℕ mΩ)
    (hm : mΩ = ⨆ n, F n)
    (h : ∀ n S, MeasurableSet[F n] S → μ S = ν S) : μ = ν := by
proof · 17 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Ω = generateFrom C := by
    rw [hm]
    apply le_antisymm
    · apply iSup_le; intro n S hS
      exact measurableSet_generateFrom ⟨n,hS⟩
    · apply generateFrom_le
      rintro S ⟨n,hn⟩
      exact (le_iSup (fun n => F n) n) S hn
  exact ext_of_generate_finite C hgen hc (fun S ⟨n,hn⟩ => h n S hn)
    (h 0 Set.univ MeasurableSet.univ)
364
theorem full_reference_born (hm : mΩ = ⨆ n, F n) :
    ∃ W : PositiveMatrixMeasure Ω ι,
      (∀ n S, MeasurableSet[F n] S → W.value S = H.finiteValue n S) ∧
      ∀ (E : Matrix ι ι ℂ), E.PosSemidef → ∀ (ν : Measure Ω) [IsFiniteMeasure ν],
        (∀ n S, MeasurableSet[F n] S →
          (trace (E * H.finiteValue n S)).re = ν.real S) → W.testMeasure E = ν := by
proof · 9 lines
  obtain ⟨W,hW⟩ := H.full_reference_measure
  refine ⟨W,hW,?_⟩
  intro E hE ν hν htests
  apply scalar_measure_ext_on_filtration _ _ F hm
  intro n S hS
  apply (ENNReal.toReal_eq_toReal_iff' (measure_ne_top _ _) (measure_ne_top _ _)).mp
  change (W.testMeasure E).real S = ν.real S
  rw [W.testMeasure_real E hE S (F.le n S hS),hW n S hS]
  exact htests n S hS
392
lemma value_sum_readout [DecidableEq ι] [Fintype α] [MeasurableSpace α] [MeasurableSingletonClass α]
    (W : PositiveMatrixMeasure Ω ι) (a : Ω → α) (ha : Measurable a)
    (S : Set Ω) (hS : MeasurableSet S) :
    ∑ i, W.value (S ∩ a ⁻¹' {i}) = W.value S := by
proof · 18 lines
  classical
  have hd : Set.PairwiseDisjoint (↑(Finset.univ : Finset α)) (fun i => S ∩ a ⁻¹' {i}) := by
    intro i _ j _ hij
    apply Set.disjoint_left.mpr
    intro x hi hj
    exact hij (hi.2.symm.trans hj.2)
  have hm : ∀ i ∈ (Finset.univ : Finset α), MeasurableSet (S ∩ a ⁻¹' {i}) :=
    fun i _ => hS.inter (ha (measurableSet_singleton i))
  have hu : (⋃ i ∈ (Finset.univ : Finset α), S ∩ a ⁻¹' {i}) = S := by
    ext x
    simp only [Finset.mem_univ,Set.iUnion_true,Set.mem_iUnion,Set.mem_inter_iff,
      Set.mem_preimage,Set.mem_singleton_iff]
    exact ⟨fun ⟨_,hx,_⟩ => hx, fun hx => ⟨a x,hx,rfl⟩⟩
  ext i j
  simp only [Matrix.sum_apply]
  have h := (W.entry i j).of_biUnion_finset hd hm
  rw [hu] at h
  simpa only [Finset.mem_univ,ite_true,value] using h.symm
415
lemma bitReadout_total (W : PositiveMatrixMeasure Ω ι) (record : Ω → T) (hr : Measurable record)
    (a b : Ω → Bool) (ha : Measurable a) (hb : Measurable b)
    (S : Set T) (hS : MeasurableSet S) :
    ∑ i, ∑ j, (W.bitReadout record hr a b ha hb i j).value S = W.value (record ⁻¹' S) := by
proof · 6 lines
  simp_rw [bitReadout_value _ _ _ _ _ _ _ _ _ _ hS, ← Set.inter_assoc]
  have hi (i : Bool) : (∑ j, W.value ((record ⁻¹' S ∩ a ⁻¹' {i}) ∩ b ⁻¹' {j})) =
      W.value (record ⁻¹' S ∩ a ⁻¹' {i}) :=
    W.value_sum_readout b hb _ ((hr hS).inter (ha (measurableSet_singleton i)))
  simp_rw [hi]
  exact W.value_sum_readout a ha _ (hr hS)
426
theorem filtered_bitReadout_normalized [Nonempty ι]
    (W : PositiveMatrixMeasure Ω ι)
    (hprobe : W.value Set.univ = keepReference (outer (localProbe (ι := ι)) localProbe))
    (record : Ω → T) (hr : Measurable record)
    (a b : Ω → Bool) (ha : Measurable a) (hb : Measurable b)
    (R : Matrix ι ι ℂ) (hR : Density R) :
    (trace (∑ i, ∑ j,
      ((W.bitReadout record hr a b ha hb i j).filter (preparationFilter R)).value Set.univ)).re = 1 := by
proof · 8 lines
  have he : (∑ i, ∑ j,
      ((W.bitReadout record hr a b ha hb i j).filter (preparationFilter R)).value Set.univ) =
      ((W.filter (preparationFilter R)).value Set.univ) := by
    simp_rw [← bitReadout_filter _ _ _ _ _ _ _ _ _ _ _ MeasurableSet.univ]
    rw [bitReadout_total _ _ _ _ _ _ _ _ MeasurableSet.univ,Set.preimage_univ]
  rw [he,filter_value,hprobe,← keepReference_filter,preparation_filter_identity,
    canonical_reference R hR.1,Matrix.trace_transpose,hR.2]
  rfl
467
structure ClassicalProtocol where
body · 10 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)
  outputA : (ℕ → O) → K
  outputB : (ℕ → O) → K
  measurableA : Measurable[d.sampler.localField true] outputA
  measurableB : Measurable[d.sampler.localField false] outputB
485
structure Completion where
body · 18 lines
  G : (ℕ → O) → Matrix ι ι ℂ
  H : (ℕ → O) → Matrix η η ℂ
  measurableG : Measurable[d.sampler.localField true] G
  measurableH : Measurable[d.sampler.localField false] H
  densityG : ∀ x, Density (G x)
  densityH : ∀ x, Density (H x)
  consistentG : ∀ n S, MeasurableSet[historyFiltration n] S →
    (∫ x in S, G x ∂d.probeLaw) = ∫ x in S,
      AdaptiveQuantum.globalLocalReferenceA d.sampler d.kernelA p.executionA n (historyPrefix n x) ∂d.probeLaw
  consistentH : ∀ n S, MeasurableSet[historyFiltration n] S →
    (∫ x in S, H x ∂d.probeLaw) = ∫ x in S,
      AdaptiveQuantum.globalLocalReferenceB d.sampler d.kernelB p.executionB n (historyPrefix n x) ∂d.probeLaw
  reference : PositiveMatrixMeasure (ℕ → O) (ι × η)
  reference_value : ∀ S, MeasurableSet S → reference.value S = ∫ x in S, G x ⊗ₖ H x ∂d.probeLaw
  reference_prefix : ∀ n S, MeasurableSet[historyFiltration n] S →
    reference.value S = ∫ x in S,
      AdaptiveQuantum.jointReference d.sampler d.kernelA d.kernelB p.executionA p.executionB
        n (historyPrefix n x) ∂d.probeLaw
506
lemma completion_exists [StandardBorelSpace M] [Nonempty M] [MeasurableSingletonClass K] [Fintype K] [DecidableEq K] [Nonempty K] : Nonempty p.Completion := by
proof · 4 lines
  obtain ⟨G,H,hGm,hHm,hG,hH,hGc,hHc,W,hW,hWc⟩ :=
    AdaptiveQuantum.full_reference_extension d.sampler d.kernelA d.kernelB p.executionA p.executionB
      d.probeLaw d.probeLaw_prefix
  exact ⟨⟨G,H,hGm,hHm,hG,hH,hGc,hHc,W,hW,hWc⟩⟩
512
def completion : p.Completion := Classical.choice p.completion_exists
514
def toFinite : FiniteProtocol (ι := ι) (η := η) (K := K) d A B ℂ ℂ ℂ ℂ where
body · 10 lines
  executionA := p.executionA
  executionB := p.executionB
  initialA := p.initialA
  initialB := p.initialB
  incomingA := scalarIncoming p.completion.G p.completion.densityG p.completion.measurableG p.completion.consistentG
  incomingB := scalarIncoming p.completion.H p.completion.densityH p.completion.measurableH p.completion.consistentH
  readoutA := (scalarIncoming p.completion.G p.completion.densityG p.completion.measurableG
    p.completion.consistentG).classicalReadout p.outputA p.measurableA
  readoutB := (scalarIncoming p.completion.H p.completion.densityH p.completion.measurableH
    p.completion.consistentH).classicalReadout p.outputB p.measurableB
526
lemma completeReference_value (i j : K) (x : ℕ → O) :
    p.toFinite.completeReference i j x =
      if p.outputA x = i ∧ p.outputB x = j then p.completion.G x ⊗ₖ p.completion.H x else 0 := by
proof · 4 lines
  simp only [FiniteProtocol.completeReference, toFinite,
    IncomingObservableProcess.classicalReadout_value, scalarIncoming]
  by_cases ha : p.outputA x = i <;> by_cases hb : p.outputB x = j <;>
    simp [ha,hb,scalarReference_one]
534
theorem output_event (i j : K) (S : Set (ℕ → M)) (hS : MeasurableSet S) :
    (p.toFinite.probeOutput i j).value S = p.completion.reference.value
      ((d.sampler.publicRecord ⁻¹' S) ∩ (p.outputA ⁻¹' {i} ∩ p.outputB ⁻¹' {j})) := by
proof · 13 lines
  have ha := p.measurableA.mono (d.sampler.localField_le true) le_rfl
  have hb := p.measurableB.mono (d.sampler.localField_le false) le_rfl
  have he := (ha (measurableSet_singleton i)).inter (hb (measurableSet_singleton j))
  rw [p.toFinite.probeOutput_event i j S hS,p.completion.reference_value _
    ((d.sampler.measurable_publicRecord hS).inter he)]
  have hind : (fun x => p.toFinite.completeReference i j x) =
      (p.outputA ⁻¹' {i} ∩ p.outputB ⁻¹' {j}).indicator
        (fun x => p.completion.G x ⊗ₖ p.completion.H x) := by
    funext x
    simp only [p.completeReference_value,Set.indicator,Set.mem_inter_iff,Set.mem_preimage,
      Set.mem_singleton_iff]
  rw [hind,integral_indicator he,Measure.restrict_restrict he]
  rw [Set.inter_comm]
551
theorem canonicalOutput_event (R : Matrix (ι × η) (ι × η) ℂ)
    (i j : K) (S : Set (ℕ → M)) (hS : MeasurableSet S) :
    (p.toFinite.canonicalOutput R i j).value S =
      (p.completion.reference.filter (preparationFilter R)).value
        ((d.sampler.publicRecord ⁻¹' S) ∩ (p.outputA ⁻¹' {i} ∩ p.outputB ⁻¹' {j})) := by
proof · 2 lines
  rw [FiniteProtocol.canonicalOutput,PositiveMatrixMeasure.filter_value,p.output_event i j S hS,
    PositiveMatrixMeasure.filter_value]
574
lemma value_eq_of_ae_set (W : PositiveMatrixMeasure Ω ι)
    (S U : Set Ω) (hS : MeasurableSet S) (hU : MeasurableSet U)
    (h : S =ᵐ[W.traceMeasure] U) : W.value S = W.value U := by
proof · 4 lines
  ext i j
  rw [← W.integral_density_entry W.traceMeasure W.value_zero_of_traceMeasure_zero S hS i j,
    ← W.integral_density_entry W.traceMeasure W.value_zero_of_traceMeasure_zero U hU i j]
  exact setIntegral_congr_set h
582
theorem bitReadout_eq_off_private_null (W : PositiveMatrixMeasure Ω ι)
    (record : Ω → T) (hr : Measurable record)
    (a b a' b' : Ω → Bool) (ha : Measurable a) (hb : Measurable b)
    (ha' : Measurable a') (hb' : Measurable b')
    (N : Set Ω) (hN : W.traceMeasure N = 0)
    (hbits : ∀ x, x ∉ N → a x = a' x ∧ b x = b' x)
    (i j : Bool) :
    W.bitReadout record hr a b ha hb i j = W.bitReadout record hr a' b' ha' hb' i j := by
proof · 16 lines
  apply eq_of_value
  intro S hS
  rw [bitReadout_value _ _ _ _ _ _ _ _ _ _ hS,
    bitReadout_value _ _ _ _ _ _ _ _ _ _ hS]
  apply value_eq_of_ae_set _ _ _
    ((hr hS).inter ((ha (measurableSet_singleton i)).inter (hb (measurableSet_singleton j))))
    ((hr hS).inter ((ha' (measurableSet_singleton i)).inter (hb' (measurableSet_singleton j))))
  have hn : ∀ᵐ x ∂W.traceMeasure, x ∉ N := by
    rw [ae_iff]
    simpa only [not_not,Set.ofPred_mem_eq] using hN
  filter_upwards [hn] with x hx
  rcases hbits x hx with ⟨he,hf⟩
  apply propext
  change (record x ∈ S ∧ (a x = i ∧ b x = j)) ↔
    (record x ∈ S ∧ (a' x = i ∧ b' x = j))
  rw [he,hf]
609
theorem prepared_default_outputs {κ : Type v12423_0} [Fintype κ] [DecidableEq κ]
    (V : PositiveMatrixMeasure Ω κ) (W : PositiveMatrixMeasure Ω ι)
    (K : Matrix κ ι ℂ) (F : Filtration ℕ mΩ) (hm : mΩ = ⨆ n, F n)
    (hfinite : ∀ n S, MeasurableSet[F n] S → V.value S = K * W.value S * Kᴴ)
    (record : Ω → T) (hr : Measurable record)
    (a b a' b' : Ω → Bool) (ha : Measurable a) (hb : Measurable b)
    (ha' : Measurable a') (hb' : Measurable b')
    (N : Set Ω) (hN : V.traceMeasure N = 0)
    (hbits : ∀ x, x ∉ N → a x = a' x ∧ b x = b' x)
    (i j : Bool) :
    V.bitReadout record hr a b ha hb i j =
      (W.bitReadout record hr a' b' ha' hb' i j).filter K := by
proof · 7 lines
  rw [V.bitReadout_eq_off_private_null record hr a b a' b' ha hb ha' hb' N hN hbits i j]
  have he : V = W.filter K := by
    exact W.filter_eq_of_filtration V K F hm hfinite
  rw [he]
  apply eq_of_value
  intro S hS
  exact W.bitReadout_filter record hr a' b' ha' hb' i j K S hS
652
theorem tensor_filter_precompose [Fintype κ] [DecidableEq ι] [DecidableEq η] [DecidableEq κ]
    (S : ReferenceFunctional A ι) (T : ReferenceFunctional B η)
    (K : Matrix κ (ι × η) ℂ) (L : C →ₗ[ℂ] A) (N : D →ₗ[ℂ] B) :
    (filterFunctional K (tensorReferenceFunctional S T)).comp (TensorProduct.map L N) =
      filterFunctional K (tensorReferenceFunctional (S.comp L) (T.comp N)) := by
proof · 4 lines
  apply TensorProduct.ext'
  intro a b
  simp only [LinearMap.comp_apply, TensorProduct.map_tmul]
  rfl
662
theorem tensor_filter_event [One C] [One D]
    (S : ReferenceFunctional A ι) (T : ReferenceFunctional B η)
    (K : Matrix κ (ι × η) ℂ) (L : C →ₗ[ℂ] A) (N : D →ₗ[ℂ] B) :
    ((filterFunctional K (tensorReferenceFunctional S T)).comp
      (TensorProduct.map L N)) (1 ⊗ₜ[ℂ] 1) = K * (S (L 1) ⊗ₖ T (N 1)) * Kᴴ := by
proof · 2 lines
  rw [tensor_filter_precompose]
  rfl
684
theorem InstrumentRun.tensor_event [One C] (E : Set O) (hE : MeasurableSet E)
    (a : B) (b : C) :
    (∫ o in E, r.posterior o a ⊗ₖ T b ∂r.law) = S (J.operation E a) ⊗ₖ T b := by
proof · 8 lines
  have hi : Integrable (fun o => r.posterior o a ⊗ₖ T b) r.law := by
    apply ZeroKey.integrable_matrix_of_entries
    intro i j
    exact (r.integrable a i.1 j.1).mul_const _
  ext i j
  rw [ZeroKey.integral_matrix_entry hi.integrableOn]
  simp only [Matrix.kroneckerMap_apply]
  rw [integral_mul_const,r.update E hE]
696
theorem InstrumentRun.filtered_tensor_event (K : Matrix κ (ι × η) ℂ)
    (E : Set O) (hE : MeasurableSet E) (a : B) (b : C) :
    (∫ o in E, K * (r.posterior o a ⊗ₖ T b) * Kᴴ ∂r.law) =
      K * (S (J.operation E a) ⊗ₖ T b) * Kᴴ := by
proof · 5 lines
  have hi : Integrable (fun o => r.posterior o a ⊗ₖ T b) r.law := by
    apply ZeroKey.integrable_matrix_of_entries
    intro i j
    exact (r.integrable a i.1 j.1).mul_const _
  rw [ZeroKey.integral_filter _ hi.integrableOn,InstrumentRun.tensor_event J S r T E hE]
727
theorem BitProtocol.filtered_output_event (K : Matrix κ (ι × η) ℂ)
    (i j : Bool) (E : Set (ℕ → M)) (hE : MeasurableSet E) :
    ((p.probeOutput i j).filter K).value E =
      ∫ x in d.sampler.publicRecord ⁻¹' E,
        ((filterFunctional K (tensorReferenceFunctional (p.incomingA.state x) (p.incomingB.state x))).comp
          (TensorProduct.map ((p.readoutA.instrument x).operation {i})
            ((p.readoutB.instrument x).operation {j}))) (1 ⊗ₜ[ℂ] 1) ∂d.probeLaw := by
proof · 3 lines
  rw [PositiveMatrixMeasure.filter_value,p.probeOutput_event i j E hE]
  simp_rw [tensor_filter_event]
  exact (integral_filter _ (p.completeReference_integrable i j).integrableOn).symm
763
def toBit : BitProtocol (ι := ι) (η := η) d A B ℂ ℂ ℂ ℂ := p.toFinite.toBit id
765
lemma toBit_completeReference (i j : Bool) (x : ℕ → O) :
    p.toBit.completeReference i j x = p.toFinite.completeReference i j x := rfl
768
lemma toBit_probeOutput (i j : Bool) : p.toBit.probeOutput i j = p.toFinite.probeOutput i j := by
proof · 4 lines
  apply PositiveMatrixMeasure.eq_of_value
  intro S hS
  rw [p.toBit.probeOutput_event i j S hS,p.toFinite.probeOutput_event i j S hS]
  rfl
776
theorem toBit_purifiedOutput {κ : Type v12596_0} [Fintype κ] [DecidableEq κ]
    (K : Matrix κ (ι × η) ℂ) (i j : Bool) :
    (p.toBit.probeOutput i j).filter K =
      (p.completion.reference.filter K).bitReadout
        d.sampler.publicRecord d.sampler.measurable_publicRecord p.outputA p.outputB
        (p.measurableA.mono (d.sampler.localField_le true) le_rfl)
        (p.measurableB.mono (d.sampler.localField_le false) le_rfl) i j := by
proof · 5 lines
  apply PositiveMatrixMeasure.eq_of_value
  intro S hS
  rw [PositiveMatrixMeasure.bitReadout_value _ _ _ _ _ _ _ _ _ _ hS,
    PositiveMatrixMeasure.filter_value,PositiveMatrixMeasure.filter_value,
    p.toBit_probeOutput,p.output_event i j S hS]
791
theorem prepared_actual_output {κ : Type v12613_0} [Fintype κ] [DecidableEq κ]
    (K : Matrix κ (ι × η) ℂ) (V : PositiveMatrixMeasure (ℕ → O) κ)
    (hfinite : ∀ n S, MeasurableSet[historyFiltration n] S →
      V.value S = K * (∫ x in S,
        AdaptiveQuantum.jointReference d.sampler d.kernelA d.kernelB p.executionA p.executionB
          n (historyPrefix n x) ∂d.probeLaw) * Kᴴ)
    (a b : (ℕ → O) → Bool) (ha : Measurable a) (hb : Measurable b)
    (N : Set (ℕ → O)) (hN : V.traceMeasure N = 0)
    (hbits : ∀ x, x ∉ N → a x = p.outputA x ∧ b x = p.outputB x)
    (i j : Bool) :
    V.bitReadout d.sampler.publicRecord d.sampler.measurable_publicRecord a b ha hb i j =
      (p.toBit.probeOutput i j).filter K := by
proof · 10 lines
  have he : V = p.completion.reference.filter K := by
    apply p.completion.reference.filter_eq_of_filtration V K historyFiltration
    · exact historyFiltration_generates
    · intro n S hS
      rw [p.completion.reference_prefix n S hS]
      exact hfinite n S hS
  rw [V.bitReadout_eq_off_private_null _ _ a b p.outputA p.outputB ha hb
    (p.measurableA.mono (d.sampler.localField_le true) le_rfl)
    (p.measurableB.mono (d.sampler.localField_le false) le_rfl) N hN hbits i j,he,
    p.toBit_purifiedOutput K i j]
832
def independentCoinInstrument : ObservableInstrument O A A where
body · 3 lines
  operation E := (μ.real E : ℂ) • LinearMap.id
  positive E hE a ha := positive_scaling (μ.real E) ENNReal.toReal_nonneg a ha
  total_unit := by simp [Measure.real]
837
def independentCoinRun (S : ReferenceFunctional A ι) (hS : PositiveFunctional S)
    (hn : trace (S 1) = 1) : InstrumentRun (independentCoinInstrument μ positive_scaling) S where
body · 11 lines
  law := μ
  probability := inferInstance
  posterior _ := S
  posterior_positive _ := hS
  posterior_trace _ := hn
  integrable _ _ _ := integrable_const _
  update E hE a i j := by
    simp only [independentCoinInstrument,LinearMap.smul_apply,LinearMap.id_apply,
      map_smul,Matrix.smul_apply,integral_const,
      smul_eq_mul,Complex.real_smul]
    simp only [Measure.real, Measure.restrict_apply_univ]
852
lemma independentCoinRun_reference [DecidableEq ι] (S : ReferenceFunctional A ι) (hS : PositiveFunctional S)
    (hn : trace (S 1) = 1) (o : O) (a : A) :
    (independentCoinRun μ positive_scaling S hS hn).posterior o a = S a := rfl