-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quantum.v
1771 lines (1536 loc) · 49.7 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
Require Import Psatz.
Require Import Reals.
Require Export Matrix.
(* 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.
(****************)
(** * 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.
(* Alternative definitions:
Definition pauli_x : Matrix 2 2 := fun x y => if x + y =? 1 then 1 else 0.
Definition pauli_y : Matrix 2 2 := fun x y => if x + y =? 1 then (-1) ^ x * Ci else 0.
Definition pauli_z : Matrix 2 2 := fun x y => if (x =? y) && (x <? 2)
then (-1) ^ x * Ci else 0.
*)
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 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.
Lemma cnot_eq : cnot = control σx.
Proof.
unfold cnot, control, σx.
solve_matrix.
Qed.
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.
Hint Unfold qubit0 qubit1 hadamard σx σy σz control cnot swap bra ket : U_db.
(** ** Rotation Matrices *)
(* Standard(?) definition, but it makes equivalence-checking a little annoying
because of 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.
(* z_rotation lemmas are further down *)
Definition phase_shift (ϕ : R) : Matrix 2 2 :=
fun x y => match x, y with
| 0, 0 => C1
| 1, 1 => Cexp ϕ
| _, _ => C0
end.
(* Notation z_rotation := phase_shift. *)
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.
*)
Lemma x_rotation_pi : x_rotation PI = -Ci .* σx.
Proof.
unfold σx, x_rotation, scale.
prep_matrix_equality.
destruct_m_eq;
autorewrite with trig_db C_db;
reflexivity.
Qed.
Lemma y_rotation_pi : y_rotation PI = -Ci .* σy.
Proof.
unfold σy, y_rotation, scale.
prep_matrix_equality.
destruct_m_eq;
autorewrite with trig_db C_db;
try reflexivity.
Qed.
Lemma hadamard_rotation : rotation (PI/2) 0 PI = hadamard.
Proof.
unfold hadamard, rotation.
prep_matrix_equality.
destruct_m_eq; try reflexivity;
unfold Cexp; apply injective_projections; simpl;
autorewrite with R_db;
autorewrite with trig_db;
autorewrite with R_db;
try reflexivity.
all: rewrite Rmult_assoc;
replace (/2 * /2)%R with (/4)%R by lra;
repeat rewrite <- Rdiv_unfold;
autorewrite with trig_db;
rewrite sqrt2_div2;
lra.
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.
unfold I, rotation.
prep_matrix_equality.
destruct_m_eq; try reflexivity;
unfold Cexp; apply injective_projections; simpl;
autorewrite with R_db;
autorewrite with trig_db;
autorewrite with R_db;
try reflexivity.
bdestruct (x =? y); bdestruct (S (S x) <? 2); simpl; try reflexivity; lia.
destruct (x =? y); destruct (S (S x) <? 2); reflexivity.
Qed.
(* Lemmas *)
(* Additional tactics for ∣0⟩, ∣1⟩, cnot and σx. *)
Lemma Mmult00 : ⟨0∣ × ∣0⟩ = I 1. Proof. solve_matrix. Qed.
Lemma Mmult01 : ⟨0∣ × ∣1⟩ = Zero. Proof. solve_matrix. Qed.
Lemma Mmult10 : ⟨1∣ × ∣0⟩ = Zero. Proof. solve_matrix. Qed.
Lemma Mmult11 : ⟨1∣ × ∣1⟩ = I 1. Proof. solve_matrix. Qed.
Lemma MmultX1 : σx × ∣1⟩ = ∣0⟩. Proof. solve_matrix. Qed.
Lemma Mmult1X : ⟨1∣ × σx = ⟨0∣. Proof. solve_matrix. Qed.
Lemma MmultX0 : σx × ∣0⟩ = ∣1⟩. Proof. solve_matrix. Qed.
Lemma Mmult0X : ⟨0∣ × σx = ⟨1∣. Proof. solve_matrix. Qed.
Lemma MmultXX : σx × σx = I 2. Proof. solve_matrix. Qed.
Lemma MmultYY : σy × σy = I 2. Proof. solve_matrix. Qed.
Lemma MmultZZ : σz × σz = I 2. Proof. solve_matrix. Qed.
Lemma MmultHH : hadamard × hadamard = I 2. Proof. solve_matrix. Qed.
Lemma Mplus01 : ∣0⟩⟨0∣ .+ ∣1⟩⟨1∣ = I 2. Proof. solve_matrix. Qed.
Lemma Mplus10 : ∣1⟩⟨1∣ .+ ∣0⟩⟨0∣ = I 2. Proof. solve_matrix. 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.
Hint Rewrite Mmult00 Mmult01 Mmult10 Mmult11 Mmult0X MmultX0 Mmult1X MmultX1 : Q_db.
Hint Rewrite MmultXX MmultYY MmultZZ MmultHH Mplus01 Mplus10 : Q_db.
Hint Rewrite σx_on_right0 σx_on_right1 σx_on_left0 σx_on_left1 : Q_db.
Hint Rewrite cancel00 cancel01 cancel10 cancel11 using (auto with wf_db) : Q_db.
Lemma swap_swap : swap × swap = I (2*2). Proof. solve_matrix. 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.
Msimpl.
reflexivity.
Qed.
Hint Rewrite swap_swap swap_swap_r using (auto 100 with wf_db): Q_db.
(* 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)).
*)
(** 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_braqubit0 : WF_Matrix ∣0⟩⟨0∣. Proof. show_wf. Qed.
Lemma WF_braqubit1 : WF_Matrix ∣1⟩⟨1∣. Proof. show_wf. Qed.
Lemma WF_bra : forall (x : nat), WF_Matrix (bra x).
Proof. intros x. unfold bra. destruct (x =? 0). show_wf. show_wf.
Qed.
Lemma WF_ket : forall (x : nat), WF_Matrix (ket x).
Proof. intros x. unfold ket. destruct (x =? 0). show_wf. show_wf.
Qed.
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_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.
Hint Resolve WF_bra0 WF_bra1 WF_qubit0 WF_qubit1 WF_bra WF_ket WF_braqubit0 WF_braqubit1 : wf_db.
Hint Resolve WF_bool_to_ket WF_bool_to_matrix WF_bool_to_matrix' : wf_db.
Hint Resolve WF_bools_to_matrix : 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_cnot : WF_Matrix cnot. 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_phase : forall ϕ, WF_Matrix (phase_shift ϕ). Proof. intros. 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.
Hint Resolve WF_hadamard WF_σx WF_σy WF_σz WF_cnot WF_swap WF_phase : wf_db.
Hint Resolve WF_rotation : wf_db.
Hint Extern 2 (WF_Matrix (phase_shift _)) => apply WF_phase : wf_db.
Hint Extern 2 (WF_Matrix (control _)) => apply WF_control : wf_db.
(***************************)
(** 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.
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.
show_wf.
unfold Mmult, I.
prep_matrix_equality.
autounfold with U_db.
destruct x as [| [|x]]; destruct y as [|[|y]]; simpl; autorewrite with C_db;
try reflexivity.
replace ((S (S x) <? 2)) with false by reflexivity.
rewrite andb_false_r.
reflexivity.
Qed.
Lemma σx_unitary : WF_Unitary σx.
Proof.
split.
show_wf.
unfold Mmult, I.
prep_matrix_equality.
destruct x as [| [|x]]; destruct y as [|[|y]]; try lca.
simpl.
replace ((S (S x) <? 2)) with false by reflexivity.
rewrite andb_false_r.
lca.
Qed.
Lemma σy_unitary : WF_Unitary σy.
Proof.
split.
show_wf.
unfold Mmult, I.
prep_matrix_equality.
destruct x as [| [|x]]; destruct y as [|[|y]]; try lca.
simpl.
replace ((S (S x) <? 2)) with false by reflexivity.
rewrite andb_false_r.
lca.
Qed.
Lemma σz_unitary : WF_Unitary σz.
Proof.
split.
show_wf.
unfold Mmult, I.
prep_matrix_equality.
destruct x as [| [|x]]; destruct y as [|[|y]]; try lca.
simpl.
replace ((S (S x) <? 2)) with false by reflexivity.
rewrite andb_false_r.
lca.
Qed.
Lemma phase_unitary : forall ϕ, @WF_Unitary 2 (phase_shift ϕ).
Proof.
intros ϕ.
split; [show_wf|].
unfold Mmult, I, phase_shift, adjoint, Cexp.
prep_matrix_equality.
destruct x as [| [|x]]; destruct y as [|[|y]]; try lca.
- simpl.
Csimpl.
unfold Cconj, Cmult.
simpl.
unfold Rminus.
rewrite Ropp_mult_distr_l.
rewrite Ropp_involutive.
replace (cos ϕ * cos ϕ)%R with ((cos ϕ)²) by easy.
replace (sin ϕ * sin ϕ)%R with ((sin ϕ)²) by easy.
rewrite Rplus_comm.
rewrite sin2_cos2.
lca.
- simpl. Csimpl.
replace ((S (S x) <? 2)) with false by reflexivity.
rewrite andb_false_r.
lca.
Qed.
Lemma rotation_unitary : forall θ ϕ λ, @WF_Unitary 2 (rotation θ ϕ λ).
Proof.
intros.
split; [show_wf|].
unfold Mmult, I, rotation, adjoint, Cexp.
prep_matrix_equality.
destruct_m_eq; try lca;
unfold Cexp, Cconj;
apply injective_projections; simpl;
autorewrite with R_db;
try lra.
(* some general rewriting *)
all: (repeat rewrite <- Rmult_assoc;
repeat rewrite Ropp_mult_distr_l;
repeat rewrite <- Rmult_plus_distr_r;
repeat rewrite Rmult_assoc;
repeat rewrite (Rmult_comm (cos (θ * / 2)));
repeat rewrite (Rmult_comm (sin (θ * / 2)));
repeat rewrite <- Rmult_assoc;
repeat rewrite <- Rmult_plus_distr_r).
(* all the cases are about the same; just setting up applications of
cos_minus/sin_minus and simplifying *)
all: repeat rewrite <- cos_minus.
3: (rewrite (Rmult_comm (cos ϕ));
rewrite <- (Ropp_mult_distr_l (sin ϕ));
rewrite (Rmult_comm (sin ϕ));
rewrite <- Rminus_unfold).
5: (rewrite (Rmult_comm _ (cos ϕ));
rewrite (Rmult_comm _ (sin ϕ));
rewrite <- Ropp_mult_distr_r;
rewrite <- Rminus_unfold).
all: try rewrite <- sin_minus.
all: autorewrite with R_db.
all: repeat rewrite Rplus_opp_r.
all: try (rewrite Ropp_plus_distr;
repeat rewrite <- Rplus_assoc;
rewrite Rplus_opp_r).
all: try (rewrite (Rplus_comm ϕ λ);
rewrite Rplus_assoc;
rewrite Rplus_opp_r).
all: (autorewrite with R_db;
autorewrite with trig_db;
autorewrite with R_db).
all: try lra.
all: try (replace (cos (θ * / 2) * cos (θ * / 2))%R with ((cos (θ * / 2))²) by easy;
replace (sin (θ * / 2) * sin (θ * / 2))%R with ((sin (θ * / 2))²) by easy).
1: rewrite Rplus_comm.
all: try (rewrite sin2_cos2; reflexivity).
(* two weird left-over cases *)
all: (destruct ((x =? y) && (S (S x) <? 2)) eqn:E;
try reflexivity).
apply andb_prop in E as [_ E].
apply Nat.ltb_lt in E; lia.
Qed.
Lemma x_rotation_unitary : forall θ, @WF_Unitary 2 (x_rotation θ).
Proof. intros. rewrite <- Rx_rotation. apply rotation_unitary. Qed.
Lemma y_rotation_unitary : forall θ, @WF_Unitary 2 (y_rotation θ).
Proof. intros. rewrite <- Ry_rotation. apply rotation_unitary. Qed.
(* caused errors so commenting out for now:
Lemma control_unitary : forall n (A : Matrix n n),
WF_Unitary A -> WF_Unitary (control A).
Proof.
intros n A H.
destruct H as [WF U].
split; auto with wf_db.
unfold control, adjoint, Mmult, I.
prep_matrix_equality.
simpl.
bdestruct (x =? y).
- subst; simpl.
rewrite Csum_sum.
bdestruct (y <? n + (n + 0)).
+ bdestruct (n <=? y).
* rewrite Csum_0_bounded. Csimpl.
rewrite (Csum_eq _ (fun x => A x (y - n)%nat ^* * A x (y - n)%nat)).
++ unfold control, adjoint, Mmult, I in U.
rewrite Nat.add_0_r.
eapply (equal_f) in U.
eapply (equal_f) in U.
rewrite U.
rewrite Nat.eqb_refl. simpl.
bdestruct (y - n <? n).
easy.
++ apply functional_extensionality. intros x.
bdestruct (n + x <? n).
bdestruct (n <=? n + x).
rewrite minus_plus.
easy.
++ intros x L.
bdestruct (y =? x).
rewrite andb_false_r.
bdestructΩ (n <=? x).
simpl. lca.
* rewrite (Csum_unique 1).
rewrite Csum_0_bounded.
++ lca.
++ intros.
rewrite andb_false_r.
bdestruct (n + x <? n).
simpl.
lca.
++ exists y.
repeat rewrite andb_false_r.
split. easy.
split.
rewrite Nat.eqb_refl.
bdestructΩ (y <? n).
simpl. lca.
intros x Ne.
bdestruct (y =? x ).
repeat rewrite andb_false_r.
lca.
+ rewrite 2 Csum_0_bounded; [lca| |].
* intros x L.
rewrite WF by (right; lia).
bdestructΩ (n + x <? n).
bdestructΩ (n <=? n + x).
bdestructΩ (n <=? y).
lca.
* intros x L.
bdestructΩ (y =? x).
rewrite andb_false_r.
bdestructΩ (n <=? x).
simpl. lca.
- simpl.
rewrite Csum_sum.
bdestructΩ (y <? n + (n + 0)).
+ bdestructΩ (n <=? y).
* rewrite Csum_0_bounded. Csimpl.
bdestructΩ (n <=? x).
rewrite (Csum_eq _ (fun z => A z (x - n)%nat ^* * A z (y - n)%nat)).
++ unfold control, adjoint, Mmult, I in U.
rewrite Nat.add_0_r.
eapply (equal_f) in U.
eapply (equal_f) in U.
rewrite U.
bdestructΩ (x - n =? y - n).
simpl.
easy.
++ apply functional_extensionality. intros z.
bdestructΩ (n + z <? n).
bdestructΩ (n <=? n + z).
rewrite minus_plus.
easy.
++ rewrite Csum_0. easy.
intros z.
bdestructΩ (n + z <? n).
rewrite andb_false_r.
Csimpl. easy.
++ intros z L.
bdestructΩ (z <? n).
bdestructΩ (n <=? z).
bdestructΩ (x =? z); bdestructΩ (y =? z); try lca.
* bdestructΩ (n <=? x).
++ rewrite Csum_0_bounded.
rewrite Csum_0_bounded. lca.
** intros z L.
bdestructΩ (n + z <? n).
rewrite andb_false_r.
lca.
** intros z L.
bdestructΩ (z <? n).
rewrite andb_false_r.
bdestructΩ (x =? z); bdestructΩ (y =? z); try lca.
bdestructΩ (n <=? z).
lca.
++ rewrite 2 Csum_0_bounded; [lca| |].
** intros z L.
rewrite andb_false_r.
bdestructΩ (x =? n + z); bdestructΩ (y =? n + z); rewrite andb_false_r; lca.
** intros z L.
rewrite andb_false_r.
bdestructΩ (x =? z); bdestructΩ (y =? z); rewrite andb_false_r; lca.
+ rewrite 2 Csum_0_bounded; [lca| |].
* intros z L.
bdestructΩ (n + z <? n).
bdestructΩ (n <=? n + z).
bdestructΩ (n <=? y).
rewrite (WF _ (y-n)%nat) by (right; lia).
lca.
* intros z L.
bdestructΩ (y =? z).
rewrite andb_false_r.
rewrite (WF _ (y-n)%nat) by (right; lia).
destruct ((n <=? z) && (n <=? y)); lca.
Qed. *)
Lemma transpose_unitary : forall n (A : Matrix n n), WF_Unitary A -> WF_Unitary (A†).
Proof.
intros.
simpl.
split.
+ destruct H; auto with wf_db.
+ unfold WF_Unitary in *.
rewrite adjoint_involutive.
destruct H as [H H0].
apply Minv_left in H0 as [_ S]; auto with wf_db.
Qed.
Lemma cnot_unitary : WF_Unitary cnot.
Proof.
split.
apply WF_cnot.
unfold Mmult, I.
prep_matrix_equality.
do 4 (try destruct x; try destruct y; try lca).
replace ((S (S (S (S x))) <? 4)) with (false) by reflexivity.
rewrite andb_false_r.
lca.
Qed.
Lemma id_unitary : forall n, WF_Unitary (I n).
Proof.
split.
apply WF_I.
unfold WF_Unitary.
rewrite id_adjoint_eq.
apply Mmult_1_l.
apply WF_I.
Qed.
Lemma swap_unitary : WF_Unitary swap.
Proof.
split.
apply WF_swap.
unfold WF_Unitary, Mmult, I.
prep_matrix_equality.
do 4 (try destruct x; try destruct y; try lca).
replace ((S (S (S (S x))) <? 4)) with (false) by reflexivity.
rewrite andb_false_r.
lca.
Qed.
Lemma zero_not_unitary : forall n, ~ (WF_Unitary (@Zero (2^n) (2^n))).
Proof.
intros n.
intros F.
destruct F as [_ U].
apply (f_equal2_inv 0 0)%nat in U.
revert U.
rewrite Mmult_0_r.
unfold I, Zero.
simpl.
bdestruct (0 <? 2 ^ n).
intros F. inversion F. lra.
specialize (pow_positive 2 n) as P.
lia.
Qed.
Lemma kron_unitary : forall {m n} (A : Matrix m m) (B : Matrix n n),
WF_Unitary A -> WF_Unitary B -> WF_Unitary (A ⊗ B).
Proof.
intros m n A B [WFA UA] [WFB UB].
unfold WF_Unitary in *.
split.
auto with wf_db.
rewrite kron_adjoint.
rewrite kron_mixed_product.
rewrite UA, UB.
rewrite id_kron.
easy.
Qed.
Lemma Mmult_unitary : forall (n : nat) (A : Square n) (B : Square n),
WF_Unitary A ->
WF_Unitary B ->
WF_Unitary (A × B).
Proof.
intros n A B [WFA UA] [WFB UB].
split.
auto with wf_db.
Msimpl.
rewrite Mmult_assoc.
rewrite <- (Mmult_assoc A†).
rewrite UA.
Msimpl.
apply UB.
Qed.
(********************)
(* Self-adjointness *)
(********************)
(* Maybe change to "Hermitian?" *)
Definition id_sa := id_adjoint_eq.
Lemma hadamard_sa : hadamard† = hadamard.
Proof.
prep_matrix_equality.
repeat (try destruct x; try destruct y; try lca; trivial).
Qed.
Lemma σx_sa : σx† = σx.
Proof.
prep_matrix_equality.
repeat (try destruct x; try destruct y; try lca; trivial).
Qed.
Lemma σy_sa : σy† = σy.
Proof.
prep_matrix_equality.
repeat (try destruct x; try destruct y; try lca; trivial).
Qed.
Lemma σz_sa : σz† = σz.
Proof.
prep_matrix_equality.
repeat (try destruct x; try destruct y; try lca; trivial).