forked from inQWIRE/QuantumLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectorStates.v
1756 lines (1585 loc) · 49.4 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.
(* This file provides abstractions for describing quantum states as vectors.
- f_to_vec describes classical states as boolean functions
- basis_vector describes classiacal 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 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.
(* 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. Qed.
Lemma Hminus_spec : hadamard × ∣ - ⟩ = ∣ 1 ⟩.
Proof. solve_matrix. Qed.
Local Open Scope nat_scope.
(* TODO: make general *)
Lemma H0_kron_n_spec : forall n,
n ⨂ hadamard × n ⨂ ∣0⟩ = n ⨂ ∣+⟩.
Proof.
intros.
induction n; simpl.
- Msimpl_light. reflexivity.
- replace (2^n + (2^n + 0)) with (2^n * 2) by lia.
replace (1^n + 0) with (1*1) by (rewrite Nat.pow_1_l, plus_0_r; lia).
rewrite Nat.pow_1_l.
rewrite kron_mixed_product.
rewrite <- IHn.
apply f_equal_gen; try reflexivity.
lma'.
Qed.
Local Close Scope nat_scope.
(* X properties *)
Lemma X0_spec : σx × ∣ 0 ⟩ = ∣ 1 ⟩.
Proof. lma'. Qed.
Lemma X1_spec : σx × ∣ 1 ⟩ = ∣ 0 ⟩.
Proof. lma'. Qed.
(* Y properties *)
Lemma Y0_spec : σy × ∣ 0 ⟩ = Ci .* ∣ 1 ⟩.
Proof. lma'. Qed.
Lemma Y1_spec : σy × ∣ 1 ⟩ = -Ci .* ∣ 0 ⟩.
Proof. lma'. Qed.
(* Z properties *)
Lemma Z0_spec : σz × ∣ 0 ⟩ = ∣ 0 ⟩.
Proof. lma'. Qed.
Lemma Z1_spec : σz × ∣ 1 ⟩ = -1 .* ∣ 1 ⟩.
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.
Definition b2R (b : bool) : R := if b then 1%R else 0%R.
Local Coercion b2R : bool >-> R.
Local Coercion Nat.b2n : bool >-> nat.
Lemma phase_shift_on_ket : forall (θ : R) (b : bool),
phase_shift θ × ∣ b ⟩ = (Cexp (b * θ)) .* ∣ b ⟩.
Proof.
intros.
destruct b; solve_matrix; autorewrite with R_db.
reflexivity.
rewrite Cexp_0; reflexivity.
Qed.
Lemma hadamard_on_ket : forall (b : bool),
hadamard × ∣ b ⟩ = /√2 .* (∣ 0 ⟩ .+ (-1)^b .* ∣ 1 ⟩).
Proof.
intros.
destruct b; solve_matrix; autorewrite with R_db Cexp_db; lca.
Qed.
(* CNOT properties *)
Lemma CNOT_spec : forall (x y : nat), (x < 2)%nat -> (y < 2)%nat -> cnot × ∣ x,y ⟩ = ∣ x, (x + y) mod 2 ⟩.
Proof.
intros; destruct x as [| [|x]], y as [| [|y]]; try lia; lma'.
Qed.
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.
(* SWAP properties *)
Lemma SWAP_spec : forall x y, swap × ∣ x,y ⟩ = ∣ y,x ⟩.
Proof. intros. destruct x,y; lma'. Qed.
(* Automation *)
(* General matrix rewrites *)
Hint Rewrite bra0_equiv bra1_equiv ket0_equiv ket1_equiv : ket_db.
Hint Rewrite bra0ket0 bra0ket1 bra1ket0 bra1ket1 : ket_db.
Hint Rewrite Mmult_plus_distr_l Mmult_plus_distr_r kron_plus_distr_l kron_plus_distr_r Mscale_plus_distr_r : ket_db.
Hint Rewrite Mscale_mult_dist_l Mscale_mult_dist_r Mscale_kron_dist_l Mscale_kron_dist_r : ket_db.
Hint Rewrite Mscale_assoc @Mmult_assoc : ket_db.
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.
Hint Rewrite kron_0_l kron_0_r Mmult_0_l Mmult_0_r : ket_db.
Hint Rewrite @kron_mixed_product : ket_db.
(* Quantum-specific identities *)
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.
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.
Hint Rewrite ket2bra : ket_db.
(* TODO: add transpose and adjoint lemmas to ket_db? *)
Lemma ket0_transpose_bra0 : (ket 0) ⊤ = bra 0.
Proof. solve_matrix. Qed.
Lemma ket1_transpose_bra1 : (ket 1) ⊤ = bra 1.
Proof. solve_matrix. Qed.
Lemma bra0_transpose_ket0 : (bra 0) ⊤ = ket 0.
Proof. solve_matrix. Qed.
Lemma bra1_transpose_ket1 : (bra 1) ⊤ = ket 1.
Proof. solve_matrix. Qed.
Lemma bra1_adjoint_ket1 : (bra 1) † = ket 1.
Proof. solve_matrix. Qed.
Lemma ket1_adjoint_bra1 : (ket 1) † = bra 1.
Proof. solve_matrix. Qed.
Lemma bra0_adjoint_ket0 : (bra 0) † = ket 0.
Proof. solve_matrix. Qed.
Lemma ket0_adjoint_bra0 : (ket 0) † = bra 0.
Proof. solve_matrix. 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.
rewrite Mscale_1_l; reflexivity.
Qed.
(*******************************)
(** Classical States **)
(*******************************)
Local Close Scope C_scope.
Local Close Scope R_scope.
Local Open Scope nat_scope.
(* General facts about (nat -> A) functions.
TODO #1: These lemmas are probably already defined in Coq somewhere.
TODO #2: For efficiency, instead of using functions indexed by natural
numbers, we should use vectors/arrays. *)
(* update_at is the same function on lists.
update is also defined in SF. *)
(* Update the value at one index of a boolean function. *)
Definition update {A} (f : nat -> A) (i : nat) (x : A) :=
fun j => if j =? i then x else f j.
Lemma update_index_eq : forall {A} (f : nat -> A) i b, (update f i b) i = b.
Proof.
intros.
unfold update.
rewrite Nat.eqb_refl.
reflexivity.
Qed.
Lemma update_index_neq : forall {A} (f : nat -> A) i j b, i <> j -> (update f i b) j = f j.
Proof.
intros.
unfold update.
bdestruct_all; auto.
Qed.
Lemma update_same : forall {A} (f : nat -> A) i b,
b = f i -> update f i b = f.
Proof.
intros.
apply functional_extensionality.
intros.
unfold update.
bdestruct (x =? i); subst; reflexivity.
Qed.
Lemma update_twice_eq : forall {A} (f : nat -> A) i b b',
update (update f i b) i b' = update f i b'.
Proof.
intros.
apply functional_extensionality.
intros.
unfold update.
bdestruct (x =? i); subst; reflexivity.
Qed.
Lemma update_twice_neq : forall {A} (f : nat -> A) i j b b',
i <> j -> update (update f i b) j b' = update (update f j b') i b.
Proof.
intros.
apply functional_extensionality.
intros.
unfold update.
bdestruct (x =? i); bdestruct (x =? j); subst; easy.
Qed.
Definition shift {A} (f : nat -> A) k := fun i => f (i + k).
Lemma shift_0 : forall {A} (f : nat -> A), shift f 0 = f.
Proof.
intros A f.
unfold shift.
apply functional_extensionality; intro x.
rewrite Nat.add_0_r.
reflexivity.
Qed.
Lemma shift_plus : forall {A} (f : nat -> A) i j, shift (shift f j) i = shift f (i + j).
Proof.
intros A f i j.
unfold shift.
apply functional_extensionality; intro x.
rewrite Nat.add_assoc.
reflexivity.
Qed.
Lemma shift_simplify : forall {A} (f : nat -> A) i j ,
shift f i j = f (j + i).
Proof. intros. unfold shift. reflexivity. Qed.
Definition fswap {A} (f : nat -> A) x y :=
fun i => if i =? x then f y else if i =? y then f x else f i.
Lemma fswap_simpl1 : forall A f x y, @fswap A f x y x = f y.
Proof.
intros.
unfold fswap.
rewrite Nat.eqb_refl.
reflexivity.
Qed.
Lemma fswap_simpl2 : forall A f x y, @fswap A f x y y = f x.
Proof.
intros.
unfold fswap.
bdestruct (y =? x).
subst. reflexivity.
rewrite Nat.eqb_refl.
reflexivity.
Qed.
Lemma fswap_same : forall A f x, @fswap A f x x = f.
Proof.
intros.
unfold fswap.
apply functional_extensionality.
intro i.
bdestruct_all; auto.
Qed.
Lemma fswap_neq : forall {A} (f : nat -> A) a b x, a <> x -> b <> x -> fswap f a b x = f x.
Proof.
intros. unfold fswap. bdestructΩ (x =? a). bdestructΩ (x =? b). auto.
Qed.
Lemma fswap_rewrite : forall {A} (f : nat -> A) a b,
fswap f a b = update (update f b (f a)) a (f b).
Proof.
intros.
unfold fswap.
apply functional_extensionality.
intro x.
bdestruct_all; subst.
rewrite update_index_eq; auto.
rewrite update_index_neq by lia.
rewrite update_index_eq; auto.
rewrite update_index_neq by lia.
rewrite update_index_neq by lia.
reflexivity.
Qed.
(* 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 beq_nat_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.
unfold kron, basis_vector.
solve_matrix.
bdestruct (y0 =? 0).
- repeat rewrite andb_true_r.
assert (2^n > 0)%nat.
{ assert (0 < 2^n)%nat by (apply pow_positive; lia). lia.
}
specialize (divmod_decomp x0 x y (2^n)%nat H0 Hy) as G.
bdestruct (x0 =? x * 2 ^ n + y).
+ apply G in H1. destruct H1.
rewrite H1, H2. do 2 rewrite Nat.eqb_refl. lca.
+ bdestruct (x0 / 2 ^ n =? x); bdestruct (x0 mod 2 ^ n =? y); try lca.
assert ((x0 / 2 ^ n)%nat = x /\ x0 mod 2 ^ n = y) by easy.
apply G in H4.
easy.
- repeat rewrite andb_false_r.
lca.
Qed.
(* f_to_vec and basis_vector allow us to represent the same set of states.
To prove this we need lemmas about converting between natural numbers
and their binary representation. *)
(* takes [1;1;0;0] to 3, [0;0;1;0] to 4 *)
Fixpoint binlist_to_nat (l : list bool) : nat :=
match l with
| [] => 0
| b :: l' => b + 2 * binlist_to_nat l'
end.
Fixpoint funbool_to_list (len : nat) (f : nat -> bool) :=
match len with
| O => []
| S len' => f len' :: funbool_to_list len' f
end.
Definition funbool_to_nat (len : nat) (f : nat -> bool) :=
binlist_to_nat (funbool_to_list len f).
Lemma funbool_to_nat_bound : forall n f, (funbool_to_nat n f < 2 ^ n)%nat.
Proof.
intros n f.
unfold funbool_to_nat.
induction n; simpl. lia.
destruct (f n); simpl; lia.
Qed.
Lemma funbool_to_nat_eq : forall n f f',
(forall x, x < n -> f x = f' x)%nat ->
funbool_to_nat n f = funbool_to_nat n f'.
Proof.
intros.
unfold funbool_to_nat.
apply f_equal.
induction n.
reflexivity.
simpl.
rewrite H by lia.
rewrite IHn; auto.
Qed.
Local Opaque Nat.mul.
Lemma funbool_to_nat_shift : forall n f k, (k < n)%nat ->
funbool_to_nat n f = (2 ^ (n - k) * funbool_to_nat k f + funbool_to_nat (n - k) (shift f k))%nat.
Proof.
intros.
unfold shift, funbool_to_nat.
destruct n; try lia.
induction n.
destruct k; try lia.
replace (1 - 0)%nat with (S O) by lia; simpl. reflexivity.
remember (S n) as n'.
replace (S n' - k)%nat with (S (n' - k))%nat by lia.
simpl.
replace (n' - k + k)%nat with n' by lia.
bdestruct (n' =? k).
subst.
replace (S n - S n)%nat with O by lia; simpl.
lia.
rewrite IHn; lia.
Qed.
Local Transparent Nat.mul.
(* 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.
unfold basis_vector.
unfold I.
prep_matrix_equality.
bdestruct (x =? 0); bdestruct (x =? y); subst; simpl; trivial.
bdestruct_all; easy.
bdestructΩ (y <? 1); easy.
- simpl.
rewrite IHn.
unfold funbool_to_nat; simpl.
unfold basis_vector.
prep_matrix_equality. unfold kron.
rewrite Nat.div_1_r.
bdestruct (y =? 0).
2: rewrite 2 andb_false_r; lca.
rewrite 2 andb_true_r.
rewrite Nat.mod_1_r, Nat.add_0_r.
remember (binlist_to_nat (funbool_to_list n f)) as z.
destruct (f n).
+ specialize (Nat.div_mod x 2) as DM.
rewrite <- Nat.bit0_mod in *.
destruct (Nat.testbit x 0); bdestruct (x / 2 =? z);
simpl in *; bdestruct (x =? S (z + z)); try lia; try lca.
+ specialize (Nat.div_mod x 2) as DM.
rewrite <- Nat.bit0_mod in *.
destruct (Nat.testbit x 0); bdestruct (x / 2 =? z);
simpl in *; bdestruct (x =? (z + z)); try lia; try lca.
Qed.
Fixpoint incr_bin (l : list bool) :=
match l with
| [] => [true]
| false :: t => true :: t
| true :: t => false :: (incr_bin t)
end.
Fixpoint nat_to_binlist' n :=
match n with
| O => []
| S n' => incr_bin (nat_to_binlist' n')
end.
Definition nat_to_binlist len n :=
let l := nat_to_binlist' n in
l ++ (repeat false (len - length l)).
Fixpoint list_to_funbool len (l : list bool) : nat -> bool :=
match l with
| [] => fun _ => false
| h :: t => update (list_to_funbool (len - 1)%nat t) (len - 1) h
end.
Definition nat_to_funbool len n : nat -> bool :=
list_to_funbool len (nat_to_binlist len n).
Lemma binlist_to_nat_append : forall l1 l2,
binlist_to_nat (l1 ++ l2) =
(binlist_to_nat l1 + 2 ^ (length l1) * binlist_to_nat l2)%nat.
Proof. intros l1 l2. induction l1; simpl; lia. Qed.
Lemma binlist_to_nat_false : forall n, binlist_to_nat (repeat false n) = O.
Proof. induction n; simpl; lia. Qed.
Lemma binlist_to_nat_true : forall n, binlist_to_nat (repeat true n) = 2^n - 1.
Proof.
induction n; simpl; trivial.
rewrite IHn. clear.
repeat rewrite Nat.add_0_r.
rewrite <- Nat.add_succ_l.
replace (S (2 ^ n - 1)) with (1 + (2 ^ n - 1)) by easy.
rewrite <- le_plus_minus.
rewrite <- Nat.add_sub_assoc.
reflexivity.
all: induction n; simpl; try lia.
Qed.
Lemma nat_to_binlist_eq_nat_to_binlist' : forall len n,
binlist_to_nat (nat_to_binlist len n) = binlist_to_nat (nat_to_binlist' n).
Proof.
intros len n.
unfold nat_to_binlist.
rewrite binlist_to_nat_append.
rewrite binlist_to_nat_false.
lia.
Qed.
Lemma nat_to_binlist_inverse : forall len n,
binlist_to_nat (nat_to_binlist len n) = n.
Proof.
intros len n.
rewrite nat_to_binlist_eq_nat_to_binlist'.
induction n; simpl.
reflexivity.
assert (H : forall l, binlist_to_nat (incr_bin l) = S (binlist_to_nat l)).
{ clear.
induction l; simpl.
reflexivity.
destruct a; simpl; try reflexivity.
rewrite IHl. lia. }
rewrite H, IHn.
reflexivity.
Qed.
Lemma nat_to_binlist_corr : forall l n,
nat_to_binlist' n = l ->
binlist_to_nat l = n. (* Lemma this *)
Proof.
intros.
rewrite <- H.
erewrite <- (nat_to_binlist_eq_nat_to_binlist' n n).
rewrite nat_to_binlist_inverse.
reflexivity.
Qed.
Lemma incr_bin_true_length : forall l,
Forall (fun b => b = true) l ->
length (incr_bin l) = S (length l).
Proof.
intros.
induction l; trivial.
- inversion H; subst.
simpl in *.
rewrite IHl; easy.
Qed.
Lemma incr_bin_false_length : forall l,
Exists (fun b => b <> true) l ->
length (incr_bin l) = length l.
Proof.
intros.
induction l; inversion H; subst.
- destruct a; simpl; easy.
- destruct a; simpl; trivial.
rewrite IHl; easy.
Qed.
Lemma all_true_repeat : forall l,
Forall (fun b : bool => b = true) l ->
l = repeat true (length l).
Proof.
intros.
induction l; simpl; trivial.
inversion H; subst.
rewrite <- IHl; easy.
Qed.
Lemma nat_to_binlist_length' : forall k n,
n < 2 ^ k -> length (nat_to_binlist' n) <= k.
Proof.
intros.
induction n; simpl; try lia.
destruct (Forall_Exists_dec (fun b => b = true) (fun b => bool_dec b true)
(nat_to_binlist' n)) as [ALL | NALL].
- rewrite incr_bin_true_length; trivial.
apply le_lt_eq_dec in IHn; [| lia].
destruct IHn; try lia.
exfalso.
apply all_true_repeat in ALL.
apply nat_to_binlist_corr in ALL.
rewrite binlist_to_nat_true in ALL.
rewrite e in ALL.
lia.
- rewrite incr_bin_false_length; trivial.
apply IHn; lia.
Qed.
Lemma nat_to_binlist_length : forall len n,
(n < 2 ^ len)%nat -> length (nat_to_binlist len n) = len.
Proof.
intros len n Hlen.
unfold nat_to_binlist.
rewrite app_length, repeat_length.
bdestruct (n =? 0); subst; simpl. lia.
apply nat_to_binlist_length' in Hlen.
lia.
Qed.
Lemma funbool_to_list_update_oob : forall f dim b n, (dim <= n)%nat ->
funbool_to_list dim (update f n b) = funbool_to_list dim f.
Proof.
intros.
induction dim; trivial.
simpl.
rewrite IHdim by lia.
unfold update.
bdestruct (dim =? n); try lia.
reflexivity.
Qed.
Lemma list_to_funbool_inverse : forall len l,
length l = len ->
funbool_to_list len (list_to_funbool len l) = l.
Proof.
intros len l.
generalize dependent len.
induction l; intros len Hlen.
simpl in Hlen; rewrite <- Hlen.
simpl. reflexivity.
simpl in Hlen; rewrite <- Hlen.
simpl.
replace (length l - 0)%nat with (length l) by lia.
rewrite update_index_eq.
rewrite funbool_to_list_update_oob by lia.
rewrite IHl; reflexivity.
Qed.
Lemma nat_to_funbool_inverse : forall len n,
(n < 2 ^ len)%nat -> funbool_to_nat len (nat_to_funbool len n) = n.
Proof.
intros.
unfold nat_to_funbool, funbool_to_nat.
rewrite list_to_funbool_inverse.
apply nat_to_binlist_inverse.
apply nat_to_binlist_length.
assumption.
Qed.
Local Opaque Nat.mul.
Lemma nat_to_binlist'_even : forall n, (n > 0)%nat ->
nat_to_binlist' (2 * n) = false :: nat_to_binlist' n.
Proof.
intros n Hn.
destruct n; try lia. clear.
induction n.
rewrite Nat.mul_1_r. simpl. reflexivity.
replace (2 * S (S n))%nat with (S (S (2 * S n))) by lia.
simpl. rewrite IHn. reflexivity.
Qed.
Lemma nat_to_binlist'_odd : forall n,
nat_to_binlist' (2 * n + 1) = true :: nat_to_binlist' n.
Proof.
induction n.
rewrite Nat.mul_0_r, Nat.add_0_l. simpl. reflexivity.
replace (2 * S n + 1)%nat with (S (S (2 * n + 1))) by lia.
simpl. rewrite IHn. reflexivity.
Qed.
Lemma binlist_to_nat_inverse : forall l n i,
list_to_funbool n (nat_to_binlist' (binlist_to_nat l)) i = list_to_funbool n l i.
Proof.
intros.
generalize dependent n.
induction l.
reflexivity.
intros.
simpl.
destruct a; simpl Nat.b2n.
rewrite Nat.add_comm.
rewrite nat_to_binlist'_odd.
simpl. unfold update.
rewrite IHl. reflexivity.
rewrite Nat.add_0_l.
bdestruct (binlist_to_nat l =? 0).
rewrite H in *.
rewrite Nat.mul_0_r.
simpl.
unfold update.
rewrite <- IHl.
simpl.
bdestruct_all; reflexivity.
rewrite nat_to_binlist'_even by lia.
simpl. unfold update.
rewrite IHl. reflexivity.
Qed.
Lemma list_to_funbool_repeat_false : forall n i,
list_to_funbool n (repeat false n) i = false.
Proof.
intros.
induction n.
reflexivity.
simpl. rewrite Nat.sub_0_r.
unfold update.
rewrite IHn.
bdestruct_all; reflexivity.
Qed.
Lemma funbool_to_nat_0 : forall n f,
funbool_to_nat n f = O -> forall i, (i < n)%nat -> f i = false.
Proof.
intros.
induction n.
lia.
intros.
unfold funbool_to_nat in *.
simpl in *.
destruct (f n) eqn:fn; simpl in *.
inversion H.
bdestruct (i =? n). subst.
assumption.
apply IHn; lia.
Qed.
Lemma funbool_to_nat_inverse : forall len f i, (i < len)%nat ->
nat_to_funbool len (funbool_to_nat len f) i = f i.
Proof.
intros.
assert (list_to_funbool_append1 : forall l1 l2,
(i >= length l2)%nat ->
(len <= length l1 + length l2)%nat ->
list_to_funbool len (l1 ++ l2) i = list_to_funbool len l1 i).
{ intros.
generalize dependent len.
induction l1; intros; simpl in *.
generalize dependent len.
induction l2.
reflexivity.
intros.
simpl in *.
unfold update.
bdestructΩ (i =? len - 1).
unfold update.
bdestruct (i =? len - 1).
reflexivity.
apply IHl1; lia. }
assert (list_to_funbool_append2 : forall l1 l2,
(i < length l2)%nat ->
(len >= length l1 + length l2)%nat ->
list_to_funbool len (l1 ++ l2) i =
list_to_funbool (len - length l1) l2 i).
{ clear.
intros.
generalize dependent len.
induction l1; intros; simpl in *.
rewrite Nat.sub_0_r.
reflexivity.
unfold update.
bdestructΩ (i =? len - 1).
rewrite IHl1 by lia.
replace (len - 1 - length l1)%nat with (len - S (length l1))%nat by lia.
reflexivity. }
unfold nat_to_funbool, funbool_to_nat, nat_to_binlist.
remember (binlist_to_nat (funbool_to_list len f)) as n.
bdestructΩ (len - length (nat_to_binlist' n) <=? i).
rewrite list_to_funbool_append1.
all: try rewrite repeat_length; try lia.
subst.
rewrite binlist_to_nat_inverse.
clear - H.
induction len.
lia.
simpl.
rewrite Nat.sub_0_r.
bdestruct (i =? len). subst.
rewrite update_index_eq.
reflexivity.
rewrite update_index_neq by lia.
rewrite IHlen by lia.
reflexivity.
rewrite list_to_funbool_append2.
all: try rewrite repeat_length; try lia.
assert (f i = false).
{ subst.
clear - H0.
induction len.
simpl in H0. lia.
remember (binlist_to_nat (funbool_to_list (S len) f)) as n.
bdestruct (n =? 0).
subst. rewrite H in *.
eapply funbool_to_nat_0. apply H.
lia.
apply IHlen.
subst.
simpl in *.
destruct (f len); simpl Nat.b2n in *.
rewrite Nat.add_comm in H0.
rewrite nat_to_binlist'_odd in H0.
simpl in H0. lia.
rewrite Nat.add_0_l in *.
rewrite nat_to_binlist'_even in H0 by lia.
simpl in H0. lia. }
rewrite list_to_funbool_repeat_false.
rewrite H1.
reflexivity.
Qed.
Local Transparent Nat.mul.
(* 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 {dim} (A B : Square (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 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 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.