-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPermutationMatrices.v
1534 lines (1396 loc) · 44.9 KB
/
PermutationMatrices.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 VectorStates.
Require Import Kronecker.
Require Export PermutationsBase.
Require Import PermutationAutomation.
Require Import PermutationInstances.
Require Import Modulus.
Require Import Pad.
Require Import Complex.
Import Setoid.
(** Implementation of permutations as matrices and facts about those matrices. **)
(** * Prerequisite lemmas **)
Lemma basis_vector_equiv_e_i : forall n k,
basis_vector n k ≡ e_i k.
Proof.
intros n k i j Hi Hj.
unfold basis_vector, e_i.
bdestructΩ'.
Qed.
Lemma basis_vector_eq_e_i : forall n k, (k < n)%nat ->
basis_vector n k = e_i k.
Proof.
intros n k Hk.
rewrite <- mat_equiv_eq_iff by auto with wf_db.
apply basis_vector_equiv_e_i.
Qed.
Lemma vector_equiv_basis_comb : forall n (y : Vector n),
y ≡ big_sum (G:=Vector n) (fun i => y i O .* @e_i n i) n.
Proof.
intros n y.
intros i j Hi Hj.
replace j with O by lia; clear j Hj.
symmetry.
rewrite Msum_Csum.
apply big_sum_unique.
exists i.
repeat split; try easy.
- unfold ".*", e_i; bdestructΩ'; now Csimpl.
- intros l Hl Hnk.
unfold ".*", e_i; bdestructΩ'; now Csimpl.
Qed.
Lemma vector_eq_basis_comb : forall n (y : Vector n),
WF_Matrix y ->
y = big_sum (G:=Vector n) (fun i => y i O .* @e_i n i) n.
Proof.
intros n y Hwfy.
apply mat_equiv_eq; auto with wf_db.
apply vector_equiv_basis_comb.
Qed.
Lemma Mmult_if_r {n m o} (A : Matrix n m) (B B' : Matrix m o) (b : bool) :
A × (if b then B else B') =
if b then A × B else A × B'.
Proof.
now destruct b.
Qed.
Lemma Mmult_if_l {n m o} (A A' : Matrix n m) (B : Matrix m o) (b : bool) :
(if b then A else A') × B =
if b then A × B else A' × B.
Proof.
now destruct b.
Qed.
Definition direct_sum' {n m o p : nat} (A : Matrix n m) (B : Matrix o p) :
Matrix (n+o) (m+p) :=
(fun i j => if (i <? n) && (j <? m) then A i j
else if (¬ (i <? n) || (j <? m)) && (i <? n+o) && (j <? m+p) then B (i-n) (j-m)
else C0)%nat.
#[export] Hint Unfold direct_sum' : U_db.
Notation "A .⊕' B" := (direct_sum' A B)
(at level 50, left associativity): matrix_scope.
Lemma direct_sum'_WF : forall {n m o p : nat} (A : Matrix n m) (B : Matrix o p),
WF_Matrix (A .⊕' B).
Proof.
intros n m o p A B i j [Hi | Hj];
unfold direct_sum'; now bdestruct_all.
Qed.
#[export] Hint Resolve direct_sum'_WF : wf_db.
Lemma direct_sum'_assoc : forall {n m o p q r}
(A : Matrix n m) (B : Matrix o p) (C : Matrix q r),
A .⊕' B .⊕' C = A .⊕' (B .⊕' C).
Proof.
intros n m o p q r A B C.
apply mat_equiv_eq; auto using WF_Matrix_dim_change with wf_db.
intros i j Hi Hj.
unfold direct_sum'.
rewrite !Nat.sub_add_distr.
bdestructΩ'.
Qed.
Lemma direct_sum'_eq_direct_sum : forall {n m o p : nat}
(A : Matrix n m) (B : Matrix o p), WF_Matrix A -> WF_Matrix B ->
A .⊕' B = A .⊕ B.
Proof.
intros n m o p A B HA HB.
apply mat_equiv_eq; [|apply WF_direct_sum|]; auto with wf_db.
intros i j Hi Hj.
unfold direct_sum, direct_sum'.
bdestruct_all; try lia + easy;
rewrite HA by lia; easy.
Qed.
Lemma direct_sum'_simplify_mat_equiv {n m o p} : forall (A B : Matrix n m)
(C D : Matrix o p), A ≡ B -> C ≡ D -> direct_sum' A C ≡ direct_sum' B D.
Proof.
intros A B C D HAB HCD i j Hi Hj.
unfold direct_sum'.
bdestruct (i <? n);
bdestruct (j <? m); simpl; [rewrite HAB | | |]; try easy.
bdestruct_all; try lia + easy; rewrite HCD by lia; easy.
Qed.
Lemma direct_sum'_Mmult {n m o p q r} (A : Matrix n m) (B : Matrix p q)
(C : Matrix m o) (D : Matrix q r) :
(A .⊕' B) × (C .⊕' D) = (A × C) .⊕' (B × D).
Proof.
apply mat_equiv_eq; auto with wf_db.
intros i j Hi Hj.
unfold direct_sum'.
bdestruct_all; simpl.
- unfold Mmult.
rewrite big_sum_sum.
rewrite Cplus_comm.
rewrite big_sum_0_bounded by
(intros; now bdestruct_all; Csimpl).
Csimpl.
apply big_sum_eq_bounded.
intros k Hk.
now bdestruct_all; simpl; Csimpl.
- unfold Mmult.
rewrite big_sum_0_bounded; [easy|].
intros k Hk.
now bdestruct_all; simpl; Csimpl.
- unfold Mmult.
rewrite big_sum_0_bounded; [easy|].
intros k Hk.
now bdestruct_all; simpl; Csimpl.
- unfold Mmult.
rewrite big_sum_sum.
rewrite big_sum_0_bounded by
(intros; now bdestruct_all; Csimpl).
Csimpl.
apply big_sum_eq_bounded.
intros k Hk.
bdestruct_all; simpl; Csimpl.
do 2 f_equal; lia.
Qed.
Lemma direct_sum'_Mmult_mat_equiv {n m o p q r}
(A : Matrix n m) (B : Matrix p q)
(C : Matrix m o) (D : Matrix q r) :
(A .⊕' B) × (C .⊕' D) ≡ (A × C) .⊕' (B × D).
Proof.
now rewrite direct_sum'_Mmult.
Qed.
Lemma direct_sum_0_r {n m} (A : Matrix n m) o p :
WF_Matrix A ->
@direct_sum' n m o p A Zero = A.
Proof.
intros HA.
prep_matrix_equality.
unfold direct_sum', Zero.
symmetry.
bdestructΩ'_with ltac:
(try lia; try rewrite HA by lia; try reflexivity).
Qed.
Lemma direct_sum_Mscale {n m p q} (A : Matrix n m)
(B : Matrix p q) (c : C) :
(c .* A) .⊕' (c .* B) = c .* (A .⊕' B).
Proof.
apply mat_equiv_eq; auto with wf_db.
intros i j Hi Hj.
autounfold with U_db.
bdestruct_all; simpl; now Csimpl.
Qed.
Lemma ei_direct_sum_split n m k :
@e_i (n + m) k =
(if k <? n then
@e_i n k .⊕' @Zero m 0
else
@Zero n 0 .⊕' @e_i m (k - n)).
Proof.
apply mat_equiv_eq;
[try bdestruct_one; auto using WF_Matrix_dim_change with wf_db..|].
intros i j Hi Hj.
replace j with O by lia.
autounfold with U_db.
simpl.
simplify_bools_lia_one_kernel.
bdestructΩ'_with ltac:(try lia; try reflexivity).
Qed.
Add Parametric Morphism {n m o p} : (@direct_sum' n m o p)
with signature (@mat_equiv n m) ==> (@mat_equiv o p)
==> (@mat_equiv (n+o) (m+p)) as direct_sum'_mat_equiv_morph.
Proof. intros; apply direct_sum'_simplify_mat_equiv; easy. Qed.
Lemma ei_kron_split k n m :
@e_i (n*m) k =
e_i (k / m) ⊗ e_i (k mod m).
Proof.
apply mat_equiv_eq; auto with wf_db.
intros i j Hi Hj.
replace j with O by lia.
unfold e_i, kron.
do 2 simplify_bools_lia_one_kernel.
do 2 simplify_bools_moddy_lia_one_kernel.
rewrite Cmult_if_if_1_l.
apply f_equal_if; [|easy..].
now rewrite andb_comm, <- eqb_iff_div_mod_eqb.
Qed.
Lemma ei_kron_join k l n m :
(l < m)%nat ->
@e_i n k ⊗ e_i l =
@e_i (n*m) (k*m + l).
Proof.
intros Hl.
apply mat_equiv_eq; auto with wf_db.
intros i j Hi Hj.
replace j with O by lia.
unfold e_i, kron.
do 2 simplify_bools_lia_one_kernel.
do 2 simplify_bools_moddy_lia_one_kernel.
rewrite Cmult_if_if_1_l.
apply f_equal_if; [|easy..].
symmetry.
rewrite (eqb_iff_div_mod_eqb m).
rewrite mod_add_l, Nat.div_add_l by lia.
rewrite (Nat.mod_small l m Hl), (Nat.div_small l m Hl).
now rewrite Nat.add_0_r, andb_comm.
Qed.
Local Open Scope nat_scope.
Lemma matrix_times_basis_eq_lt {m n : nat} (A : Matrix m n) (i j : nat) :
j < n -> (A × basis_vector n j) i 0 = A i j.
Proof.
intros Hj.
unfold Mmult.
rewrite (big_sum_eq_bounded _ (fun k => if k =? j then A i j else 0%R)%C).
2: {
intros k Hk.
unfold basis_vector.
bdestructΩ'; lca.
}
rewrite big_sum_if_eq_C.
bdestructΩ'.
Qed.
Lemma matrix_times_basis_mat_equiv {m n : nat} (A : Matrix m n) (j : nat) :
j < n -> mat_equiv (A × basis_vector n j)
(get_col A j).
Proof.
intros Hj i z Hi Hz.
replace z with 0 by lia.
rewrite matrix_times_basis_eq_lt by easy.
unfold get_col.
bdestructΩ'.
Qed.
Lemma matrix_conj_basis_eq_lt {m n : nat} (A : Matrix m n) (i j : nat) :
i < m -> j < n -> ((basis_vector m i)⊤ × A × basis_vector n j) 0 0 = A i j.
Proof.
intros Hi Hj.
rewrite matrix_times_basis_mat_equiv by lia.
unfold get_col.
bdestructΩ'.
unfold Mmult, Matrix.transpose.
rewrite (big_sum_eq_bounded _ (fun k => if k =? i then A i j else 0%R)%C).
2: {
intros k Hk.
unfold basis_vector.
bdestructΩ'; lca.
}
rewrite big_sum_if_eq_C.
bdestructΩ'.
Qed.
Lemma mat_equiv_of_all_basis_conj {m n : nat} (A B : Matrix m n)
(H : forall (i j : nat), i < m -> j < n ->
((basis_vector m i) ⊤ × A × basis_vector n j) 0 0 =
((basis_vector m i) ⊤ × B × basis_vector n j) 0 0) :
mat_equiv A B.
Proof.
intros i j Hi Hj.
specialize (H i j Hi Hj).
now rewrite 2!matrix_conj_basis_eq_lt in H by easy.
Qed.
Local Open Scope nat_scope.
(** * Permutation matrices *)
Definition perm_mat n (p : nat -> nat) : Square n :=
(fun x y => if (x =? p y) && (x <? n) && (y <? n) then C1 else C0).
Lemma perm_mat_WF : forall n p, WF_Matrix (perm_mat n p).
Proof.
intros n p x y Hxy.
unfold perm_mat.
bdestructΩ'.
Qed.
#[export] Hint Resolve perm_mat_WF : wf_db.
Lemma perm_mat_defn n p :
perm_mat n p ≡
fun x y => if x =? p y then C1 else C0.
Proof.
intros i j Hi Hj.
unfold perm_mat.
bdestructΩ'.
Qed.
Add Parametric Morphism n : (perm_mat n) with signature
perm_eq n ==> eq as perm_mat_perm_eq_to_eq_proper.
Proof.
intros f g Hfg.
apply mat_equiv_eq; auto with wf_db.
rewrite !perm_mat_defn.
intros i j Hi Hj.
now rewrite Hfg by easy.
Qed.
Lemma perm_mat_id : forall n,
perm_mat n (Datatypes.id) = (I n).
Proof.
intros n.
apply mat_equiv_eq; auto with wf_db.
intros i j Hi Hj.
unfold Datatypes.id, perm_mat, I.
bdestructΩ'.
Qed.
Lemma perm_mat_idn n :
perm_mat n idn = (I n).
Proof.
apply mat_equiv_eq; auto with wf_db.
intros i j Hi Hj.
unfold perm_mat, I.
bdestructΩ'.
Qed.
#[export] Hint Rewrite perm_mat_idn : perm_cleanup_db.
Lemma perm_mat_unitary : forall n p,
permutation n p -> WF_Unitary (perm_mat n p).
Proof.
intros n p [pinv Hp].
split; [apply perm_mat_WF|].
apply mat_equiv_eq; auto with wf_db.
intros i j Hi Hj.
unfold Mmult, adjoint, perm_mat, I.
do 2 simplify_bools_lia_one_kernel.
bdestruct (i =? j).
- subst.
apply big_sum_unique.
exists (p j).
destruct (Hp j) as [? _]; auto.
split; auto.
split; intros; bdestructΩ'; lca.
- apply (@big_sum_0 C C_is_monoid).
intros z.
bdestruct_all; simpl; try lca.
subst.
assert (pinv (p i) = pinv (p j)) by auto.
pose proof (fun x Hx => proj1 (proj2 (proj2 (Hp x Hx)))) as Hrw.
rewrite !Hrw in * by auto.
congruence.
Qed.
Lemma perm_mat_Mmult n f g :
perm_bounded n g ->
perm_mat n f × perm_mat n g = perm_mat n (f ∘ g)%prg.
Proof.
intros Hg.
apply mat_equiv_eq; auto with wf_db.
intros i j Hi Hj.
unfold perm_mat, Mmult, compose.
do 2 simplify_bools_lia_one_kernel.
bdestruct (i =? f (g j)).
- apply big_sum_unique.
exists (g j).
specialize (Hg j Hj).
split; [now auto|].
split; [bdestructΩ'; now Csimpl|].
intros k Hk ?.
bdestructΩ'; now Csimpl.
- apply (@big_sum_0_bounded C).
intros k Hk.
bdestructΩ'; now Csimpl.
Qed.
Lemma perm_mat_I : forall n f,
(forall x, x < n -> f x = x) ->
perm_mat n f = I n.
Proof.
intros n f Hinv.
apply mat_equiv_eq; auto with wf_db.
unfold perm_mat, I.
intros i j Hi Hj.
do 2 simplify_bools_lia_one_kernel.
now rewrite Hinv by easy.
Qed.
Lemma perm_mat_col_swap : forall n f i j,
i < n -> j < n ->
perm_mat n (fswap f i j) = col_swap (perm_mat n f) i j.
Proof.
intros.
apply mat_equiv_eq; auto with wf_db.
intros k l Hk Hl.
unfold perm_mat, fswap, col_swap, I.
bdestructΩ'.
Qed.
Lemma perm_mat_col_swap_I : forall n f i j,
(forall x, x < n -> f x = x) ->
i < n -> j < n ->
perm_mat n (fswap f i j) = col_swap (I n) i j.
Proof.
intros.
rewrite perm_mat_col_swap by easy.
now rewrite perm_mat_I by easy.
Qed.
Lemma perm_mat_row_swap : forall n f i j,
i < n -> j < n ->
perm_mat n (fswap f i j) = (row_swap (perm_mat n f)† i j)†.
Proof.
intros.
apply mat_equiv_eq; auto with wf_db.
intros k l Hk Hl.
unfold perm_mat, fswap, row_swap, I, adjoint.
do 3 simplify_bools_lia_one_kernel.
rewrite !(if_dist _ _ _ Cconj).
Csimpl.
bdestructΩ'.
Qed.
Lemma perm_mat_e_i : forall n f i,
i < n ->
(perm_mat n f) × e_i i = e_i (f i).
Proof.
intros n f i Hi.
apply mat_equiv_eq; auto with wf_db.
unfold mat_equiv; intros k l Hk Hl.
replace l with 0 in * by lia.
unfold Mmult.
apply big_sum_unique.
exists i.
split; auto.
unfold e_i, perm_mat;
split; [bdestructΩ'_with ltac:(try lia; try lca)|].
intros.
bdestructΩ'_with ltac:(try lia; try lca).
Qed.
(* with get_entry_with_e_i this became soo much easier *)
Lemma perm_mat_conjugate : forall {n} (A : Square n) f (i j : nat),
WF_Matrix A ->
i < n -> j < n ->
perm_bounded n f ->
((perm_mat n f)† × A × ((perm_mat n f))) i j = A (f i) (f j).
Proof.
intros.
rewrite get_entry_with_e_i, (get_entry_with_e_i A)
by auto with perm_bounded_db.
rewrite <- 2 Mmult_assoc, <- Mmult_adjoint.
rewrite perm_mat_e_i by auto with perm_bounded_db.
rewrite 3 Mmult_assoc.
rewrite perm_mat_e_i; auto.
Qed.
Lemma perm_mat_conjugate_nonsquare :
forall {m n} (A : Matrix m n) f g (i j : nat),
WF_Matrix A ->
i < m -> j < n ->
perm_bounded m g -> perm_bounded n f ->
((perm_mat m g)† × A × ((perm_mat n f))) i j = A (g i) (f j).
Proof.
intros.
rewrite get_entry_with_e_i, (get_entry_with_e_i A) by auto.
rewrite <- 2 Mmult_assoc, <- Mmult_adjoint.
rewrite perm_mat_e_i by auto.
rewrite 3 Mmult_assoc.
rewrite perm_mat_e_i; auto.
Qed.
Lemma perm_mat_permutes_basis_vectors_r : forall n f k, (k < n)%nat ->
(perm_mat n f) × (basis_vector n k) = e_i (f k).
Proof.
intros n f k Hk.
rewrite basis_vector_eq_e_i by easy.
apply perm_mat_e_i; easy.
Qed.
Lemma perm_mat_permutes_matrix_r : forall n m f (A : Matrix n m),
permutation n f ->
(perm_mat n f) × A ≡ (fun i j => A (perm_inv n f i) j).
Proof.
intros n m f A Hperm.
apply mat_equiv_of_equiv_on_ei.
intros k Hk.
rewrite Mmult_assoc, <- 2(matrix_by_basis _ _ Hk).
rewrite (vector_equiv_basis_comb _ (get_col _ _)).
rewrite Mmult_Msum_distr_l.
erewrite big_sum_eq_bounded.
2: {
intros l Hl.
rewrite Mscale_mult_dist_r, perm_mat_e_i by easy.
reflexivity.
}
intros i j Hi Hj; replace j with O by lia; clear j Hj.
rewrite Msum_Csum.
unfold get_col, scale, e_i.
rewrite Nat.eqb_refl.
apply big_sum_unique.
exists (perm_inv n f i).
repeat split; auto with perm_bounded_db.
- rewrite (perm_inv_is_rinv_of_permutation n f Hperm i Hi), Nat.eqb_refl.
bdestructΩ'; now Csimpl.
- intros j Hj Hjne.
bdestruct (i =? f j); [|bdestructΩ'; now Csimpl].
exfalso; apply Hjne.
apply (permutation_is_injective n f Hperm); auto with perm_bounded_db.
rewrite (perm_inv_is_rinv_of_permutation n f Hperm i Hi); easy.
Qed.
Lemma perm_mat_equiv_of_perm_eq : forall n f g,
(perm_eq n f g) ->
perm_mat n f ≡ perm_mat n g.
Proof.
intros n f g Heq.
apply mat_equiv_of_equiv_on_ei.
intros k Hk.
rewrite 2!perm_mat_e_i, Heq by easy.
easy.
Qed.
#[export] Hint Resolve perm_mat_equiv_of_perm_eq : perm_inv_db.
Lemma perm_mat_eq_of_perm_eq : forall n f g,
(perm_eq n f g) ->
perm_mat n f = perm_mat n g.
Proof.
intros.
apply mat_equiv_eq; auto with wf_db.
now apply perm_mat_equiv_of_perm_eq.
Qed.
#[export] Hint Resolve perm_mat_eq_of_perm_eq : perm_inv_db.
Lemma perm_mat_equiv_of_perm_eq' : forall n m f g, n = m ->
(perm_eq n f g) ->
perm_mat n f ≡ perm_mat m g.
Proof.
intros; subst n; apply perm_mat_equiv_of_perm_eq; easy.
Qed.
Lemma perm_mat_transpose {n f} (Hf : permutation n f) :
(perm_mat n f) ⊤ ≡ perm_mat n (perm_inv n f).
Proof.
intros i j Hi Hj.
unfold "⊤".
unfold perm_mat.
simplify_bools_lia.
rewrite <- (@perm_inv_eqb_iff n f) by cleanup_perm.
now rewrite Nat.eqb_sym.
Qed.
Lemma perm_mat_transpose_eq {n f} (Hf : permutation n f) :
(perm_mat n f) ⊤ = perm_mat n (perm_inv n f).
Proof.
apply mat_equiv_eq; auto with wf_db.
now apply perm_mat_transpose.
Qed.
Lemma matrix_by_basis_perm_eq {n m} (A : Matrix n m) (i : nat) (Hi : i < m) :
get_col A i ≡ A × e_i i.
Proof.
intros k l Hk Hl.
replace l with 0 by lia.
unfold get_col.
simplify_bools_lia_one_kernel.
symmetry.
unfold Mmult, e_i.
simplify_bools_lia_one_kernel.
apply big_sum_unique.
exists i.
split; auto.
do 2 simplify_bools_lia_one_kernel.
split; intros;
simplify_bools_lia;
now Csimpl.
Qed.
Lemma perm_mat_permutes_matrix_l : forall n m f (A : Matrix n m),
perm_bounded m f ->
A × (perm_mat m f) ≡ (fun i j => A i (f j)).
Proof.
intros n m f A Hf.
apply mat_equiv_of_equiv_on_ei.
intros k Hk.
rewrite Mmult_assoc, perm_mat_e_i, <- (matrix_by_basis _ _ Hk) by easy.
rewrite <- matrix_by_basis_perm_eq by auto with perm_bounded_db.
easy.
Qed.
Lemma perm_mat_permutes_matrix_l_eq : forall n m f (A : Matrix n m),
WF_Matrix A ->
perm_bounded m f ->
A × (perm_mat m f) = make_WF (fun i j => A i (f j)).
Proof.
intros n m f A HA Hf.
apply mat_equiv_eq; auto with wf_db.
rewrite make_WF_equiv.
now apply perm_mat_permutes_matrix_l.
Qed.
Lemma perm_mat_permutes_matrix_r_eq : forall n m f (A : Matrix n m),
WF_Matrix A ->
permutation n f ->
(perm_mat n f) × A = make_WF (fun i j => A (perm_inv n f i) j).
Proof.
intros n m f A HA Hf.
apply mat_equiv_eq; auto with wf_db.
rewrite make_WF_equiv.
now apply perm_mat_permutes_matrix_r.
Qed.
Lemma perm_mat_perm_eq_idn n f :
perm_eq n f idn ->
perm_mat n f = I n.
Proof.
intros ->.
apply perm_mat_idn.
Qed.
Lemma perm_mat_transpose_rinv {n f} (Hf : permutation n f) :
(perm_mat n f) × (perm_mat n f) ⊤ = I n.
Proof.
rewrite perm_mat_transpose_eq by easy.
rewrite perm_mat_Mmult by auto with perm_db.
cleanup_perm.
Qed.
Lemma perm_mat_transpose_linv {n f} (Hf : permutation n f) :
(perm_mat n f) ⊤ × (perm_mat n f) = I n.
Proof.
rewrite perm_mat_transpose_eq by easy.
rewrite perm_mat_Mmult by auto with perm_db.
cleanup_perm.
Qed.
Lemma perm_mat_of_stack_perms n0 n1 f g :
perm_bounded n0 f -> perm_bounded n1 g ->
perm_mat (n0 + n1) (stack_perms n0 n1 f g) =
direct_sum' (perm_mat n0 f) (perm_mat n1 g).
Proof.
intros Hf Hg.
apply mat_equiv_eq; auto with wf_db.
apply mat_equiv_of_equiv_on_ei.
intros k Hk.
rewrite perm_mat_e_i by easy.
rewrite 2!ei_direct_sum_split.
rewrite Mmult_if_r.
rewrite (direct_sum'_Mmult _ _ (e_i k) (Zero)).
rewrite (direct_sum'_Mmult _ _ (@Zero n0 0) (e_i (k - n0))).
rewrite 2!Mmult_0_r.
bdestruct (k <? n0).
- rewrite perm_mat_e_i, stack_perms_left by easy.
pose proof (Hf k).
now bdestructΩ (f k <? n0).
- rewrite perm_mat_e_i, stack_perms_right by lia.
pose proof (Hg (k - n0)).
bdestructΩ (g (k - n0) + n0 <? n0).
now rewrite Nat.add_sub.
Qed.
#[export] Hint Rewrite perm_mat_of_stack_perms
using solve [auto with perm_bounded_db] : perm_cleanup_db.
Lemma perm_mat_of_tensor_perms n0 n1 f g :
perm_bounded n1 g ->
perm_mat (n0 * n1) (tensor_perms n0 n1 f g) =
perm_mat n0 f ⊗ perm_mat n1 g.
Proof.
intros Hg.
apply mat_equiv_eq; auto with wf_db.
apply mat_equiv_of_equiv_on_ei.
intros k Hk.
rewrite perm_mat_e_i by easy.
symmetry.
rewrite ei_kron_split.
restore_dims.
rewrite kron_mixed_product.
unfold tensor_perms.
simplify_bools_lia_one_kernel.
rewrite 2!perm_mat_e_i by show_moddy_lt.
now rewrite ei_kron_join by cleanup_perm.
Qed.
Lemma perm_mat_inj_mat_equiv n f g
(Hf : perm_bounded n f) (Hg : perm_bounded n g) :
perm_mat n f ≡ perm_mat n g ->
perm_eq n f g.
Proof.
intros Hequiv.
intros i Hi.
generalize (Hequiv (f i) i (Hf i Hi) Hi).
unfold perm_mat.
pose proof (Hf i Hi).
pose proof C1_nonzero.
bdestructΩ'.
Qed.
Lemma perm_mat_inj n f g
(Hf : perm_bounded n f) (Hg : perm_bounded n g) :
perm_mat n f = perm_mat n g ->
perm_eq n f g.
Proof.
rewrite <- mat_equiv_eq_iff by auto with wf_db.
now apply perm_mat_inj_mat_equiv.
Qed.
Lemma perm_mat_determinant_sqr n f (Hf : permutation n f) :
(Determinant (perm_mat n f) ^ 2)%C = 1%R.
Proof.
simpl.
Csimpl.
rewrite Determinant_transpose at 1.
rewrite Determinant_multiplicative.
rewrite perm_mat_transpose_linv by easy.
now rewrite Det_I.
Qed.
Lemma perm_mat_perm_eq_of_proportional n f g :
(exists c, perm_mat n f = c .* perm_mat n g /\ c <> 0%R) ->
perm_bounded n f ->
perm_eq n f g.
Proof.
intros (c & Heq & Hc) Hf.
rewrite <- mat_equiv_eq_iff in Heq by auto with wf_db.
intros i Hi.
pose proof (Hf i Hi) as Hfi.
generalize (Heq (f i) i Hfi Hi).
unfold perm_mat, scale.
do 3 simplify_bools_lia_one_kernel.
rewrite Cmult_if_1_r.
pose proof C1_nonzero.
bdestructΩ'.
Qed.
Lemma perm_mat_eq_of_proportional n f g :
(exists c, perm_mat n f = c .* perm_mat n g /\ c <> 0%R) ->
perm_bounded n f ->
perm_mat n f = perm_mat n g.
Proof.
intros H Hf.
apply perm_mat_eq_of_perm_eq.
now apply perm_mat_perm_eq_of_proportional.
Qed.
Lemma Mmult_perm_mat_l n m (A B : Matrix n m)
(HA : WF_Matrix A) (HB : WF_Matrix B) f (Hf : permutation n f) :
perm_mat n f × A = B <-> A = perm_mat n (perm_inv n f) × B.
Proof.
rewrite <- perm_mat_transpose_eq by auto.
split; [intros <- | intros ->];
now rewrite <- Mmult_assoc, 1?perm_mat_transpose_rinv,
1?perm_mat_transpose_linv, Mmult_1_l by auto.
Qed.
Lemma Mmult_perm_mat_l' n m (A B : Matrix n m)
(HA : WF_Matrix A) (HB : WF_Matrix B) f (Hf : permutation n f) :
B = perm_mat n f × A <-> perm_mat n (perm_inv n f) × B = A.
Proof.
split; intros H; symmetry;
apply Mmult_perm_mat_l; auto.
Qed.
Lemma Mmult_perm_mat_r n m (A B : Matrix n m)
(HA : WF_Matrix A) (HB : WF_Matrix B) f (Hf : permutation m f) :
A × perm_mat m f = B <-> A = B × perm_mat m (perm_inv m f).
Proof.
rewrite <- perm_mat_transpose_eq by auto.
split; [intros <- | intros ->];
now rewrite Mmult_assoc, 1?perm_mat_transpose_rinv,
1?perm_mat_transpose_linv, Mmult_1_r by auto.
Qed.
Lemma Mmult_perm_mat_r' n m (A B : Matrix n m)
(HA : WF_Matrix A) (HB : WF_Matrix B) f (Hf : permutation m f) :
B = A × perm_mat m f <-> B × perm_mat m (perm_inv m f) = A.
Proof.
split; intros H; symmetry;
apply Mmult_perm_mat_r; auto.
Qed.
(** Transform a (0,...,n-1) permutation into a 2^n by 2^n matrix. *)
Definition perm_to_matrix n p :=
perm_mat (2 ^ n) (qubit_perm_to_nat_perm n p).
Lemma perm_to_matrix_WF : forall n p, WF_Matrix (perm_to_matrix n p).
Proof. intros. apply perm_mat_WF. Qed.
#[export] Hint Resolve perm_to_matrix_WF : wf_db.
Add Parametric Morphism n : (perm_to_matrix n) with signature
perm_eq n ==> eq as perm_to_matrix_perm_eq_to_eq_proper.
Proof.
intros f g Hfg.
unfold perm_to_matrix.
now rewrite Hfg.
Qed.
Lemma perm_to_matrix_permutes_qubits : forall n p f,
perm_bounded n p ->
perm_to_matrix n p × f_to_vec n f = f_to_vec n (fun x => f (p x)).
Proof.
intros n p f Hp.
rewrite 2 basis_f_to_vec.
rewrite !basis_vector_eq_e_i by apply funbool_to_nat_bound.
unfold perm_to_matrix.
rewrite perm_mat_e_i by apply funbool_to_nat_bound.
f_equal.
rewrite qubit_perm_to_nat_perm_defn by apply funbool_to_nat_bound.
apply funbool_to_nat_eq.
intros i Hi.
unfold compose.
now rewrite funbool_to_nat_inverse by auto.
Qed.
Lemma perm_to_matrix_unitary : forall n p,
permutation n p ->
WF_Unitary (perm_to_matrix n p).
Proof.
intros.
apply perm_mat_unitary.
auto with perm_db.
Qed.
Lemma Private_perm_to_matrix_Mmult : forall n f g,
permutation n f -> permutation n g ->
perm_to_matrix n f × perm_to_matrix n g = perm_to_matrix n (g ∘ f)%prg.
Proof.
intros n f g Hf Hg.
unfold perm_to_matrix.
rewrite perm_mat_Mmult by auto with perm_bounded_db.
now rewrite qubit_perm_to_nat_perm_compose by auto with perm_bounded_db.
Qed.
#[deprecated(note="Use perm_to_matrix_compose instead")]
Notation perm_to_matrix_Mmult := Private_perm_to_matrix_Mmult.
Lemma perm_to_matrix_I : forall n f,
(forall x, x < n -> f x = x) ->
perm_to_matrix n f = I (2 ^ n).
Proof.
intros n f Hf.
unfold perm_to_matrix.
apply perm_mat_I.
intros x Hx.
unfold qubit_perm_to_nat_perm, compose.
erewrite funbool_to_nat_eq.
2: { intros y Hy. rewrite Hf by assumption. reflexivity. }
simplify_bools_lia_one_kernel.
apply nat_to_funbool_inverse.
assumption.
Qed.
Lemma perm_to_matrix_perm_eq n f g :
perm_eq n f g ->
perm_to_matrix n f ≡ perm_to_matrix n g.
Proof.
intros Hfg.
apply perm_mat_equiv_of_perm_eq.
now rewrite Hfg.
Qed.
#[export] Hint Resolve perm_to_matrix_perm_eq : perm_inv_db.
Lemma perm_to_matrix_eq_of_perm_eq n f g :
perm_eq n f g ->
perm_to_matrix n f = perm_to_matrix n g.
Proof.
intros Hfg.
apply mat_equiv_eq; auto with wf_db.
now apply perm_to_matrix_perm_eq.
Qed.
#[export] Hint Resolve perm_to_matrix_eq_of_perm_eq : perm_inv_db.
Lemma perm_to_matrix_transpose {n f} (Hf : permutation n f) :
(perm_to_matrix n f) ⊤ ≡ perm_to_matrix n (perm_inv n f).
Proof.
unfold perm_to_matrix.
rewrite perm_mat_transpose by auto with perm_db.
cleanup_perm_inv.
Qed.
Lemma perm_to_matrix_transpose_eq {n f} (Hf : permutation n f) :
(perm_to_matrix n f) ⊤ = perm_to_matrix n (perm_inv n f).
Proof.
apply mat_equiv_eq; auto with wf_db.
now apply perm_to_matrix_transpose.
Qed.
Lemma perm_to_matrix_transpose' {n f} (Hf : permutation n f) :
(perm_to_matrix n f) ⊤ ≡ perm_to_matrix n (perm_inv' n f).
Proof.
rewrite perm_to_matrix_transpose by easy.
cleanup_perm.
Qed.
Lemma perm_to_matrix_transpose_eq' {n f} (Hf : permutation n f) :
(perm_to_matrix n f) ⊤ = perm_to_matrix n (perm_inv' n f).
Proof.
apply mat_equiv_eq; auto with wf_db.
now apply perm_to_matrix_transpose'.
Qed.
Lemma perm_to_matrix_transpose_linv {n f} (Hf : permutation n f) :
(perm_to_matrix n f) ⊤ × perm_to_matrix n f = I (2 ^ n).
Proof.
apply perm_mat_transpose_linv; auto with perm_db.
Qed.
Lemma perm_to_matrix_transpose_rinv {n f} (Hf : permutation n f) :
perm_to_matrix n f × (perm_to_matrix n f) ⊤ = I (2 ^ n).
Proof.
apply perm_mat_transpose_rinv; auto with perm_db.
Qed.
Lemma Mmult_perm_to_matrix_l n m (A B : Matrix (2 ^ n) m)
(HA : WF_Matrix A) (HB : WF_Matrix B) f (Hf : permutation n f) :
perm_to_matrix n f × A = B <-> A = perm_to_matrix n (perm_inv n f) × B.
Proof.
rewrite <- perm_to_matrix_transpose_eq by auto.
unfold perm_to_matrix.
split; [intros <- | intros ->];
now rewrite <- Mmult_assoc, 1?perm_mat_transpose_rinv,
1?perm_mat_transpose_linv, Mmult_1_l by auto with perm_db.
Qed.
Lemma Mmult_perm_to_matrix_l' n m (A B : Matrix (2 ^ n) m)
(HA : WF_Matrix A) (HB : WF_Matrix B) f (Hf : permutation n f) :
B = perm_to_matrix n f × A <-> perm_to_matrix n (perm_inv n f) × B = A.
Proof.
split; intros H; symmetry; apply Mmult_perm_to_matrix_l;
auto.
Qed.
Lemma Mmult_perm_to_matrix_r n m (A B : Matrix n (2 ^ m))
(HA : WF_Matrix A) (HB : WF_Matrix B) f (Hf : permutation m f) :
A × perm_to_matrix m f = B <-> A = B × perm_to_matrix m (perm_inv m f).
Proof.
rewrite <- perm_to_matrix_transpose_eq by auto.
unfold perm_to_matrix.
split; [intros <- | intros ->];
now rewrite Mmult_assoc, 1?perm_mat_transpose_rinv,
1?perm_mat_transpose_linv, Mmult_1_r by auto with perm_db.
Qed.
Lemma Mmult_perm_to_matrix_r' n m (A B : Matrix n (2 ^ m))
(HA : WF_Matrix A) (HB : WF_Matrix B) f (Hf : permutation m f) :
B = A × perm_to_matrix m f <-> B × perm_to_matrix m (perm_inv m f) = A.
Proof.
split; intros H; symmetry; apply Mmult_perm_to_matrix_r;
auto.
Qed.
Lemma perm_to_matrix_permutes_qubits_l n p f
(Hp : permutation n p) :
(f_to_vec n f) ⊤ × perm_to_matrix n p =
(f_to_vec n (fun x => f (perm_inv n p x))) ⊤.
Proof.
rewrite <- (transpose_involutive _ _ (perm_to_matrix _ _)).
rewrite <- Mmult_transpose.
rewrite perm_to_matrix_transpose_eq by easy.
f_equal.
apply perm_to_matrix_permutes_qubits.
auto with perm_bounded_db.
Qed.