-
Notifications
You must be signed in to change notification settings - Fork 3
/
Estimate.v
executable file
·10642 lines (10061 loc) · 353 KB
/
Estimate.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 Classical_Prop.
Require Import OQASM.
(**********************)
(** Unitary Programs **)
(**********************)
Declare Scope exp_scope.
Delimit Scope exp_scope with exp.
Local Open Scope exp_scope.
Local Open Scope nat_scope.
Inductive upexp := Haf (e:exp) (* H; U *)
| Oracle (e:exp) | QPE (e:exp) | AA (e:exp).
(* add two types: restricted Hadamard mode, and QFT^{-1} mode. *)
(* Use restricted Hadmard mode to evaluate the difference between QFT and AQFT. *)
(* Use QFT^{-1} to show that AQFT is useful, but it is not useful in calculation. *)
(* irrelavent vars. *)
Definition vars_neq (l:list var) := forall n m x y,
nth_error l m = Some x -> nth_error l n = Some y -> n <> m -> x <> y.
Inductive exp := SKIP (p:posi) | X (p:posi) | CU (p:posi) (e:exp)
| RZ (q:nat) (p:posi) (* 2 * PI * i / 2^q *)
| RRZ (q:nat) (p:posi)
| SR (q:nat) (x:var) (* a series of RZ gates for QFT mode. *)
| SRR (q:nat) (x:var) (* a series of RRZ gates for QFT mode. *)
| HCNOT (p1:posi) (p2:posi)
| Lshift (x:var)
| Rshift (x:var)
| Rev (x:var)
| QFT (x:var)
| RQFT (x:var)
| H (x:var)
| Seq (s1:exp) (s2:exp).
Notation "p1 ; p2" := (Seq p1 p2) (at level 50) : exp_scope.
Fixpoint exp_elim (p:exp) :=
match p with
| CU q p => match exp_elim p with
| SKIP a => SKIP a
| p' => CU q p'
end
| Seq p1 p2 => match exp_elim p1, exp_elim p2 with
| SKIP _, p2' => p2'
| p1', SKIP _ => p1'
| p1', p2' => Seq p1' p2'
end
| _ => p
end.
Definition Z (p:posi) := RZ 1 p.
Fixpoint inv_exp p :=
match p with
| SKIP a => SKIP a
| X n => X n
| CU n p => CU n (inv_exp p)
| SR n x => SRR n x
| SRR n x => SR n x
| Lshift x => Rshift x
| Rshift x => Lshift x
| Rev x => Rev x
| HCNOT p1 p2 => HCNOT p1 p2
| RZ q p1 => RRZ q p1
| RRZ q p1 => RZ q p1
| QFT x => RQFT x
| RQFT x => QFT x
| H x => H x
| Seq p1 p2 => inv_exp p2; inv_exp p1
end.
Fixpoint GCCX' x n size :=
match n with
| O | S O => X (x,n - 1)
| S m => CU (x,size-n) (GCCX' x m size)
end.
Definition GCCX x n := GCCX' x n n.
Fixpoint nX x n :=
match n with 0 => X (x,0)
| S m => X (x,m); nX x m
end.
(* Grover diffusion operator. *)
Definition diff_half (x c:var) (n:nat) := H x ; H c ; ((nX x n; X (c,0))).
Definition diff_1 (x c :var) (n:nat) :=
diff_half x c n ; ((GCCX x n)) ; (inv_exp (diff_half x c n)).
(*The second implementation of grover's diffusion operator.
The whole circuit is a little different, and the input for the diff_2 circuit is asssumed to in Had mode. *)
Definition diff_2 (x c :var) (n:nat) :=
H x ; ((GCCX x n)) ; H x.
Fixpoint is_all_true C n :=
match n with 0 => true
| S m => C m && is_all_true C m
end.
Definition const_u (C :nat -> bool) (n:nat) c := if is_all_true C n then ((X (c,0))) else SKIP (c,0).
Fixpoint niter_prog n (c:var) (P : exp) : exp :=
match n with
| 0 => SKIP (c,0)
| 1 => P
| S n' => niter_prog n' c P ; P
end.
Definition body (C:nat -> bool) (x c:var) (n:nat) := const_u C n c; diff_2 x c n.
Definition grover_e (i:nat) (C:nat -> bool) (x c:var) (n:nat) :=
H x; H c ; ((Z (c,0))) ; niter_prog i c (body C x c n).
(** Definition of Deutsch-Jozsa program. **)
Definition deutsch_jozsa (x c:var) (n:nat) :=
((nX x n; X (c,0))) ; H x ; H c ; ((X (c,0))); H c ; H x.
Inductive type := Had | Phi (n:nat) | Nor.
Require Import Coq.FSets.FMapList.
Require Import Coq.FSets.FMapFacts.
Require Import Coq.Structures.OrderedTypeEx.
Module Env := FMapList.Make Nat_as_OT.
Module EnvFacts := FMapFacts.Facts (Env).
Definition env := Env.t type.
Definition empty_env := @Env.empty type.
Lemma mapsto_always_same : forall k v1 v2 s,
@Env.MapsTo (type) k v1 s ->
@Env.MapsTo (type) k v2 s ->
v1 = v2.
Proof.
intros.
apply Env.find_1 in H0.
apply Env.find_1 in H1.
rewrite H0 in H1.
injection H1.
easy.
Qed.
Lemma find_add : forall k v m,
@Env.find (type) k (Env.add k v m) = Some v.
Proof.
intros.
apply Env.find_1.
apply Env.add_1.
easy.
Qed.
Lemma mapsto_add1 : forall k v1 v2 s,
@Env.MapsTo (type) k v1 (Env.add k v2 s) -> v1 = v2.
Proof.
intros.
apply Env.find_1 in H0.
rewrite find_add in H0.
inversion H0.
reflexivity.
Qed.
Lemma mapsto_equal : forall k v s1 s2,
@Env.MapsTo type k v s1 ->
Env.Equal s1 s2 ->
Env.MapsTo k v s2.
Proof.
intros.
unfold Env.Equal in H1.
apply Env.find_2. rewrite <- H1.
apply Env.find_1.
assumption.
Qed.
Lemma map_find_add : forall x v env, @Env.find (type) x (Env.add x v env) = Some v.
Proof.
intros.
apply Env.find_1.
apply Env.add_1. easy.
Qed.
Lemma map_find_neq : forall x y v env, x <> y -> @Env.find (type) x (Env.add y v env) = Env.find x env.
Proof.
intros.
destruct (Env.find (elt:=type) x env0) eqn:eq1.
apply Env.find_1. apply Env.add_2. lia.
apply Env.find_2 in eq1. easy.
destruct (Env.find (elt:=type) x (Env.add y v env0)) eqn:eq2.
apply Env.find_2 in eq2. apply Env.add_3 in eq2.
apply Env.find_1 in eq2. rewrite eq1 in eq2. inv eq2. lia.
easy.
Qed.
Definition or_not_r (x y:var) (n1 n2:nat) := x <> y \/ n1 < n2.
Definition or_not_eq (x y:var) (n1 n2:nat) := x <> y \/ n1 <= n2.
Inductive exp_fresh (aenv:var->nat): posi -> exp -> Prop :=
| skip_fresh : forall p p1, p <> p1 -> exp_fresh aenv p (SKIP p1)
| x_fresh : forall p p' , p <> p' -> exp_fresh aenv p (X p')
| sr_fresh : forall p x n, or_not_r (fst p) x n (snd p) -> exp_fresh aenv p (SR n x)
| srr_fresh : forall p x n, or_not_r (fst p) x n (snd p) -> exp_fresh aenv p (SRR n x)
| lshift_fresh : forall p x, or_not_eq (fst p) x (aenv x) (snd p) -> exp_fresh aenv p (Lshift x)
| rshift_fresh : forall p x, or_not_eq (fst p) x (aenv x) (snd p) -> exp_fresh aenv p (Rshift x)
| rev_fresh : forall p x, or_not_eq (fst p) x (aenv x) (snd p) -> exp_fresh aenv p (Rev x)
| cu_fresh : forall p p' e, p <> p' -> exp_fresh aenv p e -> exp_fresh aenv p (CU p' e)
| cnot_fresh : forall p p1 p2, p <> p1 -> p <> p2 -> exp_fresh aenv p (HCNOT p1 p2)
| rz_fresh : forall p p' q, p <> p' -> exp_fresh aenv p (RZ q p')
| rrz_fresh : forall p p' q, p <> p' -> exp_fresh aenv p (RRZ q p')
| qft_fresh : forall p x , or_not_eq (fst p) x (aenv x) (snd p) -> exp_fresh aenv p (QFT x)
| rqft_fresh : forall p x , or_not_eq (fst p) x (aenv x) (snd p) -> exp_fresh aenv p (RQFT x)
| h_fresh : forall p x , or_not_eq (fst p) x (aenv x) (snd p) -> exp_fresh aenv p (H x)
| seq_fresh : forall p e1 e2, exp_fresh aenv p e1 -> exp_fresh aenv p e2 -> exp_fresh aenv p (Seq e1 e2).
(* Defining matching shifting stack. *)
Inductive sexp := Ls | Rs | Re.
Definition opp_ls (s : sexp) := match s with Ls => Rs | Rs => Ls | Re => Re end.
Definition fst_not_opp (s:sexp) (l : list sexp) :=
match l with [] => True
| (a::al) => s <> opp_ls a
end.
Inductive exp_neu_l (x:var) : list sexp -> exp -> list sexp -> Prop :=
| skip_neul : forall l p, exp_neu_l x l (SKIP p) l
| x_neul : forall l p, exp_neu_l x l (X p) l
| sr_neul : forall l y n, exp_neu_l x l (SR n y) l
| srr_neul : forall l y n, exp_neu_l x l (SRR n y) l
| cu_neul : forall l p e, exp_neu_l x [] e [] -> exp_neu_l x l (CU p e) l
| hcnot_neul : forall l p1 p2, exp_neu_l x l (HCNOT p1 p2) l
| rz_neul : forall l p q, exp_neu_l x l (RZ q p) l
| rrz_neul : forall l p q, exp_neu_l x l (RRZ q p) l
| qft_neul : forall l y, exp_neu_l x l (QFT y) l
| rqft_neul : forall l y , exp_neu_l x l (RQFT y) l
| h_neul : forall l y, exp_neu_l x l (H y) l
| lshift_neul_a : forall l, exp_neu_l x (Rs::l) (Lshift x) l
| lshift_neul_b : forall l, fst_not_opp Ls l -> exp_neu_l x l (Lshift x) (Ls::l)
| lshift_neul_ne : forall l y, x <> y -> exp_neu_l x l (Lshift y) l
| rshift_neul_a : forall l, exp_neu_l x (Ls::l) (Rshift x) l
| rshift_neul_b : forall l, fst_not_opp Rs l -> exp_neu_l x l (Rshift x) (Rs::l)
| rshift_neul_ne : forall l y, x <> y -> exp_neu_l x l (Rshift y) l
| rev_neul_a : forall l, exp_neu_l x (Re::l) (Rev x) l
| rev_neul_b : forall l, fst_not_opp Re l -> exp_neu_l x l (Rev x) (Re::l)
| rev_neul_ne : forall l y, x <> y -> exp_neu_l x l (Rev y) l
| seq_neul : forall l l' l'' e1 e2, exp_neu_l x l e1 l' -> exp_neu_l x l' e2 l'' -> exp_neu_l x l (Seq e1 e2) l''.
Definition exp_neu (xl : list var) (e:exp) : Prop :=
forall x, In x xl -> exp_neu_l x [] e [].
Definition exp_neu_prop (l:list sexp) :=
(forall i a, i + 1 < length l -> nth_error l i = Some a -> nth_error l (i+1) <> Some (opp_ls a)).
Lemma exp_neu_t_prop : forall p x l l', exp_neu_l x l p l' -> exp_neu_prop l -> exp_neu_prop l'.
Proof.
induction p; intros; try easy.
1-8:inv H0; easy.
unfold exp_neu_prop in *.
intros. inv H0.
destruct l'. simpl in *. lia.
destruct i. simpl in *.
destruct l'. easy.
specialize (H1 1 a).
assert (1 + 1 < S (S (length (s0 :: l')))) by lia.
apply H1 in H0. simpl in *. easy.
simpl in *. easy.
specialize (H1 (S (S i)) a).
assert (S (S i) + 1 < length (Rs :: s :: l')).
simpl in *. lia.
apply H1 in H0.
simpl in *. easy.
simpl in *. easy.
unfold fst_not_opp in H6. destruct l. simpl in *. lia.
destruct i. simpl in *. inv H3.
unfold opp_ls in *. intros R. inv R. easy.
specialize (H1 i a).
assert (i + 1 < length (s :: l)). simpl in *. lia.
apply H1 in H0. simpl in *. easy. simpl in *. easy.
apply H1; try easy.
unfold exp_neu_prop in *.
intros. inv H0.
destruct l'. simpl in *. lia.
destruct i. simpl in *.
destruct l'. easy.
specialize (H1 1 a).
assert (1 + 1 < S (S (length (s0 :: l')))) by lia.
apply H1 in H0. simpl in *. easy.
simpl in *. easy.
specialize (H1 (S (S i)) a).
assert (S (S i) + 1 < length (Ls :: s :: l')).
simpl in *. lia.
apply H1 in H0.
simpl in *. easy.
simpl in *. easy.
unfold fst_not_opp in *. destruct l. simpl in *. lia.
destruct i. simpl in *. inv H3.
unfold opp_ls. intros R. inv R. easy.
specialize (H1 i a).
assert (i + 1 < length (s :: l)). simpl in *. lia.
apply H1 in H0. simpl in *. easy. simpl in *. easy.
apply H1; try easy.
unfold exp_neu_prop in *.
intros. inv H0.
destruct i. simpl in *.
destruct l'. easy.
specialize (H1 1 a).
assert (1 + 1 < S (length (s :: l'))) by lia.
apply H1 in H0. simpl in *. easy.
simpl in *. easy.
specialize (H1 (S (S i)) a).
assert (S (S i) + 1 < length (Re :: l')).
simpl in *. lia.
apply H1 in H0.
simpl in *. easy.
simpl in *. easy.
unfold fst_not_opp in *. destruct l. simpl in *. lia.
destruct i. simpl in *. inv H3.
unfold opp_ls. intros R. inv R. easy.
specialize (H1 i a).
assert (i + 1 < length (s :: l)). simpl in *. lia.
apply H1 in H0. simpl in *. easy. simpl in *. easy.
apply H1; try easy.
1-3:inv H0; easy.
inv H0.
apply IHp2 with (x := x) (l := l'0); try easy.
apply IHp1 with (x:=x) (l := l); try easy.
Qed.
(* Type System. *)
Inductive well_typed_exp : env -> exp -> Prop :=
| skip_refl : forall env, forall p, well_typed_exp env (SKIP p)
| x_nor : forall env p, Env.MapsTo (fst p) Nor env -> well_typed_exp env (X p)
| x_had : forall env p, Env.MapsTo (fst p) Had env -> well_typed_exp env (X p)
| cnot_had : forall env p1 p2, p1 <> p2 -> Env.MapsTo (fst p1) Had env -> Env.MapsTo (fst p2) Had env
-> well_typed_exp env (HCNOT p1 p2)
| rz_nor : forall env q p, Env.MapsTo (fst p) Nor env -> well_typed_exp env (RZ q p)
| rz_had : forall env p, Env.MapsTo (fst p) Had env -> well_typed_exp env (RZ 1 p)
| rrz_nor : forall env q p, Env.MapsTo (fst p) Nor env -> well_typed_exp env (RRZ q p)
| rrz_had : forall env p, Env.MapsTo (fst p) Had env -> well_typed_exp env (RRZ 1 p)
| sr_phi : forall env n m x, Env.MapsTo x (Phi n) env -> m < n -> well_typed_exp env (SR m x)
| srr_phi : forall env n m x, Env.MapsTo x (Phi n) env -> m < n -> well_typed_exp env (SRR m x)
| lshift_nor : forall env x, Env.MapsTo x Nor env -> well_typed_exp env (Lshift x)
| lshift_had : forall env x, Env.MapsTo x Had env -> well_typed_exp env (Lshift x)
| rshift_nor : forall env x, Env.MapsTo x Nor env -> well_typed_exp env (Rshift x)
| rshift_had : forall env x, Env.MapsTo x Had env -> well_typed_exp env (Rshift x)
| rev_nor : forall env x, Env.MapsTo x Nor env -> well_typed_exp env (Rev x)
| rev_had : forall env x, Env.MapsTo x Had env -> well_typed_exp env (Rev x).
(*
Definition opp_ls (s : sexp) := match s with Ls => Rs | Rs => Ls | Re => Re end.
*)
Fixpoint get_vars e : list var :=
match e with SKIP p => [(fst p)]
| X p => [(fst p)]
| CU p e => (fst p)::(get_vars e)
| HCNOT p1 p2 => ((fst p1)::(fst p2)::[])
| RZ q p => ((fst p)::[])
| RRZ q p => ((fst p)::[])
| SR n x => (x::[])
| SRR n x => (x::[])
| Lshift x => (x::[])
| Rshift x => (x::[])
| Rev x => (x::[])
| QFT x => (x::[])
| RQFT x => (x::[])
| H x => (x::[])
| Seq e1 e2 => get_vars e1 ++ (get_vars e2)
end.
Inductive well_typed_pexp (aenv: var -> nat) : env -> exp -> env -> Prop :=
| exp_refl : forall env e,
well_typed_exp env e -> well_typed_pexp aenv env e env
| qft_nor : forall env env' x, Env.MapsTo x Nor env -> Env.Equal env' (Env.add x (Phi (aenv x)) env)
-> well_typed_pexp aenv env (QFT x) env'
| rqft_phi : forall env env' x, Env.MapsTo x (Phi (aenv x)) env -> Env.Equal env' (Env.add x Nor env) ->
well_typed_pexp aenv env (RQFT x) env'
| h_nor : forall env env' x, Env.MapsTo x Nor env -> Env.Equal env' (Env.add x Had env) ->
well_typed_pexp aenv env (H x) env'
| h_had : forall env env' x, Env.MapsTo x Had env -> Env.Equal env' (Env.add x Nor env) ->
well_typed_pexp aenv env (H x) env'
| pcu_nor : forall env p e, Env.MapsTo (fst p) Nor env -> exp_fresh aenv p e -> exp_neu (get_vars e) e ->
well_typed_pexp aenv env e env -> well_typed_pexp aenv env (CU p e) env
| pe_seq : forall env env' env'' e1 e2, well_typed_pexp aenv env e1 env' ->
well_typed_pexp aenv env' e2 env'' -> well_typed_pexp aenv env (e1 ; e2) env''.
Inductive exp_WF (aenv:var -> nat): exp -> Prop :=
| skip_wf : forall p, snd p < aenv (fst p) -> exp_WF aenv (SKIP p)
| x_wf : forall p, snd p < aenv (fst p) -> exp_WF aenv (X p)
| cu_wf : forall p e, snd p < aenv (fst p) -> exp_WF aenv e -> exp_WF aenv (CU p e)
| hcnot_wf : forall p1 p2, snd p1 < aenv (fst p1)
-> snd p2 < aenv (fst p2) -> exp_WF aenv (HCNOT p1 p2)
| rz_wf : forall p q, snd p < aenv (fst p) -> exp_WF aenv (RZ q p)
| rrz_wf : forall p q, snd p < aenv (fst p) -> exp_WF aenv (RRZ q p)
| sr_wf : forall n x, S n < aenv x -> exp_WF aenv (SR n x)
| ssr_wf : forall n x, S n < aenv x -> exp_WF aenv (SRR n x)
| seq_wf : forall e1 e2, exp_WF aenv e1 -> exp_WF aenv e2 -> exp_WF aenv (Seq e1 e2)
| lshift_wf : forall x, 0 < aenv x -> exp_WF aenv (Lshift x)
| rshift_wf : forall x, 0 < aenv x -> exp_WF aenv (Rshift x)
| rev_wf : forall x, 0 < aenv x -> exp_WF aenv (Rev x)
| qft_wf : forall x, 0 < aenv x -> exp_WF aenv (QFT x)
| rqft_wf : forall x, 0 < aenv x -> exp_WF aenv (RQFT x)
| h_wf : forall x, 0 < aenv x -> exp_WF aenv (H x).
Fixpoint init_v (n:nat) (x:var) (M: nat -> bool) :=
match n with 0 => (SKIP (x,0))
| S m => if M m then init_v m x M; X (x,m) else init_v m x M
end.
Inductive right_mode_val : type -> val -> Prop :=
| right_nor: forall b r, right_mode_val Nor (nval b r)
| right_had: forall b1 b2 r, r 0 = false -> right_mode_val Had (hval b1 b2 r)
| right_phi: forall n rc r, right_mode_val (Phi n) (qval rc r).
Definition right_mode_env (aenv: var -> nat) (env: env) (st: posi -> val)
:= forall t p, snd p < aenv (fst p) -> Env.MapsTo (fst p) t env -> right_mode_val t (st p).
(* helper functions/lemmas for NOR states. *)
Definition nor_mode (f : posi -> val) (x:posi) : Prop :=
match f x with nval a b => True | _ => False end.
Definition nor_modes (f:posi -> val) (x:var) (n:nat) :=
forall i, i < n -> nor_mode f (x,i).
Lemma type_nor_mode : forall f aenv env p,
Env.MapsTo (fst p) Nor env -> snd p < aenv (fst p) -> right_mode_env aenv env f -> nor_mode f p.
Proof.
intros. unfold right_mode_env in *.
apply (H2 Nor) in H1 ; try easy.
inv H1. unfold nor_mode.
rewrite <- H3. easy.
Qed.
Lemma type_nor_modes : forall f aenv env x,
Env.MapsTo x Nor env -> right_mode_env aenv env f -> nor_modes f x (aenv x).
Proof.
intros. unfold right_mode_env in *.
unfold nor_modes. intros.
specialize (H1 Nor (x,i)).
simpl in H1. apply H1 in H2; try easy.
inv H2. unfold nor_mode.
assert ((@pair var nat x i) = (@pair Env.key nat x i)) by easy.
rewrite H2 in *.
rewrite <- H3. easy.
Qed.
Lemma nor_mode_nval : forall f x, nor_mode f x -> (exists r, f x = nval true r \/ f x = nval false r).
Proof.
intros. unfold nor_mode in *. destruct (f x); inv H0.
exists r.
destruct b. left. easy. right. easy.
Qed.
Lemma neq_sym {A} : forall (x y: A), x <> y -> y <> x.
Proof.
intros. intros R.
subst. contradiction.
Qed.
Lemma nor_mode_up : forall f x y v, x <> y -> nor_mode f x -> nor_mode (f[y |-> v]) x.
Proof.
intros. unfold nor_mode in *.
assert ((f [y |-> v]) x = (f x)).
rewrite eupdate_index_neq. reflexivity. apply neq_sym. assumption.
rewrite H2.
destruct (f x); inv H1. easy.
Qed.
Definition put_cu (v:val) (b:bool) :=
match v with nval x r => nval b r | a => a end.
Definition get_cua (v:val) :=
match v with nval x r => x | _ => false end.
Lemma get_cua_eq : forall f x v, nor_mode f x -> get_cua ((f[x |-> put_cu (f x) v]) x) = v.
Proof.
intros.
unfold get_cua.
rewrite eupdate_index_eq.
unfold put_cu.
unfold nor_mode in H0.
destruct (f x). easy. inv H0. inv H0.
Qed.
Lemma double_put_cu : forall (f:posi -> val) x v v', put_cu (put_cu (f x) v) v' = put_cu (f x) v'.
Proof.
intros.
unfold put_cu.
destruct (f x). easy. easy. easy.
Qed.
Definition get_cus (n:nat) (f:posi -> val) (x:var) :=
fun i => if i <? n then (match f (x,i) with nval b r => b | _ => false end) else allfalse i.
Lemma get_cus_cua : forall n f x m, m < n -> get_cus n f x m = get_cua (f (x,m)).
Proof.
intros.
unfold get_cus,get_cua.
bdestruct (m <? n).
destruct (f (x,n)). easy. easy. easy.
lia.
Qed.
Definition put_cus (f:posi -> val) (x:var) (g:nat -> bool) (n:nat) : (posi -> val) :=
fun a => if fst a =? x then if snd a <? n then put_cu (f a) (g (snd a)) else f a else f a.
Lemma cus_get_neq : forall (f:posi -> val) (x y :var) g n i,
x <> y -> get_cua ((put_cus f y g n) (x,i)) = get_cua (f (x,i)).
Proof.
intros.
unfold get_cua.
unfold put_cus.
simpl.
bdestruct (x =? y).
lia. easy.
Qed.
Lemma put_cus_out : forall (f:posi -> val) (x y :var) g n i,
n <= i -> ((put_cus f y g n) (x,i)) = (f (x,i)).
Proof.
intros.
unfold put_cus.
simpl.
bdestruct (x =? y).
bdestruct (i <? n). lia.
easy. easy.
Qed.
Lemma nor_mode_cus_eq: forall f x g n y i,
nor_mode f (y,i) -> nor_mode (put_cus f x g n) (y,i).
Proof.
intros. unfold nor_mode in *.
unfold put_cus.
simpl.
bdestruct (y =? x).
bdestruct (i <? n).
destruct (f (y, i)).
unfold put_cu. easy.
inv H0. inv H0.
apply H0. apply H0.
Qed.
Lemma cus_get_eq : forall (f:posi -> val) (x :var) g n i,
i < n -> nor_modes f x n -> get_cua ((put_cus f x g n) (x,i)) = g i.
Proof.
intros.
unfold get_cua.
unfold put_cus.
simpl.
bdestruct (x =? x).
bdestruct (i <? n).
unfold put_cu.
specialize (H1 i H3). unfold nor_mode in *.
destruct (f (x, i)) eqn:eq1. easy.
inv H1. inv H1.
lia. lia.
Qed.
Lemma put_cus_eq : forall (f:posi -> val) (x:var) g n i,
i < n -> (put_cus f x g n) (x,i) = put_cu (f (x,i)) (g i).
Proof.
intros.
unfold put_cus.
simpl.
bdestruct (x =? x).
bdestruct (i <? n). easy. lia. lia.
Qed.
Lemma put_cus_neq : forall (f:posi -> val) (x y:var) g n i,
x <> y -> (put_cus f x g n) (y,i) = f (y,i).
Proof.
intros.
unfold put_cus.
simpl.
bdestruct (y =? x). lia. easy.
Qed.
Lemma put_cus_neq_1 : forall (f:posi -> val) (x:var) g n c,
x <> fst c -> (put_cus f x g n) c = f c.
Proof.
intros.
destruct c. apply put_cus_neq.
simpl in H0. assumption.
Qed.
Lemma put_cus_neq_2 : forall (f:posi -> val) (x y:var) g n i,
n <= i -> (put_cus f x g n) (y,i) = f (y,i).
Proof.
intros.
unfold put_cus.
simpl.
bdestruct (y =? x).
bdestruct (i <? n). lia. easy.
easy.
Qed.
Lemma put_cu_cus : forall (f:posi -> val) x y g i n v, put_cu (put_cus f y g n (x,i)) v = put_cu (f (x,i)) v.
Proof.
intros.
unfold put_cus,put_cu.
simpl.
bdestruct (x =? y).
bdestruct (i <? n).
destruct (f (x,i)). easy. easy. easy. easy. easy.
Qed.
Lemma nor_mode_up_1 : forall f x v, nor_mode f x -> nor_mode (f[x |-> put_cu (f x) v]) x.
Proof.
intros.
unfold nor_mode in *.
rewrite eupdate_index_eq.
unfold put_cu.
destruct (f x).
easy. inv H0. inv H0.
Qed.
Lemma nor_mode_ups : forall f f' x v, f x = f' x -> nor_mode f x ->
nor_mode (f[x |-> put_cu (f' x) v]) x.
Proof.
intros. unfold nor_mode in *.
rewrite eupdate_index_eq.
unfold put_cu. rewrite <- H0.
destruct (f x); inv H1. easy.
Qed.
Lemma nor_mode_get : forall f x, nor_mode f x -> (exists b, get_cua (f x) = b).
Proof.
intros. unfold nor_mode in *. destruct (f x); inv H0.
exists b. unfold get_cua. reflexivity.
Qed.
Lemma put_get_cus_eq :
forall f x n, nor_modes f x n -> (put_cus f x (get_cus n f x) n) = f.
Proof.
intros.
unfold put_cus,get_cus,put_cu.
apply functional_extensionality. intros.
destruct x0. simpl.
bdestruct (v =? x). bdestruct (n0 <? n).
subst.
unfold nor_modes in H0.
specialize (H0 n0 H2) as eq1. unfold nor_mode in eq1.
destruct (f (x,n0)). easy. inv eq1. inv eq1.
easy. easy.
Qed.
Lemma get_cus_put_eq :
forall f x v n, v < 2^n -> nor_modes f x n -> get_cus n (put_cus f x (nat2fb v) n) x = (nat2fb v).
Proof.
intros.
unfold get_cus.
apply functional_extensionality. intros.
bdestruct (x0 <? n).
simpl.
unfold nor_modes in H0.
assert (nor_mode (put_cus f x (nat2fb v) n) (x, x0)).
apply nor_mode_cus_eq. apply H1. easy.
unfold nor_mode in H3.
destruct (put_cus f x ((nat2fb v)) n (x, x0)) eqn:eq2.
unfold put_cus in eq2. simpl in eq2.
bdestruct (x =? x).
bdestruct (x0 <? n).
unfold put_cu in eq2.
assert (nor_mode f (x,x0)).
apply H1. easy.
unfold nor_mode in H6.
destruct (f (x, x0)). inv eq2. easy. inv H6. inv H6. lia. lia.
inv H3. inv H3.
unfold allfalse.
rewrite nat2fb_bound with (n := n); try easy.
Qed.
Lemma put_cus_same : forall f x g n, nor_modes f x n
-> get_cus n f x = g -> put_cus f x g n = f.
Proof.
intros.
rewrite <- H1.
rewrite put_get_cus_eq. easy. easy.
Qed.
Lemma get_cus_put_neq :
forall f x y g n, x <> y -> get_cus n (put_cus f x g n) y = get_cus n f y.
Proof.
intros. unfold get_cus,put_cus.
apply functional_extensionality. intros.
simpl.
bdestruct ( y =? x). lia.
destruct (f (y, x0)). easy. easy. easy.
Qed.
Lemma get_put_cus_cut_n : forall n f x g, nor_modes f x n
-> (get_cus n (put_cus f x g n) x) = cut_n g n.
Proof.
intros. unfold get_cus,put_cus,cut_n.
apply functional_extensionality. intros.
bdestruct (x0 <? n). simpl.
bdestruct (x =? x).
bdestruct (x0 <? n).
unfold put_cu.
unfold nor_modes in H0.
specialize (H0 x0 H3). unfold nor_mode in H0.
destruct (f (x,x0)). easy. lia. lia.
lia. lia. easy.
Qed.
Definition get_cu (v : val) :=
match v with nval b r => Some b
| hval b1 b2 r => Some b1
| _ => None
end.
Lemma put_get_cu : forall f x, nor_mode f x -> put_cu (f x) (get_cua (f x)) = f x.
Proof.
intros. unfold put_cu, get_cua.
unfold nor_mode in H0. destruct (f x); inv H0.
reflexivity.
Qed.
Lemma get_put_cu : forall f x v, nor_mode f x -> get_cua (put_cu (f x) v) = v.
Proof.
intros. unfold put_cu, get_cua.
unfold nor_mode in H0. destruct (f x); inv H0.
reflexivity.
Qed.
Lemma get_cua_t : forall b r, get_cua (nval b r) = b.
Proof.
intros. unfold get_cua. reflexivity.
Qed.
(* Proofs of types and syntax. *)
Ltac nor_sym := try (apply neq_sym; assumption) ; try assumption.
Lemma iner_neq : forall (x y:var) (a b:nat), x <> y -> (x,a) <> (y,b).
Proof.
intros. intros R.
inv R. contradiction.
Qed.
Lemma iner_neq_1 : forall (x:var) (c:posi) a, x <> fst c -> (x,a) <> c.
Proof.
intros. intros R.
destruct c.
inv R. contradiction.
Qed.
Lemma iner_neq_2 : forall (x:var) (c:posi) a, x <> fst c -> c <> (x,a).
Proof.
intros. intros R.
destruct c.
inv R. contradiction.
Qed.
Ltac tuple_eq := intros R; inv R; lia.
Ltac iner_p := try nor_sym; try tuple_eq ; try (apply iner_neq ; assumption)
; try (apply iner_neq_1 ; assumption) ; try (apply iner_neq_2 ; assumption).
Lemma eupdates_twice_neq : forall f x g n c v, x <> fst c
-> (put_cus f x g n)[c |-> v] = put_cus (f[c |-> v]) x g n.
Proof.
intros. unfold put_cus.
apply functional_extensionality.
intros.
bdestruct (x0 ==? c).
subst.
rewrite eupdate_index_eq.
bdestruct (fst c =? x).
subst. contradiction.
rewrite eupdate_index_eq. easy.
rewrite eupdate_index_neq.
bdestruct (fst x0 =? x).
rewrite eupdate_index_neq.
easy. nor_sym.
rewrite eupdate_index_neq by nor_sym.
easy. nor_sym.
Qed.
Lemma get_cus_up : forall n f x c v, fst c <> x -> get_cus n (f[c |-> v]) x = get_cus n f x.
Proof.
intros.
apply functional_extensionality; intro.
unfold get_cus.
bdestruct (x0 <? n). destruct c. simpl in *. rewrite eupdate_index_neq by iner_p.
easy. easy.
Qed.
Lemma get_cus_up_ge : forall n f x c v, n <= snd c -> get_cus n (f[c |-> v]) x = get_cus n f x.
Proof.
intros.
apply functional_extensionality; intro.
unfold get_cus.
bdestruct (x0 <? n). destruct c. simpl in *. rewrite eupdate_index_neq by iner_p.
easy. easy.
Qed.
(*A function to get the rotation angle of a state. *)
Definition get_r (v:val) :=
match v with nval x r => r
| qval rc r => rc
| hval x y r => r
end.
Lemma get_r_put_same : forall (f:posi -> val) x y g n i,
get_r (put_cus f x g n (y,i)) = get_r (f (y,i)).
Proof.
intros.
unfold put_cus.
simpl.
bdestruct (y =? x).
bdestruct (i <? n).
unfold put_cu.
destruct (f (y, i)).
unfold get_r. easy. easy. easy. easy. easy.
Qed.
Lemma get_r_put_cu_same : forall (f:posi -> val) p v,
nor_mode f p ->
get_r (put_cu (f p) v) = get_r (f p).
Proof.
intros.
unfold put_cu,nor_mode in *.
destruct (f p). easy. easy. easy.
Qed.
Lemma put_cu_mid_eq : forall (f g:posi -> val) a v,
nor_mode f a -> nor_mode g a -> get_r (f a) = get_r (g a) -> (put_cu (f a) v) = (put_cu (g a) v).
Proof.
intros.
unfold put_cu. unfold nor_mode in *.
destruct (f a). destruct (g a).
unfold get_r in H2. subst.
easy. inv H1. inv H1.
inv H0. inv H0.
Qed.
Lemma put_cus_twice_neq : forall (f:posi -> val) (x y:var) g1 g2 n,
x <> y -> (put_cus (put_cus f x g1 n) y g2 n)
= (put_cus (put_cus f y g2 n) x g1 n).
Proof.
intros.
apply functional_extensionality.
unfold put_cus. intros.
destruct x0. simpl.
bdestruct (v =? y).
bdestruct (v =? x). lia. easy.
easy.
Qed.
Lemma put_cu_twice_eq : forall (f:posi -> val) (x:var) v1 v2 i,
put_cu (put_cu (f (x,i)) v1) v2 = put_cu (f (x,i)) v2.
Proof.
intros. unfold put_cu.
destruct (f (x, i)). easy. easy. easy.
Qed.
Lemma put_cu_twice_eq_1 : forall (f:posi -> val) c v1 v2,
put_cu (put_cu (f c) v1) v2 = put_cu (f c) v2.
Proof.
intros. unfold put_cu.
destruct (f c). easy. easy. easy.
Qed.
Lemma put_cus_twice_eq : forall (f:posi -> val) (x:var) g1 g2 n,
(put_cus (put_cus f x g1 n) x g2 n)
= (put_cus f x g2 n).
Proof.
intros.
apply functional_extensionality.
unfold put_cus. intros.
destruct x0. simpl.
bdestruct (v =? x).
bdestruct (n0 <? n). rewrite put_cu_twice_eq. easy.
easy.
easy.
Qed.
Lemma put_cus_sem_eq : forall (f:posi -> val) (x:var) g1 g2 n,
(forall m, m < n -> g1 m = g2 m) ->
(put_cus f x g1 n) = (put_cus f x g2 n).
Proof.
intros.
apply functional_extensionality.
unfold put_cus. intros.
destruct x0. simpl.
bdestruct (v =? x).
bdestruct (n0 <? n). rewrite H0. easy.
lia. easy. easy.
Qed.
(* Defining program semantic functions. *)
Definition rotate (r :rz_val) (q:nat) := addto r q.
Definition times_rotate (v : val) (q:nat) :=
match v with nval b r => if b then nval b (rotate r q) else nval b r
| hval b1 b2 r => hval b1 (¬ b2) r
| qval rc r => qval rc (rotate r q)
end.
Fixpoint sr_rotate' (st: posi -> val) (x:var) (n:nat) (size:nat) :=
match n with 0 => st
| S m => (sr_rotate' st x m size)[(x,m) |-> times_rotate (st (x,m)) (size - m)]
end.
Definition sr_rotate st x n := sr_rotate' st x (S n) (S n).
Definition r_rotate (r :rz_val) (q:nat) := addto_n r q.
Definition times_r_rotate (v : val) (q:nat) :=
match v with nval b r => if b then nval b (r_rotate r q) else nval b r
| hval b1 b2 r => hval b1 (¬ b2) r
| qval rc r => qval rc (r_rotate r q)
end.
Fixpoint srr_rotate' (st: posi -> val) (x:var) (n:nat) (size:nat) :=
match n with 0 => st
| S m => (srr_rotate' st x m size)[(x,m) |-> times_r_rotate (st (x,m)) (size - m)]
end.
Definition srr_rotate st x n := srr_rotate' st x (S n) (S n).
Definition exchange (v: val) :=
match v with nval b r => nval (¬ b) r
| hval b1 b2 r => hval b2 b1 r
| a => a
end.
Fixpoint lshift' (n:nat) (size:nat) (f:posi -> val) (x:var) :=
match n with 0 => f[(x,0) |-> f(x,size)]
| S m => ((lshift' m size f x)[ (x,n) |-> f(x,m)])
end.
Definition lshift (f:posi -> val) (x:var) (n:nat) := lshift' (n-1) (n-1) f x.
Fixpoint rshift' (n:nat) (size:nat) (f:posi -> val) (x:var) :=
match n with 0 => f[(x,size) |-> f(x,0)]
| S m => ((rshift' m size f x)[(x,m) |-> f (x,n)])
end.
Definition rshift (f:posi -> val) (x:var) (n:nat) := rshift' (n-1) (n-1) f x.
(*
Inductive varType := SType (n1:nat) (n2:nat).
Definition inter_env (enva: var -> nat) (x:var) :=
match (enva x) with SType n1 n2 => n1 + n2 end.
*)
Definition hexchange (v1:val) (v2:val) :=
match v1 with hval b1 b2 r1 =>
match v2 with hval b3 b4 r2 => if eqb b3 b4 then v1 else hval b1 (¬ b2) r1
| _ => v1
end
| _ => v1
end.
Definition reverse (f:posi -> val) (x:var) (n:nat) := fun (a: var * nat) =>
if ((fst a) =? x) && ((snd a) <? n) then f (x, (n-1) - (snd a)) else f a.
(* Semantic definition for H gate. *)
Definition h_case (v : val) :=
match v with nval b r => if r 0 then hval false b (rotate r 1) else hval true (¬ b) r
| hval true true r => nval false r
| hval true false r => nval true r
| hval false true r => nval true (rotate r 1)
| hval false false r => nval false (rotate r 1)
| a => a
end.
Fixpoint h_sem (f:posi -> val) (x:var) (n : nat) :=
match n with 0 => f
| S m => (h_sem f x m)[(x,m) |-> h_case (f (x,m))]
end.
(* Semantics function for QFT gate. *)
Definition seq_val (v:val) :=
match v with nval b r => b
| _ => false
end.