-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypes.v
3310 lines (2705 loc) · 109 KB
/
Types.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 Psatz.
Require Import Reals.
Require Export Complex.
Require Export Matrix.
Require Export Quantum.
Require Export Eigenvectors.
Require Export Heisenberg.
Require Import Setoid.
(************************)
(* Defining coeficients *)
(************************)
Inductive Coef :=
| p_1
| p_i
| n_1
| n_i.
Definition cNeg (c : Coef) : Coef :=
match c with
| p_1 => n_1
| n_1 => p_1
| p_i => n_i
| n_i => p_i
end.
Lemma cNeg_inv : forall (c : Coef), cNeg (cNeg c) = c.
Proof. destruct c; easy.
Qed.
Definition cMul (c1 c2 : Coef) : Coef :=
match (c1, c2) with
| (p_1, _) => c2
| (_, p_1) => c1
| (n_1, _) => cNeg c2
| (_, n_1) => cNeg c1
| (p_i, p_i) => n_1
| (n_i, n_i) => n_1
| (p_i, n_i) => p_1
| (n_i, p_i) => p_1
end.
Fixpoint cBigMul (cs : list Coef) : Coef :=
match cs with
| nil => p_1
| c :: cs' => cMul c (cBigMul cs')
end.
Infix "*" := cMul (at level 40, left associativity) : heisenberg_scope.
Lemma cMul_comm : forall (c1 c2 : Coef), c1 * c2 = c2 * c1.
Proof. intros.
destruct c1;
destruct c2;
easy.
Qed.
Lemma cMul_assoc : forall (c1 c2 c3 : Coef), (c1 * c2) * c3 = c1 * (c2 * c3).
Proof. intros.
destruct c1;
destruct c2;
destruct c3;
easy.
Qed.
Lemma cBigMul_app : forall (l1 l2 : list Coef),
(cBigMul l1) * (cBigMul l2) = cBigMul (l1 ++ l2).
Proof. induction l1 as [| h]; try easy.
intros. simpl.
rewrite cMul_assoc, IHl1; easy.
Qed.
Definition translate_coef (c : Coef) : C :=
match c with
| p_1 => C1
| p_i => Ci
| n_1 => -C1
| n_i => -Ci
end.
Lemma translate_coef_cMul : forall (c1 c2 : Coef),
translate_coef (c1 * c2) = ((translate_coef c1) * (translate_coef c2))%C.
Proof. intros.
destruct c1;
destruct c2;
unfold translate_coef;
unfold cMul;
unfold cNeg;
try lca.
Qed.
Lemma translate_coef_nonzero : forall (c : Coef), translate_coef c <> C0.
Proof. destruct c; simpl;
try (apply C0_fst_neq; simpl; lra);
try (apply C0_snd_neq; simpl; lra).
Qed.
(**********************)
(* Defining the types *)
(**********************)
(* this is the lowest level, only Pauli gates are defined *)
Inductive Pauli :=
| gI
| gX
| gY
| gZ.
Definition translate_P (g : Pauli) : Square 2 :=
match g with
| gI => I 2
| gX => σx
| gY => σy
| gZ => σz
end.
Lemma WF_Matrix_Pauli : forall (g : Pauli), WF_Matrix (translate_P g).
Proof. intros.
destruct g; simpl; auto with wf_db.
Qed.
Hint Resolve WF_Matrix_Pauli : wf_db.
(* Here we define a gMul to give Coef followed by a gMul to give the actual type *)
(* this allows for an easy zip in gMulT *)
Definition gMul_Coef (g1 g2 : Pauli) : Coef :=
match g1, g2 with
| gI, _ => p_1
| _, gI => p_1
| gX, gX => p_1
| gY, gY => p_1
| gZ, gZ => p_1
| gX, gY => p_i
| gY, gX => n_i
| gX, gZ => n_i
| gZ, gX => p_i
| gY, gZ => p_i
| gZ, gY => n_i
end.
Definition gMul_base (g1 g2 : Pauli) : Pauli :=
match g1, g2 with
| gI, _ => g2
| _, gI => g1
| gX, gX => gI
| gY, gY => gI
| gZ, gZ => gI
| gX, gY => gZ
| gY, gX => gZ
| gX, gZ => gY
| gZ, gX => gY
| gY, gZ => gX
| gZ, gY => gX
end.
(* scaling, multiplication, and tensoring done at this level *)
Definition TType (len : nat) := (Coef * (list Pauli))%type.
(* we define an error TType for when things go wrong *)
Definition ErrT : TType 0 := (p_1, []).
Definition gMulT {n} (A B : TType n) : TType n :=
match A with
| (c1, g1) =>
match B with
| (c2, g2) => if length g1 =? length g2
then (c1 * c2 * (cBigMul (zipWith gMul_Coef g1 g2)),
zipWith gMul_base g1 g2)
else ErrT
end
end.
Definition gTensorT {n m} (A : TType n) (B : TType m) : TType (n + m) :=
match A with
| (c1, g1) =>
match B with
| (c2, g2) => if (length g1 =? n) && (length g2 =? m)
then (c1 * c2, g1 ++ g2)
else ErrT
end
end.
Definition gScaleT {n} (c : Coef) (A : TType n) : TType n :=
match A with
| (c1, g1) => (c * c1, g1)
end.
Definition translate {n} (A : TType n) : Square (2^n) :=
(translate_coef (fst A)) .* ⨂ (map translate_P (snd A)).
Inductive vType (n : nat) : Type :=
| G : TType n -> vType n
| Cap : vType n -> vType n -> vType n
| Arrow : vType n -> vType n -> vType n
| Err : vType n.
Arguments G {n}.
Arguments Cap {n}.
Arguments Arrow {n}.
Arguments Err {n}.
(* you cannot multiply intersection or arrow types (for now)
so any of these options returns Err *)
Definition mul {n} (A B : vType n) : vType n :=
match A with
| G a =>
match B with
| G b => G (gMulT a b)
| _ => Err
end
| _ => Err
end.
Definition tensor {n m} (A : vType n) (B : vType m) : vType (n + m) :=
match A with
| G a =>
match B with
| G b => G (gTensorT a b)
| _ => Err
end
| _ => Err
end.
(* since scaling intersections makes sense, we allow this *)
Fixpoint scale {n} (c : Coef) (A : vType n) : vType n :=
match A with
| G a => G (gScaleT c a)
| Cap g1 g2 => Cap (scale c g1) (scale c g2)
| _ => Err
end.
Definition i {n} (A : vType n) := scale p_i A.
Notation "- A" := (scale n_1 A).
Infix ".*" := mul (at level 40, left associativity).
Infix ".⊗" := tensor (at level 51, right associativity).
Notation "A → B" := (Arrow A B) (at level 60, no associativity).
Notation "A ∩ B" := (Cap A B) (at level 60, no associativity).
(******************************************************************************)
(* Defining different types of vTypes to ensure WF and Singleton translations *)
(******************************************************************************)
Inductive Sing_vt {n} : vType n -> Prop :=
| G_svt : forall tt : TType n, Sing_vt (G tt).
Inductive Cap_vt {n} : vType n -> Prop :=
| G_cvt : forall tt : TType n, Cap_vt (G tt)
| Cap_cvt : forall T1 T2 : vType n, Cap_vt T1 -> Cap_vt T2 -> Cap_vt (Cap T1 T2).
Lemma sing_implies_cap : forall {n} (T : vType n),
Sing_vt T -> Cap_vt T.
Proof. intros. inversion H; apply G_cvt. Qed.
(* we also use a bool version of Cap_vt for matching *)
Fixpoint Cap_vt_bool {n} (A : vType n) : bool :=
match A with
| G _ => true
| Cap v1 v2 => Cap_vt_bool v1 && Cap_vt_bool v2
| _ => false
end.
Lemma Cap_vt_conv : forall {n} (A : vType n),
Cap_vt A <-> Cap_vt_bool A = true.
Proof. intros. split.
+ induction A as [| | |]; try easy.
- intros.
inversion H.
simpl; rewrite IHA1, IHA2; try easy.
+ induction A as [| | |]; try easy.
- intros.
apply G_cvt.
- intros.
simpl in *.
apply andb_true_iff in H.
destruct H.
apply Cap_cvt;
try (apply IHA1);
try (apply IHA2);
assumption.
Qed.
Inductive Sing_gt {n} : vType n -> Prop :=
| Arrow_sgt : forall T1 T2 : vType n, Cap_vt T1 -> Cap_vt T2 -> Sing_gt (Arrow T1 T2).
Inductive Cap_gt {n} : vType n -> Prop :=
| Arrow_cgt : forall T : vType n, Sing_gt T -> Cap_gt T
| Cap_cgt : forall T1 T2 : vType n, Cap_gt T1 -> Cap_gt T2 -> Cap_gt (Cap T1 T2).
Fixpoint translate_vecType {n} (A : vType n) : vecType (2^n) :=
match Cap_vt_bool A with
| false => []
| true =>
match A with
| G g => [translate g]
| Cap v1 v2 => translate_vecType v1 ++ translate_vecType v2
| _ => []
end
end.
Lemma singleton_sing_vt : forall {n m} (A : vType n),
Sing_vt A -> @Singleton m (translate_vecType A).
Proof. intros. destruct A; easy.
Qed.
Lemma sing_vt_simplify : forall {n} (A : vType n),
Sing_vt A -> (exists a, A = G a).
Proof. intros. destruct A; try easy.
- exists t. reflexivity.
Qed.
Definition I : vType 1 := G (p_1, [gI]).
Definition X : vType 1 := G (p_1, [gX]).
Definition Y : vType 1 := G (p_1, [gY]).
Definition Z : vType 1 := G (p_1, [gZ]).
Lemma Itrans : translate_vecType I = I'.
Proof. simpl.
unfold translate; simpl.
rewrite Mscale_1_l, kron_1_r.
reflexivity.
Qed.
Lemma Xtrans : translate_vecType X = X'.
Proof. simpl.
unfold translate; simpl.
rewrite Mscale_1_l, kron_1_r.
reflexivity.
Qed.
Lemma Ytrans : translate_vecType Y = Y'.
Proof. simpl.
unfold translate; simpl.
rewrite Mscale_1_l, kron_1_r, Y_eq_iXZ.
distribute_scale.
reflexivity.
Qed.
Lemma Ztrans : translate_vecType Z = Z'.
Proof. simpl.
unfold translate; simpl.
rewrite Mscale_1_l, kron_1_r.
reflexivity.
Qed.
Lemma Y_is_iXZ : Y = (i (X .* Z)).
Proof. easy. Qed.
(***************)
(* Sing Lemmas *)
(***************)
Lemma SI : Sing_vt I. Proof. easy. Qed.
Lemma SX : Sing_vt X. Proof. easy. Qed.
Lemma SZ : Sing_vt Z. Proof. easy. Qed.
Lemma S_scale : forall {n} (A : vType n) (c : Coef), Sing_vt A -> (Sing_vt (scale c A)).
Proof. intros. destruct A; easy. Qed.
Lemma S_neg : forall {n} (A : vType n), Sing_vt A -> Sing_vt (- A).
Proof. intros. destruct A; easy. Qed.
Lemma S_i : forall {n} (A : vType n), Sing_vt A -> Sing_vt (i A).
Proof. intros. destruct A; easy. Qed.
Lemma S_mul : forall {n} (A B : vType n), Sing_vt A -> Sing_vt B -> Sing_vt (A .* B).
Proof. intros.
destruct A; destruct B; easy.
Qed.
Lemma S_tensor : forall {n m} (A : vType n) (B : vType m), Sing_vt A -> Sing_vt B -> Sing_vt (A .⊗ B).
Proof. intros.
destruct A; destruct B; easy.
Qed.
Hint Resolve sing_implies_cap SI SX SZ S_scale S_neg S_i S_mul S_tensor : wfvt_db.
Lemma SY : Sing_vt Y.
Proof. rewrite Y_is_iXZ. auto with wfvt_db. Qed.
(**************************)
(* Well Formedness Lemmas *)
(**************************)
Inductive WF_TType {len : nat} : TType len -> Prop :=
| WF_tt : forall tt : TType len, length (snd tt) = len -> WF_TType tt.
Inductive WF_vType {n} : vType n -> Prop :=
| WF_G : forall tt : TType n, WF_TType tt -> WF_vType (G tt)
| WF_Cap : forall T1 T2 : vType n, WF_vType T1 -> WF_vType T2 -> WF_vType (Cap T1 T2)
| WF_Arrow : forall T1 T2 : vType n, WF_vType T1 -> WF_vType T2 -> WF_vType (Arrow T1 T2).
Lemma WF_I : WF_vType I. Proof. apply WF_G; easy. Qed.
Lemma WF_X : WF_vType X. Proof. apply WF_G; easy. Qed.
Lemma WF_Z : WF_vType Z. Proof. apply WF_G; easy. Qed.
Lemma WF_scale : forall {n} (A : vType n) (c : Coef),
Sing_vt A -> WF_vType A ->
(WF_vType (scale c A)).
Proof. intros.
destruct A; try easy.
apply WF_G.
apply WF_tt.
inversion H0.
inversion H2.
destruct t; easy.
Qed.
Lemma WF_mul : forall {n} (A B : vType n),
Sing_vt A -> Sing_vt B ->
WF_vType A -> WF_vType B ->
WF_vType (A .* B).
Proof. intros.
destruct A;
destruct B; try easy.
destruct t;
destruct t0. simpl.
inversion H1; inversion H2; inversion H4; inversion H6.
simpl in *; rewrite H7, H9; bdestruct_all.
apply WF_G; apply WF_tt.
simpl.
rewrite (zipWith_len_pres _ n); easy.
Qed.
Lemma WF_tensor : forall {n m} (A : vType n) (B : vType m),
Sing_vt A -> Sing_vt B ->
WF_vType A -> WF_vType B ->
WF_vType (A .⊗ B).
Proof. intros.
destruct A;
destruct B; try easy.
destruct t;
destruct t0.
simpl in *.
inversion H1; inversion H2; inversion H4; inversion H6.
simpl in *; rewrite H7, H9; bdestruct_all.
apply WF_G; apply WF_tt.
simpl.
rewrite app_length;
lia.
Qed.
Lemma WF_neg : forall {n} (A : vType n),
Sing_vt A -> WF_vType A -> WF_vType (- A).
Proof. intros.
destruct A; try easy.
inversion H0; inversion H2.
apply WF_G; apply WF_tt.
destruct t; easy.
Qed.
Lemma WF_i : forall {n} (A : vType n),
Sing_vt A -> WF_vType A -> WF_vType (i A).
Proof. intros.
destruct A; try easy.
inversion H0; inversion H2.
apply WF_G; apply WF_tt.
destruct t; easy.
Qed.
Hint Resolve SI SX SZ WF_I WF_X WF_Z WF_mul WF_scale WF_tensor WF_neg WF_i : wfvt_db.
Lemma WF_Y : WF_vType Y.
Proof. rewrite Y_is_iXZ. auto with wfvt_db. Qed.
Lemma WF_Matrix_TType : forall {n} (A : TType n), WF_TType A -> WF_Matrix (translate A).
Proof. intros. destruct A.
unfold translate; simpl.
inversion H; simpl in *.
rewrite map_length, <- H0.
apply Matrix.WF_scale.
assert (H2 := (WF_big_kron _ _ (map translate_P l) (translate_P gI))).
rewrite map_length in *; apply H2.
intros.
rewrite map_nth.
apply WF_Matrix_Pauli.
Qed.
Hint Resolve WF_Matrix_TType : wf_db.
(*************)
(* WFS types *)
(*************)
Inductive WFS_vType {n} : vType n -> Prop :=
| WFS : forall T : vType n, Sing_vt T -> WF_vType T -> WFS_vType T.
Lemma WFS_I : WFS_vType I. Proof. apply WFS; auto with wfvt_db. Qed.
Lemma WFS_X : WFS_vType X. Proof. apply WFS; auto with wfvt_db. Qed.
Lemma WFS_Z : WFS_vType Z. Proof. apply WFS; auto with wfvt_db. Qed.
Lemma WFS_mul : forall {n} (A B : vType n),
WFS_vType A -> WFS_vType B ->
WFS_vType (A .* B).
Proof. intros n A B H H0.
inversion H; inversion H0.
apply WFS; auto with wfvt_db.
Qed.
Lemma WFS_tensor : forall {n m} (A : vType n) (B : vType m),
WFS_vType A -> WFS_vType B ->
WFS_vType (A .⊗ B).
Proof. intros n m A B H H0.
inversion H; inversion H0.
apply WFS; auto with wfvt_db.
Qed.
Lemma WFS_scale : forall {n} (A : vType n) (c : Coef),
WFS_vType A -> WFS_vType (scale c A).
Proof. intros n A c H.
inversion H.
apply WFS; auto with wfvt_db.
Qed.
Lemma WFS_neg : forall {n} (A : vType n),
WFS_vType A -> WFS_vType (- A).
Proof. intros n A [H H0].
apply WFS_scale; easy.
Qed.
Lemma WFS_i : forall {n} (A : vType n),
WFS_vType A -> WFS_vType (i A).
Proof. intros n A H.
unfold i.
apply WFS_scale; easy.
Qed.
Hint Resolve WFS_I WFS_X WFS_Z WFS_scale WFS_mul WFS_tensor WFS_neg WFS_i : wfvt_db.
(******************)
(* unitary lemmas *)
(******************)
Lemma unit_Pauli : forall (p : Pauli), WF_Unitary (translate_P p).
Proof. intros.
destruct p; simpl; auto with unit_db.
Qed.
Lemma unit_TType : forall {n} (A : TType n), WF_TType A -> WF_Unitary (translate A).
Proof. intros. destruct A.
unfold translate; simpl.
inversion H; simpl in *.
rewrite map_length, <- H0.
apply unit_scale; try (destruct c; lca).
rewrite <- (map_length translate_P _).
apply (unit_big_kron 2 (map translate_P l)).
intros.
apply in_map_iff in H2.
do 2 (destruct H2).
rewrite <- H2.
apply unit_Pauli.
Qed.
Lemma univ_TType : forall {n} (tt : TType n),
WF_TType tt -> uni_vecType ([translate tt]).
Proof. intros.
inversion H.
unfold uni_vecType.
intros A [H2 | F]; try easy.
rewrite <- H2.
apply unit_TType in H.
easy.
Qed.
Lemma unit_vType : forall {n} (A : vType n),
WF_vType A -> uni_vecType (translate_vecType A).
Proof. intros.
induction A as [| | |]; try easy.
- simpl. apply (univ_TType t).
inversion H;
easy.
- simpl.
destruct (Cap_vt_bool A1 && Cap_vt_bool A2); try easy.
simpl in H.
unfold uni_vecType; intros.
apply in_app_or in H0.
inversion H.
destruct H0 as [H5| H6].
+ apply IHA1 in H3.
apply H3; easy.
+ apply IHA2 in H4.
apply H4; easy.
Qed.
(******************************************************)
(* Showing translations preserves relevent properties *)
(******************************************************)
(* we actually use this to prove translate_mult, so we prove it first *)
Lemma translate_kron : forall {n m} (g1 : TType n) (g2 : TType m),
length (snd g1) = n -> length (snd g2) = m ->
translate (gTensorT g1 g2) = (translate g1) ⊗ (translate g2).
Proof. intros. unfold translate.
destruct g1; destruct g2.
simpl in *.
do 3 (rewrite map_length).
rewrite H, H0 in *.
rewrite Mscale_kron_dist_r.
rewrite Mscale_kron_dist_l.
rewrite Mscale_assoc.
bdestruct_all; simpl.
rewrite translate_coef_cMul.
rewrite Cmult_comm.
rewrite map_app.
assert (H3 : forall (l : list Pauli) (i0 : nat), WF_Matrix (nth i0 (map translate_P l) Zero)).
{ intros.
bdestruct (i0 <? length (map translate_P l1)).
+ apply (nth_In _ (@Zero 2 2)) in H3.
apply in_map_iff in H3.
destruct H3 as [x [H3 H4]].
rewrite <- H3; apply WF_Matrix_Pauli.
+ rewrite nth_overflow; try lia.
auto with wf_db. }
rewrite big_kron_app; auto.
do 2 (rewrite map_length).
rewrite app_length.
rewrite H, H0 in *.
reflexivity.
Qed.
Lemma gMulT_reduce : forall (n : nat) (c1 c2 : Coef) (p1 p2 : Pauli) (l1 l2 : list Pauli),
length l1 = n -> length l2 = n ->
gMulT (c1, p1 :: l1) (c2, p2 :: l2) =
@gTensorT 1 n (gMul_Coef p1 p2, [gMul_base p1 p2]) (gMulT (c1, l1) (c2, l2)).
Proof. intros. simpl.
rewrite H, H0.
bdestruct (n =? n); try lia.
rewrite (zipWith_len_pres _ n); try easy.
bdestruct (n =? n); try lia.
rewrite zipWith_cons.
apply injective_projections; try easy.
simpl.
unfold zipWith.
rewrite <- (cMul_assoc (c1 * c2)), (cMul_comm (c1 * c2)).
replace (uncurry gMul_Coef (p1, p2)) with (gMul_Coef p1 p2) by easy.
rewrite cMul_assoc; easy.
Qed.
Lemma translate_reduce : forall (n : nat) (c : Coef) (p : Pauli) (l : list Pauli),
length l = n ->
@translate (S n) (c, p :: l) = (translate_P p) ⊗ @translate n (c, l).
Proof. intros.
unfold translate.
simpl.
rewrite map_length.
replace (2^(length l) + (2^(length l) + 0))%nat with (2 * 2^(length l))%nat by lia.
rewrite <- Mscale_kron_dist_r.
rewrite H; easy.
Qed.
Lemma translate_Mmult : forall {n} (g1 g2 : TType n),
length (snd g1) = n -> length (snd g2) = n ->
translate (gMulT g1 g2) = (translate g1) × (translate g2).
Proof. intros. induction n as [| n'].
- destruct g1; destruct g2.
destruct l; destruct l0; try easy.
unfold translate. simpl.
distribute_scale.
rewrite Mmult_1_r; auto with wf_db.
rewrite <- translate_coef_cMul.
destruct c; destruct c0; try easy.
- destruct g1; destruct g2.
destruct l; destruct l0; try easy.
simpl in H; simpl in H0.
apply Nat.succ_inj in H.
apply Nat.succ_inj in H0.
rewrite gMulT_reduce; try easy.
replace (S n') with (1 + n')%nat by lia.
rewrite translate_kron; try easy.
rewrite IHn'; try easy.
rewrite (translate_reduce _ c), (translate_reduce _ c0); try easy.
restore_dims.
rewrite kron_mixed_product.
assert (H' : @translate 1 (gMul_Coef p p0, [gMul_base p p0]) =
translate_P p × translate_P p0).
{ destruct p; destruct p0; simpl.
all : unfold translate; simpl.
all : lma'. }
rewrite H'; easy.
simpl.
rewrite H, H0; bdestruct_all.
simpl.
apply zipWith_len_pres; easy.
Qed.
Lemma translate_vecType_mMult : forall {n} (A B : vType n),
WFS_vType A -> WFS_vType B ->
translate_vecType (A .* B) = (translate_vecType A) *' (translate_vecType B).
Proof. intros n A B H H0.
inversion H; inversion H0.
destruct A; destruct B; try easy.
simpl.
inversion H2; inversion H5.
inversion H8; inversion H10.
rewrite translate_Mmult; try easy.
Qed.
Lemma translate_scale : forall {n} (A : TType n) (c : Coef),
translate (gScaleT c A) = Matrix.scale (translate_coef c) (translate A).
Proof. intros.
unfold translate.
destruct A. simpl.
rewrite translate_coef_cMul.
rewrite <- Mscale_assoc.
reflexivity.
Qed.
Lemma Cap_vt_scale : forall {n} (A : vType n) (c : Coef),
Cap_vt_bool (scale c A) = Cap_vt_bool A.
Proof. intros. induction A as [| | |]; try easy.
simpl. rewrite IHA1, IHA2.
reflexivity.
Qed.
Lemma translate_vecType_scale : forall {n} (A : vType n) (c : Coef),
translate_vecType (scale c A) = (translate_coef c) · (translate_vecType A).
Proof. intros. induction A; try easy.
- simpl. rewrite translate_scale.
reflexivity.
- simpl translate_vecType.
do 2 (rewrite Cap_vt_scale).
destruct (Cap_vt_bool A1 && Cap_vt_bool A2); try easy.
rewrite IHA1, IHA2.
rewrite concat_into_scale.
reflexivity.
Qed.
(**************************)
(* Defining vector typing *)
(**************************)
(* we need this for now. should eventually rewrite defs to make proofs easier *)
Lemma fgt_conv : forall {n m} (A B : vecType n), [(A, B)] = @formGateType m A B.
Proof. easy. Qed.
Lemma ite_conv : forall {X : Type} (x1 x2 : X), (if true && true then x1 else x2) = x1.
Proof. easy. Qed.
Definition vecPair (prg_len : nat) := (Vector (2^prg_len) * C)%type.
Inductive vecHasType {prg_len : nat} : vecPair prg_len -> vType prg_len -> Prop :=
| VHT : forall vp T, Cap_vt T -> pairHasType vp (translate_vecType T) ->
vecHasType vp T.
Notation "p ;' T" := (vecHasType p T) (at level 61, no associativity).
Lemma cap_elim_l_vec : forall {n} (v : vecPair n) (A B : vType n), v ;' (A ∩ B) -> v ;' A.
Proof. intros.
inversion H; inversion H0.
apply VHT; try easy.
simpl translate_vecType in *.
apply Cap_vt_conv in H6;
apply Cap_vt_conv in H7.
rewrite H6, H7 in H1.
simpl in H1.
apply (Heisenberg.cap_elim_l_pair _ _ (translate_vecType B)).
assumption.
Qed.
Lemma cap_elim_r_vec : forall {n} (v : vecPair n) (A B : vType n), v ;' A ∩ B -> v ;' B.
Proof. intros.
inversion H; inversion H0.
apply VHT; try easy.
simpl translate_vecType in *.
apply Cap_vt_conv in H6;
apply Cap_vt_conv in H7.
rewrite H6, H7 in H1.
simpl in H1.
apply (Heisenberg.cap_elim_r_pair _ (translate_vecType A) _).
assumption.
Qed.
Hint Resolve cap_elim_l_vec cap_elim_r_vec : subtype_db.
(***************************************************************************)
(* proving some preliminary lemmas on the TType level before we prove their
counterparts on the vType level *)
(***************************************************************************)
Lemma gMulT_gTensorT_dist : forall {n m : nat} (t1 t2 : TType n) (t3 t4 : TType m),
WF_TType t1 -> WF_TType t2 -> WF_TType t3 -> WF_TType t4 ->
gMulT (gTensorT t1 t3) (gTensorT t2 t4) = gTensorT (gMulT t1 t2) (gMulT t3 t4).
Proof. intros.
destruct t1; destruct t2; destruct t3; destruct t4.
simpl gTensorT.
inversion H; inversion H0; inversion H1; inversion H2.
simpl in *.
rewrite H3, H5, H7, H9.
bdestruct_all. simpl.
rewrite (zipWith_len_pres _ n), (zipWith_len_pres _ m); try easy.
do 2 rewrite app_length.
rewrite H3, H5, H7, H9.
bdestruct_all. simpl.
apply injective_projections; simpl.
- rewrite (cMul_assoc (c * c0)).
rewrite (cMul_comm (cBigMul (zipWith gMul_Coef l l0))).
rewrite (cMul_assoc (c1 * c2)).
rewrite (cMul_comm (cBigMul (zipWith gMul_Coef l1 l2))).
rewrite cBigMul_app.
rewrite (zipWith_app_product _ n); try easy.
rewrite (cMul_assoc c), <- (cMul_assoc c1), (cMul_comm c1 c0).
repeat rewrite cMul_assoc; easy.
- rewrite (zipWith_app_product _ n); easy.
Qed.
Lemma gMulT_assoc : forall (n : nat) (t1 t2 t3 : TType n),
WF_TType t1 -> WF_TType t2 -> WF_TType t3 ->
gMulT (gMulT t1 t2) t3 = gMulT t1 (gMulT t2 t3).
Proof. induction n as [| n'].
- intros.
inversion H; inversion H0; inversion H1.
destruct t1; destruct t2; destruct t3.
destruct l; destruct l0; destruct l1; try easy.
destruct c; destruct c0; destruct c1; easy.
- intros.
inversion H; inversion H0; inversion H1.
destruct t1; destruct t2; destruct t3.
destruct l; destruct l0; destruct l1; try easy.
simpl in H2; simpl in H4; simpl in H6.
apply Nat.succ_inj in H2;
apply Nat.succ_inj in H4;
apply Nat.succ_inj in H6.
repeat rewrite gMulT_reduce; try easy.
assert (H8 : (c1, p1 :: l1) = @gTensorT 1 n' (p_1, [p1]) (c1, l1)).
{ simpl. bdestruct_all. easy. }
assert (H9 : (c, p :: l) = @gTensorT 1 n' (p_1, [p]) (c, l)).
{ simpl. bdestruct_all. easy. }
rewrite H8, H9.
do 2 replace (S n') with (1 + n')%nat by lia.
rewrite gMulT_gTensorT_dist, gMulT_gTensorT_dist; try easy.
rewrite IHn'; try easy.
assert (H10 : (@gMulT 1 (gMul_Coef p p0, [gMul_base p p0]) (p_1, [p1])) =
(@gMulT 1 (p_1, [p]) (gMul_Coef p0 p1, [gMul_base p0 p1]))).
{ destruct p; destruct p0; destruct p1; easy. }
rewrite H10; easy.
all : simpl; bdestruct_all; apply WF_tt; simpl.
all : rewrite (zipWith_len_pres _ n'); easy.
Qed.
(* Multiplication laws *)
Lemma mul_assoc : forall {n} (A B C : vType n),
WFS_vType A -> WFS_vType B -> WFS_vType C ->
A .* (B .* C) = A .* B .* C.
Proof. intros.
destruct A; destruct B; destruct C; try easy.
inversion H; inversion H0; inversion H1.
unfold mul.
inversion H3; inversion H6; inversion H9.
rewrite gMulT_assoc; easy.
Qed.
Lemma mul_I_l : forall (A : vType 1), WFS_vType A -> I .* A = A.
Proof. intros A H.
inversion H.
destruct A; try easy.
inversion H1; inversion H4.
destruct t.
do 2 (destruct l; try easy).
simpl.
destruct c; easy.
Qed.
Lemma mul_I_r : forall (A : vType 1), WFS_vType A -> A .* I = A.
Proof. intros A H.
inversion H.
destruct A; try easy.
inversion H1; inversion H4.
destruct t.
do 2 (destruct l; try easy).
simpl.
destruct p; destruct c; easy.
Qed.
Lemma Xsqr : X .* X = I.
Proof. easy. Qed.
Lemma Zsqr : Z .* Z = I.
Proof. easy. Qed.
Lemma ZmulX : Z .* X = - (X .* Z).
Proof. easy. Qed.
Lemma neg_inv : forall (n : nat) (A : vType n), WFS_vType A -> - - A = A.
Proof. intros n A H.
inversion H.
destruct A; try easy.
simpl.
unfold gScaleT.
destruct t; destruct c; easy.
Qed.
Lemma neg_dist_l : forall (n : nat) (A B : vType n),
WFS_vType A -> WFS_vType B ->
-A .* B = - (A .* B).
Proof. intros.
inversion H; inversion H0.
destruct A; destruct B; try easy.
destruct t; destruct t0; simpl.
inversion H2; inversion H5.
inversion H8; inversion H10.
simpl in *.
rewrite H11, H13.
bdestruct_all; try easy.
unfold gScaleT.
repeat rewrite cMul_assoc.
easy.
Qed.
Lemma neg_dist_r : forall (n : nat) (A B : vType n),
WFS_vType A -> WFS_vType B ->
A .* (-B) = - (A .* B).
Proof. intros.
inversion H; inversion H0.
destruct A; destruct B; try easy.
destruct t; destruct t0; simpl.
inversion H2; inversion H5.
inversion H8; inversion H10.
simpl in *.
rewrite H11, H13.
bdestruct_all; try easy.
unfold gScaleT.
rewrite <- cMul_assoc, (cMul_comm c).
repeat rewrite cMul_assoc.
easy.
Qed.