-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathQuantum.v
2186 lines (1829 loc) · 59.8 KB
/
Quantum.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
(** In this file, we define specific objects/concepts specific to quantum computing and we prove lemmas about thems. *)
Require Import Psatz.
Require Import Reals.
Require Export VecSet.
Require Export CauchySchwarz.
Require Import Kronecker.
(* Using our (complex, unbounded) matrices, their complex numbers *)
(*******************************************)
(** * Quantum basis states *)
(*******************************************)
(* Maybe change to IF statements? *)
Definition qubit0 : Vector 2 :=
fun x y => match x, y with
| 0, 0 => C1
| 1, 0 => C0
| _, _ => C0
end.
Definition qubit1 : Vector 2 :=
fun x y => match x, y with
| 0, 0 => C0
| 1, 0 => C1
| _, _ => C0
end.
(* Ket notation: \mid 0 \rangle *)
Notation "∣0⟩" := qubit0.
Notation "∣1⟩" := qubit1.
Notation "⟨0∣" := qubit0†.
Notation "⟨1∣" := qubit1†.
Notation "∣0⟩⟨0∣" := (∣0⟩×⟨0∣).
Notation "∣1⟩⟨1∣" := (∣1⟩×⟨1∣).
Notation "∣1⟩⟨0∣" := (∣1⟩×⟨0∣).
Notation "∣0⟩⟨1∣" := (∣0⟩×⟨1∣).
Definition bra (x : nat) : Matrix 1 2 := if x =? 0 then ⟨0∣ else ⟨1∣.
Definition ket (x : nat) : Matrix 2 1 := if x =? 0 then ∣0⟩ else ∣1⟩.
(* Note the 'mid' symbol for these *)
Notation "'∣' x '⟩'" := (ket x).
Notation "'⟨' x '∣'" := (bra x). (* This gives the Coq parser headaches *)
Notation "∣ x , y , .. , z ⟩" := (kron .. (kron ∣x⟩ ∣y⟩) .. ∣z⟩) (at level 0).
(* Alternative: |0⟩|1⟩. *)
Transparent bra.
Transparent ket.
Transparent qubit0.
Transparent qubit1.
Definition bool_to_ket (b : bool) : Matrix 2 1 := if b then ∣1⟩ else ∣0⟩.
Definition bool_to_matrix (b : bool) : Matrix 2 2 := if b then ∣1⟩⟨1∣ else ∣0⟩⟨0∣.
Definition bool_to_matrix' (b : bool) : Matrix 2 2 := fun x y =>
match x, y with
| 0, 0 => if b then 0 else 1
| 1, 1 => if b then 1 else 0
| _, _ => 0
end.
Lemma bool_to_matrix_eq : forall b, bool_to_matrix b = bool_to_matrix' b.
Proof. intros. destruct b; simpl; solve_matrix. Qed.
Lemma bool_to_ket_matrix_eq : forall b,
outer_product (bool_to_ket b) (bool_to_ket b) = bool_to_matrix b.
Proof. unfold outer_product. destruct b; simpl; reflexivity. Qed.
Definition bools_to_matrix (l : list bool) : Square (2^(length l)) :=
big_kron (map bool_to_matrix l).
Lemma ket_decomposition : forall (ψ : Vector 2),
WF_Matrix ψ ->
ψ = (ψ 0%nat 0%nat) .* ∣ 0 ⟩ .+ (ψ 1%nat 0%nat) .* ∣ 1 ⟩.
Proof.
intros.
prep_matrix_equality.
unfold scale, Mplus.
destruct y as [|y'].
2:{ rewrite H; try lia.
unfold ket, qubit0, qubit1. simpl.
repeat (destruct x; try lca). }
destruct x as [| [| n]]; unfold ket, qubit0, qubit1; simpl; try lca.
rewrite H; try lia.
lca.
Qed.
(* Defining other basis states *)
Definition xbasis_plus : Vector 2 := / (√ 2) .* (∣0⟩ .+ ∣1⟩).
Definition xbasis_minus : Vector 2 := / (√ 2) .* (∣0⟩ .+ ((-1) .* ∣1⟩)).
Definition ybasis_plus : Vector 2 := / (√ 2) .* (∣0⟩ .+ Ci .* ∣1⟩).
Definition ybasis_minus : Vector 2 := / (√ 2) .* (∣0⟩ .+ ((-Ci) .* ∣1⟩)).
Definition braminus := / √ 2 .* (⟨0∣ .+ (-1 .* ⟨1∣)).
Definition braplus := / √ 2 .* (⟨0∣ .+ ⟨1∣).
Notation "∣+⟩" := xbasis_plus.
Notation "∣-⟩" := xbasis_minus.
Notation "⟨+∣" := braplus.
Notation "⟨-∣" := braminus.
Notation "∣R⟩" := ybasis_plus.
Notation "∣L⟩" := ybasis_minus.
Lemma xbasis_plus_spec : ∣+⟩ = / √ 2 .* (∣0⟩ .+ ∣1⟩).
Proof. reflexivity. Qed.
Lemma xbasis_minus_spec : ∣-⟩ = / √ 2 .* (∣0⟩ .+ (- 1) .* (∣1⟩)).
Proof. reflexivity. Qed.
(* defining the EPR pair *)
Definition EPRpair : Vector 4 := / (√ 2) .* (∣0,0⟩ .+ ∣1,1⟩).
Notation "∣Φ+⟩" := EPRpair.
(****************)
(** * Unitaries *)
(****************)
Definition hadamard : Matrix 2 2 :=
(fun x y => match x, y with
| 0, 0 => (1 / √2)
| 0, 1 => (1 / √2)
| 1, 0 => (1 / √2)
| 1, 1 => -(1 / √2)
| _, _ => 0
end).
Fixpoint hadamard_k (k : nat) : Matrix (2^k) (2^k):=
match k with
| 0 => I 1
| S k' => hadamard ⊗ hadamard_k k'
end.
Lemma hadamard_1 : hadamard_k 1 = hadamard.
Proof. apply kron_1_r. Qed.
Definition σx : Matrix 2 2 :=
fun x y => match x, y with
| 0, 1 => C1
| 1, 0 => C1
| _, _ => C0
end.
Definition σy : Matrix 2 2 :=
fun x y => match x, y with
| 0, 1 => -Ci
| 1, 0 => Ci
| _, _ => C0
end.
Definition σz : Matrix 2 2 :=
fun x y => match x, y with
| 0, 0 => C1
| 1, 1 => -C1
| _, _ => C0
end.
Definition sqrtx : Matrix 2 2 :=
fun x y => match x, y with
| 0, 0 => (1 + Ci)/2
| 0, 1 => (1 - Ci)/2
| 1, 0 => (1 - Ci)/2
| 1, 1 => (1 + Ci)/2
| _, _ => C0
end.
Definition control {n : nat} (A : Matrix n n) : Matrix (2*n) (2*n) :=
fun x y => if (x <? n) && (y =? x) then 1 else
if (n <=? x) && (n <=? y) then A (x-n)%nat (y-n)%nat else 0.
(* Definition cnot := control pauli_x. *)
(* Direct definition makes our lives easier *)
(* Dimensions are given their current form for convenient
kron_mixed_product applications *)
Definition cnot : Matrix (2*2) (2*2) :=
fun x y => match x, y with
| 0, 0 => C1
| 1, 1 => C1
| 2, 3 => C1
| 3, 2 => C1
| _, _ => C0
end.
Definition notc : Matrix (2*2) (2*2) :=
fun x y => match x, y with
| 1, 3 => 1%C
| 3, 1 => 1%C
| 0, 0 => 1%C
| 2, 2 => 1%C
| _, _ => 0%C
end.
(* Swap Matrices *)
Definition swap : Matrix (2*2) (2*2) :=
fun x y => match x, y with
| 0, 0 => C1
| 1, 2 => C1
| 2, 1 => C1
| 3, 3 => C1
| _, _ => C0
end.
#[export] Hint Unfold qubit0 qubit1 hadamard σx σy σz control cnot swap bra ket : U_db.
(** ** Rotation Matrices *)
(* The definition given below is different from the standard definition (shown in the comments),
but equivalent up to a global phase.
Definition rotation (θ ϕ λ : R) : Matrix 2 2 :=
fun x y => match x, y with
| 0, 0 => (Cexp (-(ϕ + λ)/2)) * (cos (θ/2))
| 0, 1 => - (Cexp (-(ϕ - λ)/2)) * (sin (θ/2))
| 1, 0 => (Cexp ((ϕ - λ)/2)) * (sin (θ/2))
| 1, 1 => (Cexp ((ϕ + λ)/2)) * (cos (θ/2))
| _, _ => C0
end.
*)
Definition rotation (θ ϕ λ : R) : Matrix 2 2 :=
fun x y => match x, y with
| 0, 0 => (cos (θ/2))
| 0, 1 => - (Cexp λ) * (sin (θ/2))
| 1, 0 => (Cexp ϕ) * (sin (θ/2))
| 1, 1 => (Cexp (ϕ + λ)) * (cos (θ/2))
| _, _ => C0
end.
Definition phase_shift (ϕ : R) : Matrix 2 2 :=
fun x y => match x, y with
| 0, 0 => C1
| 1, 1 => Cexp ϕ
| _, _ => C0
end.
Definition x_rotation (θ : R) : Matrix 2 2 :=
fun x y => match x, y with
| 0, 0 => cos (θ / 2)
| 0, 1 => -Ci * sin (θ / 2)
| 1, 0 => -Ci * sin (θ / 2)
| 1, 1 => cos (θ / 2)
| _, _ => 0
end.
Definition y_rotation (θ : R) : Matrix 2 2 :=
fun x y => match x, y with
| 0, 0 => cos (θ / 2)
| 0, 1 => - sin (θ / 2)
| 1, 0 => sin (θ / 2)
| 1, 1 => cos (θ / 2)
| _, _ => 0
end.
(* Shifted by i so x/y_rotation PI = σx/y :
Definition x_rotation (θ : R) : Matrix 2 2 :=
fun x y => match x, y with
| 0, 0 => Ci * cos (θ / 2)
| 0, 1 => sin (θ / 2)
| 1, 0 => sin (θ / 2)
| 1, 1 => Ci * cos (θ / 2)
| _, _ => 0
end.
Definition y_rotation (θ : R) : Matrix 2 2 :=
fun x y => match x, y with
| 0, 0 => Ci * cos (θ / 2)
| 0, 1 => -Ci * sin (θ / 2)
| 1, 0 => Ci * sin (θ / 2)
| 1, 1 => Ci * cos (θ / 2)
| _, _ => 0
end.
*)
Definition Sgate : Matrix 2 2 := phase_shift (PI / 2).
Definition Tgate := phase_shift (PI / 4).
(** Well Formedness of Quantum States and Unitaries **)
Lemma WF_bra0 : WF_Matrix ⟨0∣. Proof. show_wf. Qed.
Lemma WF_bra1 : WF_Matrix ⟨1∣. Proof. show_wf. Qed.
Lemma WF_qubit0 : WF_Matrix ∣0⟩. Proof. show_wf. Qed.
Lemma WF_qubit1 : WF_Matrix ∣1⟩. Proof. show_wf. Qed.
Lemma WF_braket0 : WF_Matrix ∣0⟩⟨0∣. Proof. show_wf. Qed.
Lemma WF_braket1 : WF_Matrix ∣1⟩⟨1∣. Proof. show_wf. Qed.
#[deprecated(note="Use WF_braket0 instead")]
Notation WF_braqubit0 := WF_braket0 (only parsing).
#[deprecated(note="Use WF_braket1 instead")]
Notation WF_braqubit1 := WF_braket1 (only parsing).
Lemma WF_bool_to_ket : forall b, WF_Matrix (bool_to_ket b).
Proof. destruct b; show_wf. Qed.
Lemma WF_bool_to_matrix : forall b, WF_Matrix (bool_to_matrix b).
Proof. destruct b; show_wf. Qed.
Lemma WF_bool_to_matrix' : forall b, WF_Matrix (bool_to_matrix' b).
Proof. destruct b; show_wf. Qed.
Lemma WF_ket : forall n, WF_Matrix (ket n).
Proof. destruct n; simpl; show_wf. Qed.
Lemma WF_bra : forall n, WF_Matrix (bra n).
Proof. destruct n; simpl; show_wf. Qed.
Lemma WF_bools_to_matrix : forall l,
@WF_Matrix (2^(length l)) (2^(length l)) (bools_to_matrix l).
Proof.
induction l; auto with wf_db.
unfold bools_to_matrix in *; simpl.
apply WF_kron; try rewrite map_length; try lia.
apply WF_bool_to_matrix.
apply IHl.
Qed.
Lemma WF_xbasis_plus : WF_Matrix ∣+⟩. Proof. show_wf. Qed.
Lemma WF_xbasis_minus : WF_Matrix ∣-⟩. Proof. show_wf. Qed.
Lemma WF_braplus : WF_Matrix (⟨+∣). Proof. show_wf. Qed.
Lemma WF_braminus : WF_Matrix (⟨-∣). Proof. show_wf. Qed.
Lemma WF_ybasis_plus : WF_Matrix ∣R⟩. Proof. show_wf. Qed.
Lemma WF_ybasis_minus : WF_Matrix ∣L⟩. Proof. show_wf. Qed.
#[export] Hint Resolve WF_bra0 WF_bra1 WF_qubit0 WF_qubit1 WF_braket0 WF_braket1 : wf_db.
#[export] Hint Resolve WF_bool_to_ket WF_bool_to_matrix WF_bool_to_matrix' : wf_db.
#[export] Hint Resolve WF_ket WF_bra WF_bools_to_matrix : wf_db.
#[export] Hint Resolve WF_xbasis_plus WF_xbasis_minus WF_braplus WF_braminus
WF_ybasis_plus WF_ybasis_minus : wf_db.
Lemma WF_EPRpair : WF_Matrix ∣Φ+⟩. Proof. unfold EPRpair. auto with wf_db. Qed.
#[export] Hint Resolve WF_EPRpair : wf_db.
Lemma WF_hadamard : WF_Matrix hadamard. Proof. show_wf. Qed.
Lemma WF_σx : WF_Matrix σx. Proof. show_wf. Qed.
Lemma WF_σy : WF_Matrix σy. Proof. show_wf. Qed.
Lemma WF_σz : WF_Matrix σz. Proof. show_wf. Qed.
Lemma WF_sqrtx : WF_Matrix sqrtx. Proof. show_wf. Qed.
Lemma WF_cnot : WF_Matrix cnot. Proof. show_wf. Qed.
Lemma WF_notc : WF_Matrix notc. Proof. show_wf. Qed.
Lemma WF_swap : WF_Matrix swap. Proof. show_wf. Qed.
Lemma WF_rotation : forall θ ϕ λ, WF_Matrix (rotation θ ϕ λ). Proof. intros. show_wf. Qed.
Lemma WF_x_rotation : forall θ, WF_Matrix (x_rotation θ). Proof. intros. show_wf. Qed.
Lemma WF_y_rotation : forall θ, WF_Matrix (y_rotation θ). Proof. intros. show_wf. Qed.
Lemma WF_phase : forall ϕ, WF_Matrix (phase_shift ϕ). Proof. intros. show_wf. Qed.
Lemma WF_Sgate : WF_Matrix Sgate. Proof. show_wf. Qed.
Lemma WF_Tgate: WF_Matrix Tgate. Proof. show_wf. Qed.
Lemma WF_control : forall (n : nat) (U : Matrix n n),
WF_Matrix U -> WF_Matrix (control U).
Proof.
intros n U WFU.
unfold control, WF_Matrix in *.
intros x y [Hx | Hy];
bdestruct (x <? n); bdestruct (y =? x); bdestruct (n <=? x); bdestruct (n <=? y);
simpl; try reflexivity; try lia.
all: rewrite WFU; [reflexivity|lia].
Qed.
#[export] Hint Resolve WF_hadamard WF_σx WF_σy WF_σz WF_sqrtx WF_cnot WF_notc WF_swap : wf_db.
#[export] Hint Resolve WF_phase WF_Sgate WF_Tgate WF_rotation WF_x_rotation WF_y_rotation : wf_db.
#[export] Hint Extern 2 (WF_Matrix (phase_shift _)) => apply WF_phase : wf_db.
#[export] Hint Extern 2 (WF_Matrix (control _)) => apply WF_control : wf_db.
Lemma sqrtx_sqrtx : sqrtx × sqrtx = σx.
Proof.
prep_matrix_equivalence.
by_cell; lca.
Qed.
Lemma cnot_eq : cnot = control σx.
Proof.
prep_matrix_equivalence.
now by_cell.
Qed.
Lemma x_rotation_pi : x_rotation PI = -Ci .* σx.
Proof.
prep_matrix_equivalence.
unfold x_rotation.
autorewrite with trig_db.
by_cell; lca.
Qed.
Lemma y_rotation_pi : y_rotation PI = -Ci .* σy.
Proof.
prep_matrix_equivalence.
unfold y_rotation.
autorewrite with trig_db.
by_cell; lca.
Qed.
Lemma hadamard_rotation : rotation (PI/2) 0 PI = hadamard.
Proof.
prep_matrix_equivalence.
unfold rotation, hadamard.
replace (PI / 2 / 2)%R with (PI / 4)%R by lra.
autorewrite with trig_db.
(* autorewrite with R_db. *)
rewrite Rplus_0_l, Cexp_PI, Cexp_0, Cmult_1_l.
rewrite <- RtoC_opp, <- !RtoC_mult, <- RtoC_div.
by_cell; lca.
Qed.
Lemma pauli_x_rotation : rotation PI 0 PI = σx.
Proof.
unfold σx, rotation.
prep_matrix_equality.
destruct_m_eq; try reflexivity;
unfold Cexp; apply injective_projections; simpl;
autorewrite with trig_db;
lra.
Qed.
Lemma pauli_y_rotation : rotation PI (PI/2) (PI/2) = σy.
Proof.
unfold σy, rotation.
prep_matrix_equality.
destruct_m_eq; try reflexivity;
unfold Cexp; apply injective_projections; simpl;
autorewrite with trig_db;
lra.
Qed.
Lemma pauli_z_rotation : rotation 0 0 PI = σz.
Proof.
unfold σz, rotation.
prep_matrix_equality.
destruct_m_eq; try reflexivity;
unfold Cexp; apply injective_projections; simpl;
autorewrite with R_db;
autorewrite with trig_db;
lra.
Qed.
Lemma Rx_rotation : forall θ, rotation θ (3*PI/2) (PI/2) = x_rotation θ.
Proof.
intros.
unfold rotation, x_rotation.
prep_matrix_equality.
destruct_m_eq;
autorewrite with C_db Cexp_db; reflexivity.
Qed.
Lemma Ry_rotation : forall θ, rotation θ 0 0 = y_rotation θ.
Proof.
intros.
unfold rotation, y_rotation.
prep_matrix_equality.
destruct_m_eq;
autorewrite with C_db Cexp_db; try reflexivity.
Qed.
Lemma phase_shift_rotation : forall θ, rotation 0 0 θ = phase_shift θ.
Proof.
intros.
unfold phase_shift, rotation.
prep_matrix_equality.
destruct_m_eq; try reflexivity;
unfold Cexp; apply injective_projections; simpl;
autorewrite with R_db;
autorewrite with trig_db;
lra.
Qed.
Lemma I_rotation : rotation 0 0 0 = I 2.
Proof.
prep_matrix_equivalence.
unfold rotation.
rewrite Rdiv_0_l, Rplus_0_l, Cexp_0.
autorewrite with trig_db.
by_cell; lca.
Qed.
(* Lemmas *)
Lemma sqrtx_decompose: sqrtx = hadamard × phase_shift (PI/2) × hadamard.
Proof.
prep_matrix_equivalence.
unfold phase_shift, hadamard, Cdiv.
Csimpl.
rewrite Cexp_PI2.
do 2 reduce_matrices.
group_radicals.
by_cell; lca.
Qed.
(* Additional tactics for ∣0⟩, ∣1⟩, cnot and σx. *)
Lemma Mmult00 : ⟨0∣ × ∣0⟩ = I 1. Proof. solve_matrix_fast. Qed.
Lemma Mmult01 : ⟨0∣ × ∣1⟩ = Zero. Proof. solve_matrix_fast. Qed.
Lemma Mmult10 : ⟨1∣ × ∣0⟩ = Zero. Proof. solve_matrix_fast. Qed.
Lemma Mmult11 : ⟨1∣ × ∣1⟩ = I 1. Proof. solve_matrix_fast. Qed.
Lemma MmultX1 : σx × ∣1⟩ = ∣0⟩. Proof. solve_matrix_fast. Qed.
Lemma Mmult1X : ⟨1∣ × σx = ⟨0∣. Proof. solve_matrix_fast. Qed.
Lemma MmultX0 : σx × ∣0⟩ = ∣1⟩. Proof. solve_matrix_fast. Qed.
Lemma Mmult0X : ⟨0∣ × σx = ⟨1∣. Proof. solve_matrix_fast. Qed.
Lemma MmultXX : σx × σx = I 2. Proof. solve_matrix_fast. Qed.
Lemma MmultYY : σy × σy = I 2. Proof. solve_matrix_fast. Qed.
Lemma MmultZZ : σz × σz = I 2. Proof. solve_matrix_fast. Qed.
Lemma MmultHH : hadamard × hadamard = I 2. Proof.
solve_matrix_fast_with idtac (cbn; C_field; lca).
Qed.
Lemma MmultZH : σz × hadamard = hadamard × σx. Proof. lma'. Qed.
Lemma MmultXH : σx × hadamard = hadamard × σz. Proof. lma'. Qed.
Lemma MmultHZ : hadamard × σz = σx × hadamard. Proof. now rewrite MmultXH. Qed.
Lemma MmultHX : hadamard × σx = σz × hadamard. Proof. now rewrite MmultZH. Qed.
Lemma MmultHY : hadamard × σy = (- C1) .* (σy × hadamard). Proof. lma'. Qed.
Lemma MmultYH : σy × hadamard = (- C1) .* (hadamard × σy).
Proof.
rewrite MmultHY, Mscale_assoc.
replace (- C1 * - C1)%C with (C1) by lca.
now rewrite Mscale_1_l.
Qed.
Lemma Mmult_phase_11 a : ∣1⟩⟨1∣ × phase_shift a = phase_shift a × ∣1⟩⟨1∣.
Proof. lma'. Qed.
Lemma Mmult_phase_00 a : ∣0⟩⟨0∣ × phase_shift a = phase_shift a × ∣0⟩⟨0∣.
Proof. lma'. Qed.
Lemma Mmult_phase_X_phase_X a a' :
σx × phase_shift a × σx × phase_shift a' =
phase_shift a' × σx × phase_shift a × σx.
Proof. lma'. Qed.
Lemma Mplus01 : ∣0⟩⟨0∣ .+ ∣1⟩⟨1∣ = I 2. Proof. solve_matrix_fast. Qed.
Lemma Mplus10 : ∣1⟩⟨1∣ .+ ∣0⟩⟨0∣ = I 2. Proof. solve_matrix_fast. Qed.
Lemma EPRpair_creation : cnot × (hadamard ⊗ I 2) × ∣0,0⟩ = EPRpair.
Proof. solve_matrix_fast_with (rewrite kron_I_r) lca. Qed.
Lemma σx_on_right0 : forall (q : Vector 2), (q × ⟨0∣) × σx = q × ⟨1∣.
Proof. intros. rewrite Mmult_assoc, Mmult0X. reflexivity. Qed.
Lemma σx_on_right1 : forall (q : Vector 2), (q × ⟨1∣) × σx = q × ⟨0∣.
Proof. intros. rewrite Mmult_assoc, Mmult1X. reflexivity. Qed.
Lemma σx_on_left0 : forall (q : Matrix 1 2), σx × (∣0⟩ × q) = ∣1⟩ × q.
Proof. intros. rewrite <- Mmult_assoc, MmultX0. reflexivity. Qed.
Lemma σx_on_left1 : forall (q : Matrix 1 2), σx × (∣1⟩ × q) = ∣0⟩ × q.
Proof. intros. rewrite <- Mmult_assoc, MmultX1. reflexivity. Qed.
Lemma cancel00 : forall (q1 : Matrix 2 1) (q2 : Matrix 1 2),
WF_Matrix q2 ->
(q1 × ⟨0∣) × (∣0⟩ × q2) = q1 × q2.
Proof.
intros.
rewrite Mmult_assoc.
rewrite <- (Mmult_assoc ⟨0∣).
rewrite Mmult00.
Msimpl; reflexivity.
Qed.
Lemma cancel01 : forall (q1 : Matrix 2 1) (q2 : Matrix 1 2),
(q1 × ⟨0∣) × (∣1⟩ × q2) = Zero.
Proof.
intros.
rewrite Mmult_assoc.
rewrite <- (Mmult_assoc ⟨0∣).
rewrite Mmult01.
Msimpl_light; reflexivity.
Qed.
Lemma cancel10 : forall (q1 : Matrix 2 1) (q2 : Matrix 1 2),
(q1 × ⟨1∣) × (∣0⟩ × q2) = Zero.
Proof.
intros.
rewrite Mmult_assoc.
rewrite <- (Mmult_assoc ⟨1∣).
rewrite Mmult10.
Msimpl_light; reflexivity.
Qed.
Lemma cancel11 : forall (q1 : Matrix 2 1) (q2 : Matrix 1 2),
WF_Matrix q2 ->
(q1 × ⟨1∣) × (∣1⟩ × q2) = q1 × q2.
Proof.
intros.
rewrite Mmult_assoc.
rewrite <- (Mmult_assoc ⟨1∣).
rewrite Mmult11.
Msimpl; reflexivity.
Qed.
#[global] Hint Rewrite Mmult00 Mmult01 Mmult10 Mmult11 Mmult0X MmultX0 Mmult1X MmultX1 : Q_db.
#[global] Hint Rewrite MmultXX MmultYY MmultZZ MmultHH Mplus01 Mplus10 EPRpair_creation : Q_db.
#[global] Hint Rewrite σx_on_right0 σx_on_right1 σx_on_left0 σx_on_left1 : Q_db.
#[global] Hint Rewrite cancel00 cancel01 cancel10 cancel11 using (auto with wf_db) : Q_db.
(* TODO: move these swap lemmas to Permutation.v? *)
(* The input k is really k+1, to appease to Coq termination gods *)
(* NOTE: Check that the offsets are right *)
(* Requires: i + 1 < n *)
Fixpoint swap_to_0_aux (n i : nat) {struct i} : Matrix (2^n) (2^n) :=
match i with
| O => swap ⊗ I (2^(n-2))
| S i' => (I (2^i) ⊗ swap ⊗ I (2^(n-i-2))) × (* swap i-1 with i *)
swap_to_0_aux n i' ×
(I (2^i) ⊗ swap ⊗ I (2^(n-i-2))) (* swap i-1 with 0 *)
end.
(* Requires: i < n *)
Definition swap_to_0 (n i : nat) : Matrix (2^n) (2^n) :=
match i with
| O => I (2^n)
| S i' => swap_to_0_aux n i'
end.
(* Swapping qubits i and j in an n-qubit system, where i < j *)
(* Requires i < j, j < n *)
Fixpoint swap_two_aux (n i j : nat) : Matrix (2^n) (2^n) :=
match i with
| O => swap_to_0 n j
| S i' => I 2 ⊗ swap_two_aux (n-1) (i') (j-1)
end.
(* Swapping qubits i and j in an n-qubit system *)
(* Requires i < n, j < n *)
Definition swap_two (n i j : nat) : Matrix (2^n) (2^n) :=
if i =? j then I (2^n)
else if i <? j then swap_two_aux n i j
else swap_two_aux n j i.
(* Simpler version of swap_to_0 that shifts other elements *)
(* Requires: i+1 < n *)
Fixpoint move_to_0_aux (n i : nat) {struct i}: Matrix (2^n) (2^n) :=
match i with
| O => swap ⊗ I (2^(n-2))
| S i' => (move_to_0_aux n i') × (I (2^i) ⊗ swap ⊗ I (2^(n-i-2)))
end.
(* Requires: i < n *)
Definition move_to_0 (n i : nat) : Matrix (2^n) (2^n) :=
match i with
| O => I (2^n)
| S i' => move_to_0_aux n i'
end.
(* Always moves up in the matrix from i to k *)
(* Requires: k < i < n *)
Fixpoint move_to (n i k : nat) : Matrix (2^n) (2^n) :=
match k with
| O => move_to_0 n i
| S k' => I 2 ⊗ move_to (n-1) (i-1) (k')
end.
(*
Eval compute in ((swap_two 1 0 1) 0 0)%nat.
Eval compute in (print_matrix (swap_two 1 0 2)).
*)
Lemma swap_eq_kron_comm : swap = kron_comm 2 2.
Proof. solve_matrix_fast_with idtac reflexivity. Qed.
Lemma swap_swap : swap × swap = I (2*2).
Proof. rewrite swap_eq_kron_comm. apply kron_comm_mul_inv. Qed.
Lemma swap_swap_r : forall (A : Matrix (2*2) (2*2)),
WF_Matrix A ->
A × swap × swap = A.
Proof.
intros.
rewrite Mmult_assoc.
rewrite swap_swap.
now apply Mmult_1_r.
Qed.
#[global] Hint Rewrite swap_swap swap_swap_r using (auto 100 with wf_db): Q_db.
Lemma braplus_transpose_ketplus :
⟨+∣⊤ = ∣+⟩.
Proof. solve_matrix_fast. Qed.
Lemma braminus_transpose_ketminus :
⟨-∣⊤ = ∣-⟩.
Proof. solve_matrix_fast. Qed.
Lemma Mmultplus0 :
⟨+∣ × ∣0⟩ = / (√2)%R .* I 1.
Proof.
unfold braplus.
rewrite Mscale_mult_dist_l.
rewrite Mmult_plus_distr_r.
rewrite Mmult00.
rewrite Mmult10.
lma.
Qed.
Lemma Mmult0plus :
⟨0∣ × ∣+⟩ = / (√2)%R .* I 1.
Proof.
unfold xbasis_plus.
rewrite Mscale_mult_dist_r.
rewrite Mmult_plus_distr_l.
rewrite Mmult00.
rewrite Mmult01.
lma.
Qed.
Lemma Mmultplus1 :
⟨+∣ × ∣1⟩ = / (√2)%R .* I 1.
Proof.
unfold braplus.
rewrite Mscale_mult_dist_l.
rewrite Mmult_plus_distr_r.
rewrite Mmult01.
rewrite Mmult11.
lma.
Qed.
Lemma Mmult1plus :
⟨1∣ × ∣+⟩ = / (√2)%R .* I 1.
Proof.
unfold xbasis_plus.
rewrite Mscale_mult_dist_r.
rewrite Mmult_plus_distr_l.
rewrite Mmult10.
rewrite Mmult11.
lma.
Qed.
Lemma Mmultminus0 :
⟨-∣ × ∣0⟩ = / (√2)%R .* I 1.
Proof.
unfold braminus.
rewrite Mscale_mult_dist_l.
rewrite Mmult_plus_distr_r.
rewrite Mmult00.
rewrite Mscale_mult_dist_l.
rewrite Mmult10.
lma.
Qed.
Lemma Mmult0minus :
⟨0∣ × ∣-⟩ = / (√2)%R .* I 1.
Proof.
unfold xbasis_minus.
rewrite Mscale_mult_dist_r.
rewrite Mmult_plus_distr_l.
rewrite Mmult00.
rewrite Mscale_mult_dist_r.
rewrite Mmult01.
lma.
Qed.
Lemma Mmultminus1 :
⟨-∣ × ∣1⟩ = - / (√2)%R .* I 1.
Proof.
unfold braminus.
rewrite Mscale_mult_dist_l.
rewrite Mmult_plus_distr_r.
rewrite Mmult01.
rewrite Mscale_mult_dist_l.
rewrite Mmult11.
lma.
Qed.
Lemma Mmult1minus :
⟨1∣ × ∣-⟩ = - / (√2)%R .* I 1.
Proof.
unfold xbasis_minus.
rewrite Mscale_mult_dist_r.
rewrite Mmult_plus_distr_l.
rewrite Mmult10.
rewrite Mscale_mult_dist_r.
rewrite Mmult11.
lma.
Qed.
Lemma Mmultminusminus :
⟨-∣ × ∣-⟩ = I 1.
Proof.
prep_matrix_equivalence.
unfold braminus.
unfold xbasis_minus.
distribute_scale.
group_radicals.
by_cell; lca.
Qed.
Lemma Mmultplusminus :
⟨+∣ × ∣-⟩ = Zero.
Proof.
prep_matrix_equivalence.
unfold braplus, xbasis_minus.
distribute_scale.
group_radicals.
by_cell; lca.
Qed.
Lemma Mmultminusplus :
⟨-∣ × ∣+⟩ = Zero.
Proof.
prep_matrix_equivalence.
unfold xbasis_plus, braminus.
distribute_scale.
group_radicals.
by_cell; lca.
Qed.
Lemma Mmultplusplus :
⟨+∣ × ∣+⟩ = I 1.
Proof.
prep_matrix_equivalence.
unfold xbasis_plus, braplus.
distribute_scale.
group_radicals.
by_cell; lca.
Qed.
#[export] Hint Rewrite
Mmult00 Mmult01 Mmult10 Mmult11
Mmultplus0 Mmultplus1 Mmultminus0 Mmultminus1
Mmult0plus Mmult0minus Mmult1plus Mmult1minus
Mmultplusplus Mmultplusminus Mmultminusplus Mmultminusminus
: ketbra_mult_db.
Lemma bra0transpose :
⟨0∣⊤ = ∣0⟩.
Proof. solve_matrix_fast. Qed.
Lemma bra1transpose :
⟨1∣⊤ = ∣1⟩.
Proof. solve_matrix_fast. Qed.
Lemma ket0transpose :
∣0⟩⊤ = ⟨0∣.
Proof. solve_matrix_fast. Qed.
Lemma ket1transpose :
∣1⟩⊤ = ⟨1∣.
Proof. solve_matrix_fast. Qed.
Lemma Mplus_plus_minus : ∣+⟩ .+ ∣-⟩ = (√2)%R .* ∣0⟩.
Proof.
solve_matrix_fast_with
(unfold xbasis_plus, xbasis_minus; autounfold with U_db)
(cbn; try lca; Csimpl; C_field; lca).
Qed.
Lemma Mplus_plus_minus_opp : ∣+⟩ .+ -1 .* ∣-⟩ = (√2)%R .* ∣1⟩.
Proof.
solve_matrix_fast_with
(unfold xbasis_plus, xbasis_minus; autounfold with U_db)
(cbn; try lca; Csimpl; C_field; lca).
Qed.
Lemma Mplus_minus_plus : ∣-⟩ .+ ∣+⟩ = (√2)%R .* ∣0⟩.
Proof.
solve_matrix_fast_with
(unfold xbasis_plus, xbasis_minus; autounfold with U_db)
(cbn; try lca; Csimpl; C_field; lca).
Qed.
Lemma Mplus_minus_opp_plus : -1 .* ∣-⟩ .+ ∣+⟩ = (√2)%R .* ∣1⟩.
Proof.
solve_matrix_fast_with
(unfold xbasis_plus, xbasis_minus; autounfold with U_db)
(cbn; try lca; Csimpl; C_field; lca).
Qed.
Lemma Mplus_0_1 : ∣0⟩ .+ ∣1⟩ = (√2)%R .* ∣+⟩.
Proof.
solve_matrix_fast_with
(unfold xbasis_plus, xbasis_minus; autounfold with U_db)
(cbn; try lca; Csimpl; C_field; lca).
Qed.
Lemma Mplus_0_1_opp : ∣0⟩ .+ -1 .* ∣1⟩ = (√2)%R .* ∣-⟩.
Proof.
solve_matrix_fast_with
(unfold xbasis_plus, xbasis_minus; autounfold with U_db)
(cbn; try lca; Csimpl; C_field; lca).
Qed.
Lemma Mplus_1_0 : ∣1⟩ .+ ∣0⟩ = (√2)%R .* ∣+⟩.
Proof.
solve_matrix_fast_with
(unfold xbasis_plus, xbasis_minus; autounfold with U_db)
(cbn; try lca; Csimpl; C_field; lca).
Qed.
Lemma Mplus_1_opp_0 : -1 .* ∣1⟩ .+ ∣0⟩ = (√2)%R .* ∣-⟩.
Proof.
solve_matrix_fast_with
(unfold xbasis_plus, xbasis_minus; autounfold with U_db)
(cbn; try lca; Csimpl; C_field; lca).
Qed.
Lemma σz_decomposition : σz = ∣0⟩⟨0∣ .+ -1 .* ∣1⟩⟨1∣.
Proof. solve_matrix_fast. Qed.
(* It may be possible to remove the WF_Matrix B hypothesis *)
Lemma direct_sum_decomp : forall (m n o p : nat) (A B : Matrix m n),
WF_Matrix A -> WF_Matrix B ->
A .⊕ B = ∣0⟩⟨0∣ ⊗ A .+ ∣1⟩⟨1∣ ⊗ B.
Proof.
intros m n o p A B HA HB.
prep_matrix_equivalence.
unfold direct_sum, kron, Mplus.
intros i j Hi Hj.
bdestruct (i <? m); bdestruct (j <? n).
- rewrite ?Nat.div_small, ?Nat.mod_small by easy; lca.
- simpl.
rewrite Nat.div_small, HA by lia.
replace (j/n)%nat with 1%nat by
(symmetry; rewrite div_eq_iff; lia).
lca.
- simpl.
rewrite (Nat.div_small j n), HA by lia.
replace (i/m)%nat with 1%nat by
(symmetry; rewrite div_eq_iff; lia).
lca.
- simpl.
replace (i/m)%nat with 1%nat by
(symmetry; rewrite div_eq_iff; lia).
replace (j/n)%nat with 1%nat by
(symmetry; rewrite div_eq_iff; lia).
rewrite 2!Modulus.mod_n_to_2n by lia.
lca.
Qed.
Lemma cnot_decomposition : ∣1⟩⟨1∣ ⊗ σx .+ ∣0⟩⟨0∣ ⊗ I 2 = cnot.
Proof. solve_matrix_fast. Qed.
Lemma notc_decomposition : σx ⊗ ∣1⟩⟨1∣ .+ I 2 ⊗ ∣0⟩⟨0∣ = notc.
Proof. solve_matrix_fast. Qed.
(***************************)
(** Unitaries are unitary **)
(***************************)
(* For this section, we could just convert all single-qubit unitaries into their
rotation form and use rotation_unitary. *)
Definition WF_Unitary {n: nat} (U : Matrix n n): Prop :=
WF_Matrix U /\ U † × U = I n.
#[export] Hint Unfold WF_Unitary : U_db.
(* More precise *)
(* Definition unitary_matrix' {n: nat} (A : Matrix n n): Prop := Minv A A†. *)
Lemma H_unitary : WF_Unitary hadamard.
Proof.
split; [auto_wf|].
solve_matrix_fast_with (autounfold with U_db)
(now autorewrite with C_db).
Qed.
Lemma σx_unitary : WF_Unitary σx.
Proof.
split; [auto_wf|].
solve_matrix_fast.
Qed.
Lemma σy_unitary : WF_Unitary σy.
Proof.
split; [auto_wf|].
solve_matrix_fast.
Qed.
Lemma σz_unitary : WF_Unitary σz.
Proof.
split; [auto_wf|].
solve_matrix_fast.
Qed.
Lemma phase_unitary : forall ϕ, @WF_Unitary 2 (phase_shift ϕ).
Proof.
intros ϕ.
split; [auto_wf|].
lma'.
unfold adjoint.
cbn.
autorewrite with C_db.