-
Notifications
You must be signed in to change notification settings - Fork 10
/
Quantum.v
2171 lines (1853 loc) · 63 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.
(* 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⟩)).
Notation "∣+⟩" := xbasis_plus.
Notation "∣-⟩" := xbasis_minus.
Notation "∣R⟩" := ybasis_plus.
Notation "∣L⟩" := ybasis_minus.
(* 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.
Lemma sqrtx_sqrtx : sqrtx × sqrtx = σx.
Proof.
unfold sqrtx, σx, Mmult.
prep_matrix_equality.
destruct_m_eq;
autorewrite with trig_db C_db; try lca.
Qed.
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.
#[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).
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 *)
Lemma sqrtx_decompose: sqrtx = hadamard × phase_shift (PI/2) × hadamard.
Proof.
solve_matrix.
all: rewrite Cexp_PI2; group_radicals; lca.
Qed.
(* 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 EPRpair_creation : cnot × (hadamard ⊗ I 2) × ∣0,0⟩ = EPRpair.
Proof. unfold EPRpair. 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.
#[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.
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.
#[global] Hint Rewrite swap_swap swap_swap_r using (auto 100 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)).
*)
(** 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_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_braqubit0 WF_braqubit1 : 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_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_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_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_cnot WF_notc WF_swap : wf_db.
#[export] Hint Resolve WF_phase WF_Sgate WF_Tgate WF_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.
(* how to make this proof shorter? *)
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.
unfold direct_sum, kron, Mplus.
prep_matrix_equality.
bdestruct_all; try lia; simpl.
- repeat (rewrite Nat.div_small, Nat.mod_small; try easy); lca.
- rewrite H; auto.
destruct n. rewrite H, H0; try lca; try (right; lia).
rewrite (Nat.div_small x m), (Nat.mod_small x m); try easy.
replace (y / S n)%nat with (1 + (y - S n)/S n)%nat.
unfold Mmult, adjoint; simpl.
destruct (fst (Nat.divmod (y - S n) n 0 n)); try lca.
rewrite <- Nat.div_add_l; auto.
replace ((1 * S n + (y - S n)))%nat with y by lia; easy.
- rewrite H; auto.
destruct m. rewrite H, H0; try lca; try (left; lia).
rewrite (Nat.div_small y n), (Nat.mod_small y n); try easy.
replace (x / S m)%nat with (1 + (x - S m)/S m)%nat.
unfold Mmult, adjoint; simpl.
destruct (fst (Nat.divmod (x - S m) m 0 m)); try lca.
rewrite <- Nat.div_add_l; auto.
replace ((1 * S m + (x - S m)))%nat with x by lia; easy.
- destruct n; destruct m.
try (rewrite H, H0, H0; try lca);
try (left; lia); try (right; lia).
try (rewrite H, H0, H0; try lca);
try (left; lia); try (right; lia).
try (rewrite H, H0, H0; try lca);
try (left; lia); try (right; lia).
bdestruct (x - S m <? S m); bdestruct (y - S n <? S n).
replace (x / S m)%nat with 1%nat.
replace (y / S n)%nat with 1%nat.
replace (x mod S m) with (x - S m)%nat.
replace (y mod S n) with (y - S n)%nat.
lca.
replace y with ((y - S n) + 1*(S n))%nat by lia.
rewrite Nat.mod_add; try lia.
rewrite Nat.mod_small; lia.
replace x with ((x - S m) + 1*(S m))%nat by lia.
rewrite Nat.mod_add; try lia.
rewrite Nat.mod_small; lia.
replace y with ((y - S n) + 1*(S n))%nat by lia.
rewrite Nat.div_add; try lia.
rewrite Nat.div_small; lia.
replace x with ((x - S m) + 1*(S m))%nat by lia.
rewrite Nat.div_add; try lia.
rewrite Nat.div_small; lia.
rewrite H0; try (right; easy).
bdestruct (y <? 2*(S n)); try lia.
replace y with ((y - 2 * S n) + 2*(S n))%nat by lia.
rewrite Nat.div_add; try lia.
rewrite WF_braket0, WF_braket1; try lca; try (right; lia).
rewrite H0; try (left; easy).
bdestruct (x <? 2*(S m)); try lia.
replace x with ((x - 2 * S m) + 2*(S m))%nat by lia.
rewrite Nat.div_add; try lia.
rewrite WF_braket0, WF_braket1; try lca; try (left; lia).
rewrite H0; try (left; easy).
bdestruct (x <? 2*(S m)); try lia.
replace x with ((x - 2 * S m) + 2*(S m))%nat by lia.
rewrite Nat.div_add; try lia.
rewrite WF_braket0, WF_braket1; try lca; try (left; lia).
Qed.
Lemma cnot_decomposition : ∣1⟩⟨1∣ ⊗ σx .+ ∣0⟩⟨0∣ ⊗ I 2 = cnot.
Proof. solve_matrix. Qed.
Lemma notc_decomposition : σx ⊗ ∣1⟩⟨1∣ .+ I 2 ⊗ ∣0⟩⟨0∣ = notc.
Proof. solve_matrix. 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.
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 S_unitary : WF_Unitary Sgate. Proof. apply phase_unitary. Qed.
Lemma T_unitary : WF_Unitary Tgate. Proof. apply phase_unitary. 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.
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 big_sum_sum.
bdestructΩ (y <? n + (n + 0)).
+ bdestructΩ (n <=? y).
* rewrite big_sum_0_bounded. Csimpl.
rewrite (big_sum_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 (Nat.add_comm n x).
rewrite Nat.add_sub.
easy.
++ intros x L.
bdestructΩ (y =? x).
rewrite andb_false_r.
bdestructΩ (n <=? x).
simpl. lca.
* rewrite (@big_sum_unique C C_is_monoid 1).
rewrite big_sum_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.
intros.
lca.
+ rewrite 2 big_sum_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 big_sum_sum.
bdestructΩ (y <? n + (n + 0)).
+ bdestructΩ (n <=? y).
* rewrite big_sum_0_bounded. Csimpl.
bdestructΩ (n <=? x).
rewrite (big_sum_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 (Nat.add_comm n z).
rewrite Nat.add_sub.
easy.
++ rewrite big_sum_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 big_sum_0_bounded.
rewrite big_sum_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.