-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredicates.v
More file actions
5703 lines (5144 loc) · 201 KB
/
Predicates.v
File metadata and controls
5703 lines (5144 loc) · 201 KB
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 Export HeisenbergFoundations.Preamble.
Declare Scope Predicate_scope.
Delimit Scope Predicate_scope with P.
Local Open Scope Predicate_scope.
(************************)
(* Defining coeficients *)
(************************)
Definition Coef := C.
Definition cBigMul (cs : list Coef) : Coef :=
fold_left Cmult cs C1.
Lemma cBigMul_app : forall (l1 l2 : list Coef),
(cBigMul l1) * (cBigMul l2) = cBigMul (l1 ++ l2).
Proof. induction l1 as [| h]; try easy.
intros. simpl.
- unfold cBigMul. simpl. lca.
- intros l2. simpl. unfold cBigMul. simpl.
rewrite 2 fold_left_Cmult. rewrite <- Cmult_assoc.
unfold cBigMul in IHl1.
rewrite IHl1; easy.
Qed.
(**********************)
(* Defining the types *)
(**********************)
(* this is the lowest level, only Pauli gates are defined *)
Inductive Pauli :=
| gI
| gX
| gY
| gZ.
Definition beq_Pauli (a b : Pauli) : bool :=
match a, b with
| gI, gI => true
| gZ, gI => false
| gY, gI => false
| gX, gI => false
| gI, gZ => false
| gZ, gZ => true
| gY, gZ => false
| gX, gZ => false
| gI, gY => false
| gZ, gY => false
| gY, gY => true
| gX, gY => false
| gI, gX => false
| gZ, gX => false
| gY, gX => false
| gX, gX => true
end.
Lemma eqdec_Pauli (a b : Pauli) : { a = b } + { a <> b }.
Proof. destruct a, b.
all : try (left; reflexivity); try (right; intro; discriminate).
Qed.
Definition not_gI (g : Pauli) : Prop := g = gX \/ g = gY \/ g = gZ.
Lemma not_gI_equiv : forall (g : Pauli), not_gI g <-> g <> gI.
Proof. intros g.
unfold not_gI.
split; intros.
- intro; subst.
repeat (destruct H; try discriminate).
- destruct g; try contradiction.
+ left; reflexivity.
+ right; left; reflexivity.
+ right; right; reflexivity.
Qed.
Definition translate_P (g : Pauli) : Square 2 :=
match g with
| gI => I 2
| gX => σx
| gY => σy
| gZ => σz
end.
Lemma list_Pauli_Hermitian : forall (l : list Pauli), (⨂ map translate_P l) † = ⨂ map translate_P l.
Proof. intros l. induction l.
- simpl. lma'.
- simpl. setoid_rewrite kron_adjoint. rewrite IHl.
destruct a; simpl.
+ replace (Matrix.I 2) † with (Matrix.I 2) by lma'. reflexivity.
+ replace (σx) † with (σx) by lma'. reflexivity.
+ replace (σy) † with (σy) by lma'. reflexivity.
+ replace (σz) † with (σz) by lma'. reflexivity.
Qed.
Lemma WF_Matrix_Pauli : forall (g : Pauli), WF_Matrix (translate_P g).
Proof. intros.
destruct g; simpl; auto with wf_db.
Qed.
Lemma In_list_WF_Matrix_Pauli : forall (A : list Pauli),
forall a : Square 2, In a (map translate_P A) -> WF_Matrix a.
Proof. induction A; intros.
- simpl in H. exfalso. assumption.
- simpl in H. destruct H.
+ rewrite <- H. apply WF_Matrix_Pauli.
+ apply IHA. assumption.
Qed.
Lemma WF_Matrix_nth_Pauli : forall (l : list Pauli),
forall i : nat, WF_Matrix (nth i (map translate_P l) Zero).
Proof. intros. destruct (nth_in_or_default i (map translate_P l) Zero).
- apply In_list_WF_Matrix_Pauli with (A := l). assumption.
- rewrite e. auto with wf_db.
Qed.
Lemma WF_Unitary_Pauli : forall (g : Pauli), WF_Unitary (translate_P g).
Proof. intros.
destruct g; simpl; auto with unit_db.
Qed.
Lemma WF_Matrix_Big_Pauli : forall (l : list Pauli), WF_Matrix (⨂ map translate_P l).
Proof. intros.
induction l; simpl; auto with wf_db.
apply Matrix.WF_kron; try lia; try apply IHl.
apply WF_Matrix_Pauli.
Qed.
#[export] Hint Resolve WF_Matrix_Pauli WF_Matrix_Big_Pauli : wf_db.
#[export] Hint Resolve WF_Unitary_Pauli : unit_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, _ => C1
| _, gI => C1
| gX, gX => C1
| gY, gY => C1
| gZ, gZ => C1
| gX, gY => Ci
| gY, gX => (- Ci)%C
| gX, gZ => (-Ci)%C
| gZ, gX => Ci
| gY, gZ => Ci
| gZ, gY => (-Ci)%C
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, and tensoring done at this level *)
Definition TType (len : nat) := (Coef * (list Pauli))%type.
Definition TTypes := (Coef * (list Pauli))%type.
Definition ForgetT {len : nat} (t : TType len) := (t : TTypes).
Definition AssignT (len : nat) (t : TTypes) := (t : TType len).
Lemma AssignTForgetT : forall (len : nat) (t : TType len),
AssignT len (ForgetT t) = (t : TType len).
Proof. intros. unfold AssignT, ForgetT. auto. Qed.
Lemma ForgetTAssignT : forall (len : nat) (t : TTypes),
ForgetT (AssignT len t) = (t : TTypes).
Proof. intros. unfold AssignT, ForgetT. auto. Qed.
Definition PtoT (P : Pauli) : TType 1%nat := (C1, [P]).
Coercion PtoT : Pauli >-> TType.
Definition defaultT_Z (n : nat) : TType n := (C1, repeat gZ n).
Definition defaultT_I (n : nat) : TType n := (C1, repeat gI n).
(* we define an error TType for when things go wrong *)
Definition ErrT : TType 0 := (C1, []).
Definition proper_length_TType_nil {n : nat} (t : TType n) : Prop := length (snd t) = n.
Definition proper_length_TType {n : nat} (t : TType n) : Prop := n <> O /\ length (snd t) = n.
Lemma proper_length_TType_implies_proper_length_TType_nil : forall {n} (t : TType n),
proper_length_TType t -> proper_length_TType_nil t.
Proof. intros. destruct H. auto. Qed.
Lemma proper_length_TType_defaultT_I : forall (n : nat),
n <> 0%nat -> proper_length_TType (defaultT_I n).
Proof. intros n H.
unfold proper_length_TType.
split; auto.
unfold defaultT_I.
simpl. rewrite repeat_length; auto.
Qed.
Lemma proper_length_TType_defaultT_Z : forall (n : nat),
n <> 0%nat -> proper_length_TType (defaultT_Z n).
Proof. intros n H.
unfold proper_length_TType.
split; auto.
unfold defaultT_Z.
simpl. rewrite repeat_length; auto.
Qed.
Definition gMulT {n} (A B : TType n) : TType n :=
match A with
| (c1, g1) =>
match B with
| (c2, g2) =>(c1 * c2 * (cBigMul (zipWith gMul_Coef g1 g2)),
zipWith gMul_base g1 g2)
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) => (c1 * c2, g1 ++ g2)
end
end.
Definition gScaleT {n} (c : Coef) (A : TType n) : TType n :=
match A with
| (c1, g1) => (c * c1, g1)
end.
Definition gScaleTTypes (c : Coef) (A : TTypes) : TTypes :=
match A with
| (c1, g1) => (c * c1, g1)
end.
Definition translate {n} (A : TType n) : Square (2 ^ n)%nat :=
(fst A) .* ⨂ (map translate_P (snd A)).
Lemma translate_defaultT_I : forall {n : nat},
translate (defaultT_I n) = I (2 ^ n)%nat.
Proof. intros n. unfold translate. simpl.
rewrite Mscale_1_l.
induction n; auto.
replace (2 ^ (S n))%nat with (2 * (2 ^ n))%nat by (simpl; lia).
rewrite <- id_kron. simpl. rewrite ! IHn.
rewrite map_length, repeat_length; auto.
Qed.
Lemma gScaleT_1 : forall n (A : TType n), gScaleT C1 A = A.
Proof. intros n A. destruct A. simpl. rewrite Cmult_1_l. reflexivity. Qed.
Lemma gScaleT_comm : forall {n} (c1 c2 : Coef) (t : TType n),
gScaleT c1 (gScaleT c2 t) = gScaleT c2 (gScaleT c1 t).
Proof. intros n c1 c2 t.
unfold gScaleT. destruct t. f_equal. lca.
Qed.
Lemma gScaleT_merge : forall {n} (c1 c2 : Coef) (t : TType n),
gScaleT c1 (gScaleT c2 t) = gScaleT (c1 * c2)%C t.
Proof. intros n c1 c2 t.
unfold gScaleT. destruct t. f_equal. lca.
Qed.
Lemma gScaleTTypes_1 : forall (A : TTypes), gScaleTTypes C1 A = A.
Proof. intros A. destruct A. simpl. rewrite Cmult_1_l. reflexivity. Qed.
Lemma gScaleTTypes_comm : forall (c1 c2 : Coef) (t : TTypes),
gScaleTTypes c1 (gScaleTTypes c2 t) = gScaleTTypes c2 (gScaleTTypes c1 t).
Proof. intros c1 c2 t.
unfold gScaleTTypes. destruct t. f_equal. lca.
Qed.
Lemma gScaleTTypes_merge : forall (c1 c2 : Coef) (t : TTypes),
gScaleTTypes c1 (gScaleTTypes c2 t) = gScaleTTypes (c1 * c2)%C t.
Proof. intros c1 c2 t.
unfold gScaleTTypes. destruct t. f_equal. lca.
Qed.
Lemma proper_length_TType_gScaleT : forall {n : nat} (c : Coef) (t : TType n),
proper_length_TType t -> proper_length_TType (gScaleT c t).
Proof. intros n c t H. destruct t. unfold proper_length_TType in *. simpl in *. assumption.
Qed.
(* Additive Type: list elements are added to each other *)
(* len is the number of qubits (= number of tensoring elements) *)
Definition AType (len : nat) := list (TType len).
Definition TtoA {n : nat} (t : TType n) : AType n := [t].
Coercion TtoA : TType >-> AType.
Definition AtoT {n : nat} (a : AType n) : TType n :=
hd (defaultT_I n) a.
(* we define an error AType for when things go wrong *)
Definition ErrA : AType 0 := [].
Definition defaultA_Z (n : nat) : AType n := [defaultT_Z n].
Definition defaultA_I (n : nat) : AType n := [defaultT_I n].
Inductive proper_length_AType_nil (n : nat) : AType n -> Prop :=
| proper_length_A_nil_Base : proper_length_AType_nil n nil
| proper_length_A_nil_Cons (t : TType n) (a : AType n) : proper_length_TType t -> proper_length_AType_nil n a -> proper_length_AType_nil n (t :: a).
Arguments proper_length_AType_nil {n}.
Inductive proper_length_AType (n : nat) : AType n -> Prop :=
| proper_length_A_Sing (t : TType n) : proper_length_TType t -> proper_length_AType n (cons t nil)
| proper_length_A_Cons (t : TType n) (a : AType n) : proper_length_TType t -> proper_length_AType n a -> proper_length_AType n (t :: a).
Arguments proper_length_AType {n}.
Lemma proper_length_AType_implies_proper_length_AType_nil : forall {n} (a : AType n),
proper_length_AType a -> proper_length_AType_nil a.
Proof. intros. induction H; constructor; try easy; constructor. Qed.
Fixpoint gTensorA {n m : nat} (a : AType n) (b : AType m) {struct a}: AType (n+m) :=
match a with
| [] => []
| h :: t => map (fun x : TType m => gTensorT h x) b ++ gTensorA t b
end.
Fixpoint gTensorA' {n m : nat} (a : AType n) (b : AType m) {struct b}: AType (n+m) :=
match b with
| [] => []
| h :: t => map (fun x : TType n => gTensorT x h) a ++ gTensorA' a t
end.
Fixpoint gMulA {n : nat} (a b : AType n) {struct a} : AType n :=
match a with
| [] => []
| h :: t => map (fun x : TType n => gMulT h x) b ++ gMulA t b
end.
Fixpoint gMulA' {n : nat} (a b : AType n) {struct b} : AType n :=
match b with
| [] => []
| h :: t => map (fun x : TType n => gMulT x h) a ++ gMulA' a t
end.
Lemma gMulA_nil_r : forall n (a : AType n), gMulA a [] = [].
Proof. intros n a. induction a; try easy. Qed.
Lemma gMulA'_nil_l : forall n (a : AType n), gMulA [] a = [].
Proof. intros n a. induction a; try easy. Qed.
Definition gScaleA {n : nat} (c : Coef) (a : AType n) :=
map (fun a' => gScaleT c a') a .
Definition gAddA {n : nat} (a b : AType n) : AType n := a ++ b.
Definition translateA {n} (a : AType n) : Square (2 ^ n) :=
fold_left Mplus (map translate a) Zero.
Lemma translateA_defaultA_I : forall {n : nat}, translateA (defaultA_I n) = I (2 ^ n)%nat.
Proof. intros n. unfold translateA, defaultA_I. simpl.
rewrite Mplus_0_l. apply translate_defaultT_I.
Qed.
Lemma translateA_app : forall {n} (a1 a2 : AType n),
translateA (a1 ++ a2) = (translateA a1) .+ (translateA a2).
Proof. intros. unfold translateA. rewrite map_app.
rewrite fold_left_Mplus_app_Zero. reflexivity.
Qed.
Lemma gScaleA_1 : forall n (A : AType n), gScaleA C1 A = A.
Proof. intros n A. induction A; simpl; try easy. rewrite gScaleT_1. rewrite IHA. reflexivity. Qed.
Lemma gScaleA_dist_app : forall {n} (c : Coef) (a1 a2 : AType n),
gScaleA c (a1 ++ a2) = (gScaleA c a1) ++ (gScaleA c a2).
Proof. intros n c a1 a2.
unfold gScaleA. apply map_app.
Qed.
Lemma gScaleA_comm : forall {n} (c1 c2 : Coef) (a : AType n),
gScaleA c1 (gScaleA c2 a) = gScaleA c2 (gScaleA c1 a).
Proof. intros n c1 c2 a.
unfold gScaleA. rewrite ! map_map.
f_equal. apply functional_extensionality.
intros. rewrite gScaleT_comm.
reflexivity.
Qed.
Lemma gScaleA_merge : forall {n} (c1 c2 : Coef) (a : AType n),
gScaleA c1 (gScaleA c2 a) = gScaleA (c1 * c2)%C a.
Proof. intros n c1 c2 a.
unfold gScaleA. rewrite ! map_map.
f_equal. apply functional_extensionality.
intros. apply gScaleT_merge.
Qed.
Lemma in_gScaleTA_mult : forall {n} (c: Coef) (t : TType n) (a : AType n),
In t a -> In (gScaleT c t) (gScaleA c a).
Proof. intros n c t a H.
induction a.
- inversion H.
- inversion H; subst; clear H.
+ simpl; auto.
+ apply IHa in H0.
simpl; auto.
Qed.
Lemma in_gScaleTA_mult_inv : forall {n} (c: Coef) (t : TType n) (a : AType n),
c <> C0 -> In (gScaleT c t) (gScaleA c a) -> In t a.
Proof. intros n c t a H H0.
apply in_gScaleTA_mult with (c := /c) in H0.
rewrite gScaleT_merge, gScaleA_merge in H0.
rewrite Cinv_l in H0; auto.
rewrite gScaleT_1, gScaleA_1 in H0; auto.
Qed.
Lemma proper_length_AType_nil_gScaleA : forall {n : nat} (c : Coef) (a : AType n),
proper_length_AType_nil a -> proper_length_AType_nil (gScaleA c a).
Proof. intros n c a H. induction H.
- constructor.
- simpl in *. constructor.
+ apply proper_length_TType_gScaleT. assumption.
+ assumption.
Qed.
Lemma proper_length_AType_gScaleA : forall {n : nat} (c : Coef) (a : AType n),
proper_length_AType a -> proper_length_AType (gScaleA c a).
Proof. intros n c a H. induction H.
- constructor. apply proper_length_TType_gScaleT. assumption.
- simpl in *. constructor.
+ apply proper_length_TType_gScaleT. assumption.
+ assumption.
Qed.
Lemma proper_length_AType_nil_App : forall {n : nat} (a1 a2 : AType n),
proper_length_AType_nil a1 -> proper_length_AType_nil a2 ->
proper_length_AType_nil (a1 ++ a2).
Proof. intros n a1 a2 H H0.
induction H; simpl; try constructor; assumption.
Qed.
Lemma proper_length_AType_App : forall {n : nat} (a1 a2 : AType n),
proper_length_AType a1 -> proper_length_AType a2 ->
proper_length_AType (a1 ++ a2).
Proof. intros n a1 a2 H H0.
induction H; simpl; constructor; assumption.
Qed.
Inductive Predicate (n : nat) : Type :=
| AtoPred : AType n -> Predicate n
| Cap : list (AType n) -> Predicate n
| Sep : (list nat) * (list (list TTypes)) * (list nat) -> Predicate n
| Cup : Predicate n -> Predicate n -> Predicate n
| Err : Predicate n.
Coercion AtoPred : AType >-> Predicate.
Arguments AtoPred {n}.
Arguments Cap {n}.
Arguments Sep {n}.
Arguments Cup {n}.
Arguments Err {n}.
Definition defaultP_Z (n : nat) : Predicate n := AtoPred (defaultA_Z n).
Definition defaultP_I (n : nat) : Predicate n := AtoPred (defaultA_I n).
Fixpoint pad_Sep_listP (Lp : list Pauli) (Ln : list nat) (m : nat) :=
match Ln with
| [] => repeat gI m
| n :: Ln' =>
match Lp with
| [] => repeat gI m
| p :: Lp' => switch (pad_Sep_listP Lp' Ln' m) p n
end
end.
Lemma pad_Sep_listP_length : forall (Lp : list Pauli) (Ln : list nat) (m : nat),
length (pad_Sep_listP Lp Ln m) = m.
Proof. intros Lp Ln m.
gen Lp m.
induction Ln.
- induction Lp; intros; simpl;
rewrite repeat_length; auto.
- induction Lp; intros; simpl;
try rewrite repeat_length; auto.
rewrite switch_len.
apply IHLn.
Qed.
Lemma pad_Sep_listP_nil_l : forall (Ln : list nat) (m : nat),
pad_Sep_listP [] Ln m = repeat gI m.
Proof. intros Ln m.
induction Ln; auto.
Qed.
Lemma pad_Sep_listP_nil_r : forall (Lp : list Pauli) (m : nat),
pad_Sep_listP Lp [] m = repeat gI m.
Proof. intros Lp m.
induction Lp; auto.
Qed.
Lemma pad_Sep_listP_nth : forall (i m : nat) (Ln : list nat) (Lp : list Pauli),
NoDup Ln -> (i < length Ln)%nat -> length Ln = length Lp -> incl Ln (List.seq 0 m) ->
nth (nth i Ln 0%nat) (pad_Sep_listP Lp Ln m) gI = nth i Lp gI.
Proof. intros i m Ln Lp H H0 H1 H2.
gen Ln Lp i.
induction m.
- induction Ln; intros; simpl in *.
+ inversion H0.
+ apply incl_cons_inv in H2.
destruct H2.
inversion H2.
- induction Ln; intros.
+ inversion H0.
+ destruct Lp.
* rewrite pad_Sep_listP_nil_l.
rewrite nth_repeat.
destruct i; auto.
* simpl.
destruct i.
-- rewrite nth_switch_hit; auto.
rewrite pad_Sep_listP_length.
apply incl_cons_inv in H2.
destruct H2.
rewrite in_seq in H2.
lia.
-- rewrite NoDup_cons_iff in H.
destruct H.
bdestruct (nth i Ln 0%nat =? a)%nat.
++ subst.
simpl in H0.
rewrite <- Nat.succ_lt_mono in H0.
assert (In (nth i Ln 0%nat) Ln).
{ apply nth_In; lia. }
contradiction.
++ simpl in H0.
rewrite <- Nat.succ_lt_mono in H0.
rewrite nth_switch_miss; auto.
rewrite IHLn; auto.
apply incl_cons_inv in H2.
destruct H2; auto.
Qed.
Lemma pad_Sep_listP_not_in : forall (m k : nat) (Ln : list nat) (Lp : list Pauli),
NoDup Ln -> length Ln = length Lp -> incl Ln (List.seq 0 m) ->
~ In k Ln -> nth k (pad_Sep_listP Lp Ln m) gI = gI.
Proof. intros m k Ln Lp H H0 H1 H2.
bdestruct (k <? m).
- destruct Lp as [ | p Lp'].
+ rewrite pad_Sep_listP_nil_l.
rewrite nth_repeat; auto.
+ destruct Ln as [ | n Ln'].
* rewrite pad_Sep_listP_nil_r.
rewrite nth_repeat; auto.
* simpl.
bdestruct (k =? n)%nat.
-- subst.
apply not_in_cons in H2.
destruct H2. contradiction.
-- rewrite nth_switch_miss; auto.
apply incl_cons_inv in H1.
destruct H1.
apply not_in_cons in H2.
destruct H2.
rewrite NoDup_cons_iff in H.
destruct H.
clear H. simpl in H0. clear H2. clear H1. clear H4.
clear n. clear p.
apply Nat.succ_inj in H0.
gen Lp'.
induction Ln'; intros.
++ rewrite pad_Sep_listP_nil_r, nth_repeat; auto.
++ destruct Lp'.
** rewrite pad_Sep_listP_nil_l, nth_repeat; auto.
** simpl in H0. apply Nat.succ_inj in H0.
rewrite NoDup_cons_iff in H7.
destruct H7.
apply incl_cons_inv in H5.
destruct H5.
rewrite not_in_cons in H6.
destruct H6.
simpl.
rewrite nth_switch_miss; auto.
- rewrite nth_overflow; auto; rewrite pad_Sep_listP_length; lia.
Qed.
Lemma pad_Sep_listP_seq : forall (Lp : list Pauli) (m : nat),
length Lp = m -> pad_Sep_listP Lp (List.seq 0 m) m = Lp.
Proof. intros Lp m H.
apply nth_ext with (d := gI) (d' := gI).
- rewrite pad_Sep_listP_length, H; auto.
- intros. rewrite pad_Sep_listP_length in H0.
assert (nth n (List.seq 0 m) 0%nat = n).
{ rewrite seq_nth; auto. }
setoid_rewrite <- H1 at 1.
rewrite pad_Sep_listP_nth; auto.
apply seq_NoDup.
rewrite seq_length; lia.
rewrite seq_length, H; lia.
apply incl_refl.
Qed.
Definition unpad_Sep_listP (Lp : list Pauli) (Ln : list nat) := map (fun n => nth n Lp gI) Ln.
Lemma unpad_pad_Sep_listP : forall (Lp : list Pauli) (Ln : list nat) (m : nat),
NoDup Ln -> length Ln = length Lp -> incl Ln (List.seq 0 m) ->
unpad_Sep_listP (pad_Sep_listP Lp Ln m) Ln = Lp.
Proof. intros Lp Ln m H H0 H1.
unfold unpad_Sep_listP.
apply nth_ext with (d := gI) (d' := gI).
- rewrite map_length; auto.
- intros n H2.
rewrite map_length in H2.
assert ((fun n0 : nat => nth n0 (pad_Sep_listP Lp Ln m) gI) m = gI).
{ assert (~ In m Ln).
{ intro. apply H1 in H3. rewrite in_seq in H3. lia. }
rewrite pad_Sep_listP_not_in; auto. }
rewrite <- H3 at 1.
rewrite map_nth with (d := m).
rewrite nth_indep with (d' := 0%nat); auto.
rewrite pad_Sep_listP_nth; auto.
Qed.
Definition pad_Sep_TTypes (t : TTypes) (l : list nat) (m : nat) : TType m :=
(fst t, pad_Sep_listP (snd t) l m).
Definition pad_Sep_TType {n : nat} (t : TType n) (l : list nat) (m : nat) : TType m :=
(fst t, pad_Sep_listP (snd t) l m).
Lemma pad_Sep_TType_seq : forall {n : nat} (t : TType n) (m : nat),
proper_length_TType t -> m = n -> pad_Sep_TType t (List.seq 0 m) m = t.
Proof. intros n t m H H0.
subst.
destruct t as [c l]; simpl in *.
unfold pad_Sep_TType; simpl.
rewrite pad_Sep_listP_seq; auto.
destruct H as [H H']; simpl in *; auto.
Qed.
Lemma proper_length_TType_pad_Sep_TType : forall {n : nat} (t : TType n) (l : list nat) (m : nat),
m <> 0%nat -> proper_length_TType (pad_Sep_TType t l m).
Proof. intros n t l m H.
destruct t.
unfold proper_length_TType, pad_Sep_TType in *.
simpl in *.
split; try rewrite pad_Sep_listP_length; auto.
Qed.
Definition unpad_Sep_TTypes (t : TTypes) (l : list nat) : TType (length l) :=
(fst t, unpad_Sep_listP (snd t) l).
Definition unpad_Sep_TType {n : nat} (t : TType n) (l : list nat) : TType (length l) :=
(fst t, unpad_Sep_listP (snd t) l).
Lemma unpad_pad_Sep_TType : forall {n : nat} (t : TType n) (l : list nat) (m : nat),
proper_length_TType t ->
NoDup l -> length l = n -> incl l (List.seq 0 m) ->
unpad_Sep_TType (pad_Sep_TType t l m) l = t.
Proof. intros n t l m H H0 H1 H2.
unfold unpad_Sep_TType, pad_Sep_TType.
inversion H. subst. destruct t. simpl in *. f_equal.
apply unpad_pad_Sep_listP; auto.
Qed.
Definition pad_Sep_AType {n : nat} (a : AType n) (l : list nat) (m : nat) : AType m :=
map (fun t => pad_Sep_TType t l m) a.
Lemma pad_Sep_AType_seq_nil : forall {n : nat} (a : AType n) (m : nat),
proper_length_AType_nil a -> m = n -> pad_Sep_AType a (List.seq 0 m) m = a.
Proof. intros n a m H H0.
subst.
unfold pad_Sep_AType.
induction a; auto.
inversion H; subst.
simpl. f_equal; auto.
apply pad_Sep_TType_seq; auto.
Qed.
Lemma pad_Sep_AType_seq : forall {n : nat} (a : AType n) (m : nat),
proper_length_AType a -> m = n -> pad_Sep_AType a (List.seq 0 m) m = a.
Proof. intros. subst.
apply pad_Sep_AType_seq_nil; auto.
apply proper_length_AType_implies_proper_length_AType_nil; auto.
Qed.
Lemma proper_length_AType_nil_pad_Sep_AType : forall {n : nat} (a : AType n) (l : list nat) (m : nat),
m <> 0%nat -> proper_length_AType_nil (pad_Sep_AType a l m).
Proof. intros n a l m H.
unfold pad_Sep_AType in *.
induction a.
- simpl. constructor.
- simpl. constructor; auto.
apply proper_length_TType_pad_Sep_TType; auto.
Qed.
Lemma proper_length_AType_pad_Sep_AType : forall {n : nat} (a : AType n) (l : list nat) (m : nat),
m <> 0%nat -> a <> [] -> proper_length_AType (pad_Sep_AType a l m).
Proof. intros n a l m H H0.
unfold pad_Sep_AType in *.
induction a.
- simpl. contradiction.
- simpl.
destruct a0.
+ simpl. constructor.
apply proper_length_TType_pad_Sep_TType; auto.
+ constructor; auto.
* apply proper_length_TType_pad_Sep_TType; auto.
* assert (t :: a0 <> []). { intro. inversion H1. }
apply IHa in H1; auto.
Qed.
Definition unpad_Sep_AType {n : nat} (a : AType n) (l : list nat) : AType (length l) :=
map (fun t => unpad_Sep_TType t l) a.
Lemma unpad_pad_Sep_AType : forall {n : nat} (a : AType n) (l : list nat) (m : nat),
proper_length_AType a ->
NoDup l -> length l = n -> incl l (List.seq 0 m) ->
unpad_Sep_AType (pad_Sep_AType a l m) l = a.
Proof. intros n a l m H H0 H1 H2.
unfold unpad_Sep_AType, pad_Sep_AType.
rewrite map_map.
induction H; subst; simpl in *; f_equal; try apply unpad_pad_Sep_TType; auto.
Qed.
Definition translateP {n} (p : Predicate n) :=
match p with
| AtoPred a => translateA a
| _ => Zero
end.
Lemma translateP_defaultP_I : forall {n : nat},
(0 < n)%nat -> translateP (defaultP_I n) = I (2 ^ n)%nat.
Proof. intros n H.
unfold translateP, defaultP_I.
unfold defaultP_I.
apply translateA_defaultA_I; auto.
Qed.
(* you cannot multiply cap or cup types
so any of these options returns Err *)
Definition mul {n} (A B : Predicate n) : Predicate n :=
match A with
| AtoPred a =>
match B with
| AtoPred b => AtoPred (gMulA a b)
| _ => Err
end
| _ => Err
end.
Definition add {n} (A B : Predicate n) : Predicate n :=
match A with
| AtoPred a =>
match B with
| AtoPred b => AtoPred (gAddA a b)
| _ => Err
end
| _ => Err
end.
Definition tensor {n m} (A : Predicate n) (B : Predicate m): Predicate (n + m) :=
match A with
| AtoPred a =>
match B with
| AtoPred b => AtoPred (gTensorA a b)
| _ => Err
end
| _ => Err
end.
(** We define scale to work for all predicate structures.
This will allow - - p = p for any predicate p. **)
Fixpoint scale {n} (c : Coef) (A : Predicate n) : Predicate n :=
match A with
| AtoPred a => AtoPred (gScaleA c a)
| Cap la => Cap (map (gScaleA c) la)
| Sep Ln_LLT_Perm => Sep (fst (fst Ln_LLT_Perm), (map (fun LT => map (fun T => gScaleTTypes c T) LT) (snd (fst Ln_LLT_Perm))), snd Ln_LLT_Perm)
| Cup a b => Cup (scale c a) (scale c b)
| Err => Err
end.
Lemma scale_merge : forall {n : nat} (c1 c2 : Coef) (P : Predicate n),
scale c2 (scale c1 P) = scale (c2 * c1) P.
Proof. intros n c1 c2 P.
induction P; simpl; try rewrite gScaleA_merge; try rewrite IHP1, IHP2; auto.
- rewrite map_map. do 2 f_equal.
apply functional_extensionality; intros.
rewrite gScaleA_merge; auto.
- rewrite ! map_map. do 4 f_equal.
apply functional_extensionality; intros.
rewrite map_map. f_equal.
apply functional_extensionality; intros.
rewrite gScaleTTypes_merge; auto.
Qed.
#[export] Hint Rewrite gMulA_nil_r gMulA'_nil_l gScaleT_1 gScaleA_1 : typing_db.
Notation "- A" := (scale (Copp C1) A) (at level 35, right associativity) : Predicate_scope.
Notation "+i A" := (scale Ci A) (at level 35, right associativity) : Predicate_scope.
Notation "-i A" := (scale (Copp Ci) A) (at level 35, right associativity) : Predicate_scope.
Lemma neg_inv : forall {n : nat} (p : Predicate n), (- (- p)) = p.
Proof. intros n p.
induction p; simpl; try (rewrite IHp1, IHp2); try rewrite IHp; auto.
- rewrite gScaleA_merge.
replace (- C1 * - C1) with C1 by lca.
rewrite gScaleA_1; auto.
- rewrite map_map. f_equal.
assert (H : (fun x : AType n => gScaleA (- C1)%C (gScaleA (- C1)%C x))
= (fun x : AType n => x)).
{ apply functional_extensionality. intros.
rewrite gScaleA_merge.
assert (H : (- C1 * - C1) = C1). { lca. }
rewrite H, gScaleA_1. auto. }
setoid_rewrite H. rewrite map_id. auto.
- f_equal. do 2 destruct p. simpl. do 2 f_equal.
rewrite ! map_map. setoid_rewrite <- map_id at 6.
f_equal. apply functional_extensionality; intros.
rewrite map_map. setoid_rewrite <- map_id at 3.
f_equal. apply functional_extensionality; intros.
rewrite gScaleTTypes_merge.
replace ((- C1) * (- C1))%C with C1 by lca.
rewrite gScaleTTypes_1. auto.
Qed.
Infix "⊗'" := tensor (at level 39, left associativity) : Predicate_scope.
Infix "*'" := mul (at level 40, left associativity) : Predicate_scope.
Infix "·'" := scale (at level 43, left associativity) : Predicate_scope.
Infix "+'" := add (at level 50, left associativity) : Predicate_scope.
Notation "⋂ la" := (Cap la) (at level 61, left associativity) : Predicate_scope.
Notation "A ⊍ B" := (Cup A B) (at level 61, left associativity) : Predicate_scope.
Notation "∩ Ln_LLT_Perm" := (Sep Ln_LLT_Perm) (at level 30, no associativity) : Predicate_scope.
Notation tI := (C1, [gI]).
Notation tX := (C1, [gX]).
Notation tY := (C1, [gY]).
Notation tZ := (C1, [gZ]).
Notation tII := (C1, [gI; gI]).
Notation tXI := (C1, [gX; gI]).
Notation tIX := (C1, [gI; gX]).
Notation tXX := (C1, [gX; gX]).
Notation tYI := (C1, [gY; gI]).
Notation tIY := (C1, [gI; gY]).
Notation tYY := (C1, [gY; gY]).
Notation tYX := (C1, [gY; gX]).
Notation tXY := (C1, [gX; gY]).
Notation tZY := (C1, [gZ; gY]).
Notation tYZ := (C1, [gY; gZ]).
Notation tXZ := (C1, [gX; gZ]).
Notation tZX := (C1, [gZ; gX]).
Notation tZI := (C1, [gZ; gI]).
Notation tIZ := (C1, [gI; gZ]).
Notation tZZ := (C1, [gZ; gZ]).
Notation mtI := ((- C1)%C, [gI]).
Notation mtX := ((- C1)%C, [gX]).
Notation mtY := ((- C1)%C, [gY]).
Notation mtZ := ((- C1)%C, [gZ]).
Notation mtII := ((- C1)%C, [gI; gI]).
Notation mtXI := ((- C1)%C, [gX; gI]).
Notation mtIX := ((- C1)%C, [gI; gX]).
Notation mtXX := ((- C1)%C, [gX; gX]).
Notation mtYI := ((- C1)%C, [gY; gI]).
Notation mtIY := ((- C1)%C, [gI; gY]).
Notation mtYY := ((- C1)%C, [gY; gY]).
Notation mtYX := ((- C1)%C, [gY; gX]).
Notation mtXY := ((- C1)%C, [gX; gY]).
Notation mtZY := ((- C1)%C, [gZ; gY]).
Notation mtYZ := ((- C1)%C, [gY; gZ]).
Notation mtXZ := ((- C1)%C, [gX; gZ]).
Notation mtZX := ((- C1)%C, [gZ; gX]).
Notation mtZI := ((- C1)%C, [gZ; gI]).
Notation mtIZ := ((- C1)%C, [gI; gZ]).
Notation mtZZ := ((- C1)%C, [gZ; gZ]).
Notation aI := [tI].
Notation aX := [tX].
Notation aY := [tY].
Notation aZ := [tZ].
Notation aII := [tII].
Notation aXI := [tXI].
Notation aIX := [tIX].
Notation aXX := [tXX].
Notation aYI := [tYI].
Notation aIY := [tIY].
Notation aYY := [tYY].
Notation aYX := [tYX].
Notation aXY := [tXY].
Notation aZY := [tZY].
Notation aYZ := [tYZ].
Notation aXZ := [tXZ].
Notation aZX := [tZX].
Notation aZI := [tZI].
Notation aIZ := [tIZ].
Notation aZZ := [tZZ].
Notation maI := [mtI].
Notation maX := [mtX].
Notation maY := [mtY].
Notation maZ := [mtZ].
Notation maII := [mtII].
Notation maXI := [mtXI].
Notation maIX := [mtIX].
Notation maXX := [mtXX].
Notation maYI := [mtYI].
Notation maIY := [mtIY].
Notation maYY := [mtYY].
Notation maYX := [mtYX].
Notation maXY := [mtXY].
Notation maZY := [mtZY].
Notation maYZ := [mtYZ].
Notation maXZ := [mtXZ].
Notation maZX := [mtZX].
Notation maZI := [mtZI].
Notation maIZ := [mtIZ].
Notation maZZ := [mtZZ].
Notation aXY2 := (gScaleA (C1/√2)%C [(C1, [gX]); (C1, [gY])]).
Notation aYX2 := (gScaleA (C1/√2)%C [(C1, [gY]); (C1, [gX])]).
Notation aX2Y2 := [((C1/√2)%C, [gX]); ((C1/√2)%C, [gY])].
Notation aY2X2 := [((C1/√2)%C, [gY]); ((C1/√2)%C, [gX])].
Notation aYmX2 := (gScaleA (C1/√2)%C [(C1, [gY]); ((- C1)%C, [gX])]).
Notation amXY2 := (gScaleA (C1/√2)%C [((- C1)%C, [gX]); (C1, [gY])]).
Notation aY2mX2 := [((C1/√2)%C, [gY]); (((C1/√2) * (- C1))%C, [gX])].
Notation amX2Y2 := [(((C1/√2) * (- C1))%C, [gX]); ((C1/√2)%C, [gY])].
Notation pI := (@AtoPred 1 aI).
Notation pX := (@AtoPred 1 aX).
Notation pY := (@AtoPred 1 aY).
Notation pZ := (@AtoPred 1 aZ).
Notation pII := (@AtoPred 2 aII).
Notation pXI := (@AtoPred 2 aXI).
Notation pIX := (@AtoPred 2 aIX).
Notation pXX := (@AtoPred 2 aXX).
Notation pYI := (@AtoPred 2 aYI).
Notation pIY := (@AtoPred 2 aIY).
Notation pYY := (@AtoPred 2 aYY).
Notation pYX := (@AtoPred 2 aYX).
Notation pXY := (@AtoPred 2 aXY).
Notation pZY := (@AtoPred 2 aZY).
Notation pYZ := (@AtoPred 2 aYZ).
Notation pXZ := (@AtoPred 2 aXZ).
Notation pZX := (@AtoPred 2 aZX).
Notation pZI := (@AtoPred 2 aZI).
Notation pIZ := (@AtoPred 2 aIZ).
Notation pZZ := (@AtoPred 2 aZZ).
Notation mpI := (@AtoPred 1 maI).
Notation mpX := (@AtoPred 1 maX).
Notation mpY := (@AtoPred 1 maY).
Notation mpZ := (@AtoPred 1 maZ).
Notation mpII := (@AtoPred 2 maII).
Notation mpXI := (@AtoPred 2 maXI).
Notation mpIX := (@AtoPred 2 maIX).
Notation mpXX := (@AtoPred 2 maXX).
Notation mpYI := (@AtoPred 2 maYI).
Notation mpIY := (@AtoPred 2 maIY).
Notation mpYY := (@AtoPred 2 maYY).
Notation mpYX := (@AtoPred 2 maYX).
Notation mpXY := (@AtoPred 2 maXY).
Notation mpZY := (@AtoPred 2 maZY).
Notation mpYZ := (@AtoPred 2 maYZ).
Notation mpXZ := (@AtoPred 2 maXZ).
Notation mpZX := (@AtoPred 2 maZX).