File size: 22,641 Bytes
f61e547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import OAI.Probability.EntangledGames.CoherentFilters

namespace OAI

universe u_n u_A u_m u_ι u_p u_q u_B

open scoped BigOperators ComplexOrder
open scoped MatrixOrder
open Matrix
open MeasureTheory Filter Set
open scoped Topology
open scoped Matrix.Norms.Elementwise
open scoped Interval
open scoped Kronecker

noncomputable section
namespace ThresholdParallelRepetition.QuantumSampling

structure POVM (n : Type u_n) (A : Type u_A) [Fintype n] [DecidableEq n] [Fintype A] where
  effect : A → Matrix n n ℂ
  pos : ∀ a, (effect a).PosSemidef
  total : ∑ a, effect a = 1

variable {m : Type u_m} {n : Type u_n} {A : Type u_A} [Fintype m] [Fintype n] [DecidableEq m] [DecidableEq n]
  [Fintype A]

def POVM.default (a₀ : A) : POVM n A := by
  classical
  exact { effect := fun a => if a = a₀ then 1 else 0
          pos := fun a => by split <;> first | exact Matrix.PosSemidef.one | exact Matrix.PosSemidef.zero
          total := by simp }

def POVM.pull (P : POVM m A) (K : Matrix m n ℂ) (a : A) : Matrix n n ℂ :=
  Kᴴ * P.effect a * K

omit [DecidableEq n] in
lemma POVM.pull_pos (P : POVM m A) (K : Matrix m n ℂ) (a : A) :
    (P.pull K a).PosSemidef := (P.pos a).conjTranspose_mul_mul_same K
omit [Fintype n] [DecidableEq n] in
lemma POVM.pull_sum (P : POVM m A) (K : Matrix m n ℂ) :
    ∑ a, P.pull K a = Kᴴ * K := by
  simp only [POVM.pull, ← Matrix.sum_mul, ← Matrix.mul_sum, P.total, Matrix.mul_one]

omit [Fintype m] [Fintype n] [DecidableEq m] [DecidableEq n] in
lemma kronecker_sum_left {ι : Type u_ι} [Fintype ι] (M : ι → Matrix m m ℂ)
    (N : Matrix n n ℂ) : (∑ i, M i) ⊗ₖ N = ∑ i, M i ⊗ₖ N := by
  ext i j; simp only [Matrix.kroneckerMap_apply, Matrix.sum_apply, Finset.sum_mul]
omit [Fintype m] [Fintype n] [DecidableEq m] [DecidableEq n] in
lemma kronecker_sum_right {ι : Type u_ι} [Fintype ι] (M : Matrix m m ℂ)
    (N : ι → Matrix n n ℂ) : M ⊗ₖ (∑ i, N i) = ∑ i, M ⊗ₖ N i := by
  ext i j; simp only [Matrix.kroneckerMap_apply, Matrix.sum_apply, Finset.mul_sum]

@[reducible] def Copies (n : Type) : ℕ → Type
  | 0 => Unit
  | t+1 => n × Copies n t
instance copiesFintype {n : Type} [Fintype n] : (t : ℕ) → Fintype (Copies n t)
  | 0 => inferInstanceAs (Fintype Unit)
  | t+1 => by
    letI := copiesFintype (n := n) t
    exact inferInstanceAs (Fintype (n × Copies n t))
instance copiesDecidableEq {n : Type} [DecidableEq n] : (t : ℕ) → DecidableEq (Copies n t)
  | 0 => inferInstanceAs (DecidableEq Unit)
  | t+1 => @instDecidableEqProd n (Copies n t) _ (copiesDecidableEq t)
instance copiesNonempty {n : Type} [Nonempty n] : (t : ℕ) → Nonempty (Copies n t)
  | 0 => inferInstanceAs (Nonempty Unit)
  | t+1 => by
    let := copiesNonempty (n := n) t
    exact inferInstanceAs (Nonempty (n × Copies n t))

def tensorPower {m n : Type} (D : Matrix m n ℂ) : (t : ℕ) → Matrix (Copies m t) (Copies n t) ℂ
  | 0 => fun _ _ => 1
  | t+1 => D ⊗ₖ tensorPower D t

