-
Notifications
You must be signed in to change notification settings - Fork 10
/
DiscreteProb.v
1637 lines (1522 loc) · 47.3 KB
/
DiscreteProb.v
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import Bits VectorStates Measurement.
(** This file describes some theory of discrete probability distributions.
Its main feature is 'apply_u', a function to describe the output distribution
of running a quantum circuit. *)
Definition Cmod2 (c : C) : R := fst c ^ 2 + snd c ^ 2.
Lemma Cmod2_ge_0 : forall c, 0 <= Cmod2 c.
Proof.
intros. simpl. field_simplify. apply Rplus_le_le_0_compat; apply pow2_ge_0.
Qed.
Lemma Cmod2_Cmod_sqr : forall c, (Cmod2 c = (Cmod c)^2)%R.
Proof.
intros. unfold Cmod2, Cmod. rewrite R_sqrt.pow2_sqrt. lra.
simpl. nra.
Qed.
(* ========================================== *)
(** * Definition of probability distribution **)
(* ========================================== *)
(** We represent a (discrete) probability distribution over (0,n)
using a length n list of real numbers. We support sampling from this
distribution using the 'sample' function. *)
Definition sum_over_list (l : list R) := big_sum (fun i => nth i l 0) (length l).
Definition distribution (l : list R) :=
Forall (fun x => 0 <= x) l /\ sum_over_list l = 1.
Lemma sum_over_list_nil : sum_over_list [] = 0.
Proof. unfold sum_over_list. simpl. reflexivity. Qed.
Lemma sum_over_list_cons : forall x l,
sum_over_list (x :: l) = (x + sum_over_list l)%R.
Proof.
intros x l.
unfold sum_over_list.
simpl length.
rewrite big_sum_shift.
simpl nth.
reflexivity.
Qed.
Lemma sum_over_list_append : forall l1 l2,
sum_over_list (l1 ++ l2) = (sum_over_list l1 + sum_over_list l2)%R.
Proof.
intros l1 l2.
induction l1.
unfold sum_over_list.
simpl. lra.
simpl.
rewrite 2 sum_over_list_cons, IHl1.
lra.
Qed.
Lemma sum_over_list_geq_0 : forall l,
Forall (fun x => 0 <= x) l ->
0 <= sum_over_list l.
Proof.
induction l; intros.
- unfold sum_over_list. simpl. lra.
- inversion H; subst. specialize (IHl H3).
rewrite sum_over_list_cons. lra.
Qed.
(* ================================ *)
(** * Sample from a distribution **)
(* ================================ *)
(** Choose an element from the distribution based on random number r ∈ (0,1).
Example: Say that the input list is l = (.2, .3, .4, .1) (which might correspond
to the probabilities of measuring the outcomes 00, 01, 10, 11). Then this
function will return:
- 0 for r ∈ (0, .2)
- 1 for r ∈ (.2, .5)
- 2 for r ∈ (.5, .9)
- 3 for r ∈ (.9, 1)
The probability of getting a particular outcome is the size of the intervals of
r values that produce that outcome. (See r_interval below.) *)
Fixpoint sample (l : list R) (r : R) : nat :=
match l with
| nil => 0 (* error case *)
| x :: l' => if Rlt_le_dec r x then 0 else S (sample l' (r-x))
end.
Lemma sample_ub : forall l r, (sample l r <= length l)%nat.
Proof.
induction l; intros. easy.
simpl. specialize (IHl (r - a)%R). destruct (Rlt_le_dec r a); lia.
Qed.
Lemma sample_ub_lt : forall l r,
0 <= r < sum_over_list l ->
(sample l r < length l)%nat.
Proof.
induction l; intros. unfold sum_over_list in H. simpl in H. lra.
simpl. destruct (Rlt_le_dec r a). lia.
apply -> Nat.succ_lt_mono. apply IHl. rewrite sum_over_list_cons in H. lra.
Qed.
Lemma sample_lb : forall l r, (0 <= sample l r)%nat.
Proof.
induction l; intros. easy.
simpl. specialize (IHl (r - a)%R). destruct (Rlt_le_dec r a); lia.
Qed.
Lemma sample_max : forall l r,
Forall (fun x => 0 <= x) l ->
sum_over_list l <= r ->
sample l r = length l.
Proof.
induction l; intros. easy.
simpl. rewrite sum_over_list_cons in H0.
inversion H; subst.
specialize (sum_over_list_geq_0 l H4) as G.
assert (sum_over_list l <= r - a)%R by lra.
specialize (IHl (r - a)%R H4 H1) as T. rewrite T.
destruct (Rlt_le_dec r a). lra. lia.
Qed.
Lemma sample_append_l : forall l1 l2 r,
0 <= r ->
r < sum_over_list l1 ->
sample (l1 ++ l2) r = sample l1 r.
Proof.
induction l1; intros.
- rewrite sum_over_list_nil in H0. lra.
- rewrite sum_over_list_cons in H0. simpl.
destruct (Rlt_le_dec r a).
reflexivity.
rewrite IHl1.
reflexivity. lra. lra.
Qed.
Lemma sample_append_r : forall l1 l2 r,
Forall (fun x => 0 <= x) l1 ->
Forall (fun x => 0 <= x) l2 ->
sum_over_list l1 <= r ->
(sample (l1 ++ l2) r = (length l1) + sample l2 (r - sum_over_list l1))%nat.
Proof.
induction l1; intros.
- simpl. f_equal. rewrite sum_over_list_nil. lra.
- inversion H; subst.
specialize (sum_over_list_geq_0 l1 H5) as G.
rewrite sum_over_list_cons in *.
simpl.
destruct (Rlt_le_dec r a); try lra.
f_equal. rewrite IHl1; try easy; try lra.
f_equal. f_equal. lra.
Qed.
Lemma sample_repeat_lb : forall m l r,
0 <= r ->
(m <= sample (repeat 0%R m ++ l) r)%nat.
Proof.
induction m; intros.
lia. simpl.
destruct (Rlt_le_dec r 0). lra.
specialize (IHm l r H).
replace (r - 0)%R with r by lra.
lia.
Qed.
(* ========================================================================= *)
(** * Probability that a distribution satisfies a predicate (pr_outcome_sum) *)
(* ========================================================================= *)
(** Intuitively, the probability that an element satisfies boolean predicate
f is the sum over all element for which f holds. *)
Definition pr_outcome_sum (l : list R) (f : nat -> bool) : R :=
big_sum (fun i => if f i then nth i l 0 else 0) (length l).
Lemma pr_outcome_sum_extend : forall x l f,
pr_outcome_sum (x :: l) f
= if f O
then (x + pr_outcome_sum l (fun y => f (S y)))%R
else pr_outcome_sum l (fun y => f (S y)).
Proof.
intros x l f.
unfold pr_outcome_sum.
simpl length.
rewrite big_sum_shift.
destruct (f O); simpl.
reflexivity.
lra.
Qed.
Lemma pr_outcome_sum_append : forall l1 l2 f,
pr_outcome_sum (l1 ++ l2) f
= (pr_outcome_sum l1 f + pr_outcome_sum l2 (fun x => f (length l1 + x)%nat))%R.
Proof.
intros l1 l2.
induction l1; intro f.
unfold pr_outcome_sum.
simpl.
lra.
simpl.
rewrite 2 pr_outcome_sum_extend.
rewrite IHl1.
destruct (f O); lra.
Qed.
Lemma pr_outcome_sum_repeat_false : forall n f,
pr_outcome_sum (repeat 0 n) f = 0.
Proof.
intros n f.
unfold pr_outcome_sum.
destruct n as [| n]; trivial.
rewrite big_sum_0_bounded; try easy.
intros x Hx.
destruct (f x); trivial.
destruct x; trivial.
rewrite repeat_length in Hx.
simpl.
replace n with (x + (S (n - x - 1)))%nat by lia.
rewrite <- repeat_combine.
simpl.
rewrite <- repeat_length with (n:=x) (x:=0) at 1.
rewrite nth_middle.
trivial.
Qed.
Definition pr_outcome_sum_extend' :
forall l f a,
(pr_outcome_sum (a :: l) f = (if (f O) then a else 0) + pr_outcome_sum l (fun i => f (S i)))%R.
Proof.
intros.
rewrite pr_outcome_sum_extend.
destruct (f O).
reflexivity. lra.
Qed.
Lemma pr_outcome_sum_replace_f : forall l f1 f2,
(forall x, (x < length l)%nat -> f1 x = f2 x) ->
pr_outcome_sum l f1 = pr_outcome_sum l f2.
Proof.
intros l f1 f2 H.
unfold pr_outcome_sum.
apply big_sum_eq_bounded.
intros.
rewrite H; auto.
Qed.
Lemma pr_outcome_sum_false : forall l f,
(forall i, (i < length l)%nat -> f i = false) ->
pr_outcome_sum l f = 0.
Proof.
induction l; intros f Hf.
reflexivity.
rewrite pr_outcome_sum_extend.
rewrite Hf.
apply IHl.
intros i Hi.
apply Hf.
simpl. lia.
simpl. lia.
Qed.
Lemma pr_outcome_sum_true : forall l f,
(forall i, (i < length l)%nat -> f i = true) ->
pr_outcome_sum l f = sum_over_list l.
Proof.
induction l; intros f Hf.
reflexivity.
rewrite pr_outcome_sum_extend.
rewrite Hf.
rewrite IHl.
rewrite sum_over_list_cons.
reflexivity.
intros i Hi.
apply Hf.
simpl. lia.
simpl. lia.
Qed.
Lemma pr_outcome_sum_negb : forall l f,
pr_outcome_sum l f = (sum_over_list l - pr_outcome_sum l (fun x => negb (f x)))%R.
Proof.
induction l; intro f.
unfold pr_outcome_sum, sum_over_list.
simpl. lra.
rewrite 2 pr_outcome_sum_extend.
rewrite sum_over_list_cons.
rewrite IHl.
destruct (f O); simpl; lra.
Qed.
Lemma pr_outcome_sum_orb : forall l f1 f2,
Forall (fun x => 0 <= x) l ->
pr_outcome_sum l f1 <= pr_outcome_sum l (fun rnd => f1 rnd || f2 rnd).
Proof.
intros l f1 f2 Hl.
gen f1 f2.
induction l; intros f1 f2.
unfold pr_outcome_sum.
simpl. lra.
rewrite 2 pr_outcome_sum_extend.
inversion Hl; subst.
destruct (f1 O); simpl.
apply Rplus_le_compat_l.
apply IHl; auto.
destruct (f2 O); simpl.
rewrite <- (Rplus_0_l (pr_outcome_sum l _)).
apply Rplus_le_compat; auto.
apply IHl; auto.
Qed.
Lemma pr_outcome_sum_implies : forall l f1 f2,
Forall (fun x => 0 <= x) l ->
(forall x, f1 x = true -> f2 x = true) ->
(pr_outcome_sum l f1 <= pr_outcome_sum l f2)%R.
Proof.
intros l f1 f2 Hl.
gen f1 f2.
induction l; intros f1 f2 H.
unfold pr_outcome_sum.
simpl. lra.
rewrite 2 pr_outcome_sum_extend.
inversion Hl; subst.
destruct (f1 O) eqn:f1O.
apply H in f1O.
rewrite f1O.
apply Rplus_le_compat_l.
auto.
destruct (f2 O).
rewrite <- (Rplus_0_l (pr_outcome_sum _ _)).
apply Rplus_le_compat; auto.
auto.
Qed.
Lemma pr_outcome_sum_ge_0 :
forall l f, Forall (fun x => 0 <= x) l -> 0 <= pr_outcome_sum l f.
Proof.
induction l; intros.
- unfold pr_outcome_sum. simpl. lra.
- inversion H; subst. unfold pr_outcome_sum.
replace (length (a :: l)) with (S (length l)) by easy.
rewrite big_sum_shift. simpl.
specialize (IHl (fun x => f (S x)) H3).
unfold pr_outcome_sum in IHl.
destruct (f O); lra.
Qed.
(* ================================================================== *)
(** * Probability that a distribution satisfies a predicate (pr_P) **)
(* ================================================================== *)
(** Mathematically, the probability that an element satisifes a (not necessarily
boolean) predicate is the size of the range of r-values for which the element
returned from 'sample' satisfies the predicate. *)
Inductive interval_sum (P : R -> Prop) (rl rr : R) : R -> Prop :=
| SingleInterval : forall r1 r2, rl <= r1 <= r2 /\ r2 <= rr ->
(forall r, r1 < r < r2 -> P r) ->
(forall r, rl < r < r1 -> ~ P r) ->
(forall r, r2 < r < rr -> ~ P r) ->
interval_sum P rl rr (r2 - r1)%R
(* We could add [~ P rm] to this case to guarantee unique intervals *)
| CombineIntervals : forall rm r1 r2, rl <= rm <= rr ->
interval_sum P rl rm r1 ->
interval_sum P rm rr r2 ->
interval_sum P rl rr (r1 + r2).
Lemma interval_sum_shift :
forall P rl rr r a,
interval_sum P rl rr r ->
interval_sum (fun x => P (x - a)%R) (rl + a)%R (rr + a)%R r.
Proof.
intros.
induction H.
- replace (r2 - r1)%R with ((r2 + a) - (r1 + a))%R by lra.
constructor; intros; try lra.
apply H0. lra.
apply H1. lra.
apply H2. lra.
- apply CombineIntervals with (rm := (rm + a)%R); try lra; try easy.
Qed.
Lemma interval_sum_same :
forall P1 P2 rl rr r,
interval_sum P1 rl rr r ->
(forall x, rl <= x < rr -> (P1 x <-> P2 x)) ->
interval_sum P2 rl rr r.
Proof.
intros.
induction H.
- constructor; intros; try lra.
apply H0. lra. apply H1. lra.
intro. apply H0 in H5. assert (~ P1 r). apply H2. lra. easy. lra.
intro. apply H0 in H5. assert (~ P1 r). apply H3. lra. easy. lra.
- apply CombineIntervals with (rm := rm); try lra.
apply IHinterval_sum1. intros. apply H0. lra.
apply IHinterval_sum2. intros. apply H0. lra.
Qed.
Lemma interval_sum_shift_alt :
forall P rl rr r a,
interval_sum (fun x => P (x + a)%R) (rl - a)%R (rr - a)%R r ->
interval_sum P rl rr r.
Proof.
intros.
replace rl with ((rl - a) + a)%R by lra.
replace rr with ((rr - a) + a)%R by lra.
remember (fun x => P (x + a)%R) as P'.
eapply interval_sum_same.
apply interval_sum_shift. apply H.
split; intros. subst. replace (x - a + a)%R with x in H1 by lra. easy.
subst. replace (x - a + a)%R with x by lra. easy.
Qed.
Lemma interval_sum_gt_0 : forall P rl rr r, interval_sum P rl rr r -> r >= 0.
Proof. intros. induction H; lra. Qed.
Lemma interval_sum_break :
forall P rl rm rr r,
interval_sum P rl rr r ->
rl <= rm <= rr ->
exists r1 r2 : R, interval_sum P rl rm r1 /\ interval_sum P rm rr r2 /\ (r = r1 + r2)%R.
Proof.
intros.
induction H.
- intros.
destruct (Rle_lt_dec rm r1); [| destruct (Rlt_le_dec rm r2)].
+ exists (rm - rm)%R, (r2 - r1)%R.
repeat split; try lra.
* apply SingleInterval; intros; auto; try lra.
apply H2; lra.
* apply SingleInterval; intros; auto; try lra.
apply H2; lra.
+ exists (rm - r1)%R, (r2 - rm)%R.
repeat split; try lra.
* apply SingleInterval; intros; auto; try lra.
apply H1; lra.
* apply SingleInterval; intros; auto; try lra.
apply H1; lra.
+ exists (r2 - r1)%R, (rm - rm)%R.
repeat split; try lra.
* apply SingleInterval; intros; auto; try lra.
apply H3; lra.
* apply SingleInterval; intros; auto; try lra.
apply H3; lra.
- intros.
destruct (Rtotal_order rm0 rm) as [L1 | [L2 | L3]].
+ clear IHinterval_sum1.
destruct IHinterval_sum2 as [x1 [x2 [S1 [S2 E]]]]. lra.
subst.
exists (r1 + x1)%R, x2.
repeat split; auto; try lra.
apply CombineIntervals with rm0; auto; lra.
+ subst. eauto.
+ clear IHinterval_sum2.
destruct IHinterval_sum1 as [x1 [x2 [S1 [S2 E]]]]. lra.
subst.
exists x1, (x2 + r2)%R.
repeat split; auto; try lra.
apply CombineIntervals with rm0; auto; lra.
Qed.
(* TODO: clean up this proof *)
Lemma interval_sum_unique : forall P rl rr r1 r2,
interval_sum P rl rr r1 ->
interval_sum P rl rr r2 ->
r1 = r2.
Proof.
intros.
(* r3 - r0 = r4 - r1, H 1234 5678 *)
(* P : R -> Prop
rl, rr, r0, r3 : R
H : interval_sum P rl rr (r3 - r0)
r1, r4 : R
H0 : interval_sum P rl rr (r4 - r1)
H1 : rl <= r0 <= r3 /\ r3 <= rr
H2 : forall r : R, r0 < r < r3 -> P r
H3 : forall r : R, rl < r < r0 -> ~ P r
H4 : forall r : R, r3 < r < rr -> ~ P r
H5 : rl <= r1 <= r4 /\ r4 <= rr
H6 : forall r : R, r1 < r < r4 -> P r
H7 : forall r : R, rl < r < r1 -> ~ P r
H8 : forall r : R, r4 < r < rr -> ~ P r
============================
(r3 - r0)%R = (r4 - r1)%R
*)
gen r2.
induction H; intros.
gen r1 r2.
induction H3; intros.
rename r2 into r4.
rename H2 into H8. rename H1 into H7. rename H6 into tmp6. rename H0 into H6. rename H5 into tmp5. rename H into H5. rename H4 into H1. rename tmp6 into H4. rename tmp5 into H2.
{
destruct (total_order_T r0 r3) as [[L03 | E03] | G03]; try lra.
2: {
destruct (total_order_T r1 r4) as [[L14 | E14] | G14]; try lra.
remember ((r1 + r4) / 2)%R as r14.
assert (r1 < r14 < r4)%R by lra.
destruct (total_order_T r0 r14) as [[L | E] | G]; try lra.
+ assert (P r14) by (apply H6; lra).
assert (~ P r14) by (apply H4; lra).
easy.
+ remember ((r14 + r4) / 2)%R as r144.
assert (r14 < r144 < r4)%R by lra.
assert (P r144) by (apply H6; lra).
assert (~ P r144) by (apply H4; lra).
easy.
+ assert (P r14) by (apply H6; lra).
assert (~ P r14) by (apply H3; lra).
easy.
}
destruct (total_order_T r0 r1) as [[L01 | E01] | G01].
- destruct (total_order_T r1 r3) as [[L13 | E13] | G13].
+ remember ((r0 + r1) / 2)%R as r01.
assert (r0 < r01 < r1)%R by lra.
assert (P r01) by (apply H2; lra).
assert (~ P r01) by (apply H7; lra).
easy.
+ remember ((r0 + r1) / 2)%R as r01.
assert (r0 < r01 < r1)%R by lra.
assert (P r01) by (apply H2; lra).
assert (~ P r01) by (apply H7; lra).
easy.
+ remember ((r0 + r3) / 2)%R as r03.
assert (r0 < r03 < r3)%R by lra.
assert (P r03) by (apply H2; lra).
assert (~ P r03) by (apply H7; lra).
easy.
- destruct (total_order_T r3 r4) as [[L34 | E34] | G34]; try lra.
+ remember ((r3 + r4) / 2)%R as r34.
assert (r3 < r34 < r4)%R by lra.
assert (P r34) by (apply H6; lra).
assert (~ P r34) by (apply H4; lra).
easy.
+ remember ((r3 + r4) / 2)%R as r43.
assert (r4 < r43 < r3)%R by lra.
assert (P r43) by (apply H2; lra).
assert (~ P r43) by (apply H8; lra).
easy.
- destruct (total_order_T r0 r4) as [[L04 | E04] | G04].
+ remember ((r0 + r1) / 2)%R as r10.
assert (r1 < r10 < r0)%R by lra.
assert (P r10) by (apply H6; lra).
assert (~ P r10) by (apply H3; lra).
easy.
+ remember ((r0 + r1) / 2)%R as r10.
assert (r1 < r10 < r0)%R by lra.
assert (P r10) by (apply H6; lra).
assert (~ P r10) by (apply H3; lra).
easy.
+ remember ((r0 + r3) / 2)%R as r03.
assert (r0 < r03 < r3)%R by lra.
assert (P r03) by (apply H2; lra).
assert (~ P r03) by (apply H8; lra).
easy.
}
- destruct (Rlt_le_dec r0 rm) as [LT0m | LE0m]; try lra;
destruct (Rlt_le_dec r3 rm) as [LT3m | LE3m]; try lra.
+ assert (r3 - r0 = r1)%R.
{ apply IHinterval_sum1; try intros. apply H1; lra. lra. apply H2; lra. apply H3; lra.
}
assert (rr - rr = r2)%R.
{ apply IHinterval_sum2; try intros. apply H3; lra. lra. apply H2; lra. apply H3; lra.
}
lra.
+ assert (rm - r0 = r1)%R.
{ apply IHinterval_sum1; try intros. apply H1; lra. lra. apply H2; lra. apply H3; lra.
}
assert (r3 - rm = r2)%R.
{ apply IHinterval_sum2; try intros. apply H3; lra. lra. apply H2; lra. apply H3; lra.
}
lra.
+ assert (rm - rm = r1)%R.
{ apply IHinterval_sum1; try intros. apply H1; lra. lra. apply H2; lra. apply H3; lra.
}
assert (r3 - r0 = r2)%R.
{ apply IHinterval_sum2; try intros. apply H1; lra. lra. apply H2; lra. apply H3; lra.
}
lra.
- inversion H2.
+ destruct (Rlt_le_dec r3 rm) as [LT3m | LE3m];
destruct (Rlt_le_dec r4 rm) as [LT4m | LE4m]; try lra.
* assert (r1 = r4 - r3)%R.
{ apply IHinterval_sum1.
constructor; try intros. lra.
apply H4; lra. apply H5; lra. apply H6; lra.
}
assert (r2 = rr - rr)%R.
{ apply IHinterval_sum2.
constructor; try intros. lra.
apply H4; lra. apply H6; lra. apply H6; lra.
}
lra.
* assert (r1 = rm - r3)%R.
{ apply IHinterval_sum1.
constructor; try intros. lra.
apply H4; lra. apply H5; lra. apply H6; lra.
}
assert (r2 = r4 - rm)%R.
{ apply IHinterval_sum2.
constructor; try intros. lra.
apply H4; lra. apply H6; lra. apply H6; lra.
}
lra.
* assert (r1 = rm - rm)%R.
{ apply IHinterval_sum1.
constructor; try intros. lra.
apply H4; lra. apply H5; lra. apply H6; lra.
}
assert (r2 = r4 - r3)%R.
{ apply IHinterval_sum2.
constructor; try intros. lra.
apply H4; lra. apply H5; lra. apply H6; lra.
}
lra.
+ destruct (Rlt_le_dec rm rm0) as [LTmm | LEmm].
* destruct (interval_sum_break P rl rm rm0 _ H4) as [l1 [l2 [G1 [G2 G3]]]]. lra.
assert (r1 = l1) by (apply IHinterval_sum1; easy).
assert (r2 = l2 + r4)%R.
{ apply IHinterval_sum2. apply CombineIntervals with (rm := rm0); try lra; try easy.
}
lra.
* destruct (interval_sum_break P rm0 rm rr _ H5) as [l1 [l2 [G1 [G2 G3]]]]. lra.
assert (r1 = r3 + l1)%R.
{ apply IHinterval_sum1. apply CombineIntervals with (rm := rm0); try lra; try easy.
}
assert (r2 = l2) by (apply IHinterval_sum2; easy).
lra.
Qed.
(** Mathematical measure of P on the interval (0,1) *)
Definition pr_P P r := interval_sum P 0%R 1%R r.
Lemma pr_P_same :
forall P1 P2 r,
(forall rnd, 0 <= rnd < 1 -> P1 rnd <-> P2 rnd) ->
pr_P P1 r ->
pr_P P2 r.
Proof.
unfold pr_P. intros.
apply interval_sum_same with (P1 := P1); assumption.
Qed.
Lemma pr_outcome_sum_eq_aux' : forall (l : list R) (f : nat -> bool) r,
Forall (fun x => 0 <= x) l ->
sum_over_list l = r ->
interval_sum (fun rnd => f (sample l rnd) = true) 0 r (pr_outcome_sum l f).
Proof.
induction l; intros.
- unfold sum_over_list in *.
simpl in *. subst. unfold pr_outcome_sum. simpl.
replace 0 with (0 - 0)%R by lra. constructor; try intros; try lra.
- rewrite sum_over_list_cons in H0.
remember (fun i => f (S i)) as sf.
assert (interval_sum (fun rnd : R => sf (sample l rnd) = true) 0 (r - a)%R (pr_outcome_sum l sf)).
{ apply IHl. inversion H; easy. lra.
}
rewrite pr_outcome_sum_extend'.
assert (interval_sum (fun rnd : R => f (sample (a :: l) rnd) = true) 0 a (if f O then a else 0)).
{ destruct (f O) eqn:E.
- replace a with (a - 0)%R by lra.
constructor;
try (inversion H; lra);
try (intros; simpl; destruct (Rlt_le_dec r0 (a - 0)); try easy; try lra).
- replace 0 with (a - a)%R by lra.
constructor;
try (inversion H; lra);
try (intros; simpl; destruct (Rlt_le_dec r0 a); try easy; try lra).
rewrite E. easy.
}
assert (interval_sum (fun rnd : R => f (sample (a :: l) rnd) = true) a r (pr_outcome_sum l sf)).
{ (*remember (fun rnd : R => f (sample (a :: l) rnd) = true) as P.*)
apply interval_sum_shift_alt with (a := a).
replace (a - a)%R with 0 by lra.
eapply interval_sum_same. apply H1.
split; intros.
- simpl. destruct (Rlt_le_dec (x + a) a); try lra. subst. replace (x + a - a)%R with x by lra. easy.
- subst. simpl in H4. destruct (Rlt_le_dec (x + a) a) in H4; try lra. replace (x + a - a)%R with x in H4 by lra. easy.
}
rewrite Heqsf in H3.
apply CombineIntervals with (rm := a); try easy.
inversion H.
assert (0 <= sum_over_list l) by (apply sum_over_list_geq_0; easy).
lra.
Qed.
(** The pr_outcome_sum and pr_P definitions of probability are consistent. *)
Lemma pr_outcome_sum_eq_aux : forall (l : list R) (f : nat -> bool),
distribution l ->
pr_P (fun rnd => f (sample l rnd) = true) (pr_outcome_sum l f).
Proof.
intros. destruct H as [H H0]. unfold pr_P.
apply pr_outcome_sum_eq_aux'; easy.
Qed.
Lemma pr_outcome_sum_leq_exists : forall l f r,
distribution l ->
pr_outcome_sum l f <= r ->
exists r0, (0 <= r0 <= r)%R /\ pr_P (fun rnd => f (sample l rnd) = true) r0.
Proof.
intros l f r HlHr.
exists (pr_outcome_sum l f).
split; auto.
split. apply pr_outcome_sum_ge_0. apply HlHr. auto.
apply pr_outcome_sum_eq_aux; auto.
Qed.
Lemma pr_P_unique : forall P r1 r2,
pr_P P r1 ->
pr_P P r2 ->
r1 = r2.
Proof.
intros.
apply interval_sum_unique with (P:=P) (rl:=R0) (rr:=R1); auto.
Qed.
(* Alternative definition that requires uniqueness. *)
Lemma pr_outcome_sum_eq : forall f l r,
distribution l ->
pr_outcome_sum l f = r <-> pr_P (fun rnd => f (sample l rnd) = true) r.
Proof.
split; intros.
- rewrite <- H0.
apply pr_outcome_sum_eq_aux.
easy.
- eapply pr_P_unique.
apply pr_outcome_sum_eq_aux; trivial.
easy.
Qed.
(* ======================================================= *)
(** * Distribution created by running a quantum program **)
(* ======================================================= *)
(** Given our definition of sample, we can define a function to apply a
quantum program and return the result of measuring all qubits.
rnd is a random input in (0,1). *)
Definition apply_u {dim} (u : Square (2 ^ dim)) : list R :=
let v := u × basis_vector (2^dim) 0 in
map Cmod2 (vec_to_list v).
Lemma pos_Cmod2_list :
forall l, Forall (fun x => 0 <= x) (map Cmod2 l).
Proof.
induction l; intros.
- simpl. constructor.
- simpl. constructor. apply Cmod2_ge_0. apply IHl.
Qed.
Local Opaque big_sum.
Lemma sum_over_list_Cmod2_vec_to_list' : forall d x l,
sum_over_list (map Cmod2 (@vec_to_list' x d l)) =
big_sum (fun i : nat => (Cmod (l (i + x - d)%nat O) ^ 2)%R) d.
Proof.
induction d; intros.
- unfold sum_over_list. reflexivity.
- simpl. rewrite sum_over_list_cons. rewrite IHd. simpl.
rewrite big_sum_shift.
replace (0 + x - S d)%nat with (x - S d)%nat by lia.
rewrite Cmod2_Cmod_sqr. simpl.
f_equal.
Qed.
Local Transparent big_sum.
Lemma sum_over_list_Cmod2_vec_to_list :
forall d (l : Vector d),
sum_over_list (map Cmod2 (vec_to_list l)) =
big_sum (fun i : nat => (Cmod (l i O) ^ 2)%R) d.
Proof.
intros. unfold vec_to_list.
rewrite sum_over_list_Cmod2_vec_to_list'.
apply big_sum_eq_bounded. intros.
replace (x + d - d)%nat with x by lia.
reflexivity.
Qed.
Lemma distribution_apply_u : forall {dim} u,
WF_Unitary u ->
distribution (@apply_u dim u).
Proof.
intros. unfold apply_u. split.
- apply pos_Cmod2_list.
- rewrite sum_over_list_Cmod2_vec_to_list.
rewrite <- rewrite_norm.
unfold inner_product.
rewrite Mmult_adjoint.
rewrite <- Mmult_assoc.
rewrite Mmult_assoc with (A := (basis_vector (2 ^ dim) 0) †).
destruct H as [_ H].
rewrite H.
restore_dims. rewrite Mmult_1_r.
rewrite basis_vector_product_eq. reflexivity.
apply pow_positive. lia.
apply WF_adjoint. apply basis_vector_WF.
apply pow_positive. lia.
Qed.
Lemma length_apply_u : forall n (u : Square (2 ^ n)),
length (apply_u u) = (2 ^ n)%nat.
Proof.
intros n u.
unfold apply_u.
rewrite map_length.
rewrite vec_to_list_length.
reflexivity.
Qed.
Lemma nth_apply_u_probability_of_outcome : forall n (u : Square (2 ^ n)) x,
(x < 2 ^ n)%nat ->
WF_Matrix u ->
nth x (apply_u u) 0
= probability_of_outcome
(basis_vector (2^n) x)
(u × basis_vector (2^n) 0).
Proof.
intros n u x Hx WFu.
unfold apply_u, probability_of_outcome.
rewrite nth_indep with (d':=Cmod2 0).
rewrite map_nth.
remember (u × basis_vector (2 ^ n) 0) as ψ.
rewrite nth_vec_to_list by assumption.
rewrite (basis_vector_decomp ψ) at 2.
unfold inner_product.
rewrite Mmult_Msum_distr_l, <- Cmod2_Cmod_sqr.
apply f_equal.
symmetry.
erewrite big_sum_unique.
2 : { exists x. split. assumption.
split.
rewrite Mscale_mult_dist_r.
rewrite basis_vector_product_eq.
reflexivity. assumption.
intros y Hy Hxy.
rewrite Mscale_mult_dist_r.
rewrite basis_vector_product_neq by auto.
lma. }
unfold scale, I.
bdestruct_all; try lia; lca.
subst.
apply WF_mult.
assumption.
apply basis_vector_WF.
apply pow_positive. lia.
rewrite map_length.
rewrite vec_to_list_length.
assumption.
Qed.
(* ========================== *)
(** * Uniform distribution **)
(* ========================== *)
(** Uniform sampling in the range (lower, upper) *)
Definition uniform (lower upper : nat) : list R :=
repeat 0 lower ++ repeat (1/ INR (upper - lower))%R (upper - lower).
Lemma repeat_gt0 : forall m r, 0 <= r -> Forall (fun x => 0 <= x) (repeat r m).
Proof.
induction m; intros. simpl. constructor.
simpl. constructor. easy. apply IHm. easy.
Qed.
Lemma sum_over_list_repeat : forall m x, (sum_over_list (repeat x m) = INR m * x)%R.
Proof.
induction m; intros.
- simpl. unfold sum_over_list. simpl. lra.
- simpl. rewrite sum_over_list_cons. rewrite IHm.
destruct m; simpl; lra.
Qed.
Lemma sample_uniform : forall l u r,
(l < u)%nat -> 0 <= r < 1 -> (l <= sample (uniform l u) r < u)%nat.
Proof.
intros. split.
- unfold uniform. apply sample_repeat_lb. easy.
- unfold uniform. rewrite sample_append_r. rewrite repeat_length.
assert (T: (forall a b c, a < c -> b < c - a -> a + b < c)%nat) by (intros; lia).
apply T. easy.
replace (u - l)%nat
with (length (repeat (1 / INR (u - l))%R (u - l))) at 3 by apply repeat_length.
apply sample_ub_lt.
replace l with (length (repeat 0 l)) at 1 2 by apply repeat_length.
repeat rewrite sum_over_list_repeat.
replace (INR (u - l) * (1 / INR (u - l)))%R
with (INR (u - l) * / INR (u - l))%R by lra.
rewrite Rinv_r. lra.
apply not_0_INR. lia.
apply repeat_gt0; lra.
apply repeat_gt0. unfold Rdiv. rewrite Rmult_1_l.
apply Rlt_le. apply Rinv_0_lt_compat. apply lt_0_INR. lia.
rewrite sum_over_list_repeat. lra.
Qed.
Lemma distribution_uniform : forall l r,
(l < r)%nat ->
distribution (uniform l r).
Proof.
intros. split; unfold uniform.
- apply Forall_app. split; apply repeat_gt0. lra.
unfold Rdiv.
assert (0 < / INR (r - l)).
{ apply Rinv_0_lt_compat. apply lt_0_INR. lia. }
lra.
- rewrite sum_over_list_append.
do 2 rewrite sum_over_list_repeat.
unfold Rdiv.
replace (INR l * 0 + INR (r - l) * (1 * / INR (r - l)))%R
with (INR (r - l) * / INR (r - l))%R by lra.
rewrite <- Rinv_r_sym. easy.
assert (0 < INR (r - l)) by (apply lt_0_INR; lia).
lra.
Qed.
Lemma length_uniform : forall l r, (l <= r)%nat -> (length (uniform l r) = r)%nat.
Proof.
intros. unfold uniform. rewrite app_length, repeat_length, repeat_length. lia.
Qed.
(* ======================= *)
(** * Joint distribution **)
(* ======================= *)
Fixpoint scale r l :=
match l with
| nil => nil
| h :: t => (r * h)%R :: scale r t
end.
(** Combine distributions l1 and l2, where l2 may depend on the value of l1 *)
Fixpoint join' l1 l2 n :=
match n with
| O => nil
| S n' => join' l1 l2 n' ++ scale (nth n' l1 0) (l2 n')
end.
Definition join l1 l2 := join' l1 l2 (length l1).
(** Given a nat consisting of (n+m) bits, extract the first n or last m.
Example application: when sampling from (join l1 l2) where |l1|=n and
|l2|=m, you can use fst and snd to split the result. *)
(* TODO: come up with better names *)
Definition fst (m x : nat) := (x / 2 ^ m)%nat.
Definition snd (m x : nat) := (x mod 2 ^ m)%nat.
Lemma fst_0 : forall m, fst m 0 = O.
Proof.
intros. unfold fst. apply Nat.div_0_l. apply Nat.pow_nonzero. lia.
Qed.
Lemma fst_plus : forall m x, fst m (2 ^ m + x) = S (fst m x).
Proof.
intros.
unfold fst.
rewrite <- (Nat.mul_1_l (2 ^ m)) at 1.
rewrite Nat.div_add_l.
lia.
apply Nat.pow_nonzero. lia.
Qed.
Lemma fst_small : forall m x, (x < 2 ^ m)%nat -> fst m x = O.
Proof. intros. unfold fst. apply Nat.div_small. auto. Qed.
Lemma snd_0 : forall m, snd m 0 = O.
Proof.
intros. unfold snd. apply Nat.mod_0_l. apply Nat.pow_nonzero. lia.
Qed.
Lemma snd_small : forall m x, (x < 2 ^ m)%nat -> snd m x = x.
Proof. intros. unfold snd. apply Nat.mod_small. auto. Qed.
Lemma snd_plus : forall m x, snd m (2 ^ m + x) = snd m x.
Proof.
intros.
unfold snd.
rewrite Nat.add_comm.
rewrite <- (Nat.mul_1_l (2 ^ m)) at 1.
apply Nat.mod_add.
apply Nat.pow_nonzero. lia.
Qed.
Lemma simplify_fst : forall n x y,
(y < 2 ^ n)%nat ->
fst n (x * 2 ^ n + y) = x.
Proof.
intros n x y Hy.
unfold fst.
rewrite Nat.div_add_l.
rewrite Nat.div_small by assumption.
lia.
apply Nat.pow_nonzero.
lia.
Qed.