-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRZArith.v
executable file
·4676 lines (4422 loc) · 180 KB
/
RZArith.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.
Require Import CLArith.
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 the modular
multiplier circuit based on QFT.
https://arxiv.org/abs/quant-ph/0205095
The modular multiplier circuit computes ((A * x) % N) where A and N are integer
constants and x is an integer variable. The main definition in this file is
(rz_modmult_full y x n c A Ainv N). The main correctness property is
rz_modmult_full_sem.
In rz_modmult_full (y:var) (x:var) (n:nat) (c:posi) (A:nat) (Ainv :nat) (N:nat),
y is a group of n ancilla qubits, x is the input number, (n-2) is the qubit size of x.
c is an ancilla qubit for storing data, A is the input number and Ainv is the invers of A,
such that (A * Ainv) mod N = 1, N is the mod factor.
The result of the circuit will produce (A*x) mod N in the y group qubits,
while the x group will be all zero. If users want to make the values to x,
feel free to add a swap gates between x and y.
*)
(*********** Definitions ***********)
Fixpoint rz_adder' (x:var) (n:nat) (size:nat) (M: nat -> bool) :=
match n with
| 0 => SKIP (x,0)
| S m => rz_adder' x m size M ; if M m then SR (size - n) x else SKIP (x,m)
end.
Definition rz_adder (x:var) (n:nat) (M:nat -> bool) := rz_adder' x n n M.
Fixpoint rz_sub' (x:var) (n:nat) (size:nat) (M: nat -> bool) :=
match n with
| 0 => SKIP (x,0)
| S m => rz_sub' x m size M ; if M m then SRR (size - n) x else SKIP (x,m)
end.
Definition rz_sub (x:var) (n:nat) (M:nat -> bool) := rz_sub' x n n M.
Definition rz_compare_half (x:var) (n:nat) (c:posi) (M:nat) :=
(rz_sub x n (nat2fb M)) ; RQFT x n; (CNOT (x,0) c).
Definition rz_compare (x:var) (n:nat) (c:posi) (M:nat) :=
rz_compare_half x n c M ; (inv_exp ( (rz_sub x n (nat2fb M)) ; RQFT x n)).
Definition qft_cu (x:var) (c:posi) (n:nat) :=
RQFT x n; (CNOT (x,0) c) ; QFT x n.
Definition qft_acu (x:var) (c:posi) (n:nat) :=
RQFT x n; (X (x,0); CNOT (x,0) c; X (x,0)) ; QFT x n.
Definition one_cu_adder (x:var) (n:nat) (c:posi) (M:nat -> bool) := CU c (rz_adder x n M).
Definition mod_adder_half (x:var) (n:nat) (c:posi) (A:nat -> bool) (M:nat -> bool) :=
(rz_adder x n A; (rz_sub x n M)) ; qft_cu x c n; (one_cu_adder x n c M).
Definition clean_hbit (x:var) (n:nat) (c:posi) (M:nat -> bool) :=
(rz_sub x n M) ; qft_acu x c n; ( inv_exp (rz_sub x n M)).
Definition mod_adder (x:var) (n:nat) (c:posi) (A:nat -> bool) (M:nat -> bool) :=
mod_adder_half x n c A M ; clean_hbit x n c A.
(* modular multiplier: takes [x][0] -> [x][ax%N] where a and N are constant. *)
Fixpoint rz_modmult' (y:var) (x:var) (n:nat) (size:nat) (c:posi) (A:nat) (M:nat) :=
match n with
| 0 => (SKIP (y,0))
| S m => rz_modmult' y x m size c A M;
CU (x,size - n) (mod_adder y size c (nat2fb ((2^m * A) mod M)) (nat2fb M))
end.
Definition rz_modmult_half y x size c A M :=
QFT y size; rz_modmult' y x size size c A M ; RQFT y size.
Definition rz_modmult_full (y:var) (x:var) (n:nat) (c:posi) (A:nat) (Ainv :nat) (N:nat) :=
rz_modmult_half y x n c A N ; inv_exp (rz_modmult_half x y n c Ainv N).
Definition vars_for_rz' (size:nat) := gen_vars size (x_var::(y_var::[])).
Definition vars_for_rz (size:nat) :=
fun x => if x =? z_var then (size * 2,1,id_nat,id_nat) else vars_for_rz' size x.
Definition real_rz_modmult_rev (M C Cinv size:nat) :=
rz_modmult_full y_var x_var size (z_var,0) C Cinv M.
Definition trans_rz_modmult_rev (M C Cinv size:nat) :=
trans_exp (vars_for_rz size) (2*size+1) (real_rz_modmult_rev M C Cinv size) (avs_for_arith size).
(*An alternative implementation for comparison on efficiency. *)
Definition one_cu_sub (x:var) (n:nat) (c:posi) (M:nat -> bool) := CU c (rz_sub x n M).
Definition rz_modadder_alt (c1:posi) (x:var) (n:nat) (c:posi) (A:nat -> bool) (M:nat -> bool) :=
(one_cu_adder x n c1 A; (rz_sub x n M)) ; qft_cu x c n; (one_cu_adder x n c M; one_cu_sub x n c1 A)
; qft_acu x c n; ( (one_cu_adder x n c1 A)).
Fixpoint rz_modmult_alt' (y:var) (x:var) (n:nat) (size:nat) (c:posi) (A:nat) (M:nat) :=
match n with
| 0 => (SKIP (y,0))
| S m => rz_modmult_alt' y x m size c A M;
rz_modadder_alt (x,size-n) y size c (nat2fb ((2^m * A) mod M)) (nat2fb M)
end.
Definition rz_modmult_half_alt y x size c A M :=
QFT y size; rz_modmult_alt' y x size size c A M ; RQFT y size.
Definition rz_modmult_full_alt (y:var) (x:var) (n:nat) (c:posi) (A:nat) (Ainv :nat) (N:nat) :=
rz_modmult_half_alt y x n c A N ; inv_exp (rz_modmult_half_alt x y n c Ainv N).
Definition real_rz_modmult_rev_alt (M C Cinv size:nat) :=
rz_modmult_full_alt y_var x_var size (z_var,0) C Cinv M.
Definition trans_rz_modmult_rev_alt (M C Cinv size:nat) :=
trans_exp (vars_for_rz size) (2*size+1) (real_rz_modmult_rev_alt M C Cinv size) (avs_for_arith size).
(*********** Proofs ***********)
Lemma rz_adder_well_typed : forall n x size M aenv tenv, n <= size -> size = aenv x ->
Env.MapsTo x (Phi (aenv x)) tenv -> well_typed_oexp aenv tenv (rz_adder' x n size M) tenv.
Proof.
induction n; intros; simpl.
constructor. constructor.
apply pe_seq with (env' := tenv).
apply IHn; try easy. lia.
destruct (M n).
constructor.
apply sr_phi with (b := aenv x). easy.
subst. lia.
constructor. constructor.
Qed.
Lemma rz_adder_phi_modes : forall n x size M f aenv tenv, n <= size -> size = aenv x ->
Env.MapsTo x (Phi (aenv x)) tenv -> right_mode_env aenv tenv f ->
phi_modes (exp_sem aenv (rz_adder' x n size M) f) x size.
Proof.
induction n; intros.
simpl in *. rewrite H0. apply type_phi_modes with (env := tenv) (b := aenv x); easy.
simpl.
destruct (M n).
apply sr_phi_modes.
apply IHn with (tenv := tenv); try easy. lia.
simpl.
apply IHn with (tenv := tenv); try easy. lia.
Qed.
Lemma rz_adder_gt : forall n size aenv M f x, n <= size ->
(forall i, i >= size -> get_r_qft f x i = false) ->
(forall i, i >= size -> get_r_qft (exp_sem aenv (rz_adder' x n size M) f) x i = false).
Proof.
induction n; intros; simpl.
unfold get_r_qft in *.
destruct (f (x,0)). easy. rewrite H0. easy. easy.
destruct (M n). simpl.
unfold sr_rotate.
rewrite sr_rotate_get_r ; try lia.
unfold get_r_qft in IHn.
destruct ((exp_sem aenv (rz_adder' x n size M) f (x, 0))) eqn:eq1.
unfold get_phi_r.
unfold times_rotate. destruct b. easy. easy.
unfold get_phi_r.
unfold times_rotate.
unfold get_phi_r.
unfold times_rotate.
specialize (IHn size aenv M f x).
assert (n <= size) by lia.
specialize (IHn H2).
rewrite eq1 in IHn.
specialize (IHn H0).
assert ((S (size - S n)) = size - n) by lia.
rewrite H3.
unfold rotate,addto.
bdestruct (i <? size - n). lia. rewrite IHn. easy. lia.
simpl. apply IHn. lia. apply H0. lia.
Qed.
Lemma rz_adder_sem : forall n size f x A M aenv tenv, n <= size -> size = aenv x ->
Env.MapsTo x (Phi (aenv x)) tenv -> right_mode_env aenv tenv f ->
M < 2^size -> A < 2^size ->
fbrev size (get_r_qft f x) = nat2fb A ->
(get_r_qft (exp_sem aenv (rz_adder' x n size (nat2fb M)) f) x)
= (fbrev size (nat2fb ((A + (bindecomp n M)) mod 2^size))).
Proof.
induction n;intros;simpl.
unfold bindecomp. simpl.
rewrite plus_0_r.
rewrite Nat.mod_small by lia.
rewrite <- H5.
rewrite fbrev_twice_same. easy.
destruct (nat2fb M n) eqn:eq1.
simpl.
unfold sr_rotate.
rewrite sr_rotate_get_r by lia.
unfold get_phi_r.
specialize (IHn size f x A M aenv tenv).
assert (n <= size) by lia.
specialize (IHn H6 H0 H1 H2 H3 H4 H5).
unfold get_r_qft in IHn.
specialize (rz_adder_phi_modes n x size (nat2fb M) f aenv tenv H6 H0 H1 H2) as eq3.
unfold phi_modes in eq3. assert (0 < size) by lia. specialize (eq3 0 H7).
unfold phi_mode in eq3.
specialize (rz_adder_gt n size aenv (nat2fb M) f x) as X1.
assert (n <= size) by lia.
specialize (X1 H8).
assert ((forall i : nat, i >= size -> get_r_qft f x i = false)).
intros.
specialize (nat2fb_bound size A H4 i H9) as X2.
rewrite <- H5 in X2.
unfold fbrev in X2. bdestruct (i <? size). lia.
easy. specialize (X1 H9).
unfold get_r_qft in X1.
destruct (exp_sem aenv (rz_adder' x n size (nat2fb M)) f (@pair var nat x O)) eqn:eq2.
lia.
unfold times_rotate,rotate.
rewrite (add_to_sem size); (try easy; try lia).
rewrite cut_n_fbrev_flip.
rewrite IHn. rewrite fbrev_twice_same.
rewrite sumfb_correct_carry0.
assert ((size - S (size - S n)) = n) by lia.
rewrite H10.
rewrite bindecomp_seq. rewrite eq1. simpl.
rewrite plus_0_r.
rewrite cut_n_mod.
assert (2 ^ n = 2 ^ n mod 2 ^ size).
rewrite Nat.mod_small. easy.
apply Nat.pow_lt_mono_r; try lia.
assert (((A + bindecomp n M) mod 2 ^ size + 2 ^ n)
= ((A + bindecomp n M) mod 2 ^ size + 2 ^ n mod 2^size)).
rewrite <- H11. easy. rewrite H12.
rewrite <- Nat.add_mod by lia.
rewrite plus_assoc. easy.
simpl.
rewrite IHn with (A := A) (tenv:=tenv); try easy.
rewrite bindecomp_seq.
rewrite eq1. simpl. rewrite plus_0_r. easy. lia.
Qed.
Lemma rz_adder_full : forall size f x A M aenv tenv, size = aenv x ->
Env.MapsTo x (Phi (aenv x)) tenv -> right_mode_env aenv tenv f ->
M < 2^size -> A < 2^size ->
fbrev size (get_r_qft f x) = nat2fb A ->
(get_r_qft (exp_sem aenv (rz_adder x size (nat2fb M)) f) x)
= (fbrev size (nat2fb ((A + M) mod 2^size))).
Proof.
intros. unfold rz_adder. rewrite rz_adder_sem with (A:=A) (tenv := tenv); try easy.
rewrite bindecomp_spec.
rewrite (Nat.mod_small M) by easy. easy.
Qed.
Lemma rz_sub_well_typed : forall n x size M aenv tenv, n <= size -> size = aenv x ->
Env.MapsTo x (Phi (aenv x)) tenv -> well_typed_oexp aenv tenv (rz_sub' x n size M) tenv.
Proof.
induction n; intros; simpl.
constructor. constructor.
apply pe_seq with (env' := tenv).
apply IHn; try easy. lia.
destruct (M n).
constructor.
apply srr_phi with (b := aenv x). easy.
subst. lia.
constructor. constructor.
Qed.
Lemma rz_sub_phi_modes : forall n x size M f aenv tenv, n <= size -> size = aenv x ->
Env.MapsTo x (Phi (aenv x)) tenv -> right_mode_env aenv tenv f ->
phi_modes (exp_sem aenv (rz_sub' x n size M) f) x size.
Proof.
induction n; intros.
simpl in *. rewrite H0. apply type_phi_modes with (env := tenv) (b := aenv x); easy.
simpl.
destruct (M n).
apply srr_phi_modes.
apply IHn with (tenv := tenv); try easy. lia.
simpl.
apply IHn with (tenv := tenv); try easy. lia.
Qed.
Lemma rz_sub_gt : forall n size aenv M f x, n <= size ->
(forall i, i >= size -> get_r_qft f x i = false) ->
(forall i, i >= size -> get_r_qft (exp_sem aenv (rz_sub' x n size M) f) x i = false).
Proof.
induction n; intros; simpl.
unfold get_r_qft in *.
destruct (f (x,0)). easy. rewrite H0. easy. easy.
destruct (M n). simpl.
unfold srr_rotate.
rewrite srr_rotate_get_r ; try lia.
unfold get_r_qft in IHn.
destruct ((exp_sem aenv (rz_sub' x n size M) f (x, 0))) eqn:eq1.
unfold get_phi_r.
unfold times_r_rotate. destruct b. easy. easy.
unfold get_phi_r.
unfold times_r_rotate.
unfold get_phi_r.
unfold times_r_rotate.
specialize (IHn size aenv M f x).
assert (n <= size) by lia.
specialize (IHn H2).
rewrite eq1 in IHn.
specialize (IHn H0).
assert ((S (size - S n)) = size - n) by lia.
rewrite H3.
unfold r_rotate,addto_n.
bdestruct (i <? size - n). lia. rewrite IHn. easy. lia.
simpl. apply IHn. lia. apply H0. lia.
Qed.
Lemma rz_sub_sem : forall n size f x A M aenv tenv, n <= size -> size = aenv x ->
Env.MapsTo x (Phi (aenv x)) tenv -> right_mode_env aenv tenv f ->
M < 2^size -> A < 2^size ->
fbrev size (get_r_qft f x) = nat2fb A ->
(get_r_qft (exp_sem aenv (rz_sub' x n size (nat2fb M)) f) x)
= (fbrev size (nat2fb ((A + 2^size - (bindecomp n M)) mod 2^size))).
Proof.
induction n;intros;simpl.
unfold bindecomp. simpl.
assert ((A + 2 ^ size - 0) = A + 2^size) by lia. rewrite H6.
rewrite Nat.add_mod by lia.
rewrite Nat.mod_same by lia.
rewrite plus_0_r.
rewrite Nat.mod_mod by lia.
rewrite Nat.mod_small by lia.
rewrite <- H5.
rewrite fbrev_twice_same. easy.
destruct (nat2fb M n) eqn:eq1.
simpl.
unfold srr_rotate.
rewrite srr_rotate_get_r by lia.
unfold get_phi_r.
specialize (IHn size f x A M aenv tenv).
assert (n <= size) by lia.
specialize (IHn H6 H0 H1 H2 H3 H4 H5).
unfold get_r_qft in IHn.
specialize (rz_sub_phi_modes n x size (nat2fb M) f aenv tenv H6 H0 H1 H2) as eq3.
unfold phi_modes in eq3. assert (0 < size) by lia. specialize (eq3 0 H7).
unfold phi_mode in eq3.
specialize (rz_sub_gt n size aenv (nat2fb M) f x) as X1.
assert (n <= size) by lia.
specialize (X1 H8).
assert ((forall i : nat, i >= size -> get_r_qft f x i = false)).
intros.
specialize (nat2fb_bound size A H4 i H9) as X2.
rewrite <- H5 in X2.
unfold fbrev in X2. bdestruct (i <? size). lia.
easy. specialize (X1 H9).
unfold get_r_qft in X1.
destruct (exp_sem aenv (rz_sub' x n size (nat2fb M)) f (@pair var nat x O)) eqn:eq2. lia.
unfold times_r_rotate,r_rotate.
rewrite (add_to_n_sem size); (try easy; try lia).
rewrite cut_n_fbrev_flip.
rewrite IHn. rewrite fbrev_twice_same.
rewrite sumfb_correct_carry0.
assert ((size - S (size - S n)) = n) by lia.
rewrite H10.
rewrite bindecomp_seq. rewrite eq1. simpl.
rewrite plus_0_r.
rewrite cut_n_mod.
assert (2^n < 2^size).
apply Nat.pow_lt_mono_r_iff. lia. lia.
assert (2 ^ n <> 0).
apply Nat.pow_nonzero. lia.
assert ((2 ^ size - 2 ^ n) = (2 ^ size - 2 ^ n) mod 2 ^ size).
rewrite Nat.mod_small. easy. lia.
rewrite H13.
rewrite <- Nat.add_mod by lia.
assert (bindecomp n M < 2^n).
rewrite bindecomp_spec.
apply Nat.mod_upper_bound ; lia.
assert (2 ^ S n <= 2^size).
apply Nat.pow_le_mono_r; lia.
simpl in H15.
assert (A + 2 ^ size - bindecomp n M + (2 ^ size - 2 ^ n) =
(2 ^ size + (A + 2 ^ size - (bindecomp n M + 2 ^ n)))) by lia.
rewrite H16.
rewrite Nat.add_mod by lia.
rewrite Nat.mod_same by lia. rewrite plus_0_l.
rewrite Nat.mod_mod by lia. easy.
simpl.
rewrite IHn with (A := A) (tenv:=tenv); try easy.
rewrite bindecomp_seq.
rewrite eq1. simpl. rewrite plus_0_r. easy. lia.
Qed.
Lemma rz_sub_full : forall size f x A M aenv tenv, size = aenv x ->
Env.MapsTo x (Phi (aenv x)) tenv -> right_mode_env aenv tenv f ->
M < 2^size -> A < 2^size ->
fbrev size (get_r_qft f x) = nat2fb A ->
(get_r_qft (exp_sem aenv (rz_sub x size (nat2fb M)) f) x)
= (fbrev size (nat2fb ((A + 2^size - M) mod 2^size))).
Proof.
intros. unfold rz_sub. rewrite rz_sub_sem with (A:=A) (tenv := tenv); try easy.
rewrite bindecomp_spec.
rewrite (Nat.mod_small M) by easy. easy.
Qed.
Lemma efresh_rz_adder: forall n c x size M aenv, fst c <> x -> n <= size -> exp_fresh aenv c (rz_adder' x n size M).
Proof.
induction n;intros; simpl.
constructor. destruct c. iner_p.
constructor. apply IHn. easy. lia.
destruct (M n).
constructor.
unfold or_not_r. left. easy.
constructor. destruct c. iner_p.
Qed.
Lemma efresh_rz_sub: forall n c x size M aenv, fst c <> x -> n <= size -> exp_fresh aenv c (rz_sub' x n size M).
Proof.
induction n;intros; simpl.
constructor. destruct c. iner_p.
constructor. apply IHn. easy. lia.
destruct (M n).
constructor.
unfold or_not_r. left. easy.
constructor. destruct c. iner_p.
Qed.
Lemma exp_WF_rz_adder: forall n x size M aenv, n <= size <= aenv x -> 0 < aenv x -> exp_WF aenv (rz_adder' x n size M).
Proof.
induction n;intros; simpl.
constructor. simpl. lia.
constructor. apply IHn. lia. easy.
destruct (M n).
constructor. lia. constructor. simpl;lia.
Qed.
Lemma exp_WF_rz_sub: forall n x size M aenv, n <= size <= aenv x -> 0 < aenv x -> exp_WF aenv (rz_sub' x n size M).
Proof.
induction n;intros; simpl.
constructor. simpl. lia.
constructor. apply IHn. lia. easy.
destruct (M n).
constructor. lia. constructor. simpl;lia.
Qed.
Lemma exp_WF_clean_hbit: forall n x c M aenv, n <= aenv x -> 0 < aenv x -> snd c < aenv (fst c)
-> exp_WF aenv (clean_hbit x n c M).
Proof.
intros. constructor.
constructor. apply exp_WF_rz_sub; try lia.
constructor. constructor. constructor; lia.
constructor. constructor. constructor. simpl;lia.
Local Transparent CNOT. constructor. simpl; lia.
constructor. simpl; lia.
Local Opaque CNOT.
constructor. simpl;lia.
constructor. simpl;lia.
lia.
apply exp_WF_inv.
apply exp_WF_rz_sub; try lia.
Qed.
Lemma exp_WF_mod_adder: forall n x c A M aenv, n <= aenv x -> 0 < aenv x -> snd c < aenv (fst c)
-> exp_WF aenv (mod_adder_half x n c A M).
Proof.
intros. unfold mod_adder_half.
unfold rz_adder,rz_sub,qft_cu in *. simpl.
constructor. constructor. constructor. simpl.
apply exp_WF_rz_adder; try lia.
apply exp_WF_rz_sub; try lia.
constructor. constructor. constructor. simpl;easy. lia.
Local Transparent CNOT. constructor. simpl; lia.
constructor. simpl; lia.
Local Opaque CNOT.
constructor. simpl. lia. lia.
constructor. easy.
apply exp_WF_rz_adder; try lia.
Qed.
Lemma exp_WF_rz_modmult' : forall n size x y c A M aenv, n <= size <= aenv x -> 0 < aenv x
-> aenv x = aenv y -> 0 < aenv y -> snd c < aenv (fst c) -> exp_WF aenv (rz_modmult' y x n size c A M).
Proof.
induction n;intros;simpl.
constructor. simpl;lia.
constructor. apply IHn;try lia.
constructor. simpl;lia.
unfold mod_adder.
constructor.
apply exp_WF_mod_adder; try lia.
apply exp_WF_clean_hbit;try lia.
Qed.
Lemma rz_compare_half_well_typed : forall x c size M aenv tenv,
x <> fst c -> size = aenv x ->
Env.MapsTo (fst c) Nor tenv -> Env.MapsTo x (Phi (aenv x)) tenv ->
well_typed_oexp aenv tenv (rz_compare_half x size c M) (Env.add x Nor tenv).
Proof.
intros. unfold rz_compare_half.
apply pe_seq with (env' := (Env.add x Nor tenv)).
apply pe_seq with (env' := tenv).
apply rz_sub_well_typed; try easy.
apply rqft_phi. lia. subst. easy. easy.
apply cnot_wt_nor. iner_p.
simpl. apply Env.add_1. easy.
simpl. apply Env.add_2. easy. easy.
Qed.
Lemma rz_compare_half_sem : forall size f c x A M aenv tenv,
aenv x = S size -> fst c <> x -> Env.MapsTo x (Phi (aenv x)) tenv ->
Env.MapsTo (fst c) Nor tenv -> get_cua (f c) = false ->
right_mode_env aenv tenv f -> snd c < aenv (fst c)
-> M < 2^size -> A < 2^S size -> A < 2*M ->
fbrev (S size) (get_r_qft f x) = nat2fb A ->
get_cua ((exp_sem aenv (rz_compare_half x (S size) c M) f) c) = (A <? M).
Proof.
intros. unfold rz_compare_half.
assert (well_typed_oexp aenv tenv (rz_sub x (S size) (nat2fb M)) tenv) as X1.
apply rz_sub_well_typed; try easy.
assert (well_typed_oexp aenv tenv (RQFT x (S size)) (Env.add x Nor tenv)) as X2.
apply rqft_phi; try easy.
assert (well_typed_oexp aenv (Env.add x Nor tenv) (CNOT (@pair var nat x O) c) (Env.add x Nor tenv)) as X3.
apply cnot_wt_nor. destruct c. iner_p.
simpl. apply Env.add_1. easy.
apply Env.add_2. iner_p. easy. lia. rewrite <- H. easy.
assert (nor_mode f c) as X4.
apply type_nor_mode with (aenv := aenv) (env := tenv); try easy.
remember (rz_sub x (S size) (nat2fb M)) as e1. simpl.
rewrite Heqe1 in *. clear Heqe1.
unfold turn_rqft.
rewrite rz_sub_full with (A:=A) (tenv:= tenv); try easy.
unfold rz_compare_half in X1.
rewrite cnot_sem.
rewrite eupdate_index_eq.
rewrite get_put_cu.
rewrite assign_h_r_lt_same by lia.
rewrite assign_seq_lt by lia.
rewrite assign_h_r_out by iner_p.
rewrite assign_seq_out by iner_p.
rewrite efresh_exp_sem_irrelevant with (p := c).
rewrite H3.
unfold get_cua. bt_simpl.
unfold fbrev. bdestruct (0 <? S size). simpl.
assert ((size - 0 - 0) = size) by lia. rewrite H11.
rewrite <- highbit_means_lt with (size := size); try easy.
unfold fbrev.
bdestruct (0 <? S size). simpl.
rewrite H11. easy. lia. lia.
apply exp_WF_rz_sub;try lia.
apply efresh_rz_sub; try easy.
unfold nor_mode.
rewrite assign_h_r_out by iner_p.
rewrite assign_seq_out by iner_p.
rewrite efresh_exp_sem_irrelevant with (p := c).
apply X4.
apply exp_WF_rz_sub;try lia.
apply efresh_rz_sub; try easy.
unfold nor_mode.
rewrite assign_h_r_lt_same by lia.
rewrite assign_seq_lt by lia. lia.
unfold nor_mode.
rewrite assign_h_r_out by iner_p.
rewrite assign_seq_out by iner_p.
rewrite efresh_exp_sem_irrelevant with (p := c).
apply X4.
apply exp_WF_rz_sub;try lia.
apply efresh_rz_sub; try easy.
simpl. lia.
Qed.
Lemma rz_compare_sem : forall size f c x A M aenv tenv,
aenv x = S size -> fst c <> x -> Env.MapsTo x (Phi (aenv x)) tenv ->
Env.MapsTo (fst c) Nor tenv -> get_cua (f c) = false -> snd c < aenv (fst c)
-> M < 2^size -> A < 2^S size -> A < 2*M -> get_cua (f c) = false
-> right_mode_env aenv tenv f -> qft_uniform aenv tenv f -> qft_gt aenv tenv f
-> fbrev (S size) (get_r_qft f x) = nat2fb A -> at_match aenv tenv ->
exp_sem aenv (rz_compare x (S size) c M) f = f[c|-> put_cu (f c) (A <? M)].
Proof.
intros. unfold rz_compare. unfold rz_compare_half in *.
assert (well_typed_oexp aenv tenv (rz_sub x (S size) (nat2fb M)) tenv) as X1.
apply rz_sub_well_typed; try easy.
assert (well_typed_oexp aenv tenv (RQFT x (S size)) (Env.add x Nor tenv)) as X2.
apply rqft_phi; try easy.
assert (well_typed_oexp aenv (Env.add x Nor tenv) (CNOT (@pair var nat x O) c) (Env.add x Nor tenv)) as X3.
apply cnot_wt_nor. destruct c. iner_p.
simpl. apply Env.add_1. easy.
apply Env.add_2. iner_p. easy. lia. rewrite <- H. easy.
assert (nor_mode f c) as X4.
apply type_nor_mode with (aenv := aenv) (env := tenv); try easy.
remember (rz_sub x (S size) (nat2fb M); RQFT x (S size)) as e1.
remember (exp_sem aenv e1 f) as g.
simpl.
rewrite <- Heqg.
rewrite cnot_sem.
rewrite inv_pexp_reverse_1 with (tenv:= tenv) (tenv' := (Env.add x Nor tenv)) (f:=f); try easy.
rewrite Heqg.
rewrite Heqe1 in *.
remember (rz_sub x (S size) (nat2fb M)) as e2. simpl in *.
unfold turn_rqft. rewrite Heqe2 in *.
rewrite rz_sub_full with (A:=A) (tenv:= tenv); try easy.
rewrite assign_h_r_lt_same by lia.
rewrite assign_seq_lt by lia.
rewrite assign_h_r_out by iner_p.
rewrite assign_seq_out by iner_p.
rewrite efresh_exp_sem_irrelevant with (p := c).
rewrite H3.
unfold get_cua. bt_simpl.
unfold fbrev. bdestruct (0 <? S size). simpl.
assert ((size - 0 - 0) = size) by lia. rewrite H15.
assert ((nat2fb ((A + (2 ^ size + (2 ^ size + 0)) - M)
mod (2 ^ size + (2 ^ size + 0))) size) = (A <? M)).
unfold nat2fb.
rewrite N2fb_Ntestbit.
bdestruct (A <? M).
apply Ntestbit_in_pow2n_pow2Sn.
assert ((2 ^ size + (2 ^ size + 0)) = 2^ S size). simpl. easy.
rewrite H17.
split.
assert (2^size <= (A + 2 ^ S size - M) mod 2 ^ S size).
assert ((A + 2 ^ S size - M) = 2^S size - (M - A)) by lia.
rewrite H18.
assert ((2 ^ S size - (M - A)) < 2 ^ S size) by lia.
rewrite Nat.mod_small by lia.
assert (M - A < 2^size) by lia. lia.
assert (N.of_nat(2 ^ size) <= N.of_nat ((A + 2 ^ S size - M) mod 2 ^ S size))%N by lia.
simpl in *. rewrite Nofnat_pow in H19. simpl in H19. lia.
assert ((A + 2 ^ S size - M) mod 2 ^ S size < 2 ^ S size).
apply Nat.mod_upper_bound. lia.
assert (N.of_nat ((A + 2 ^ S size - M) mod 2 ^ S size) < N.of_nat (2 ^ S size))%N by lia.
rewrite Nofnat_pow in H19.
assert (N.of_nat (S size) = N.succ (N.of_nat size)) by lia.
rewrite H20 in H19. simpl in *. lia.
apply Ntestbit_lt_pow2n.
assert ((2 ^ size + (2 ^ size + 0)) = 2^ S size). simpl. easy.
rewrite H17. clear H17.
assert ((A + 2 ^ S size - M) mod 2 ^ S size < 2 ^ size).
assert ((A + 2 ^ S size - M) = 2 ^ S size + (A - M)) by lia.
rewrite H17. clear H17.
assert (2^ size <> 0).
apply Nat.pow_nonzero. lia.
rewrite Nat.add_mod by (simpl;lia).
rewrite Nat.mod_same by (simpl;lia).
rewrite plus_0_l.
rewrite Nat.mod_mod by (simpl;lia).
rewrite Nat.mod_small by (simpl;lia).
simpl. lia.
assert (N.of_nat ((A + 2 ^ S size - M) mod 2 ^ S size) < N.of_nat (2 ^ size))%N by lia.
rewrite Nofnat_pow in H18.
simpl in *. lia. rewrite H16. easy. lia.
apply exp_WF_rz_sub;try lia.
apply efresh_rz_sub; try easy. simpl. lia.
subst.
apply pe_seq with (env' := tenv).
easy. easy.
subst.
constructor.
apply efresh_rz_sub; try easy.
constructor. unfold or_not_eq. left. easy.
subst. constructor.
apply exp_WF_rz_sub;try lia.
constructor. lia. lia.
subst. simpl. unfold turn_rqft.
unfold nor_mode.
rewrite assign_h_r_lt_same by lia.
rewrite assign_seq_lt. lia. lia.
subst.
unfold nor_mode.
rewrite efresh_exp_sem_irrelevant with (p := c).
apply X4.
constructor.
apply exp_WF_rz_sub;try lia. constructor. lia. lia.
constructor.
apply efresh_rz_sub; try easy.
constructor. unfold or_not_eq. left. easy.
Qed.
(*phi_mode proofs.*)
Inductive exp_scope (aenv: var -> nat) : var -> nat -> exp -> Prop :=
| skip_scope : forall x n p, exp_scope aenv x n (SKIP p)
| x_scope : forall x n p, exp_scope aenv x n (X p)
| sr_scope : forall x n y m, exp_scope aenv x n (SR m y)
| srr_scope : forall x n y m, exp_scope aenv x n (SRR m y)
| lshift_scope_hit : forall x n, 0 < aenv x <= n -> exp_scope aenv x n (Lshift x)
| lshift_scope_nhit : forall x n y, x <> y -> exp_scope aenv x n (Lshift y)
| rshift_scope_hit : forall x n, 0 < aenv x <= n -> exp_scope aenv x n (Rshift x)
| rshift_scope_nhit : forall x n y, x <> y -> exp_scope aenv x n (Rshift y)
| rev_scope_hit : forall x n, 0 < aenv x <= n -> exp_scope aenv x n (Rev x)
| rev_scope_nhit : forall x n y, x <> y -> exp_scope aenv x n (Rev y)
| cu_scope : forall x n p e, exp_scope aenv x n e -> exp_scope aenv x n (CU p e)
(* | hcnot_scope : forall x n p1 p2, exp_scope aenv x n (HCNOT p1 p2) *)
| rz_scope : forall x n q p, exp_scope aenv x n (RZ q p)
| rrz_scope : forall x n q p, exp_scope aenv x n (RRZ q p)
| seq_scope : forall x n e1 e2, exp_scope aenv x n e1 -> exp_scope aenv x n e2 -> exp_scope aenv x n (Seq e1 e2).
Lemma escope_rz_adder: forall m x size M y n aenv, exp_scope aenv y n (rz_adder' x m size M).
Proof.
induction m;intros; simpl. constructor.
constructor. apply IHm.
destruct (M m). constructor. constructor.
Qed.
Lemma escope_rz_sub: forall m x size M y n aenv, exp_scope aenv y n (rz_sub' x m size M).
Proof.
induction m;intros; simpl. constructor.
constructor. apply IHm.
destruct (M m). constructor. constructor.
Qed.
Lemma escope_inv : forall e y n aenv,
exp_scope aenv y n e -> exp_scope aenv y n (inv_exp e).
Proof.
induction e;intros; simpl.
constructor. constructor. constructor.
inv H. apply IHe. easy.
1-4:constructor.
inv H. constructor. easy.
apply rshift_scope_nhit. easy.
inv H. constructor. easy.
apply lshift_scope_nhit. easy.
inv H. constructor. easy.
apply rev_scope_nhit. easy.
inv H. inv H. inv H.
constructor.
apply IHe2. easy. apply IHe1. easy.
Qed.
Lemma sr_rotate'_phi_modes : forall n size f x y m, phi_modes f y m -> phi_modes (sr_rotate' f x n size) y m.
Proof.
induction n;intros;simpl. easy.
unfold phi_modes in *.
intros.
unfold phi_mode in *.
bdestruct ((y,i) ==? (x,n)). rewrite H1.
rewrite eupdate_index_eq.
unfold times_rotate.
specialize (H n). inv H1. apply H in H0.
destruct (f (x,n)); try lia.
rewrite eupdate_index_neq by iner_p.
apply IHn with (m := m). easy. lia.
Qed.
Lemma srr_rotate'_phi_modes : forall n size f x y m, phi_modes f y m -> phi_modes (srr_rotate' f x n size) y m.
Proof.
induction n;intros;simpl. easy.
unfold phi_modes in *.
intros.
unfold phi_mode in *.
bdestruct ((y,i) ==? (x,n)). rewrite H1.
rewrite eupdate_index_eq.
unfold times_r_rotate.
specialize (H n). inv H1. apply H in H0.
destruct (f (x,n)); try lia.
rewrite eupdate_index_neq by iner_p.
apply IHn with (m := m). easy. lia.
Qed.
Lemma lshift'_phi_modes : forall n size f x y m, size < m -> phi_modes f y m -> phi_modes (lshift' n size f x) y m.
Proof.
induction n;intros;simpl.
unfold phi_modes in *.
intros.
unfold phi_mode in *.
bdestruct ((y,i) ==? (x,0)).
rewrite H2.
rewrite eupdate_index_eq.
specialize (H0 size). apply H0 in H. inv H2. easy.
rewrite eupdate_index_neq by iner_p.
apply H0. lia.
unfold phi_modes in *.
intros.
unfold phi_mode in *.
bdestruct ((y, i) ==? (x, S n)). inv H2.
rewrite eupdate_index_eq. apply H0. lia.
rewrite eupdate_index_neq by iner_p. apply IHn with (m := m). lia.
easy. lia.
Qed.
Lemma rshift'_phi_modes : forall n size f x y m, n <= size < m -> phi_modes f y m -> phi_modes (rshift' n size f x) y m.
Proof.
induction n;intros;simpl.
unfold phi_modes in *.
intros.
unfold phi_mode in *.
bdestruct ((y,i) ==? (x,size)).
rewrite H2.
rewrite eupdate_index_eq.
specialize (H0 0).
assert (0 < m) by lia. apply H0 in H3. inv H2. easy.
rewrite eupdate_index_neq by iner_p.
apply H0. lia.
unfold phi_modes in *.
intros.
unfold phi_mode in *.
bdestruct ((y, i) ==? (x, n)). inv H2.
rewrite eupdate_index_eq. apply H0. lia.
rewrite eupdate_index_neq by iner_p. apply IHn with (m := m). lia.
easy. lia.
Qed.
Lemma phi_modes_exp : forall e aenv f x size, exp_scope aenv x size e
-> phi_modes f x size -> phi_modes (exp_sem aenv e f) x size.
Proof.
induction e; intros.
simpl; easy.
simpl.
unfold phi_modes in *.
unfold phi_mode in *. intros.
bdestruct (p ==? (x, i)).
subst.
rewrite eupdate_index_eq.
unfold exchange.
specialize (H0 i H1).
destruct (f (x, i)); try lia.
rewrite eupdate_index_neq by easy. apply H0. easy.
simpl.
destruct (get_cua (f p)). apply IHe. inv H. easy. easy. easy.
simpl.
unfold phi_modes in *.
unfold phi_mode in *. intros.
bdestruct (p ==? (x, i)).
subst.
rewrite eupdate_index_eq.
unfold times_rotate.
specialize (H0 i H1).
destruct (f (x, i)); try lia.
rewrite eupdate_index_neq by easy. apply H0. easy.
simpl.
unfold phi_modes in *.
unfold phi_mode in *. intros.
bdestruct (p ==? (x, i)).
subst.
rewrite eupdate_index_eq.
unfold times_r_rotate.
specialize (H0 i H1).
destruct (f (x, i)); try lia.
rewrite eupdate_index_neq by easy. apply H0. easy.
simpl.
apply sr_rotate'_phi_modes. easy.
simpl.
apply srr_rotate'_phi_modes. easy.
simpl.
unfold lshift.
bdestruct (x0 =? x). subst.
apply lshift'_phi_modes. inv H. lia. lia.
easy.
unfold phi_modes in *.
unfold phi_mode in *. intros.
rewrite lshift'_irrelevant. apply H0; try lia. iner_p.
simpl.
unfold rshift.
bdestruct (x0 =? x). subst.
apply rshift'_phi_modes. inv H. lia. lia.
easy.
unfold phi_modes in *.
unfold phi_mode in *. intros.
rewrite rshift'_irrelevant. apply H0; try lia. iner_p.
simpl.
unfold reverse.
unfold phi_modes in *.
unfold phi_mode in *.
intros.
simpl.
bdestruct (x0 =? x).
bdestruct (i <? aenv x). simpl.
subst.
apply H0. inv H. lia. lia.
simpl. apply H0. lia. simpl. apply H0. lia.
simpl.
inv H. inv H. inv H.
specialize (IHe1 aenv f x size H5 H0).
specialize (IHe2 aenv (exp_sem aenv e1 f) x size H6 IHe1). easy.
Qed.
Lemma adder_sub_seq : forall size f x B A M aenv tenv,
size = aenv x -> Env.MapsTo x (Phi (aenv x)) tenv -> right_mode_env aenv tenv f ->
1 < M < 2^size -> A < 2^size -> B < 2^size ->
fbrev size (get_r_qft f x) = nat2fb B ->
(get_r_qft (exp_sem aenv (rz_adder x size (nat2fb A); (rz_sub x size (nat2fb M))) f) x)
= (fbrev size (nat2fb (((B + A) + 2^size - M) mod 2^size))).
Proof.
intros.
specialize (rz_adder_full size f x B A aenv tenv H H0 H1 H3 H4 H5) as eq1.
simpl.
assert (fbrev size (get_r_qft (exp_sem aenv (rz_adder x size (nat2fb A)) f) x)
= (nat2fb ((B + A) mod 2 ^ size))).
rewrite eq1. rewrite fbrev_twice_same. easy.
rewrite rz_sub_full with (A:= ((B + A) mod 2 ^ size)) (tenv:=tenv); try easy.
assert (2 ^ size - M = (2 ^ size - M) mod 2^size).
rewrite Nat.mod_small by lia. easy.
assert (((B + A) mod 2 ^ size + 2 ^ size - M) =
((B + A) mod 2 ^ size + (2 ^ size - M))) by lia.
rewrite H8. rewrite H7.
rewrite <- Nat.add_mod by lia.
assert ((B + A + (2 ^ size - M)) = ((B + A + 2 ^ size - M))) by lia.
rewrite H9. easy.
apply well_typed_right_mode_pexp with (tenv := tenv); try easy.
apply rz_adder_well_typed; try easy.
apply Nat.mod_upper_bound. lia.
Qed.
Lemma qft_cu_sem : forall tenv aenv f x c size,
aenv x = S size -> fst c <> x -> Env.MapsTo x (Phi (aenv x)) tenv ->
Env.MapsTo (fst c) Nor tenv -> snd c < aenv (fst c) ->
right_mode_env aenv tenv f -> qft_uniform aenv tenv f -> qft_gt aenv tenv f -> at_match aenv tenv ->
exp_sem aenv (qft_cu x c (S size)) f = f[c |-> put_cu (f c) ((get_r_qft f x 0) ⊕ get_cua (f c))].
Proof.
intros.
unfold qft_cu.
remember (RQFT x (S size)) as e.
assert (QFT x (S size) = inv_exp e). rewrite Heqe. simpl. easy.
rewrite H8. simpl.
rewrite cnot_sem.
rewrite efresh_exp_sem_out.
assert ((exp_sem aenv (inv_exp e) (exp_sem aenv e f))
= exp_sem aenv (e ; inv_exp e) f). simpl. easy.
rewrite H9.
rewrite inv_exp_correct_rev with (tenv := tenv) (tenv' := Env.add x Nor tenv); try easy.
apply eupdate_same_1. easy.
rewrite Heqe. simpl.
unfold turn_rqft,turn_qft.
rewrite assign_h_r_out; try iner_p.
rewrite assign_seq_out; try iner_p.
rewrite assign_h_r_lt_same; try lia.
rewrite assign_seq_lt; try lia.
assert (get_cua (nval (get_r_qft f x 0) (get_r (f (@pair var nat x O)))) = (get_r_qft f x 0)).
unfold get_cua. easy.
rewrite H10. easy.
rewrite Heqe.
apply rqft_phi. lia. rewrite <- H. easy.
easy. subst. constructor; lia.
rewrite Heqe. simpl.
constructor. unfold or_not_eq. left. easy.
subst. simpl. constructor; lia.
rewrite Heqe. simpl.
unfold turn_rqft.
unfold nor_mode.
rewrite assign_h_r_lt_same;try lia.
rewrite assign_seq_lt; try lia.
rewrite Heqe. simpl.
unfold nor_mode,turn_rqft.
assert (nor_mode f c).
apply type_nor_mode with (aenv := aenv) (env := tenv); try easy.
rewrite assign_h_r_out;try easy.
rewrite assign_seq_out; easy.
Qed.
Lemma qft_acu_sem : forall tenv aenv f x c size,
aenv x = S size -> fst c <> x -> Env.MapsTo x (Phi (aenv x)) tenv ->
Env.MapsTo (fst c) Nor tenv -> snd c < aenv (fst c) ->
right_mode_env aenv tenv f -> qft_uniform aenv tenv f -> qft_gt aenv tenv f -> at_match aenv tenv ->
exp_sem aenv (qft_acu x c (S size)) f = f[c |-> put_cu (f c) ((¬ (get_r_qft f x 0)) ⊕ get_cua (f c))].
Proof.
intros.
unfold qft_acu.
remember (RQFT x (S size)) as e.
assert (QFT x (S size) = inv_exp e). rewrite Heqe. simpl. easy.
rewrite H8. simpl.
rewrite cnot_sem.
destruct c.
rewrite eupdate_index_eq.
rewrite eupdate_twice_neq by iner_p.
rewrite eupdate_twice_eq.
rewrite eupdate_index_neq by iner_p.
rewrite eupdate_index_eq.
rewrite eupdate_index_neq by iner_p.
assert (((exp_sem aenv e f) [(@pair var nat x O)
|-> exchange (exchange (exp_sem aenv e f (@pair var nat x O)))]) = (exp_sem aenv e f)).
rewrite eupdate_same. easy.
unfold exchange.
destruct (exp_sem aenv e f (@pair var nat x O)) eqn:eq1.
assert ((¬ (¬ b)) = b) by btauto. rewrite H9. 1-2:easy.
rewrite H9.
rewrite efresh_exp_sem_out.
assert ((exp_sem aenv (inv_exp e) (exp_sem aenv e f))
= exp_sem aenv (e ; inv_exp e) f). simpl. easy.
rewrite H10.
rewrite inv_exp_correct_rev with (tenv := tenv) (tenv' := Env.add x Nor tenv); try easy.
apply eupdate_same_1. easy.
rewrite Heqe. simpl.
unfold turn_rqft,turn_qft.
rewrite assign_h_r_out;try iner_p.
rewrite assign_seq_out; try iner_p.
rewrite assign_h_r_lt_same;try lia.
rewrite assign_seq_lt; try lia.
unfold exchange. unfold get_cua. easy.
rewrite Heqe. simpl. constructor. lia. rewrite <- H. easy. easy.
subst. constructor; lia.
subst. simpl. constructor.
unfold or_not_eq. left. easy.
subst. simpl. constructor; lia.
unfold nor_mode. rewrite eupdate_index_eq.
rewrite Heqe. simpl.
unfold turn_rqft.