lemma hsSq_tensorPower {m n : Type} [Fintype m] [Fintype n] (D : Matrix m n ℂ) (t : ℕ) :
    hsSq (tensorPower D t) = hsSq D ^ t := by
  induction t with
  | zero => simp [tensorPower, hsSq]
  | succ t ih => rw [tensorPower, hsSq_kronecker, ih, pow_succ']

def firstSuccess {m n : Type} [Fintype m] [Fintype n] [DecidableEq m] [DecidableEq n]
    {A : Type} [Fintype A] (P : POVM m A) (K : Matrix m n ℂ)
    (hK : Kᴴ*K ≤ 1) (a₀ : A) : (t : ℕ) → POVM (Copies n t) A
  | 0 => POVM.default a₀
  | t+1 => {
      effect := fun a => P.pull K a ⊗ₖ 1 + (1-Kᴴ*K) ⊗ₖ (firstSuccess P K hK a₀ t).effect a
      pos := fun a => ((P.pull_pos K a).kronecker Matrix.PosSemidef.one).add
        ((Matrix.nonneg_iff_posSemidef.mp (sub_nonneg.mpr hK)).kronecker
          ((firstSuccess P K hK a₀ t).pos a))
      total := by
        rw [Finset.sum_add_distrib, ← kronecker_sum_left, ← kronecker_sum_right,
          P.pull_sum, (firstSuccess P K hK a₀ t).total, ← Matrix.add_kronecker]
        simp only [add_sub_cancel, Matrix.one_kronecker_one] }

lemma eval_submatrix {m : Type u_m} {n : Type u_n} {p : Type u_p} {q : Type u_q} [Fintype m] [Fintype n] [Fintype p] [Fintype q]
    (C : Matrix m n ℂ) (U : Matrix m m ℂ) (V : Matrix n n ℂ) (e : p ≃ m) (f : q ≃ n) :
    eval (C.submatrix e f) (U.submatrix e e) (V.submatrix f f) = eval C U V := by
  simp only [eval_eq_sum, Matrix.submatrix_apply]
  apply Fintype.sum_equiv e
  intro i
  apply Fintype.sum_equiv f
  intro j
  apply Fintype.sum_equiv e
  intro k
  exact Fintype.sum_equiv f _ _ (fun _ => rfl)

lemma exists_strategy_of_finite {m n X Y A B : Type}
    [Fintype m] [Fintype n] [DecidableEq m] [DecidableEq n] [Nonempty m] [Nonempty n]
    [Fintype A] [Fintype B] (C : Matrix m n ℂ) (hC : hsSq C = 1)
    (P : X → POVM m A) (Q : Y → POVM n B) :
    ∃ S : Strategy X Y A B, ∀ x y a b, S.born x y a b = prob C ((P x).effect a) ((Q y).effect b) := by
  let e : Fin (Fintype.card m - 1 + 1) ≃ m :=
    (Fintype.equivFinOfCardEq (Nat.sub_add_cancel Fintype.card_pos).symm).symm
  let f : Fin (Fintype.card n - 1 + 1) ≃ n :=
    (Fintype.equivFinOfCardEq (Nat.sub_add_cancel Fintype.card_pos).symm).symm
  let S : Strategy X Y A B := {
    dimA := Fintype.card m - 1
    dimB := Fintype.card n - 1
    state := vec (C.submatrix e f)
    state_unit := by
      simp only [vec, Matrix.submatrix_apply, Fintype.sum_prod_type]
      rw [show (∑ i, ∑ j, Complex.normSq (C (e i) (f j))) = hsSq C by
        unfold hsSq
        apply Fintype.sum_equiv e
        intro i
        exact Fintype.sum_equiv f _ _ (fun _ => rfl)]
      exact hC
    alice := fun x a => ((P x).effect a).submatrix e e
    bob := fun y b => ((Q y).effect b).submatrix f f
    alice_pos := fun x a => ((P x).pos a).submatrix e
    bob_pos := fun y b => ((Q y).pos b).submatrix f
    alice_total := fun x => by
      calc
        _ = (∑ a, (P x).effect a).submatrix e e := by ext i j; simp [Matrix.sum_apply]
        _ = 1 := by rw [(P x).total, Matrix.submatrix_one_equiv]
    bob_total := fun y => by
      calc
        _ = (∑ b, (Q y).effect b).submatrix f f := by ext i j; simp [Matrix.sum_apply]
        _ = 1 := by rw [(Q y).total, Matrix.submatrix_one_equiv] }
  refine ⟨S, ?_⟩
  intro x y a b
  rw [Strategy.born_eq_quadratic]
  change prob (C.submatrix e f) (((P x).effect a).submatrix e e)
    (((Q y).effect b).submatrix f f) = _
  exact congrArg Complex.re (eval_submatrix C _ _ e f)

lemma prob_first_step_ge {m : Type u_m} {n : Type u_n} {p : Type u_p} {q : Type u_q} [Fintype m] [Fintype n] [Fintype p] [Fintype q]
    [DecidableEq p] [DecidableEq q]
    (C : Matrix m n ℂ) (D : Matrix p q ℂ) (hD : hsSq D = 1)
    {G F : Matrix m m ℂ} {H J : Matrix n n ℂ} {E : Matrix p p ℂ} {T : Matrix q q ℂ}
    (hG : G.PosSemidef) (hF : F.PosSemidef) (hH : H.PosSemidef) (hJ : J.PosSemidef)
    (hE : E.PosSemidef) (hT : T.PosSemidef) :
    prob C G H + prob C F J * prob D E T ≤
      prob (C ⊗ₖ D) (G ⊗ₖ 1 + F ⊗ₖ E) (H ⊗ₖ 1 + J ⊗ₖ T) := by
  rw [prob_add_left, prob_add_right, prob_add_right,
    prob_kronecker C D hG hH Matrix.PosSemidef.one Matrix.PosSemidef.one,
    prob_one, hD, mul_one, prob_kronecker C D hF hJ hE hT]
  have h1 := prob_nonneg (C := C ⊗ₖ D) (hG.kronecker Matrix.PosSemidef.one) (hJ.kronecker hT)
  have h2 := prob_nonneg (C := C ⊗ₖ D) (hF.kronecker hE) (hH.kronecker Matrix.PosSemidef.one)
  linarith

lemma firstSuccess_prob_succ {m n p q A B : Type} [Fintype m] [Fintype n]
    [Fintype p] [Fintype q] [DecidableEq m] [DecidableEq n] [DecidableEq p] [DecidableEq q]
    [Fintype A] [Fintype B]
    (D : Matrix n q ℂ) (hD : hsSq D = 1) (P : POVM m A) (Q : POVM p B)
    (K : Matrix m n ℂ) (L : Matrix p q ℂ) (hK : Kᴴ*K ≤ 1) (hL : Lᴴ*L ≤ 1)
    (a₀ : A) (b₀ : B) (t : ℕ) (a : A) (b : B) :
    prob (K*D*Lᵀ) (P.effect a) (Q.effect b) +
      prob D (1-Kᴴ*K) (1-Lᴴ*L) *
        prob (tensorPower D t) ((firstSuccess P K hK a₀ t).effect a)
          ((firstSuccess Q L hL b₀ t).effect b) ≤
      prob (tensorPower D (t+1)) ((firstSuccess P K hK a₀ (t+1)).effect a)
        ((firstSuccess Q L hL b₀ (t+1)).effect b) := by
  change _ ≤ prob (D ⊗ₖ tensorPower D t)
    (P.pull K a ⊗ₖ 1 + (1-Kᴴ*K) ⊗ₖ (firstSuccess P K hK a₀ t).effect a)
    (Q.pull L b ⊗ₖ 1 + (1-Lᴴ*L) ⊗ₖ (firstSuccess Q L hL b₀ t).effect b)
  have hnorm : hsSq (tensorPower D t) = 1 := by rw [hsSq_tensorPower, hD, one_pow]
  have h := prob_first_step_ge D (tensorPower D t) hnorm (P.pull_pos K a)
    (Matrix.nonneg_iff_posSemidef.mp (sub_nonneg.mpr hK)) (Q.pull_pos L b)
    (Matrix.nonneg_iff_posSemidef.mp (sub_nonneg.mpr hL))
    ((firstSuccess P K hK a₀ t).pos a) ((firstSuccess Q L hL b₀ t).pos b)
  simpa only [POVM.pull, prob_pullback] using h

lemma prob_sum_left {m : Type u_m} {n : Type u_n} {ι : Type u_ι} [Fintype m] [Fintype n] [Fintype ι]
    (C : Matrix m n ℂ) (A : ι → Matrix m m ℂ) (B : Matrix n n ℂ) :
    prob C (∑ i, A i) B = ∑ i, prob C (A i) B := by
  simp only [prob, eval_eq_trace, Matrix.sum_mul, Matrix.mul_sum, Matrix.trace_sum, Complex.re_sum]
lemma prob_sum_right {m : Type u_m} {n : Type u_n} {ι : Type u_ι} [Fintype m] [Fintype n] [Fintype ι]
    (C : Matrix m n ℂ) (A : Matrix m m ℂ) (B : ι → Matrix n n ℂ) :
    prob C A (∑ i, B i) = ∑ i, prob C A (B i) := by
  simp only [prob, eval_eq_trace, Matrix.transpose_sum, Matrix.mul_sum,
    Matrix.trace_sum, Complex.re_sum]

section Payoff
variable {m : Type u_m} {n : Type u_n} {A : Type u_A} {B : Type u_B} [Fintype m] [Fintype n] [DecidableEq m] [DecidableEq n]
    [Fintype A] [Fintype B]

def payoff (C : Matrix m n ℂ) (P : POVM m A) (Q : POVM n B) (V : A → B → Bool) : ℝ :=
  ∑ a, ∑ b, if V a b then prob C (P.effect a) (Q.effect b) else 0
lemma prob_total (C : Matrix m n ℂ) (P : POVM m A) (Q : POVM n B) :
    ∑ a, ∑ b, prob C (P.effect a) (Q.effect b) = hsSq C := by
  simp_rw [← prob_sum_right, ← prob_sum_left, P.total, Q.total, prob_one]
lemma payoff_nonneg (C : Matrix m n ℂ) (P : POVM m A) (Q : POVM n B) (V : A → B → Bool) :
    0 ≤ payoff C P Q V := by
  apply Finset.sum_nonneg; intro a _
  apply Finset.sum_nonneg; intro b _
  split
  · exact prob_nonneg (P.pos a) (Q.pos b)
  · exact le_rfl
lemma payoff_le_hsSq (C : Matrix m n ℂ) (P : POVM m A) (Q : POVM n B) (V : A → B → Bool) :
    payoff C P Q V ≤ hsSq C := by
  rw [← prob_total C P Q]
  apply Finset.sum_le_sum; intro a _
  apply Finset.sum_le_sum; intro b _
  split
  · exact le_rfl
  · exact prob_nonneg (P.pos a) (Q.pos b)
end Payoff

lemma firstSuccess_prob_geometric {m n p q A B : Type} [Fintype m] [Fintype n]
    [Fintype p] [Fintype q] [DecidableEq m] [DecidableEq n] [DecidableEq p] [DecidableEq q]
    [Fintype A] [Fintype B]
    (D : Matrix n q ℂ) (hD : hsSq D = 1) (P : POVM m A) (Q : POVM p B)
    (K : Matrix m n ℂ) (L : Matrix p q ℂ) (hK : Kᴴ*K ≤ 1) (hL : Lᴴ*L ≤ 1)
    (a₀ : A) (b₀ : B) (t : ℕ) (a : A) (b : B) :
    prob (K*D*Lᵀ) (P.effect a) (Q.effect b) *
      (∑ j ∈ Finset.range t, (prob D (1-Kᴴ*K) (1-Lᴴ*L))^j) ≤
      prob (tensorPower D t) ((firstSuccess P K hK a₀ t).effect a)
        ((firstSuccess Q L hL b₀ t).effect b) := by
  let z := prob D (1-Kᴴ*K) (1-Lᴴ*L)
  have hz : 0 ≤ z := prob_nonneg
    (Matrix.nonneg_iff_posSemidef.mp (sub_nonneg.mpr hK))
    (Matrix.nonneg_iff_posSemidef.mp (sub_nonneg.mpr hL))
  induction t with
  | zero =>
    simpa only [Finset.range_zero, Finset.sum_empty, mul_zero] using
      (prob_nonneg ((firstSuccess P K hK a₀ 0).pos a) ((firstSuccess Q L hL b₀ 0).pos b)
        (C := tensorPower D 0))
  | succ t ih =>
    calc
      _ = prob (K*D*Lᵀ) (P.effect a) (Q.effect b) + z *
          (prob (K*D*Lᵀ) (P.effect a) (Q.effect b) * ∑ j ∈ Finset.range t, z^j) := by
        change _ * (∑ j ∈ Finset.range (t+1), z^j) = _
        rw [Finset.sum_range_succ']
        simp only [pow_succ', ← Finset.mul_sum, pow_zero]
        ring
      _ ≤ prob (K*D*Lᵀ) (P.effect a) (Q.effect b) + z *
          prob (tensorPower D t) ((firstSuccess P K hK a₀ t).effect a)
            ((firstSuccess Q L hL b₀ t).effect b) :=
        add_le_add le_rfl (mul_le_mul_of_nonneg_left ih hz)
      _ ≤ _ := firstSuccess_prob_succ D hD P Q K L hK hL a₀ b₀ t a b

lemma firstSuccess_payoff_geometric {m n p q A B : Type} [Fintype m] [Fintype n]
    [Fintype p] [Fintype q] [DecidableEq m] [DecidableEq n] [DecidableEq p] [DecidableEq q]
    [Fintype A] [Fintype B]
    (D : Matrix n q ℂ) (hD : hsSq D = 1) (P : POVM m A) (Q : POVM p B)
    (K : Matrix m n ℂ) (L : Matrix p q ℂ) (hK : Kᴴ*K ≤ 1) (hL : Lᴴ*L ≤ 1)
    (a₀ : A) (b₀ : B) (t : ℕ) (V : A → B → Bool) :
    payoff (K*D*Lᵀ) P Q V *
      (∑ j ∈ Finset.range t, (prob D (1-Kᴴ*K) (1-Lᴴ*L))^j) ≤
      payoff (tensorPower D t) (firstSuccess P K hK a₀ t) (firstSuccess Q L hL b₀ t) V := by
  simp only [payoff, Finset.sum_mul]
  apply Finset.sum_le_sum; intro a _
  apply Finset.sum_le_sum; intro b _
  split
  · exact firstSuccess_prob_geometric D hD P Q K L hK hL a₀ b₀ t a b
  · simp

lemma finite_filter_geometric_value {m n p q : Type} [Fintype m] [Fintype n]
    [Fintype p] [Fintype q] [DecidableEq m] [DecidableEq n] [DecidableEq p] [DecidableEq q]
    [Nonempty n] [Nonempty q] {x y a b : ℕ} (G : Game x y a b)
    (D : Matrix n q ℂ) (hD : hsSq D = 1)
    (P : Fin (x+1) → POVM m (Fin (a+1))) (Q : Fin (y+1) → POVM p (Fin (b+1)))
    (K : Fin (x+1) → Matrix m n ℂ) (L : Fin (y+1) → Matrix p q ℂ)
    (hK : ∀ x, (K x)ᴴ*K x ≤ 1) (hL : ∀ y, (L y)ᴴ*L y ≤ 1) (t : ℕ) :
    (∑ z : Fin (x+1) × Fin (y+1), G.questionProb z *
      (payoff (K z.1*D*(L z.2)ᵀ) (P z.1) (Q z.2) (G.accepts z.1 z.2) *
        ∑ j ∈ Finset.range t, (prob D (1-(K z.1)ᴴ*K z.1) (1-(L z.2)ᴴ*L z.2))^j)) ≤
      entangledValue G := by
  have hnorm : hsSq (tensorPower D t) = 1 := by rw [hsSq_tensorPower, hD, one_pow]
  obtain ⟨S, hS⟩ := exists_strategy_of_finite (tensorPower D t) hnorm
    (fun x => firstSuccess (P x) (K x) (hK x) 0 t)
    (fun y => firstSuccess (Q y) (L y) (hL y) 0 t)
  apply le_trans ?_ (successProbability_le_value G S)
  unfold successProbability
  apply Finset.sum_le_sum; intro z _
  apply mul_le_mul_of_nonneg_left _ (G.questionProb_nonneg z)
  rw [Fintype.sum_prod_type]
  simp_rw [hS]
  exact firstSuccess_payoff_geometric D hD (P z.1) (Q z.2) (K z.1) (L z.2)
    (hK z.1) (hL z.2) 0 0 t (G.accepts z.1 z.2)

section FilterMass
variable {m : Type u_m} {n : Type u_n} {p : Type u_p} {q : Type u_q} [Fintype m] [Fintype n] [Fintype p] [Fintype q]
    [DecidableEq m] [DecidableEq n] [DecidableEq p] [DecidableEq q]

omit [Fintype p] [Fintype q] [DecidableEq m] [DecidableEq n] [DecidableEq p] [DecidableEq q] in
lemma prob_sub_left (C : Matrix m n ℂ) (A A' : Matrix m m ℂ) (B : Matrix n n ℂ) :
    prob C (A-A') B = prob C A B - prob C A' B := by
  simp only [prob, eval_eq_trace, Matrix.sub_mul, Matrix.mul_sub, Matrix.trace_sub, Complex.sub_re]
omit [Fintype p] [Fintype q] [DecidableEq m] [DecidableEq n] [DecidableEq p] [DecidableEq q] in
lemma prob_sub_right (C : Matrix m n ℂ) (A : Matrix m m ℂ) (B B' : Matrix n n ℂ) :
    prob C A (B-B') = prob C A B - prob C A B' := by
  simp only [prob, eval_eq_trace, Matrix.transpose_sub, Matrix.mul_sub, Matrix.trace_sub, Complex.sub_re]

omit [DecidableEq n] [DecidableEq q] in
lemma prob_gram (D : Matrix n q ℂ) (K : Matrix m n ℂ) (L : Matrix p q ℂ) :
    prob D (Kᴴ*K) (Lᴴ*L) = hsSq (K*D*Lᵀ) := by
  simpa only [Matrix.mul_one, prob_one] using prob_pullback D K L (1 : Matrix m m ℂ) (1 : Matrix p p ℂ)
omit [Fintype p] [DecidableEq p] [DecidableEq n] in
lemma prob_left_gram (D : Matrix n q ℂ) (K : Matrix m n ℂ) :
    prob D (Kᴴ*K) 1 = hsSq (K*D) := by
  simpa only [Matrix.conjTranspose_one, Matrix.one_mul, Matrix.transpose_one, Matrix.mul_one] using
    prob_gram D K (1 : Matrix q q ℂ)
omit [Fintype m] [DecidableEq m] [DecidableEq q] in
lemma prob_right_gram (D : Matrix n q ℂ) (L : Matrix p q ℂ) :
    prob D 1 (Lᴴ*L) = hsSq (D*Lᵀ) := by
  simpa only [Matrix.conjTranspose_one, Matrix.one_mul] using
    prob_gram D (1 : Matrix n n ℂ) L

lemma prob_fail (D : Matrix n q ℂ) (K : Matrix m n ℂ) (L : Matrix p q ℂ) :
    prob D (1-Kᴴ*K) (1-Lᴴ*L) =
      hsSq D - hsSq (K*D) - hsSq (D*Lᵀ) + hsSq (K*D*Lᵀ) := by
  rw [prob_sub_left, prob_sub_right, prob_sub_right, prob_one, prob_right_gram,
    prob_left_gram, prob_gram]
  ring

lemma filter_mass_bounds (D : Matrix n q ℂ) (K : Matrix m n ℂ) (L : Matrix p q ℂ)
    (hK : Kᴴ*K ≤ 1) (hL : Lᴴ*L ≤ 1) :
    hsSq (K*D*Lᵀ) ≤ hsSq (K*D) ∧ hsSq (K*D*Lᵀ) ≤ hsSq (D*Lᵀ) ∧
      hsSq (K*D) ≤ hsSq D ∧ hsSq (D*Lᵀ) ≤ hsSq D := by
  have hF := Matrix.nonneg_iff_posSemidef.mp (sub_nonneg.mpr hK)
  have hJ := Matrix.nonneg_iff_posSemidef.mp (sub_nonneg.mpr hL)
  have hG := Matrix.posSemidef_conjTranspose_mul_self K
  have hH := Matrix.posSemidef_conjTranspose_mul_self L
  have h1 := prob_nonneg (C := D) hG hJ
  have h2 := prob_nonneg (C := D) hF hH
  have h3 := prob_nonneg (C := D) hF Matrix.PosSemidef.one
  have h4 := prob_nonneg (C := D) Matrix.PosSemidef.one hJ
  rw [prob_sub_right, prob_left_gram, prob_gram] at h1
  rw [prob_sub_left, prob_right_gram, prob_gram] at h2
  rw [prob_sub_left, prob_one, prob_left_gram] at h3
  rw [prob_sub_right, prob_one, prob_right_gram] at h4
  exact ⟨sub_nonneg.mp h1, sub_nonneg.mp h2, sub_nonneg.mp h3, sub_nonneg.mp h4⟩

lemma prob_fail_le (D : Matrix n q ℂ) (K : Matrix m n ℂ) (L : Matrix p q ℂ)
    (hK : Kᴴ*K ≤ 1) (hL : Lᴴ*L ≤ 1) :
    prob D (1-Kᴴ*K) (1-Lᴴ*L) ≤ hsSq D := by
  rw [prob_fail]
  have h := (filter_mass_bounds D K L hK hL).1
  have hb := hsSq_nonneg (D*Lᵀ)
  linarith

lemma output_zero_of_fail_full (D : Matrix n q ℂ) (K : Matrix m n ℂ) (L : Matrix p q ℂ)
    (hK : Kᴴ*K ≤ 1) (hL : Lᴴ*L ≤ 1)
    (h : prob D (1-Kᴴ*K) (1-Lᴴ*L) = hsSq D) : hsSq (K*D*Lᵀ) = 0 := by
  rw [prob_fail] at h
  obtain ⟨h1,h2,_,_⟩ := filter_mass_bounds D K L hK hL
  have h0 := hsSq_nonneg (K*D*Lᵀ)
  linarith
end FilterMass

open Filter Topology
lemma geometric_contribution_tendsto {w z : ℝ} (hz0 : 0 ≤ z) (hz1 : z ≤ 1)
    (hw : z = 1 → w = 0) :
    Tendsto (fun t : ℕ => w * ∑ j ∈ Finset.range t, z^j) atTop (𝓝 (w/(1-z))) := by
  by_cases hz : z = 1
  · simp only [hw hz, zero_mul, zero_div]
    exact tendsto_const_nhds
  · simpa only [div_eq_mul_inv] using
      ((hasSum_geometric_of_lt_one hz0 (lt_of_le_of_ne hz1 hz)).tendsto_sum_nat.const_mul w)

theorem finite_filter_value {m n p q : Type} [Fintype m] [Fintype n]
    [Fintype p] [Fintype q] [DecidableEq m] [DecidableEq n] [DecidableEq p] [DecidableEq q]
    [Nonempty n] [Nonempty q] {x y a b : ℕ} (G : Game x y a b)
    (D : Matrix n q ℂ) (hD : hsSq D = 1)
    (P : Fin (x+1) → POVM m (Fin (a+1))) (Q : Fin (y+1) → POVM p (Fin (b+1)))
    (K : Fin (x+1) → Matrix m n ℂ) (L : Fin (y+1) → Matrix p q ℂ)
    (hK : ∀ x, (K x)ᴴ*K x ≤ 1) (hL : ∀ y, (L y)ᴴ*L y ≤ 1) :
    (∑ z : Fin (x+1) × Fin (y+1), G.questionProb z *
      (payoff (K z.1*D*(L z.2)ᵀ) (P z.1) (Q z.2) (G.accepts z.1 z.2) /
        (1-prob D (1-(K z.1)ᴴ*K z.1) (1-(L z.2)ᴴ*L z.2)))) ≤ entangledValue G := by
  apply le_of_tendsto' (x := atTop) (f := fun t : ℕ => ∑ z : Fin (x+1) × Fin (y+1), G.questionProb z *
    (payoff (K z.1*D*(L z.2)ᵀ) (P z.1) (Q z.2) (G.accepts z.1 z.2) *
       ∑ j ∈ Finset.range t, (prob D (1-(K z.1)ᴴ*K z.1) (1-(L z.2)ᴴ*L z.2))^j))
  · apply tendsto_finsetSum
    intro z _
    apply Filter.Tendsto.const_mul
    apply geometric_contribution_tendsto
    · exact prob_nonneg (Matrix.nonneg_iff_posSemidef.mp (sub_nonneg.mpr (hK z.1)))
        (Matrix.nonneg_iff_posSemidef.mp (sub_nonneg.mpr (hL z.2)))
    · rw [← hD]; exact prob_fail_le D (K z.1) (L z.2) (hK z.1) (hL z.2)
    · intro hz
      have hn := output_zero_of_fail_full D (K z.1) (L z.2) (hK z.1) (hL z.2) (hz.trans hD.symm)
      exact le_antisymm ((payoff_le_hsSq _ _ _ _).trans_eq hn) (payoff_nonneg _ _ _ _)
  · exact fun t => finite_filter_geometric_value G D hD P Q K L hK hL t

lemma prob_smul {m : Type u_m} {n : Type u_n} [Fintype m] [Fintype n]
    (r : ℝ) (C : Matrix m n ℂ) (A : Matrix m m ℂ) (B : Matrix n n ℂ) :
    prob (r • C) A B = r^2 * prob C A B := by
  simp only [prob, eval_eq_trace, Matrix.conjTranspose_smul, star_trivial,
    Matrix.smul_mul, Matrix.mul_smul, Matrix.trace_smul, Complex.real_smul,
    Complex.mul_re, Complex.ofReal_re, Complex.ofReal_im, zero_mul, sub_zero]
  ring

lemma payoff_smul {m : Type u_m} {n : Type u_n} {A : Type u_A} {B : Type u_B} [Fintype m] [Fintype n] [DecidableEq m] [DecidableEq n]
    [Fintype A] [Fintype B] (r : ℝ) (C : Matrix m n ℂ) (P : POVM m A) (Q : POVM n B)
    (V : A → B → Bool) : payoff (r • C) P Q V = r^2 * payoff C P Q V := by
  simp only [payoff, prob_smul, Finset.mul_sum, mul_ite, mul_zero]

theorem finite_filter_value_unnormalized {m n p q : Type} [Fintype m] [Fintype n]
    [Fintype p] [Fintype q] [DecidableEq m] [DecidableEq n] [DecidableEq p] [DecidableEq q]
    [Nonempty n] [Nonempty q] {x y a b : ℕ} (G : Game x y a b)
    (D : Matrix n q ℂ) (hD : 0 < hsSq D)
    (P : Fin (x+1) → POVM m (Fin (a+1))) (Q : Fin (y+1) → POVM p (Fin (b+1)))
    (K : Fin (x+1) → Matrix m n ℂ) (L : Fin (y+1) → Matrix p q ℂ)
    (hK : ∀ x, (K x)ᴴ*K x ≤ 1) (hL : ∀ y, (L y)ᴴ*L y ≤ 1) :
    (∑ z : Fin (x+1) × Fin (y+1), G.questionProb z *
      (payoff (K z.1*D*(L z.2)ᵀ) (P z.1) (Q z.2) (G.accepts z.1 z.2) /
        (hsSq (K z.1*D) + hsSq (D*(L z.2)ᵀ) - hsSq (K z.1*D*(L z.2)ᵀ)))) ≤
      entangledValue G := by
  let r : ℝ := (Real.sqrt (hsSq D))⁻¹
  have hr : r^2 = (hsSq D)⁻¹ := by
    dsimp [r]
    rw [inv_pow, Real.sq_sqrt hD.le]
  have hnorm : hsSq (r • D) = 1 := by
    rw [hsSq_smul, hr, inv_mul_cancel₀ hD.ne']
  have hv := finite_filter_value G (r • D) hnorm P Q K L hK hL
  convert hv using 1
  apply Finset.sum_congr rfl; intro z _
  congr 1
  rw [Matrix.mul_smul, Matrix.smul_mul, payoff_smul, prob_smul, hr]
  have hden : (1 - (hsSq D)⁻¹ * prob D (1-(K z.1)ᴴ*K z.1) (1-(L z.2)ᴴ*L z.2)) =
      (hsSq D - prob D (1-(K z.1)ᴴ*K z.1) (1-(L z.2)ᴴ*L z.2)) / hsSq D := by
    field_simp
  rw [hden, inv_mul_eq_div, div_div_div_cancel_right₀ hD.ne', prob_fail]
  congr 1
  ring

end ThresholdParallelRepetition.QuantumSampling

end

end OAI