-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathVectorStates.v
1725 lines (1553 loc) · 49.7 KB
/
VectorStates.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 Export Pad.
Require Export CauchySchwarz.
Require Import PermutationInstances.
Require Export Bits.
(* This file provides abstractions for describing quantum states as vectors.
- f_to_vec describes classical states as boolean functions
- basis_vector describes classical states as natural numbers
- vsum describes superposition states
- vkron describes states as the tensor product of qubit states
It also provides automation (ket_db, f_to_vec_db) for simplifying
matrix × vector expressions. *)
(************************************)
(* Unitary Properties on Basis Kets *)
(************************************)
Notation "∣ + ⟩" := (/√2 .* ∣ 0 ⟩ .+ /√2 .* ∣ 1 ⟩).
Notation "∣ - ⟩" := (/√2 .* ∣ 0 ⟩ .+ (-/√2) .* ∣ 1 ⟩).
(* Bra-Ket properties *)
Lemma bra0_equiv : ⟨0∣ = bra 0.
Proof. reflexivity. Qed.
Lemma bra1_equiv : ⟨1∣ = bra 1.
Proof. reflexivity. Qed.
Lemma ket0_equiv : ∣0⟩ = ket 0.
Proof. reflexivity. Qed.
Lemma ket1_equiv : ∣1⟩ = ket 1.
Proof. reflexivity. Qed.
Lemma plus_equiv : ∣+⟩ = ∣ + ⟩.
Proof. lma'. Qed.
Lemma minus_equiv : ∣-⟩ = ∣ - ⟩.
Proof. lma'. Qed.
Lemma bra0_eqb : ⟨0∣ = (fun i j => if (i =? 0) && (j =? 0) then C1 else C0).
Proof. lma'. intros i j []; Modulus.bdestructΩ'. Qed.
Lemma bra1_eqb : ⟨1∣ = (fun i j => if (i =? 0) && (j =? 1) then C1 else C0).
Proof. lma'. intros i j []; Modulus.bdestructΩ'. Qed.
Lemma ket0_eqb : ∣0⟩ = (fun i j => if (i =? 0) && (j =? 0) then C1 else C0).
Proof. lma'. intros i j []; Modulus.bdestructΩ'. Qed.
Lemma ket1_eqb : ∣1⟩ = (fun i j => if (i =? 1) && (j =? 0) then C1 else C0).
Proof. lma'. intros i j []; Modulus.bdestructΩ'. Qed.
Lemma bra0ket0 : bra 0 × ket 0 = I 1.
Proof. lma'. Qed.
Lemma bra0ket1 : bra 0 × ket 1 = Zero.
Proof. lma'. Qed.
Lemma bra1ket0 : bra 1 × ket 0 = Zero.
Proof. lma'. Qed.
Lemma bra1ket1 : bra 1 × ket 1 = I 1.
Proof. lma'. Qed.
Lemma bra0ket_eqb i : bra 0 × ket i =
if i =? 0 then I 1 else Zero.
Proof.
destruct i; simpl.
- apply bra0ket0.
- apply bra0ket1.
Qed.
Lemma bra1ket_eqb i : bra 1 × ket i =
if i =? 0 then Zero else I 1.
Proof.
destruct i; simpl.
- apply bra1ket0.
- apply bra1ket1.
Qed.
(* Hadamard properties *)
Lemma H0_spec : hadamard × ∣ 0 ⟩ = ∣ + ⟩.
Proof. lma'. Qed.
Lemma H1_spec : hadamard × ∣ 1 ⟩ = ∣ - ⟩.
Proof. lma'. Qed.
Lemma Hplus_spec : hadamard × ∣ + ⟩ = ∣ 0 ⟩.
Proof. solve_matrix_fast_with
(autounfold with U_db) (try lca; C_field; lca). Qed.
Lemma Hminus_spec : hadamard × ∣ - ⟩ = ∣ 1 ⟩.
Proof. solve_matrix_fast_with
(autounfold with U_db) (try lca; C_field; lca). Qed.
Local Open Scope nat_scope.
(* TODO: make general *)
Lemma H0_kron_n_spec : forall n,
n ⨂ hadamard × n ⨂ ∣0⟩ = n ⨂ ∣+⟩.
Proof.
intros n.
rewrite kron_n_mult.
rewrite ket0_equiv, plus_equiv.
now rewrite H0_spec.
Qed.
Local Close Scope nat_scope.
Definition b2R (b : bool) : R := if b then 1%R else 0%R.
Local Coercion b2R : bool >-> R.
Local Coercion Nat.b2n : bool >-> nat.
(* X properties *)
Lemma X0_spec : σx × ∣ 0 ⟩ = ∣ 1 ⟩.
Proof. lma'. Qed.
Lemma X1_spec : σx × ∣ 1 ⟩ = ∣ 0 ⟩.
Proof. lma'. Qed.
Lemma X_specb (b : bool) : σx × ∣ b ⟩ = ∣ negb b ⟩.
Proof.
destruct b.
- apply X1_spec.
- apply X0_spec.
Qed.
(* Y properties *)
Lemma Y0_spec : σy × ∣ 0 ⟩ = Ci .* ∣ 1 ⟩.
Proof. lma'. Qed.
Lemma Y1_spec : σy × ∣ 1 ⟩ = -Ci .* ∣ 0 ⟩.
Proof. lma'. Qed.
Lemma Y_specb (b : bool) :
σy × ∣ b ⟩ = (-1)^b * Ci .* ∣ negb b ⟩.
Proof.
destruct b.
- simpl. rewrite Y1_spec.
f_equal; lca.
- simpl. rewrite Y0_spec.
f_equal; lca.
Qed.
(* Z properties *)
Lemma Z0_spec : σz × ∣ 0 ⟩ = ∣ 0 ⟩.
Proof. lma'. Qed.
Lemma Z1_spec : σz × ∣ 1 ⟩ = -1 .* ∣ 1 ⟩.
Proof. lma'. Qed.
Lemma Z_specb (b : bool) :
σz × ∣ b ⟩ = (-1)^b .* ∣ b ⟩.
Proof.
destruct b.
- simpl. rewrite Z1_spec.
now Csimpl.
- simpl. rewrite Z0_spec.
now rewrite Mscale_1_l.
Qed.
Lemma Z_bspec (b : bool) :
bra b × σz = (-1)^b .* bra b.
Proof.
destruct b.
- simpl. lma'.
- simpl. lma'.
Qed.
Lemma MmultZ1 : σz × ∣1⟩ = - C1 .* ∣1⟩.
Proof. rewrite ket1_equiv, Z1_spec. f_equal; lca. Qed.
Lemma MmultZ0 : σz × ∣0⟩ = ∣0⟩.
Proof. rewrite ket0_equiv, Z0_spec. reflexivity. Qed.
Lemma Mmult1Z : ⟨1∣ × σz = - C1 .* ⟨1∣.
Proof. lma'. Qed.
Lemma Mmult0Z : ⟨0∣ × σz = ⟨0∣.
Proof. lma'. Qed.
(* phase shift properties *)
Lemma phase0_spec : forall ϕ, phase_shift ϕ × ket 0 = ket 0.
Proof. intros. lma'. Qed.
Lemma phase1_spec : forall ϕ, phase_shift ϕ × ket 1 = Cexp ϕ .* ket 1.
Proof. intros. lma'. Qed.
Lemma phase_shift_on_ket : forall (θ : R) (b : bool),
phase_shift θ × ∣ b ⟩ = (Cexp (b * θ)) .* ∣ b ⟩.
Proof.
intros.
destruct b; simpl;
[rewrite Rmult_1_l | rewrite Rmult_0_l, Cexp_0];
solve_matrix_fast.
Qed.
Lemma hadamard_on_ket : forall (b : bool),
hadamard × ∣ b ⟩ = /√2 .* (∣ 0 ⟩ .+ (-1)^b .* ∣ 1 ⟩).
Proof.
intros.
destruct b; solve_matrix_fast.
Qed.
(* CNOT properties *)
Lemma CNOT00_spec : cnot × ∣ 0,0 ⟩ = ∣ 0,0 ⟩.
Proof. lma'. Qed.
Lemma CNOT01_spec : cnot × ∣ 0,1 ⟩ = ∣ 0,1 ⟩.
Proof. lma'. Qed.
Lemma CNOT10_spec : cnot × ∣ 1,0 ⟩ = ∣ 1,1 ⟩.
Proof. lma'. Qed.
Lemma CNOT11_spec : cnot × ∣ 1,1 ⟩ = ∣ 1,0 ⟩.
Proof. lma'. Qed.
Lemma CNOT_spec : forall (x y : nat), (x < 2)%nat -> (y < 2)%nat ->
cnot × ∣ x,y ⟩ = ∣ x, (x + y) mod 2 ⟩.
Proof.
by_cell_no_intros.
- apply CNOT00_spec.
- apply CNOT01_spec.
- apply CNOT10_spec.
- apply CNOT11_spec.
Qed.
(* SWAP properties *)
Lemma SWAP_spec : forall x y, swap × ∣ x,y ⟩ = ∣ y,x ⟩.
Proof. intros. apply swap_spec; auto_wf. Qed.
(* Automation *)
(* General matrix rewrites *)
#[global] Hint Rewrite bra0_equiv bra1_equiv ket0_equiv ket1_equiv : ket_db.
#[global] Hint Rewrite bra0ket0 bra0ket1 bra1ket0 bra1ket1 : ket_db.
#[global] Hint Rewrite Mmult_plus_distr_l Mmult_plus_distr_r kron_plus_distr_l kron_plus_distr_r Mscale_plus_distr_r : ket_db.
#[global] Hint Rewrite Mscale_mult_dist_l Mscale_mult_dist_r Mscale_kron_dist_l Mscale_kron_dist_r : ket_db.
#[global] Hint Rewrite Mscale_assoc @Mmult_assoc : ket_db.
#[global] Hint Rewrite Mmult_1_l Mmult_1_r kron_1_l kron_1_r Mscale_0_l Mscale_0_r Mscale_1_l Mplus_0_l Mplus_0_r using (auto with wf_db) : ket_db.
#[global] Hint Rewrite kron_0_l kron_0_r Mmult_0_l Mmult_0_r : ket_db.
#[global] Hint Rewrite @kron_mixed_product : ket_db.
(* Quantum-specific identities *)
#[global] Hint Rewrite H0_spec H1_spec Hplus_spec Hminus_spec X0_spec X1_spec Y0_spec Y1_spec
Z0_spec Z1_spec phase0_spec phase1_spec : ket_db.
#[global] Hint Rewrite CNOT00_spec CNOT01_spec CNOT10_spec CNOT11_spec SWAP_spec : ket_db.
Lemma ket2bra : forall n, (ket n) † = bra n.
Proof. destruct n; reflexivity. Qed.
#[global] Hint Rewrite ket2bra : ket_db.
(* TODO: add transpose and adjoint lemmas to ket_db? *)
Lemma ket0_transpose_bra0 : (ket 0) ⊤ = bra 0.
Proof. lma'. Qed.
Lemma ket1_transpose_bra1 : (ket 1) ⊤ = bra 1.
Proof. lma'. Qed.
Lemma bra0_transpose_ket0 : (bra 0) ⊤ = ket 0.
Proof. lma'. Qed.
Lemma bra1_transpose_ket1 : (bra 1) ⊤ = ket 1.
Proof. lma'. Qed.
Lemma bra1_adjoint_ket1 : (bra 1) † = ket 1.
Proof. lma'. Qed.
Lemma ket1_adjoint_bra1 : (ket 1) † = bra 1.
Proof. lma'. Qed.
Lemma bra0_adjoint_ket0 : (bra 0) † = ket 0.
Proof. lma'. Qed.
Lemma ket0_adjoint_bra0 : (ket 0) † = bra 0.
Proof. lma'. Qed.
(* Examples using ket_db *)
Lemma XYZ0 : -Ci .* σx × σy × σz × ∣ 0 ⟩ = ∣ 0 ⟩.
Proof. autorewrite with ket_db C_db; easy. Qed.
Lemma XYZ1 : -Ci .* σx × σy × σz × ∣ 1 ⟩ = ∣ 1 ⟩.
Proof.
autorewrite with ket_db C_db.
replace (Ci * -1 * Ci) with (RtoC 1) by lca.
apply Mscale_1_l.
Qed.
(*******************************)
(** Classical States **)
(*******************************)
Local Close Scope C_scope.
Local Close Scope R_scope.
Local Open Scope nat_scope.
(* Convert a boolean function to a vector; examples:
f_to_vec 3 f --> I 1 ⊗ ∣ f 0 ⟩ ⊗ ∣ f 1 ⟩ ⊗ | f 2 ⟩
f_to_vec 2 (shift f 2) --> I 1 ⊗ ∣ f 2 ⟩ ⊗ ∣ f 3 ⟩
*)
Fixpoint f_to_vec (n : nat) (f : nat -> bool) : Vector (2^n) :=
match n with
| 0 => I 1
| S n' => (f_to_vec n' f) ⊗ ∣ f n' ⟩
end.
Lemma f_to_vec_WF : forall (n : nat) (f : nat -> bool),
WF_Matrix (f_to_vec n f).
Proof.
intros.
induction n; simpl; try auto with wf_db.
Qed.
#[export] Hint Resolve f_to_vec_WF : wf_db.
Lemma f_to_vec_eq : forall n f f',
(forall i, i < n -> f i = f' i) ->
f_to_vec n f = f_to_vec n f'.
Proof.
intros.
induction n.
reflexivity.
simpl.
replace (f' n) with (f n) by auto.
rewrite IHn by auto.
reflexivity.
Qed.
(* Convert a natural number to a vector *)
(* TODO: this is very similar to e_i in VecSet.v. Could use just e_i? *)
Definition basis_vector (n k : nat) : Vector n :=
fun i j => if (i =? k) && (j =? 0) then C1 else C0.
Lemma basis_vector_WF : forall n i, (i < n)%nat -> WF_Matrix (basis_vector n i).
Proof.
unfold basis_vector, WF_Matrix.
intros.
bdestruct (n <=? x)%nat; bdestruct (1 <=? y)%nat; try lia.
bdestructΩ (x =? i)%nat. reflexivity.
bdestructΩ (x =? i)%nat. reflexivity.
bdestructΩ (y =? 0)%nat. rewrite andb_false_r. reflexivity.
Qed.
#[export] Hint Resolve basis_vector_WF : wf_db.
Lemma basis_vector_product_eq : forall d n,
n < d -> (basis_vector d n)† × basis_vector d n = I 1.
Proof.
intros.
prep_matrix_equality.
unfold basis_vector, adjoint, Mmult, I.
bdestruct (x =? y); bdestruct (x <? 1); simpl.
apply big_sum_unique.
exists n.
repeat split; auto.
bdestruct_all; simpl; lca.
intros i Hi. bdestructΩ (i =? n).
intros. lca.
all: apply (@big_sum_0 C C_is_monoid); intro i; bdestruct_all; simpl; lca.
Qed.
Lemma basis_vector_pure_state : forall n i,
(i < n)%nat -> Pure_State_Vector (basis_vector n i).
Proof.
intros. split. apply basis_vector_WF. easy.
apply basis_vector_product_eq. easy.
Qed.
Lemma basis_vector_product_neq : forall d m n,
(m < d)%nat -> (n < d)%nat -> (m <> n)%nat -> (basis_vector d m)† × basis_vector d n = Zero.
Proof.
intros.
prep_matrix_equality.
unfold basis_vector, adjoint, Mmult, Zero.
apply (@big_sum_0 C C_is_monoid).
intro i; bdestruct_all; lca.
Qed.
Lemma matrix_times_basis_eq : forall m n (A : Matrix m n) i j,
WF_Matrix A ->
(A × basis_vector n j) i 0 = A i j.
Proof.
intros m n A i j WFA.
unfold basis_vector.
unfold Mmult.
bdestruct (j <? n).
2:{ rewrite big_sum_0. rewrite WFA; auto.
intros j'. bdestruct (j' =? j); subst; simpl; try lca.
rewrite WFA by auto. lca. }
erewrite big_sum_unique.
reflexivity.
exists j.
repeat split; trivial.
rewrite 2 Nat.eqb_refl; simpl; lca.
intros j' Hj.
bdestruct_all; auto.
all : intros; simpl; try lca.
subst; easy.
Qed.
Lemma equal_on_basis_vectors_implies_equal : forall m n (A B : Matrix m n),
WF_Matrix A ->
WF_Matrix B ->
(forall k, k < n -> A × (basis_vector n k) = B × (basis_vector n k)) ->
A = B.
Proof.
intros m n A B WFA WFB H.
prep_matrix_equality.
bdestruct (y <? n). 2: rewrite WFA, WFB; auto.
rewrite <- matrix_times_basis_eq; trivial.
rewrite H; trivial.
rewrite matrix_times_basis_eq; easy.
Qed.
Lemma divmod_decomp : forall x y z r,
(r > 0)%nat ->
(z < r)%nat ->
(x = y * r + z <-> x / r = y /\ x mod r = z)%nat.
Proof.
split; intros.
- split. symmetry. apply Nat.div_unique with (r := z); try lia.
symmetry. apply Nat.mod_unique with (q := y); try lia.
- destruct H1.
replace (y * r)%nat with (r * y)%nat by lia.
rewrite <- H1, <- H2.
apply Nat.div_mod.
lia.
Qed.
Lemma split_basis_vector : forall m n x y,
(x < 2 ^ m)%nat ->
(y < 2 ^ n)%nat ->
basis_vector (2 ^ (m + n)) (x * 2 ^ n + y)
= basis_vector (2 ^ m) x ⊗ basis_vector (2 ^ n) y.
Proof.
intros m n x y Hx Hy.
apply mat_equiv_eq;
[apply basis_vector_WF; Modulus.show_moddy_lt|auto_wf|].
unfold kron, basis_vector.
intros i j Hi Hj.
replace j with 0 by lia.
Modulus.simpl_bools.
rewrite Cmult_if_if_1_l.
rewrite Modulus.eqb_comb_iff_div_mod_eqb by easy.
now rewrite andb_comm.
Qed.
(* rewrite f_to_vec as basis_vector *)
Lemma basis_f_to_vec : forall n f,
f_to_vec n f = basis_vector (2^n) (funbool_to_nat n f).
Proof.
intros.
induction n.
- unfold funbool_to_nat; simpl.
solve_matrix_fast.
- pose proof (funbool_to_nat_bound (S n) f).
prep_matrix_equivalence.
cbn -[Nat.pow Nat.mul].
rewrite IHn.
unfold funbool_to_nat; cbn -[Nat.pow Nat.mul].
unfold basis_vector.
intros i j Hi Hj.
replace j with 0 by lia.
unfold kron.
rewrite Nat.div_1_r, Nat.mod_1_r.
Modulus.simpl_bools.
rewrite Nat.add_comm, Nat.mul_comm.
rewrite Modulus.eqb_comb_iff_div_mod_eqb
by (destruct (f n); simpl; lia).
rewrite andb_comm, <- Cmult_if_if_1_l.
f_equal.
generalize (Nat.mod_upper_bound i 2 ltac:(lia)).
generalize (f n) (i mod 2).
now intros []; (intros [|[]]; [..|easy]).
Qed.
(* rewrite basis_vector as f_to_vec *)
Lemma basis_f_to_vec_alt : forall len n, (n < 2 ^ len)%nat ->
basis_vector (2 ^ len) n = f_to_vec len (nat_to_funbool len n).
Proof.
intros.
rewrite basis_f_to_vec.
rewrite nat_to_funbool_inverse; auto.
Qed.
(* allows us to prove equivalence of unitary programs using
vector state reasoning *)
Lemma equal_on_basis_states_implies_equal : forall {n dim} (A B : Matrix n (2 ^ dim)),
WF_Matrix A ->
WF_Matrix B ->
(forall f, A × (f_to_vec dim f) = B × (f_to_vec dim f)) ->
A = B.
Proof.
intros n dim A B WFA WFB H.
apply equal_on_basis_vectors_implies_equal; trivial.
intros k Lk.
rewrite basis_f_to_vec_alt; auto.
Qed.
Lemma equal_on_conj_basis_states_implies_equal {n m}
(A B : Matrix (2 ^ n) (2 ^ m)) : WF_Matrix A -> WF_Matrix B ->
(forall f g, (f_to_vec n g) ⊤ × (A × f_to_vec m f) =
(f_to_vec n g) ⊤ × (B × f_to_vec m f)) -> A = B.
Proof.
intros HA HB HAB.
apply equal_on_basis_states_implies_equal; [auto..|].
intros f.
apply transpose_matrices.
apply equal_on_basis_states_implies_equal; [auto_wf..|].
intros g.
apply transpose_matrices.
rewrite Mmult_transpose, transpose_involutive, HAB.
rewrite Mmult_transpose, transpose_involutive.
reflexivity.
Qed.
Lemma f_to_vec_update_oob : forall (n : nat) (f : nat -> bool) (i : nat) (b : bool),
n <= i -> f_to_vec n (update f i b) = f_to_vec n f.
Proof.
intros.
induction n; simpl; try reflexivity.
rewrite <- IHn by lia.
unfold update.
bdestructΩ (n =? i).
reflexivity.
Qed.
Lemma f_to_vec_shift_update_oob : forall (n : nat) (f : nat -> bool) (i j : nat) (b : bool),
j + n <= i \/ i < j ->
f_to_vec n (shift (update f i b) j) = f_to_vec n (shift f j).
Proof.
intros. destruct H.
- induction n; simpl; try reflexivity.
rewrite <- IHn by lia.
unfold shift, update.
bdestructΩ (n + j =? i).
reflexivity.
- induction n; simpl; try reflexivity.
rewrite <- IHn by lia.
unfold shift, update.
bdestructΩ (n + j =? i).
reflexivity.
Qed.
Lemma f_to_vec_split : forall (base n i : nat) (f : nat -> bool),
i < n ->
f_to_vec n f = (f_to_vec i f) ⊗ ∣ f i ⟩ ⊗ (f_to_vec (n - 1 - i) (shift f (i + 1))).
Proof.
intros.
induction n.
- contradict H. lia.
- bdestruct (i =? n).
+ subst.
replace (S n - 1 - n)%nat with O by lia.
simpl. Msimpl.
reflexivity.
+ assert (i < n)%nat by lia.
specialize (IHn H1).
replace (S n - 1 - i)%nat with (S (n - 1 - i))%nat by lia.
simpl.
rewrite IHn.
restore_dims; repeat rewrite kron_assoc by auto with wf_db.
unfold shift; simpl.
replace (n - 1 - i + (i + 1))%nat with n by lia.
reflexivity.
Qed.
Lemma f_to_vec_merge : forall f1 f2 m n,
f_to_vec m f1 ⊗ f_to_vec n f2 =
f_to_vec (m + n) (fun x => if x <? m then f1 x else f2 (x - m)%nat).
Proof.
intros f1 f2 m n.
induction n.
- simpl. Msimpl.
replace (m + 0)%nat with m by lia.
apply f_to_vec_eq; intros i Hi.
bdestructΩ (i <? m).
reflexivity.
- replace (m + S n)%nat with (S (m + n)) by lia.
simpl.
restore_dims.
rewrite <- kron_assoc; auto with wf_db.
rewrite IHn.
bdestructΩ (m + n <? m).
replace (m + n - m)%nat with n by lia.
reflexivity.
Qed.
(* lemmas to describe the action of various gates on f_to_vec states *)
Lemma f_to_vec_σx : forall (n i : nat) (f : nat -> bool),
i < n ->
(pad_u n i σx) × (f_to_vec n f) = f_to_vec n (update f i (¬ (f i))).
Proof.
intros.
unfold pad_u, pad.
rewrite (f_to_vec_split 0 n i f H).
repad.
replace (i + 1 + x - 1 - i) with x by lia.
Msimpl.
rewrite (f_to_vec_split 0 (i + 1 + x) i) by lia.
rewrite f_to_vec_update_oob by lia.
rewrite f_to_vec_shift_update_oob by lia.
rewrite update_index_eq.
replace (i + 1 + x - 1 - i) with x by lia.
destruct (f i); simpl; autorewrite with ket_db; reflexivity.
Qed.
Lemma f_to_vec_σy : forall (n i : nat) (f : nat -> bool),
i < n ->
(pad_u n i σy) × (f_to_vec n f) =
(-1)%R^(f i) * Ci .* f_to_vec n (update f i (¬ f i)).
Proof.
intros n i f Hi.
unfold pad_u, pad.
rewrite (f_to_vec_split 0 n i f Hi).
repad.
replace (i + 1 + x - 1 - i) with x by lia.
Msimpl.
rewrite Y_specb.
distribute_scale.
rewrite (f_to_vec_split 0 (i + 1 + x) i) by lia.
rewrite f_to_vec_update_oob by lia.
rewrite f_to_vec_shift_update_oob by lia.
rewrite update_index_eq.
replace (i + 1 + x - 1 - i) with x by lia.
easy.
Qed.
Lemma f_to_vec_σz : forall (n i : nat) (f : nat -> bool),
i < n ->
(pad_u n i σz) × (f_to_vec n f) =
(-1)%R^(f i) .* f_to_vec n f.
Proof.
intros n i f Hi.
unfold pad_u, pad.
rewrite (f_to_vec_split 0 n i f Hi).
repad.
replace (i + 1 + x - 1 - i) with x by lia.
Msimpl.
rewrite Z_specb.
distribute_scale.
reflexivity.
Qed.
Lemma f_to_vec_cnot : forall (n i j : nat) (f : nat -> bool),
i < n -> j < n -> i <> j ->
(pad_ctrl n i j σx) × (f_to_vec n f) = f_to_vec n (update f j (f j ⊕ f i)).
Proof.
intros.
unfold pad_ctrl, pad.
repad.
- repeat rewrite (f_to_vec_split 0 (i + (1 + d + 1) + x) i) by lia.
rewrite f_to_vec_update_oob by lia.
rewrite update_index_neq by lia.
repeat rewrite (f_to_vec_split (0 + i + 1) (i + (1 + d + 1) + x - 1 - i) d) by lia.
repeat rewrite shift_plus.
replace (i + (1 + d + 1) + x - 1 - i - 1 - d) with x by lia.
repeat rewrite f_to_vec_shift_update_oob by lia.
repeat rewrite shift_simplify.
replace (d + (i + 1)) with (i + 1 + d) by lia.
rewrite update_index_eq.
restore_dims.
rewrite <- !kron_assoc by auto_wf.
restore_dims.
rewrite kron_mixed_product' by lia.
rewrite Mmult_1_l by auto_wf.
restore_dims.
rewrite (kron_assoc (f_to_vec i f)) by auto_wf.
restore_dims.
rewrite !(kron_assoc (f_to_vec i f)) by auto_wf.
restore_dims.
f_equal.
rewrite kron_mixed_product, Mmult_1_l by auto_wf.
f_equal.
simpl.
restore_dims.
distribute_plus.
rewrite !kron_mixed_product.
rewrite 2!Mmult_1_l by auto_wf.
symmetry.
rewrite <- (Mmult_1_l _ _ (∣ f i ⟩)) at 1 by auto_wf.
rewrite <- Mplus10.
distribute_plus.
rewrite !(Mmult_assoc _ _ (∣ f i ⟩)).
rewrite bra1_equiv, bra1ket_eqb.
rewrite bra0_equiv, bra0ket_eqb.
destruct (f i); simpl; rewrite Mmult_0_r, !kron_0_l.
+ rewrite xorb_true_r.
now rewrite X_specb.
+ now rewrite xorb_false_r.
- repeat rewrite (f_to_vec_split 0 (j + (1 + d + 1) + x0) j); try lia.
rewrite f_to_vec_update_oob by lia.
rewrite update_index_eq.
repeat rewrite (f_to_vec_split (j + 1) (j + (1 + d + 1) + x0 - 1 - j) d); try lia.
repeat rewrite shift_plus.
repeat rewrite f_to_vec_shift_update_oob by lia.
repeat rewrite shift_simplify.
replace (d + (j + 1)) with (j + 1 + d) by lia.
rewrite update_index_neq by lia.
replace (j + (1 + d + 1) + x0 - 1 - j - 1 - d) with x0 by lia.
restore_dims.
rewrite kron_assoc, !(kron_assoc (f_to_vec j f)) by auto_wf.
restore_dims.
rewrite kron_mixed_product' by lia.
f_equal; [lia | apply Mmult_1_l; auto_wf|].
rewrite <- 4!kron_assoc by auto_wf.
restore_dims.
rewrite kron_mixed_product.
f_equal; [| apply Mmult_1_l; auto_wf].
distribute_plus.
rewrite !kron_mixed_product, Mmult_1_l by auto_wf.
rewrite !Mmult_assoc.
rewrite Mmult_1_l by auto_wf.
rewrite bra1_equiv, bra1ket_eqb.
rewrite bra0_equiv, bra0ket_eqb.
destruct (f (j + 1 + d)); simpl; rewrite Mmult_0_r, !kron_0_r.
+ rewrite Mplus_0_r.
rewrite xorb_true_r.
rewrite Mmult_1_r, ket1_equiv by auto_wf.
now rewrite X_specb.
+ rewrite Mplus_0_l.
rewrite Mmult_1_r, ket0_equiv by auto_wf.
now rewrite xorb_false_r.
Qed.
Lemma f_to_vec_swap : forall (n i j : nat) (f : nat -> bool),
i < n -> j < n -> i <> j ->
(pad_swap n i j) × (f_to_vec n f) = f_to_vec n (fswap f i j).
Proof.
intros n i j f ? ? ?.
unfold pad_swap.
repeat rewrite Mmult_assoc.
rewrite 3 f_to_vec_cnot by auto.
repeat rewrite update_index_eq.
repeat rewrite update_index_neq by lia.
repeat rewrite update_index_eq.
replace ((f j ⊕ f i) ⊕ (f i ⊕ (f j ⊕ f i))) with (f i).
replace (f i ⊕ (f j ⊕ f i)) with (f j).
rewrite update_twice_neq by auto.
rewrite update_twice_eq.
reflexivity.
all: destruct (f i); destruct (f j); reflexivity.
Qed.
Lemma f_to_vec_phase_shift : forall (n i : nat) (θ : R) (f : nat -> bool),
(i < n)%nat ->
(pad_u n i (phase_shift θ)) × (f_to_vec n f) =
(Cexp ((f i) * θ)) .* f_to_vec n f.
Proof.
intros.
unfold pad_u, pad.
rewrite (f_to_vec_split 0 n i f H).
simpl; replace (n - 1 - i)%nat with (n - (i + 1))%nat by lia.
repad.
Msimpl.
rewrite phase_shift_on_ket.
rewrite Mscale_kron_dist_r.
rewrite Mscale_kron_dist_l.
reflexivity.
Qed.
Local Open Scope R_scope.
Lemma f_to_vec_hadamard : forall (n i : nat) (f : nat -> bool),
(i < n)%nat ->
(pad_u n i hadamard) × (f_to_vec n f)
= /√2 .* ((f_to_vec n (update f i false)) .+
(Cexp ((f i) * PI)) .* f_to_vec n (update f i true)).
Proof.
intros.
unfold pad_u, pad.
rewrite (f_to_vec_split 0 n i f H).
simpl; replace (n - 1 - i)%nat with (n - (i + 1))%nat by lia.
repad.
Msimpl.
rewrite hadamard_on_ket.
rewrite Mscale_kron_dist_r, Mscale_kron_dist_l.
rewrite kron_plus_distr_l, kron_plus_distr_r.
rewrite Mscale_kron_dist_r, Mscale_kron_dist_l.
rewrite 2 (f_to_vec_split 0 (i + 1 + x) i _) by lia.
replace (i + 1 + x - 1 - i)%nat with x by lia.
simpl.
rewrite 2 update_index_eq.
repeat rewrite f_to_vec_update_oob by lia.
repeat rewrite f_to_vec_shift_update_oob by lia.
do 3 (apply f_equal2; auto).
destruct (f i); simpl; autorewrite with R_db Cexp_db; lca.
Qed.
Local Close Scope R_scope.
#[global] Hint Rewrite f_to_vec_cnot f_to_vec_σx f_to_vec_phase_shift using lia : f_to_vec_db.
#[global] Hint Rewrite (@update_index_eq bool) (@update_index_neq bool) (@update_twice_eq bool) (@update_same bool) using lia : f_to_vec_db.
Import Modulus.
Lemma kron_f_to_vec {n m p q} (A : Matrix (2^n) (2^m))
(B : Matrix (2^p) (2^q)) f :
@mat_equiv _ 1 (A ⊗ B × f_to_vec (m + q) f)
((A × f_to_vec m f (* : Matrix _ 1 *)) ⊗
(B × f_to_vec q (fun k => f (m + k)) (* : Matrix _ 1) *))).
Proof.
rewrite <- kron_mixed_product.
rewrite f_to_vec_merge.
Morphisms.f_equiv.
apply f_to_vec_eq.
intros; bdestructΩ'; f_equal; lia.
Qed.
Lemma kron_f_to_vec_eq {n m p q : nat} (A : Matrix (2^n) (2^m))
(B : Matrix (2^p) (2^q)) (f : nat -> bool) : WF_Matrix A -> WF_Matrix B ->
A ⊗ B × f_to_vec (m + q) f
= A × f_to_vec m f ⊗ (B × f_to_vec q (fun k : nat => f (m + k))).
Proof.
intros.
prep_matrix_equivalence.
apply kron_f_to_vec.
Qed.
Lemma f_to_vec_split' n m f :
mat_equiv (f_to_vec (n + m) f)
(f_to_vec n f ⊗ f_to_vec m (fun k => f (n + k))).
Proof.
intros i j Hi Hj.
rewrite f_to_vec_merge.
erewrite f_to_vec_eq; [reflexivity|].
intros; simpl; bdestructΩ'; f_equal; lia.
Qed.
Lemma f_to_vec_split'_eq n m f :
(f_to_vec (n + m) f) =
(f_to_vec n f ⊗ f_to_vec m (fun k => f (n + k))).
Proof.
apply mat_equiv_eq; [..|apply f_to_vec_split']; auto with wf_db.
Qed.
Lemma f_to_vec_1_eq f :
f_to_vec 1 f = if f 0 then ∣1⟩ else ∣0⟩.
Proof.
cbn.
unfold ket.
rewrite kron_1_l by (destruct (f 0); auto with wf_db).
now destruct (f 0).
Qed.
Lemma f_to_vec_1_mult_r f (A : Matrix (2^1) (2^1)) :
A × f_to_vec 1 f = (fun x j => if j =? 0 then A x (Nat.b2n (f 0)) else 0%R).
Proof.
cbn.
rewrite kron_1_l by auto with wf_db.
apply functional_extensionality; intros i.
apply functional_extensionality; intros j.
unfold Mmult.
simpl.
destruct (f 0);
unfold ket;
simpl;
now destruct j; simpl; Csimpl.
Qed.
Lemma f_to_vec_1_mult_r_decomp f (A : Matrix (2^1) (2^1)) :
A × f_to_vec 1 f ≡
A 0 (Nat.b2n (f 0)) .* ∣0⟩ .+
A 1 (Nat.b2n (f 0)) .* ∣1⟩.
Proof.
rewrite f_to_vec_1_mult_r.
intros i j Hi Hj.
replace j with 0 by lia.
simpl.
autounfold with U_db.
do 2 (try destruct i); [..| simpl in *; lia];
now Csimpl.
Qed.
Lemma f_to_vec_1_mult_r_decomp_eq f (A : Matrix (2^1) (2^1)) :
WF_Matrix A ->
A × f_to_vec 1 f =
A 0 (Nat.b2n (f 0)) .* ∣0⟩ .+
A 1 (Nat.b2n (f 0)) .* ∣1⟩.
Proof.
intros.
apply mat_equiv_eq; auto with wf_db.
apply f_to_vec_1_mult_r_decomp.
Qed.
Lemma qubit0_f_to_vec : ∣0⟩ = f_to_vec 1 (fun x => false).
Proof. now rewrite f_to_vec_1_eq. Qed.
Lemma qubit1_f_to_vec : ∣1⟩ = f_to_vec 1 (fun x => x =? 0).
Proof. now rewrite f_to_vec_1_eq. Qed.
Lemma ket_f_to_vec b : ∣ Nat.b2n b ⟩ = f_to_vec 1 (fun x => b).
Proof.
destruct b; [apply qubit1_f_to_vec | apply qubit0_f_to_vec].
Qed.
Lemma f_to_vec_1_mult_r_decomp_eq' f (A : Matrix (2^1) (2^1)) :
WF_Matrix A ->
A × f_to_vec 1 f =
A 0 (Nat.b2n (f 0)) .* f_to_vec 1 (fun x => false) .+
A 1 (Nat.b2n (f 0)) .* f_to_vec 1 (fun x => x=?0).
Proof.
intros.
apply mat_equiv_eq; auto with wf_db.
rewrite f_to_vec_1_mult_r_decomp.
rewrite 2!f_to_vec_1_eq.
easy.
Qed.
Lemma f_to_vec_1_mult_l_decomp f (A : Matrix (2^1) (2^1)) :
(f_to_vec 1 f) ⊤ × A ≡
A (Nat.b2n (f 0)) 0 .* (∣0⟩ ⊤) .+
A (Nat.b2n (f 0)) 1 .* (∣1⟩ ⊤).
Proof.
rewrite <- (transpose_involutive _ _ A).
rewrite <- Mmult_transpose, <- Mscale_trans.
intros i j Hi Hj.
apply (f_to_vec_1_mult_r_decomp f (A ⊤)); easy.
Qed.
Lemma f_to_vec_1_mult_l_decomp_eq f (A : Matrix (2^1) (2^1)) :
WF_Matrix A ->
(f_to_vec 1 f) ⊤ × A =
A (Nat.b2n (f 0)) 0 .* (∣0⟩ ⊤) .+
A (Nat.b2n (f 0)) 1 .* (∣1⟩ ⊤).
Proof.
intros.
apply mat_equiv_eq; auto with wf_db.
apply f_to_vec_1_mult_l_decomp.
Qed.
Lemma f_to_vec_1_mult_l_decomp_eq' f (A : Matrix (2^1) (2^1)) :
WF_Matrix A ->
(f_to_vec 1 f) ⊤ × A =
A (Nat.b2n (f 0)) 0 .* ((f_to_vec 1 (fun x => false)) ⊤) .+
A (Nat.b2n (f 0)) 1 .* ((f_to_vec 1 (fun x => x =? 0)) ⊤).
Proof.
intros.
apply mat_equiv_eq; auto with wf_db.
rewrite f_to_vec_1_mult_l_decomp_eq by easy.
now rewrite qubit0_f_to_vec, qubit1_f_to_vec.
Qed.
Lemma basis_trans_basis {n} i j :
((basis_vector n i) ⊤ × basis_vector n j) 0 0 =
if (i =? j) && (i <? n) then C1 else 0%R.
Proof.
unfold Mmult, basis_vector, Matrix.transpose.
bdestructΩ'.
- erewrite big_sum_eq_bounded.
2: {
intros k Hk.
simpl_bools.
rewrite Cmult_if_if_1_l.
replace_bool_lia ((k =? j) && (k =? j)) (k =? j).
reflexivity.
}
rewrite big_sum_if_eq_C.
bdestructΩ'.
- rewrite big_sum_0_bounded; [easy|].
intros; bdestructΩ'; lca.
- rewrite big_sum_0_bounded; [easy|].
intros; bdestructΩ'; lca.
- rewrite big_sum_0_bounded; [easy|].
intros; bdestructΩ'; lca.
Qed.
Lemma f_to_vec_transpose_f_to_vec n f g :
transpose (f_to_vec n f) × f_to_vec n g =
b2R (forallb (fun k => eqb (f k) (g k)) (seq 0 n)) .* I 1.
Proof.
prep_matrix_equivalence.
intros [] []; [|lia..]; intros _ _.
rewrite 2!basis_f_to_vec.
rewrite basis_trans_basis.
pose proof (funbool_to_nat_bound n f).
simplify_bools_lia_one_kernel.
unfold scale.
cbn.
rewrite Cmult_1_r.
unfold b2R.
rewrite (if_dist _ _ _ RtoC).
apply f_equal_if; [|easy..].
apply eq_iff_eq_true.
rewrite Nat.eqb_eq, forallb_seq0, <- funbool_to_nat_eq_iff.
now setoid_rewrite eqb_true_iff.
Qed.
Lemma f_to_vec_transpose_f_to_vec' n f g :
transpose (f_to_vec n f) × f_to_vec n g =
(if funbool_to_nat n f =? funbool_to_nat n g then