-
Notifications
You must be signed in to change notification settings - Fork 3
/
CLArith.v
executable file
·4681 lines (4467 loc) · 171 KB
/
CLArith.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 Reals.
Require Import Psatz.
Require Import SQIR.
Require Import VectorStates UnitaryOps Coq.btauto.Btauto Coq.NArith.Nnat.
Require Import Dirac.
Require Import QPE.
Require Import BasicUtility.
Require Import MathSpec.
Require Import OQASM.
Require Import OQASMProof.
Local Open Scope exp_scope.
Local Open Scope nat_scope.
Local Opaque CNOT. Local Opaque CCX.
(*
This file contains an implementation and proof of correctness for a modular
multiplier similar to the one defined in shor/ModMult.v, but rewritten to
use VQO.
*)
(*********** Definitions ***********)
(* modmult adder based on classical circuits. *)
Definition MAJ a b c := CNOT c b ; CNOT c a ; CCX a b c.
Definition UMA a b c := CCX a b c ; CNOT c a ; CNOT a b.
(* The following defines n-bits MAJ and UMA circuit.
Eventually, MAJ;UMA circuit takes [x][y] and produce [x][(x+y) % 2 ^ n] *)
Fixpoint MAJseq' n x y c : exp :=
match n with
| 0 => MAJ c (y,0) (x,0)
| S m => MAJseq' m x y c; MAJ (x, m) (y, n) (x, n)
end.
Definition MAJseq n x y c := MAJseq' (n - 1) x y c.
Fixpoint UMAseq' n x y c : exp :=
match n with
| 0 => UMA c (y,0) (x,0)
| S m => UMA (x, m) (y,n) (x, n); UMAseq' m x y c
end.
Definition UMAseq n x y c := UMAseq' (n - 1) x y c.
Definition adder01 n x y c: exp := MAJseq n x y c; UMAseq n x y c.
(* The following implements an comparator.
THe first step is to adjust the adder circuit above to be
MAJ;high_bit_manipulate;UMA.
This is based on a binary number circuit observation that:
To compare if x < y, we just need to do x - y, and see the high bit of the binary
format of x - y. If the high_bit is zero, that means that x >= y;
otherwise x < y. *)
Definition highbit n x c2 := X (x,n-1) ; X c2 ; CNOT (x,n-1) c2 ; X c2; X (x,n-1).
Definition highb01 n x y c1 c2: exp := MAJseq n x y c1; highbit n x c2 ; inv_exp (MAJseq n x y c1).
(* The following will do the negation of the first input value in the qubit sequence 00[x][y][z].
THe actual effect is to make the sequence to be 00[-x][y][z]. *)
Fixpoint negator0 i x : exp :=
match i with
| 0 => SKIP (x,0)
| S i' => negator0 i' x; X (x, i')
end.
(* The actual comparator implementation.
We first flip the x positions, then use the high-bit comparator above.
Then, we use an inverse circuit of flipping x positions to turn the
low bits back to store the value x.
The actual implementation in the comparator is to do (x' + y)' as x - y,
and then, the high-bit actually stores the boolean result of x - y < 0. *)
Definition comparator01 n x y c1 c2 := (X c1; negator0 n x); highb01 n x y c1 c2; inv_exp (X c1; negator0 n x).
(* The implementation of a subtractor. It takes two values [x][y], and the produces
the result of [x][y + 2^n - x]. *)
Definition subtractor01 n x y c1:= (X c1; negator0 n x); adder01 n x y c1; inv_exp (X c1; negator0 n x).
(* The implementation of a modulo adder. It takes [M][x][y], and then produces the result of [M][x+y % M][y].
The modulo operation is not reversible. It will flip the high-bit to be the comparator factor.
To flip the high-bit to zero, we use the inverse circuit of the comparator in the modulo adder to
flip the high-bit back to zero.*)
Definition modadder21 n x y M c1 c2 := adder01 n y x c1 ; (* adding y to x *)
comparator01 n M x c1 c2; (* compare M > x + y (in position x) *)
X c2 ; CU c2 (subtractor01 n M x c1) ; (* doing -M + x to x, then flip c2. *)
inv_exp(comparator01 n y x c1 c2). (* compare x with x+y % M to clean c2. *)
(* Here we implement the doubler circuit based on binary shift operation.
It assumes an n-1 value x that live in a cell of n-bits (so the high-bit must be zero).
Then, we shift one position, so that the value looks like 2*x in a n-bit cell. *)
Definition doubler1 y := Lshift y.
(* Another version of the mod adder only for computing [x][M] -> [2*x % M][M].
This version will mark the high-bit, and the high-bit is not clearable.
However, eventually, we will clean all high-bit
by using a inverse circuit of the whole implementation. *)
Definition moddoubler01 n x M c1 c2 :=
doubler1 x; X c2; (comparator01 n M x c1 c2; CU c2 (subtractor01 n M x c1)).
(* The following implements the modulo adder for all bit positions in the
binary boolean function of C.
For every bit in C, we do the two items:
we first to double the factor (originally 2^(i-1) * x %M, now 2^i * x %M).
Then, we see if we need to add the factor result to the sum of C*x%M
based on if the i-th bit of C is zero or not.
modadder21 n x y M c1 c2
[M][x][0][0] -> [M][2^i * x % M][C^i*x % M][0]
*)
(* A function to compile positive to a bool function. *)
(* fb_push is to take a qubit and then push it to the zero position
in the bool function representation of a number. *)
(* A function to compile a natural number to a bool function. *)
Fixpoint modsummer' i n M x y c1 s (fC : nat -> bool) :=
match i with
| 0 => SKIP (x,0)
| S i' => modsummer' i' n M x y c1 s fC;
(if (fC i') then (modadder21 n y x M c1 (s,i')) else (SKIP (x,i'))); moddoubler01 n x M c1 (s,i')
end.
Definition modsummer n M x y c1 s C := modsummer' (n-1) n M x y c1 s (nat2fb C).
(* This is the final clean-up step of the mod multiplier to do C*x %M.
Here, modmult_half will first clean up all high bits. *)
Definition modmult_half n M x y c1 s C := modsummer n M x y c1 s C; (inv_exp (modsummer n M x y c1 s 0)).
Definition modmult_full C Cinv n M x y c1 s := modmult_half n M x y c1 s C; inv_exp (modmult_half n M y x c1 s Cinv).
Definition modmult M C Cinv n x y z s c1 := (init_v n z M); modmult_full C Cinv n z x y c1 s; inv_exp ( (init_v n z M)).
Definition modmult_rev M C Cinv n x y z s c1 := modmult M C Cinv n x y z s c1.
Definition x_var := 0. Definition y_var := 1. Definition z_var := 2. Definition s_var := 3.
Definition c_var := 4.
Definition vars_for_cl' (size:nat) := gen_vars size (x_var::(y_var::(z_var::(s_var::[])))).
Definition vars_for_cl (size:nat) := fun x => if x =? c_var then (size * 4,1,id_nat,id_nat) else vars_for_cl' size x.
Definition real_modmult_rev (M C Cinv size:nat) :=
modmult (nat2fb M) C Cinv size x_var y_var z_var s_var (c_var,0).
Definition trans_modmult_rev (M C Cinv size:nat) :=
trans_exp (vars_for_cl size) (4*size+1) (real_modmult_rev M C Cinv size) (avs_for_arith size).
(*********** Proofs ***********)
Definition no_equal (x y:var) (c1 c2 : posi) : Prop := x <> y /\ x <> fst c1 /\ x <> fst c2
/\ y <> fst c1 /\ y <> fst c2 /\ c1 <> c2.
Definition no_equal_5 (x y M:var) (c1 c2 : posi) : Prop := x <> y /\ x <> M /\ x <> fst c1 /\ x <> fst c2
/\ y <> M /\ y <> fst c1 /\ y <> fst c2 /\ M <> fst c1 /\ M <> fst c2 /\ c1 <> c2.
Definition no_equal_5a (x y M s:var) (c1 : posi) : Prop := x <> y /\ x <> M /\ x <> s /\ x <> fst c1
/\ y <> M /\ y <> s /\ y <> fst c1 /\ M <> s /\ M <> fst c1 /\ s <> fst c1.
Definition hbf n M x := fun (i : nat) =>
if (i <? n)
then (M <=? 2 * ((2^i * x) mod M)) else false.
Lemma maj_wt : forall aenv env x y z, x <> y -> y <> z -> z <> x ->
Env.MapsTo (fst x) Nor env -> Env.MapsTo (fst y) Nor env
-> Env.MapsTo (fst z) Nor env -> well_typed_oexp aenv env (MAJ x y z) env.
Proof.
intros.
unfold MAJ.
apply pe_seq with (env' := env).
apply pe_seq with (env' := env).
apply cnot_wt_nor; try easy. iner_p.
apply cnot_wt_nor; try easy.
apply ccx_wt_nor; try easy.
Qed.
Lemma uma_wt : forall aenv env x y z, x <> y -> y <> z -> z <> x ->
Env.MapsTo (fst x) Nor env -> Env.MapsTo (fst y) Nor env
-> Env.MapsTo (fst z) Nor env -> well_typed_oexp aenv env (UMA x y z) env.
Proof.
intros.
unfold UMA.
apply pe_seq with (env' := env).
apply pe_seq with (env' := env).
apply ccx_wt_nor; try easy.
apply cnot_wt_nor; try easy.
apply cnot_wt_nor; try easy.
Qed.
Local Transparent CNOT CCX.
Lemma maj_fresh : forall aenv x y z c, x <> c -> y <> c -> z <> c ->
exp_fresh aenv c (MAJ x y z).
Proof.
intros.
unfold MAJ. constructor. constructor.
unfold CNOT. constructor. iner_p. constructor. iner_p.
unfold CNOT. constructor. iner_p. constructor. iner_p.
unfold CCX.
unfold CNOT. constructor. iner_p. constructor. iner_p.
constructor. iner_p.
Qed.
Lemma uma_fresh : forall aenv x y z c, x <> c -> y <> c -> z <> c ->
exp_fresh aenv c (UMA x y z).
Proof.
intros.
unfold UMA. constructor. constructor.
unfold CNOT. constructor. iner_p. constructor. iner_p.
unfold CNOT. constructor. iner_p. constructor. iner_p.
constructor. iner_p.
unfold CNOT. constructor. iner_p. constructor. iner_p.
Qed.
Lemma maj_neu : forall l x y z, exp_neu_l l [] (MAJ x y z) [].
Proof.
intros.
unfold MAJ.
apply seq_neul with (l' := []).
unfold CNOT.
apply seq_neul with (l' := []).
constructor. constructor.
constructor. constructor.
unfold CCX.
unfold CNOT. constructor. constructor. constructor.
Qed.
Lemma uma_neu : forall l x y z, exp_neu_l l [] (UMA x y z) [].
Proof.
intros.
unfold UMA.
apply seq_neul with (l' := []).
unfold CCX.
unfold CNOT.
apply seq_neul with (l' := []). constructor. constructor.
constructor. constructor. constructor.
unfold CNOT. constructor. constructor.
Qed.
Lemma exp_WF_MAJseq': forall size x y c aenv, size < aenv x -> size < aenv y
-> snd c < aenv (fst c) -> exp_WF aenv (MAJseq' size x y c).
Proof.
induction size;intros;simpl.
constructor. constructor. unfold CNOT. constructor. simpl. easy.
constructor; simpl; lia. constructor. simpl. lia.
constructor; simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
destruct size. simpl. constructor.
constructor. constructor. constructor. simpl. lia. constructor; simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. constructor. constructor. simpl. lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor.
apply IHsize; try lia.
destruct size. simpl. constructor. constructor. simpl.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. constructor.
constructor. simpl; lia.
constructor. simpl. lia. constructor. simpl. lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
Qed.
Lemma exp_WF_UMAseq': forall size x y c aenv, size < aenv x -> size < aenv y
-> snd c < aenv (fst c) -> exp_WF aenv (UMAseq' size x y c).
Proof.
induction size;intros;simpl.
constructor. constructor. constructor. simpl. easy.
constructor. simpl; lia. constructor. simpl. lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
destruct size. simpl. constructor.
constructor. constructor. constructor. simpl. lia. constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. constructor. constructor. simpl. lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor.
constructor.
constructor.
constructor. simpl; lia.
constructor. simpl. lia. constructor. simpl. lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
constructor. simpl; lia.
apply IHsize; try lia.
Qed.
Lemma exp_WF_negator0: forall size x aenv, size <= aenv x -> 0 < aenv x -> exp_WF aenv (negator0 size x).
Proof.
induction size;intros;simpl. constructor. simpl. lia.
constructor. apply IHsize. lia. easy.
constructor. simpl; lia.
Qed.
Lemma exp_WF_inv : forall e aenv, exp_WF aenv e -> exp_WF aenv (inv_exp e).
Proof.
induction e;intros;simpl;eauto.
constructor. inv H. easy. apply IHe. inv H. easy.
inv H. constructor; easy.
inv H. constructor; easy.
inv H. constructor; easy.
inv H. constructor; easy.
inv H. constructor; easy.
inv H. constructor; easy.
inv H. constructor; easy.
inv H. constructor; easy.
inv H. constructor. apply IHe2. easy. apply IHe1. easy.
Qed.
Lemma exp_WF_highb01 : forall n aenv x y c1 c2, n <= aenv x -> 0 < aenv x
-> 0 < aenv y -> n <= aenv y -> snd c1 < aenv (fst c1)
-> snd c2 < aenv (fst c2) -> exp_WF aenv (highb01 n x y c1 c2).
Proof.
intros.
constructor. constructor. apply exp_WF_MAJseq'; try lia.
constructor. constructor. constructor. constructor. constructor. simpl;lia.
constructor. simpl;lia.
constructor. simpl;lia.
constructor. simpl;lia.
constructor. simpl;lia.
constructor. simpl;lia.
apply exp_WF_inv.
apply exp_WF_MAJseq'; try lia.
Qed.
Lemma exp_WF_comparator01 : forall n aenv x y c1 c2, n <= aenv x -> 0 < aenv x
-> n <= aenv y -> 0 < aenv y -> snd c1 < aenv (fst c1)
-> snd c2 < aenv (fst c2) -> exp_WF aenv (comparator01 n x y c1 c2).
Proof.
intros. constructor. constructor.
constructor. constructor. lia. apply exp_WF_negator0; try lia.
apply exp_WF_highb01; try lia.
apply exp_WF_inv.
constructor. constructor. iner_p.
apply exp_WF_negator0;try lia.
Qed.
Lemma exp_WF_modadder21: forall n x y M c1 c2 aenv, n <= aenv x -> n <= aenv y -> n <= aenv M
-> 0 < aenv x -> 0 < aenv y -> 0 < aenv M ->
snd c1 < aenv (fst c1) -> snd c2 < aenv (fst c2) -> exp_WF aenv (modadder21 n x y M c1 c2).
Proof.
intros.
constructor.
constructor. constructor. constructor. constructor.
apply exp_WF_MAJseq'; try lia.
apply exp_WF_UMAseq'; try lia.
apply exp_WF_comparator01;try lia.
constructor. lia.
constructor. easy.
constructor.
constructor. constructor. constructor. easy.
apply exp_WF_negator0;try lia.
constructor.
apply exp_WF_MAJseq'; try lia.
apply exp_WF_UMAseq'; try lia.
apply exp_WF_inv.
constructor. constructor;easy.
apply exp_WF_negator0;try lia.
apply exp_WF_inv. apply exp_WF_comparator01;try lia.
Qed.
Lemma exp_WF_moddoubler01: forall n x M c1 c2 aenv, n <= aenv x -> n <= aenv M
-> 0 < aenv x -> 0 < aenv M -> snd c1 < aenv (fst c1) -> snd c2 < aenv (fst c2)
-> exp_WF aenv (moddoubler01 n x M c1 c2).
Proof.
intros.
constructor.
constructor. constructor. lia. constructor. iner_p.
constructor.
apply exp_WF_comparator01;try lia.
constructor. iner_p. constructor.
constructor. constructor. constructor;iner_p.
apply exp_WF_negator0;try lia.
constructor. apply exp_WF_MAJseq';try lia.
apply exp_WF_UMAseq';try lia.
apply exp_WF_inv. constructor.
constructor;iner_p.
apply exp_WF_negator0;try lia.
Qed.
Lemma exp_WF_modsummer': forall i n x y s M c1 FC aenv, i < n ->
n <= aenv x -> n <= aenv y -> n <= aenv M ->
snd c1 < aenv (fst c1) -> n <= aenv s
-> exp_WF aenv (modsummer' i n M x y c1 s FC).
Proof.
induction i;intros;simpl.
constructor. iner_p. simpl in *. lia.
constructor. constructor. apply IHi. lia.
easy. easy. easy. easy. easy.
destruct (FC i).
apply exp_WF_modadder21; try lia. simpl in *. lia.
constructor. simpl. lia.
apply exp_WF_moddoubler01; try lia. simpl in *. lia.
Qed.
Local Opaque CNOT CCX.
Lemma MAJseq'_wt : forall n aenv tenv x y c, x <> fst c -> y <> fst c -> x <> y -> Env.MapsTo x Nor tenv
-> Env.MapsTo y Nor tenv -> Env.MapsTo (fst c) Nor tenv ->
well_typed_oexp aenv tenv (MAJseq' n x y c) tenv.
Proof.
induction n;intros;simpl.
apply maj_wt. iner_p. iner_p. iner_p. easy. simpl. easy. simpl. easy.
apply pe_seq with (env' := tenv).
apply IHn; try easy.
apply maj_wt. iner_p. iner_p. iner_p. easy. easy. easy.
Qed.
Lemma MAJseq'_fresh : forall n aenv x y c c2, c <> c2 -> y <> fst c2 -> x <> fst c2->
exp_fresh aenv c2 (MAJseq' n x y c).
Proof.
induction n;intros;simpl.
apply maj_fresh. destruct c2. destruct c. iner_p. iner_p. iner_p.
constructor.
apply IHn; try easy.
apply maj_fresh. iner_p. iner_p. iner_p.
Qed.
Lemma MAJseq'_neu : forall n l x y c, exp_neu_l l [] (MAJseq' n x y c) [].
Proof.
induction n;intros;simpl.
apply maj_neu.
apply seq_neul with (l' := []).
apply IHn.
apply maj_neu.
Qed.
Lemma MAJseq_wt : forall n aenv tenv x y c, x <> fst c -> y <> fst c -> x <> y -> Env.MapsTo x Nor tenv
-> Env.MapsTo y Nor tenv -> Env.MapsTo (fst c) Nor tenv ->
well_typed_oexp aenv tenv (MAJseq n x y c) tenv.
Proof.
intros. unfold MAJseq. apply MAJseq'_wt; try easy.
Qed.
Lemma MAJseq_fresh : forall n aenv x y c c2, c <> c2 -> y <> fst c2 -> x <> fst c2->
exp_fresh aenv c2 (MAJseq n x y c).
Proof.
intros. unfold MAJseq. apply MAJseq'_fresh; try easy.
Qed.
Lemma MAJseq_neu : forall n l x y c, exp_neu_l l [] (MAJseq n x y c) [].
Proof.
intros. unfold MAJseq. apply MAJseq'_neu; try easy.
Qed.
Lemma UMAseq'_wt : forall n aenv tenv x y c, x <> fst c -> y <> fst c -> x <> y -> Env.MapsTo x Nor tenv
-> Env.MapsTo y Nor tenv -> Env.MapsTo (fst c) Nor tenv ->
well_typed_oexp aenv tenv (UMAseq' n x y c) tenv.
Proof.
induction n;intros;simpl.
apply uma_wt. iner_p. iner_p. iner_p. easy. simpl. easy. simpl. easy.
apply pe_seq with (env' := tenv).
apply uma_wt. iner_p. iner_p. iner_p. easy. simpl. easy. simpl. easy.
apply IHn; try easy.
Qed.
Lemma UMAseq'_fresh : forall n aenv x y c c2, c <> c2 -> y <> fst c2 -> x <> fst c2->
exp_fresh aenv c2 (UMAseq' n x y c).
Proof.
induction n;intros;simpl.
apply uma_fresh. destruct c2. destruct c. iner_p. iner_p. iner_p.
constructor.
apply uma_fresh. iner_p. iner_p. iner_p.
apply IHn; try easy.
Qed.
Lemma UMAseq'_neu : forall n l x y c, exp_neu_l l [] (UMAseq' n x y c) [].
Proof.
induction n;intros;simpl.
apply uma_neu.
apply seq_neul with (l' := []).
apply uma_neu.
apply IHn.
Qed.
Lemma UMAseq_wt : forall n aenv tenv x y c, x <> fst c -> y <> fst c -> x <> y -> Env.MapsTo x Nor tenv
-> Env.MapsTo y Nor tenv -> Env.MapsTo (fst c) Nor tenv ->
well_typed_oexp aenv tenv (UMAseq n x y c) tenv.
Proof.
intros. unfold UMAseq. apply UMAseq'_wt; try easy.
Qed.
Lemma UMAseq_fresh : forall n aenv x y c c2, c <> c2 -> y <> fst c2 -> x <> fst c2->
exp_fresh aenv c2 (UMAseq n x y c).
Proof.
intros. unfold UMAseq. apply UMAseq'_fresh; try easy.
Qed.
Lemma UMAseq_neu : forall n l x y c, exp_neu_l l [] (UMAseq n x y c) [].
Proof.
intros. unfold UMAseq. apply UMAseq'_neu; try easy.
Qed.
Lemma negator0_wt : forall n aenv tenv x, Env.MapsTo x Nor tenv ->
well_typed_oexp aenv tenv (negator0 n x) tenv.
Proof.
induction n;intros;simpl. constructor. constructor.
apply pe_seq with (env' := tenv).
apply IHn. easy.
constructor. constructor. easy.
Qed.
Lemma MAJ_correct :
forall a b c f aenv,
nor_mode f a -> nor_mode f b -> nor_mode f c ->
a <> b -> b <> c -> a <> c ->
exp_sem aenv (MAJ c b a) f = (((f[a |-> put_cu (f a) (majb (get_cua (f a)) (get_cua (f b)) (get_cua (f c)))])
[b |-> put_cu (f b) (get_cua (f b) ⊕ get_cua (f a))])
[c |-> put_cu (f c) (get_cua (f c) ⊕ (get_cua (f a)))]).
Proof.
intros ? ? ? ? ? HNa HNb HNc Hab' Hbc' Hac'.
unfold MAJ.
simpl.
rewrite cnot_sem.
rewrite cnot_sem.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite ccx_sem.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
rewrite get_put_cu by assumption.
rewrite get_put_cu by assumption.
apply functional_extensionality.
intros.
bdestruct (c ==? x).
subst.
rewrite eupdate_index_eq.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
rewrite xorb_comm. easy.
bdestruct (x ==? a).
subst.
rewrite eupdate_index_eq.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
assert ((get_cua (f a)
⊕ (get_cua (f a) ⊕ get_cua (f b) && get_cua (f a) ⊕ get_cua (f c)))
= (majb (get_cua (f a)) (get_cua (f b)) (get_cua (f c)))).
unfold majb. btauto.
rewrite H0. easy.
bdestruct (x ==? b).
subst.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
rewrite xorb_comm. easy.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
easy.
rewrite eupdate_twice_neq by nor_sym.
apply nor_mode_up ; nor_sym.
apply nor_mode_up_1. assumption.
apply nor_mode_up ; nor_sym.
apply nor_mode_up_1. assumption.
apply nor_mode_up ; nor_sym.
apply nor_mode_up ; nor_sym.
1-3:nor_sym. 1-2:assumption.
rewrite cnot_sem by assumption.
apply nor_mode_up ; nor_sym.
rewrite cnot_sem by assumption.
apply nor_mode_up ; nor_sym.
Qed.
Lemma UMA_correct_partial :
forall a b c f' fa fb fc aenv,
nor_mode f' a -> nor_mode f' b -> nor_mode f' c ->
a <> b -> b <> c -> a <> c ->
get_cua (f' a) = majb fa fb fc ->
get_cua (f' b) = (fb ⊕ fa) -> get_cua (f' c) = (fc ⊕ fa) ->
exp_sem aenv (UMA c b a) f' = (((f'[a |-> put_cu (f' a) fa])
[b |-> put_cu (f' b) (fa ⊕ fb ⊕ fc)])[c |-> put_cu (f' c) fc]).
Proof.
unfold majb. intros.
unfold UMA.
simpl.
rewrite ccx_sem by (try assumption; try nor_sym).
rewrite cnot_sem.
rewrite cnot_sem.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
rewrite eupdate_index_eq.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite get_put_cu by assumption.
rewrite get_put_cu by assumption.
apply functional_extensionality.
intros.
bdestruct (x ==? c).
subst.
rewrite eupdate_index_eq.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
assert (((get_cua (f' a) ⊕ (get_cua (f' b) && get_cua (f' c))) ⊕ get_cua (f' c)) = fc).
rewrite H5. rewrite H6. rewrite H7.
btauto.
rewrite H8. easy.
bdestruct (x ==? b).
subst.
rewrite eupdate_index_eq.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
assert ((((get_cua (f' a) ⊕ (get_cua (f' b) && get_cua (f' c))) ⊕ get_cua (f' c))
⊕ get_cua (f' b)) = ((fa ⊕ fb) ⊕ fc)).
rewrite H5. rewrite H6. rewrite H7.
btauto.
rewrite H9. easy.
bdestruct (x ==? a).
subst.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_eq.
assert ((get_cua (f' a) ⊕ (get_cua (f' b) && get_cua (f' c))) = fa).
rewrite H5. rewrite H6. rewrite H7.
btauto.
rewrite H10. easy.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
rewrite eupdate_index_neq by nor_sym.
easy.
apply nor_mode_up_1. assumption.
apply nor_mode_up ; nor_sym.
rewrite cnot_sem.
apply nor_mode_up_1.
apply nor_mode_up ; nor_sym.
apply nor_mode_up_1. assumption.
apply nor_mode_up ; nor_sym.
rewrite cnot_sem.
apply nor_mode_up ; nor_sym.
apply nor_mode_up ; nor_sym.
apply nor_mode_up_1. assumption.
apply nor_mode_up ; nor_sym.
Qed.
Local Transparent CNOT. Local Transparent CCX.
Definition var_prop (f:var -> nat) (x y c : var) (n:nat) : Prop :=
n <= (f x) /\ n <= f y /\ f c = 1.
Lemma msmb_put : forall n i st x b f g,
S i < n -> put_cus st x (msmb (S i) b f g) n = (put_cus st x
(msmb i b f g) n)[(x,S i) |-> put_cu (st (x,S i)) (msmb i b f g (S i) ⊕ msma i b f g (S i))].
Proof.
intros.
apply functional_extensionality.
intros. destruct x0.
bdestruct (x =? v).
subst.
unfold put_cus. simpl.
bdestruct (n0 =? S i). subst.
rewrite eupdate_index_eq.
bdestruct (v =? v).
bdestruct (S i <? n).
assert ((msmb (S i) b f g (S i)) = (msmb i b f g (S i) ⊕ msma i b f g (S i))).
erewrite msm_eq2. easy. apply H.
rewrite H2. easy. lia. lia.
rewrite eupdate_index_neq. simpl.
bdestruct (v =? v).
bdestruct (n0 <? n).
unfold msmb.
bdestruct (n0 <=? S i).
bdestruct (n0 <=? i).
easy. lia.
bdestruct (n0 <=? i). lia. easy.
easy. easy. intros R. inv R. lia.
rewrite put_cus_neq. rewrite eupdate_index_neq.
rewrite put_cus_neq. easy. assumption.
intros R. inv R. lia. lia.
Qed.
Lemma msma_put : forall n i st x b f g,
S i < n -> put_cus st x (msma (S i) b f g) n = ((put_cus st x
(msma i b f g) n)[(x,S i) |-> put_cu (st (x,S i))
(majb (msma i b f g (S i)) (msmb i b f g (S i)) (msma i b f g i))])
[(x,i) |-> put_cu (st (x,i)) (msma i b f g i ⊕ msma i b f g (S i))].
Proof.
intros.
apply functional_extensionality.
intros. destruct x0.
unfold put_cus. simpl.
bdestruct (v =? x). subst.
bdestruct (n0 =? i). subst.
rewrite eupdate_index_eq.
bdestruct (i <? n).
unfold put_cu.
destruct (st (x, i)).
assert ((msma (S i) b f g i) = (msma i b f g i ⊕ msma i b f g (S i))).
erewrite <- msm_eq1. easy. apply H.
rewrite H1. easy. easy.
lia.
rewrite eupdate_index_neq.
bdestruct (n0 =? S i). subst.
rewrite eupdate_index_eq.
bdestruct (S i <? n).
unfold put_cu.
destruct (st (x, S i)).
assert ((msma (S i) b f g (S i)) = majb (msma i b f g (S i)) (msmb i b f g (S i)) (msma i b f g i)).
erewrite <- msm_eq3. easy. apply H1.
rewrite H2. easy. easy. lia.
rewrite eupdate_index_neq.
simpl.
bdestruct (n0 <? n).
bdestruct (x =? x).
assert ((msma (S i) b f g n0) = (msma i b f g n0)).
unfold msma.
bdestruct (n0 <? S i).
bdestruct (n0 <? i).
easy. lia.
bdestruct (n0 =? S i).
lia.
bdestruct (n0 <? i). lia.
bdestruct (n0 =? i). lia.
easy.
rewrite H4. easy.
lia.
bdestruct (x =? x). easy. easy.
intros R. inv R. lia.
intros R. inv R. lia.
rewrite eupdate_index_neq.
rewrite eupdate_index_neq. simpl.
bdestruct (v =? x). lia.
easy.
apply iner_neq. lia.
apply iner_neq. lia.
Qed.
Lemma msma_0 : forall f x b g n, 0 < n -> nor_modes f x n -> put_cus f x (msma 0 b (get_cus n f x) g) n =
f[(x,0) |-> put_cu (f (x,0)) (carry b (S 0) (get_cus n f x) g)].
Proof.
intros.
unfold put_cus, msma.
apply functional_extensionality.
intros.
destruct x0. simpl in *.
bdestruct (v =? x). subst.
bdestruct (n0 <? n).
bdestruct (n0 =? 0).
subst.
rewrite eupdate_index_eq. easy.
rewrite eupdate_index_neq.
rewrite get_cus_cua.
rewrite put_get_cu. easy.
unfold nor_modes in *. apply H0. lia. lia.
intros R. inv R. contradiction.
rewrite eupdate_index_neq. easy.
intros R. inv R. lia.
rewrite eupdate_index_neq. easy.
apply iner_neq. lia.
Qed.
Lemma msmb_0 : forall S f b y n, 0 < n -> nor_modes S y n -> put_cus S y (msmb 0 b f (get_cus n S y)) n =
S[(y,0) |-> put_cu (S (y,0)) (f 0 ⊕ (get_cua (S (y,0))))].
Proof.
intros.
unfold put_cus, msmb.
apply functional_extensionality.
intros.
destruct x. simpl in *.
bdestruct (v =? y). subst.
bdestruct (n0 <? n).
bdestruct (n0 <=? 0).
inv H2.
rewrite eupdate_index_eq.
rewrite get_cus_cua. easy. lia.
rewrite eupdate_index_neq.
rewrite get_cus_cua.
rewrite put_get_cu. easy.
unfold nor_modes in *. apply H0. lia. lia.
intros R. inv R. lia.
rewrite eupdate_index_neq. easy.
intros R. inv R. lia.
rewrite eupdate_index_neq. easy.
apply iner_neq. lia.
Qed.
Local Opaque MAJ. Local Opaque UMA.
Lemma MAJseq'_correct :
forall i n S x y c aenv,
0 < n -> i < n -> nor_modes S x n -> nor_modes S y n -> nor_mode S c ->
x <> y -> x <> fst c -> y <> fst c ->
exp_sem aenv (MAJseq' i x y c) S =
(put_cus
(put_cus (S[c |-> put_cu (S c) (get_cua (S c) ⊕ get_cua (S (x,0)))])
x (msma i (get_cua (S c)) (get_cus n S x) (get_cus n S y)) n)
y (msmb i (get_cua (S c)) (get_cus n S x) (get_cus n S y)) n).
Proof.
induction i; intros.
- simpl. rewrite MAJ_correct.
apply functional_extensionality.
intros.
bdestruct (x0 ==? c).
subst.
rewrite eupdate_index_eq.
rewrite put_cus_neq_1 by assumption.
rewrite put_cus_neq_1 by assumption.
rewrite eupdate_index_eq. easy.
rewrite eupdate_index_neq by nor_sym.
destruct x0.
bdestruct (v =? y).
subst.
bdestruct (n0 <? n).
rewrite put_cus_eq by assumption.
rewrite put_cus_neq by assumption.
unfold msmb.
bdestruct (n0 =? 0).
subst.
rewrite eupdate_index_eq.
bdestruct (0 <=? 0).
rewrite eupdate_index_neq by nor_sym.
rewrite get_cus_cua by lia.
rewrite get_cus_cua by lia.
rewrite xorb_comm. easy.
lia.
bdestruct (n0 <=? 0). lia.
rewrite eupdate_index_neq by tuple_eq.
rewrite eupdate_index_neq by tuple_eq.
rewrite eupdate_index_neq by nor_sym.
rewrite get_cus_cua by lia.
rewrite put_get_cu. easy.
apply H2. easy.
rewrite put_cus_out by lia.
rewrite eupdate_index_neq by tuple_eq.
rewrite eupdate_index_neq by tuple_eq.
rewrite put_cus_neq by lia.
rewrite eupdate_index_neq by nor_sym.
easy.
bdestruct (v =? x). subst.
rewrite eupdate_index_neq by tuple_eq.
rewrite put_cus_neq_1 by nor_sym.
bdestruct (n0 <? n).
rewrite put_cus_eq by assumption.
unfold msma.
bdestruct (n0 =? 0).
subst.
rewrite eupdate_index_eq.
bdestruct (0 <? 0). lia.
rewrite eupdate_index_neq by nor_sym.
rewrite carry_1.
rewrite get_cus_cua by lia.
rewrite get_cus_cua by lia. easy.
bdestruct (n0 <? 0). lia.
rewrite get_cus_cua by lia.
rewrite eupdate_index_neq by tuple_eq.
rewrite eupdate_index_neq by nor_sym.
rewrite put_get_cu. easy.
apply H1. lia.
rewrite eupdate_index_neq by tuple_eq.
rewrite put_cus_out by nor_sym.
rewrite eupdate_index_neq by nor_sym.
easy.
rewrite eupdate_index_neq by tuple_eq.
rewrite eupdate_index_neq by tuple_eq.
rewrite put_cus_neq by lia.
rewrite put_cus_neq by lia.
rewrite eupdate_index_neq by nor_sym.
easy.
apply H1. lia.
apply H2. lia.
assumption.
tuple_eq. destruct c. simpl in *.
tuple_eq. destruct c. simpl in *. tuple_eq.
- simpl. rewrite (IHi n).
rewrite MAJ_correct.
rewrite cus_get_eq.
rewrite cus_get_neq by lia.
rewrite cus_get_eq.
rewrite cus_get_neq by lia.
rewrite cus_get_eq.
apply functional_extensionality.
intros.
destruct x0.
bdestruct (n0 <? n).
bdestruct (v =? x). subst.
bdestruct (n0 =? i).
subst.
rewrite eupdate_index_eq.
rewrite put_cus_neq by lia.
rewrite (put_cus_neq (put_cus (S [c |-> put_cu (S c) (get_cua (S c) ⊕ get_cua (S (x, 0)))]) x
(msma (Datatypes.S i) (get_cua (S c)) (get_cus n S x) (get_cus n S y))
n)) by lia.
rewrite put_cus_eq by lia.
rewrite put_cus_eq by lia.
rewrite <- (msm_eq1 n).
rewrite put_cu_twice_eq.
easy. lia.
rewrite eupdate_index_neq by tuple_eq.
rewrite eupdate_index_neq by tuple_eq.
bdestruct (n0 =? Datatypes.S i).
subst. rewrite eupdate_index_eq.
rewrite put_cus_neq by lia.
rewrite (put_cus_neq (put_cus (S [c |-> put_cu (S c) (get_cua (S c) ⊕ get_cua (S (x, 0)))]) x
(msma (Datatypes.S i) (get_cua (S c)) (get_cus n S x) (get_cus n S y))
n)) by lia.
rewrite (put_cus_eq (S [c |-> put_cu (S c) (get_cua (S c) ⊕ get_cua (S (x, 0)))])).
rewrite (msm_eq3 n).
rewrite put_cu_twice_eq.
rewrite put_cus_eq by lia. easy.
lia. lia.
rewrite eupdate_index_neq by tuple_eq.
rewrite put_cus_neq by lia.
rewrite put_cus_eq by lia.
rewrite put_cus_neq by lia.
rewrite put_cus_eq by lia.
unfold msma.
bdestruct (n0 <? i).
bdestruct (n0 <? Datatypes.S i).
easy. lia.
bdestruct (n0 =? i). lia.
bdestruct (n0 <? Datatypes.S i). lia.
bdestruct (n0 =? Datatypes.S i). lia.
easy.
rewrite eupdate_index_neq by tuple_eq.
bdestruct (v =? y). subst.
bdestruct (n0 =? Datatypes.S i). subst.
rewrite eupdate_index_eq.
rewrite put_cus_eq by lia.
rewrite put_cus_eq by lia.
rewrite put_cu_twice_eq.
rewrite put_cus_neq by lia.
rewrite put_cus_neq by lia.
rewrite (msm_eq2 n). easy. lia.
rewrite eupdate_index_neq by tuple_eq.
rewrite eupdate_index_neq by tuple_eq.
rewrite put_cus_eq by lia.
rewrite put_cus_eq by lia.
rewrite put_cus_neq by lia.
rewrite put_cus_neq by lia.
unfold msmb.
bdestruct (n0 <=? i).
bdestruct (n0 <=? Datatypes.S i). easy.
lia.
bdestruct (n0 <=? Datatypes.S i). lia. easy.
rewrite eupdate_index_neq by tuple_eq.
rewrite eupdate_index_neq by tuple_eq.
rewrite put_cus_neq by lia.
rewrite put_cus_neq by lia.
rewrite put_cus_neq by lia.
rewrite put_cus_neq by lia. easy.
rewrite eupdate_index_neq by tuple_eq.
rewrite eupdate_index_neq by tuple_eq.
rewrite eupdate_index_neq by tuple_eq.
rewrite put_cus_neq_2 by lia.
rewrite put_cus_neq_2 by lia.
rewrite put_cus_neq_2 by lia.
rewrite put_cus_neq_2 by lia. easy. lia.
unfold nor_modes. intros.
apply nor_mode_up.
destruct c. simpl in *. tuple_eq.
apply H1. lia. lia.
unfold nor_modes. intros.
apply nor_mode_up.