-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathDenotation.v
4293 lines (4036 loc) · 135 KB
/
Denotation.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 Program.
Require Import Arith.
Require Import Monad.
Require Export Contexts.
Require Export HOASCircuits.
Require Export HOASLib.
Require Export DBCircuits.
Require Export QuantumLib.Quantum.
Require Import List.
Import ListNotations.
Set Bullet Behavior "Strict Subproofs".
Global Unset Asymmetric Patterns.
Local Opaque WF_Matrix.
Class Denote source target := {denote : source -> target}.
Notation "⟦ s ⟧" := (denote s) (at level 10).
Class Denote_Correct {source target} `(Denote source target) :=
{
correctness : target -> Prop;
denote_correct : forall (x : source), correctness (denote x)
}.
(** Unitary Denotation **)
Instance Denote_WType : Denote WType nat := {| denote := size_wtype |}.
Instance Denote_Ctx : Denote Ctx nat := {| denote := size_ctx |}.
Instance Denote_OCtx : Denote OCtx nat := {| denote := size_octx |}.
Fixpoint denote_unitary {W} (U : Unitary W) : Square (2^⟦W⟧) :=
match U with
| _H => hadamard
| _X => σx
| _Y => σy
| _Z => σz
| _R_ ϕ => phase_shift ϕ
| ctrl g => control (denote_unitary g)
| bit_ctrl g => control (denote_unitary g)
end.
Instance Denote_Unitary W : Denote (Unitary W) (Square (2^⟦W⟧)) :=
{| denote := denote_unitary |}.
Lemma WF_Matrix_U : forall {W} (U : Unitary W),
WF_Matrix (⟦U⟧).
Proof.
induction U; simpl; try apply WF_control; auto with wf_db. (* try shouldn't be necessary *)
Qed.
#[export] Hint Resolve WF_Matrix_U : wf_db.
Lemma unitary_gate_unitary : forall {W} (U : Unitary W), WF_Unitary (⟦U⟧).
Proof.
induction U.
+ apply H_unitary.
+ apply σx_unitary.
+ apply σy_unitary.
+ apply σz_unitary.
+ apply phase_unitary.
+ simpl. apply control_unitary; assumption.
+ simpl. apply control_unitary; assumption.
Qed.
Lemma denote_unitary_transpose : forall {W} (U : Unitary W), ⟦trans U⟧ = ⟦U⟧†.
Proof.
induction U; simpl; Qsimpl; trivial.
- simpl_rewrite IHU. easy.
- simpl_rewrite IHU. easy.
Qed.
(* Hint Resolve unitary_gate_unitary. Do we need this? Where? *)
Instance Denote_Unitary_Correct W : Denote_Correct (Denote_Unitary W) :=
{|
correctness := fun A => WF_Unitary A;
denote_correct := fun U => unitary_gate_unitary U
|}.
(** Gate Denotation **)
Definition denote_gate' (safe : bool) n {w1 w2} (g : Gate w1 w2)
: Superoperator (2^⟦w1⟧ * 2^n) (2^⟦w2⟧ * 2^n) :=
match g with
| U u => super (⟦u⟧ ⊗ I (2^n))
| BNOT => super (σx ⊗ I (2^n))
| init0 => super (∣0⟩ ⊗ I (2^n))
| init1 => super (∣1⟩ ⊗ I (2^n))
| new0 => super (∣0⟩ ⊗ I (2^n))
| new1 => super (∣1⟩ ⊗ I (2^n))
| meas => Splus (super (∣0⟩⟨0∣ ⊗ I (2^n))) (super (∣1⟩⟨1∣ ⊗ I (2^n)))
| measQ => Splus (super (∣0⟩⟨0∣ ⊗ I (2^n))) (super (∣1⟩⟨1∣ ⊗ I (2^n)))
| discard => Splus (super (⟨0∣ ⊗ I (2^n))) (super (⟨1∣ ⊗ I (2^n)))
(* Safe performs a standard measure-discard, unsafe takes for granted that the
qubit to be removed is in the desired state. *)
| assert0 => if safe then Splus (super (⟨0∣ ⊗ I (2^n))) (super (⟨1∣ ⊗ I (2^n)))
else super (⟨0∣ ⊗ I (2^n))
| assert1 => if safe then Splus (super (⟨0∣ ⊗ I (2^n))) (super (⟨1∣ ⊗ I (2^n)))
else super (⟨1∣ ⊗ I (2^n))
end.
Definition denote_gate safe {W1 W2} (g : Gate W1 W2) :
Superoperator (2^⟦W1⟧) (2^⟦W2⟧) := denote_gate' safe 0 g.
(* match g with
| U _ u => super (⟦u⟧)
| init0 => new0_op
| init1 => new1_op
| new0 => new0_op
| new1 => new1_op
| meas => meas_op
| discard => discard_op
end.*)
Lemma pow_gt_0 : forall n, (2^n > O)%nat.
Proof.
induction n; auto.
simpl. apply gt_trans with (2^n)%nat; auto. lia.
Qed.
Lemma WF_denote_gate : forall safe n W1 W2 (g : Gate W1 W2) ρ,
WF_Matrix ρ -> WF_Matrix (denote_gate' safe n g ρ).
Proof.
intros safe n W1 W2 g ρ wf_ρ.
assert (0 < 2^n)%nat by apply pow_gt_0.
assert (0 <> 2^n)%nat by lia.
destruct g; simpl; unfold super, Splus; try destruct safe;
auto with wf_db; try lia.
specialize (WF_Matrix_U u). intros wf_u. auto with wf_db.
specialize (WF_Matrix_U u). intros wf_u. auto with wf_db.
Qed.
#[export] Hint Resolve WF_denote_gate : wf_db.
Close Scope circ_scope.
Lemma discard_qubit_correct : forall (ρ : Matrix 2 2),
Mixed_State ρ ->
Mixed_State (⟨0∣ × ρ × ∣0⟩ .+ ⟨1∣ × ρ × ∣1⟩).
Proof.
intros ρ M.
specialize (WF_Mixed _ M) as WF.
do 4 reduce_matrices.
replace (list2D_to_matrix [[ρ 0%nat 0%nat]]) with (ρ 0%nat 0%nat .* I 1) by solve_matrix.
replace (list2D_to_matrix [[ρ 1%nat 1%nat]]) with (ρ 1%nat 1%nat .* I 1) by solve_matrix.
specialize (mixed_state_diag_real _ 0%nat M) as R0.
specialize (mixed_state_diag_real _ 1%nat M) as R1.
specialize (mixed_state_diag_in01 _ 0%nat M) as IN0.
specialize (mixed_state_diag_in01 _ 1%nat M) as IN1.
specialize (mixed_state_trace_1 _ M) as TR1.
unfold trace in TR1. simpl in TR1.
replace (ρ 0%nat 0%nat) with (RtoC (fst (ρ 0%nat 0%nat))) by lca.
replace (ρ 1%nat 1%nat) with (RtoC (fst (ρ 1%nat 1%nat))) by lca.
replace (ρ 1%nat 1%nat) with (RtoC (1 - fst (ρ 0%nat 0%nat))) by (inversion TR1; lca).
destruct (Req_dec (fst (ρ 0%nat 0%nat)) 0); [|destruct (Req_dec (fst (ρ 0%nat 0%nat)) 1)].
- rewrite H, Mscale_0_l, Mplus_0_l, Rminus_0_r, Mscale_1_l.
constructor; apply pure_id1.
- unfold Rminus.
rewrite H0, Mscale_1_l, Rplus_opp_r, Mscale_0_l, Mplus_0_r.
constructor; apply pure_id1.
- apply Mix_S.
lra.
constructor; apply pure_id1.
constructor; apply pure_id1.
Qed.
(* This is only true for "safe" gate denotation *)
Lemma denote_gate_correct : forall {W1} {W2} (g : Gate W1 W2),
WF_Superoperator (denote_gate true g).
Proof.
unfold WF_Superoperator.
intros.
induction g.
+ simpl.
rewrite kron_1_r.
rewrite Nat.mul_1_r.
apply mixed_unitary.
apply unitary_gate_unitary.
assumption.
+ simpl.
rewrite kron_1_r.
apply mixed_unitary.
apply σx_unitary.
assumption.
+ simpl in *.
rewrite kron_1_r.
unfold super.
rewrite (mixed_dim1 ρ); trivial.
rewrite Mmult_1_r.
constructor; apply pure0.
auto with wf_db.
+ simpl in *.
rewrite kron_1_r.
unfold super.
rewrite (mixed_dim1 ρ); trivial.
rewrite Mmult_1_r.
constructor; apply pure1.
auto with wf_db.
+ simpl in *.
rewrite kron_1_r.
unfold super.
rewrite (mixed_dim1 ρ); trivial.
rewrite Mmult_1_r.
constructor; apply pure0.
auto with wf_db.
+ simpl in *.
rewrite kron_1_r.
unfold super.
rewrite (mixed_dim1 ρ); trivial.
rewrite Mmult_1_r.
constructor; apply pure1.
auto with wf_db.
+ simpl in *.
rewrite kron_1_r.
unfold super.
Msimpl.
specialize (WF_Mixed _ H) as WF.
unfold Splus.
replace (∣0⟩⟨0∣ × ρ × ∣0⟩⟨0∣) with (ρ 0%nat 0%nat .* ∣0⟩⟨0∣) by solve_matrix.
replace (∣1⟩⟨1∣ × ρ × ∣1⟩⟨1∣) with (ρ 1%nat 1%nat .* ∣1⟩⟨1∣) by solve_matrix.
specialize (mixed_state_trace_1 _ H) as TR1. unfold trace in TR1. simpl in TR1.
replace (ρ 1%nat 1%nat) with (1 - ρ O O) by (rewrite <- TR1; lca).
replace (ρ O O) with ((fst (ρ O O)), snd (ρ O O)) by lca.
rewrite mixed_state_diag_real by assumption.
set (a := (ρ 0 0)%nat). replace (ρ 0 0)%nat with a in TR1 by reflexivity.
set (b := (ρ 1 1)%nat). replace (ρ 1 1)%nat with b in TR1 by reflexivity.
replace (1 - (fst a, 0)) with (RtoC (1 - fst a)) by lca.
replace (fst a, 0) with (RtoC (fst a)) by reflexivity.
destruct (Ceq_dec a C0) as [Z | NZ]; [|destruct (Ceq_dec a C1) as [O | NO]].
* rewrite Z in *.
rewrite Mscale_0_l.
rewrite Mplus_0_l.
simpl. autorewrite with R_db.
rewrite Mscale_1_l.
apply Pure_S.
apply pure1.
* rewrite O in *.
rewrite Mscale_1_l.
simpl. unfold Rminus. rewrite Rplus_opp_r.
rewrite Mscale_0_l.
rewrite Mplus_0_r.
apply Pure_S.
apply pure0.
* apply Mix_S; [| apply Pure_S, pure0| apply Pure_S, pure1].
unfold a in *.
specialize (mixed_state_diag_in01 ρ 0%nat H) as IN01.
destruct IN01 as [G L].
destruct G.
2: {
contradict NZ; apply c_proj_eq.
rewrite <- H0; reflexivity.
apply mixed_state_diag_real; easy.
}
destruct L.
2: {
contradict NO; apply c_proj_eq.
rewrite <- H1; reflexivity.
apply mixed_state_diag_real; easy.
}
lra.
+ simpl in *.
rewrite kron_1_r.
unfold super.
Msimpl.
specialize (WF_Mixed _ H) as WF.
unfold Splus.
replace (∣0⟩⟨0∣ × ρ × ∣0⟩⟨0∣) with (ρ 0%nat 0%nat .* ∣0⟩⟨0∣) by solve_matrix.
replace (∣1⟩⟨1∣ × ρ × ∣1⟩⟨1∣) with (ρ 1%nat 1%nat .* ∣1⟩⟨1∣) by solve_matrix.
specialize (mixed_state_trace_1 _ H) as TR1. unfold trace in TR1. simpl in TR1.
replace (ρ 1%nat 1%nat) with (1 - ρ O O) by (rewrite <- TR1; lca).
replace (ρ O O) with ((fst (ρ O O)), snd (ρ O O)) by lca.
rewrite mixed_state_diag_real by assumption.
replace (1 - (fst (ρ O O), 0)) with (RtoC (1 - fst (ρ O O))) by lca.
replace (fst (ρ O O), 0) with (RtoC (fst (ρ O O))) by reflexivity.
specialize (mixed_state_diag_in01 _ O H) as in01.
destruct in01 as [[L|E0] [R|E1]].
* apply Mix_S. easy. apply Pure_S. apply pure0. apply Pure_S. apply pure1.
* rewrite E1. unfold Rminus. rewrite Rplus_opp_r.
rewrite Mscale_0_l, Mscale_1_l, Mplus_0_r. apply Pure_S. apply pure0.
* rewrite <- E0. rewrite Rminus_0_r.
rewrite Mscale_0_l, Mscale_1_l. rewrite Mplus_0_l. apply Pure_S. apply pure1.
* lra.
+ simpl in *.
unfold super, Splus.
Msimpl.
specialize (WF_Mixed _ H) as WF.
restore_dims.
repeat reduce_matrices.
constructor.
apply mixed_state_trace_1 in H.
unfold trace in H. simpl in H. rewrite Cplus_0_l in H.
rewrite H.
specialize (@WF_I 1%nat) as WFI.
replace (list2D_to_matrix [[C1]]) with (I 1).
apply pure_id1.
crunch_matrix.
bdestruct (S (S x) <? 1). lia. rewrite andb_false_r. reflexivity.
+ simpl in *.
unfold super, Splus.
Msimpl.
specialize (WF_Mixed _ H) as WF.
restore_dims.
repeat reduce_matrices.
constructor.
apply mixed_state_trace_1 in H.
unfold trace in H. simpl in H. rewrite Cplus_0_l in H.
rewrite H.
specialize (@WF_I 1%nat) as WFI.
replace (list2D_to_matrix [[C1]]) with (I 1).
apply pure_id1.
crunch_matrix.
bdestruct (S (S x) <? 1). lia. rewrite andb_false_r. reflexivity.
+ simpl in *.
unfold super, Splus.
Msimpl.
specialize (WF_Mixed _ H) as WF.
restore_dims.
repeat reduce_matrices.
constructor.
apply mixed_state_trace_1 in H.
unfold trace in H. simpl in H. rewrite Cplus_0_l in H.
rewrite H.
specialize (@WF_I 1%nat) as WFI.
replace (list2D_to_matrix [[C1]]) with (I 1).
apply pure_id1.
crunch_matrix.
bdestruct (S (S x) <? 1). lia. rewrite andb_false_r. reflexivity.
Qed.
Instance Denote_Gate W1 W2 : Denote (Gate W1 W2) (Superoperator (2^⟦W1⟧) (2^⟦W2⟧)):=
{| denote := denote_gate true |}.
Instance Denote_Gate_Correct W1 W2 : Denote_Correct (Denote_Gate W1 W2) :=
{|
correctness := WF_Superoperator;
denote_correct := denote_gate_correct
|}.
(* for (i,j) ∈ l,
swaps the position of qubits i and x in the n-qubit system
*)
(* Requires: (i,j) ∈ l implies i < n and j < n *)
(* Requires: m <= n (m is structurally decreasing) *)
(* Invariant: m = length l *)
Fixpoint swap_list_aux (m n : nat) (l : list (nat * nat)) : Square (2^n) :=
match m with
| 0 => I (2^n)
| S m' => match l with
| nil => I (2^n)
| cons (a,b) xs => swap_two n a b ×
swap_list_aux m' n (map (fun z => if a =? snd z then (fst z, b) else z) xs)
end
end.
Definition zip_to (m n : nat) (l : list nat) := combine (seq m n) l.
Compute (zip_to 2 5 [1;2;3]%nat).
(* for l[i]=x, swaps the position of qubits i and x in the n-qubit system *)
(* Requires: length l <= n *)
(* Requires: x ∈ l implies x < n *)
Definition swap_list (n : nat) (l : list nat) : Square (2^n) :=
swap_list_aux n n (zip_to 0 n l).
Lemma swap_list_swap : swap_list 2 [S O] = swap.
Proof.
simpl.
unfold swap_list, swap_list_aux.
simpl.
rewrite Mmult_1_r.
apply swap_two_base.
unfold swap_two.
simpl.
rewrite kron_1_r.
auto with wf_db.
Qed.
(* Requires m < n *)
Definition pad {m} n (A : Square (2^m)) : Square (2^n) := (A ⊗ I (2^ (n - m))).
Lemma WF_pad : forall m n (A : Square m),
(m <= n)%nat ->
@WF_Matrix (2^m) (2^m) A ->
WF_Matrix (@pad m n A).
Proof.
intros. unfold pad.
apply WF_kron; auto.
rewrite <- Nat.pow_add_r.
replace (m + (n - m))%nat with n by lia.
reflexivity.
rewrite <- Nat.pow_add_r.
replace (m + (n - m))%nat with n by lia.
reflexivity.
apply WF_I.
Qed.
Lemma pad_nothing : forall m A, @pad m m A = A.
Proof.
intros.
unfold pad.
rewrite Nat.sub_diag.
Msimpl.
reflexivity.
Qed.
(* These propositions about swap_list may prove useful
Proposition swap_list_spec_1 : forall n i j (A1 : Square (2^i)%nat) (A2 : Square (2^j)%nat)
(U : Square (2^1)%nat) (ρ : Square (2^1)%nat), (i + j + 1 = n)%nat ->
super (swap_list n [i] × pad n U × (swap_list n [i])†) (A1 ⊗ ρ ⊗ A2) =
A1 ⊗ (super U ρ) ⊗ A2.
Proposition swap_list_spec_2 : forall n i j k
(A1 : Square (2^i)%nat) (A2 : Square (2^j)%nat) (A3 : Square (2^k)%nat)
(U : Square (2^2)%nat) (ρ1 ρ2 ρ1' ρ2': Square (2^1)%nat), (i + j + k + 2 = n)%nat ->
(super U (ρ1 ⊗ ρ2)) = ρ1' ⊗ ρ2' ->
super (swap_list n [i; (i+j+1)%nat] × pad n U × (swap_list n [i; (i+j+1)%nat])†)
(A1 ⊗ ρ1 ⊗ A2 ⊗ ρ2 ⊗ A3) = A1 ⊗ ρ1' ⊗ A2 ⊗ ρ2' ⊗ A3.
Proposition swap_list_shift : forall n (ρ : Square (2^1)%nat) (A : Square (2^n)),
super (swap_list (S n) (seq 1 n ++ [O])) (ρ ⊗ A) = A ⊗ ρ.
Proposition swap_list_shift' : forall (n : nat) (ρ : Square 2) (A : Square (2^n)%nat),
super (swap_list (S n) (n :: seq 0 n)) (A ⊗ ρ) = ρ ⊗ A.
*)
(* SWAP unit tests *)
(* Not quite done:
Example swap_list_test : swap ⊗ swap = (swap_list 4 [1;0;3;2]%nat).
Proof.
unfold swap_list, swap_list_aux, swap_two. simpl.
Msimpl.
*)
(***********************************)
(* Swap structures are well-formed *)
(***********************************)
(* Note that these are based on the proofs of unitarity below.
In fact, we can use the proofs of unitary in their place
(or use the WF proofs in those) *)
Lemma WF_swap_to_0_aux : forall n i,
(i + 1 < n)%nat ->
WF_Matrix (swap_to_0_aux n i).
Proof.
intros n i H.
gen n.
induction i; intros n H; simpl.
- auto with wf_db.
- replace (2^n)%nat with ((2 ^ i + (2 ^ i + 0)) * 4 * 2 ^ (n - S i - 2))%nat by
unify_pows_two.
apply WF_mult; auto with wf_db.
apply WF_mult; auto with wf_db.
unify_pows_two.
replace (i + 1 + 2 + (n - S i - 2))%nat with n by lia.
apply IHi; lia.
Qed.
Lemma WF_swap_to_0 : forall i n, (i < n)%nat -> WF_Matrix (swap_to_0 n i).
Proof.
intros i n L.
unfold swap_to_0.
destruct i; auto with wf_db.
apply WF_swap_to_0_aux.
lia.
Qed.
Lemma WF_swap_two_aux : forall n i j, (i < j < n)%nat ->
WF_Matrix (swap_two_aux n i j).
Proof.
intros n i.
gen n.
induction i.
- intros; simpl.
apply WF_swap_to_0.
lia.
- intros n j [Lij Ljn].
simpl.
destruct n; try lia.
rewrite <- (Nat.add_1_l n).
rewrite minus_plus.
apply WF_kron; unify_pows_two; auto with wf_db.
apply IHi.
lia.
Qed.
Lemma WF_swap_two : forall n i j, (i < n)%nat -> (j < n)%nat ->
WF_Matrix (swap_two n i j).
Proof.
intros n i j Lin Ljn.
unfold swap_two.
bdestruct (i =? j). apply id_unitary.
bdestruct (i <? j).
apply WF_swap_two_aux. lia.
apply WF_swap_two_aux. lia.
Qed.
Lemma WF_swap_list_aux : forall m n l,
(forall i j, In (i,j) l -> (i < n)%nat /\ (j < n)%nat) ->
(m <= n)%nat ->
WF_Matrix (swap_list_aux m n l).
Proof.
intros m n l Lall Lmn.
gen l.
induction m; intros l Lall.
- simpl. auto with wf_db.
- simpl.
destruct l.
apply id_unitary; auto with wf_db.
destruct p.
apply WF_mult.
destruct (Lall n0 n1); simpl; auto.
apply WF_swap_two; easy.
apply IHm.
lia.
intros x y IN.
split. (* This shouldn't be hard... *)
+ induction l.
easy.
destruct IN.
destruct a.
simpl in *.
destruct (n0 =? n3).
inversion H; subst. clear H.
edestruct Lall.
right. left. reflexivity.
easy.
inversion H; subst. clear H.
edestruct Lall.
right. left. reflexivity.
easy.
apply IHl.
intros.
apply Lall.
destruct H0.
left. easy.
right. right. easy.
apply H.
+ induction l.
easy.
destruct IN.
destruct a.
simpl in *.
destruct (n0 =? n3).
inversion H; subst. clear H.
edestruct Lall.
left. reflexivity.
easy.
inversion H; subst. clear H.
edestruct Lall.
right. left. reflexivity.
easy.
apply IHl.
intros.
apply Lall.
destruct H0.
left. easy.
right. right. easy.
apply H.
Qed.
Lemma WF_swap_list : forall n l, (length l <= n)%nat ->
(forall x, In x l -> x < n)%nat ->
WF_Matrix (swap_list n l).
Proof.
intros n l len Lall.
unfold swap_list.
apply WF_swap_list_aux; try lia.
intros i j IN.
split.
- unfold zip_to in *.
apply in_combine_l in IN.
apply in_seq in IN.
lia.
- unfold zip_to in *.
apply in_combine_r in IN.
apply Lall.
apply IN.
Qed.
(*******************************)
(* Swap structures are unitary *)
(*******************************)
Lemma swap_to_0_aux_unitary : forall n i,
(i + 1 < n)%nat ->
WF_Unitary (swap_to_0_aux n i).
Proof.
intros n i H.
induction i; simpl.
- specialize (kron_unitary swap (I (2^(n-2))))%nat.
replace (2 * 2 * 2^(n-2))%nat with (2^1 * 2^1 * 2^(n-2))%nat by easy.
replace (2^1 * 2^1 * 2^(n-2))%nat with (2^n)%nat by unify_pows_two.
intros KU.
apply KU.
apply swap_unitary.
apply id_unitary.
- unify_pows_two.
replace (2^n)%nat with (2^(i + 1 + 2 + (n - S i - 2)))%nat by
(apply f_equal2; trivial; try lia).
Set Printing Implicit.
apply Mmult_unitary.
apply Mmult_unitary.
rewrite Nat.pow_add_r.
apply kron_unitary.
rewrite Nat.pow_add_r.
apply kron_unitary.
apply id_unitary.
apply swap_unitary.
apply id_unitary.
replace (2^(i + 1 + 2 + (n - S i - 2)))%nat with (2^n)%nat by
(apply f_equal2; trivial; try lia).
apply IHi.
lia.
rewrite Nat.pow_add_r.
apply kron_unitary.
rewrite Nat.pow_add_r.
apply kron_unitary.
apply id_unitary.
apply swap_unitary.
apply id_unitary.
Unset Printing Implicit.
Qed.
Lemma swap_to_0_unitary : forall i n, (i < n)%nat -> WF_Unitary (swap_to_0 n i).
Proof.
intros i n L.
unfold swap_to_0.
destruct i.
apply id_unitary.
apply swap_to_0_aux_unitary.
lia.
Qed.
Lemma swap_two_aux_unitary : forall n i j, (i < j < n)%nat ->
WF_Unitary (swap_two_aux n i j).
Proof.
intros n i.
gen n.
induction i.
- intros; simpl.
apply swap_to_0_unitary.
lia.
- intros n j [Lij Ljn].
simpl.
destruct n; try lia.
rewrite <- (Nat.add_1_l n).
rewrite minus_plus.
apply kron_unitary.
apply id_unitary.
apply IHi.
lia.
Qed.
Lemma swap_two_unitary : forall n i j, (i < n)%nat -> (j < n)%nat ->
WF_Unitary (swap_two n i j).
Proof.
intros n i j Lin Ljn.
unfold swap_two.
bdestruct (i =? j). apply id_unitary.
bdestruct (i <? j).
apply swap_two_aux_unitary. lia.
apply swap_two_aux_unitary. lia.
Qed.
Lemma swap_list_aux_unitary : forall m n l,
(forall i j, In (i,j) l -> (i < n)%nat /\ (j < n)%nat) ->
(m <= n)%nat ->
WF_Unitary (swap_list_aux m n l).
Proof.
intros m n l Lall Lmn.
gen l.
induction m; intros l Lall.
- simpl.
apply id_unitary.
- simpl.
destruct l.
apply id_unitary.
destruct p.
apply Mmult_unitary.
destruct (Lall n0 n1); simpl; auto.
apply swap_two_unitary; easy.
apply IHm.
lia.
intros x y IN.
split. (* This shouldn't be hard... *)
+ induction l.
easy.
destruct IN.
destruct a.
simpl in *.
destruct (n0 =? n3).
inversion H; subst. clear H.
edestruct Lall.
right. left. reflexivity.
easy.
inversion H; subst. clear H.
edestruct Lall.
right. left. reflexivity.
easy.
apply IHl.
intros.
apply Lall.
destruct H0.
left. easy.
right. right. easy.
apply H.
+ induction l.
easy.
destruct IN.
destruct a.
simpl in *.
destruct (n0 =? n3).
inversion H; subst. clear H.
edestruct Lall.
left. reflexivity.
easy.
inversion H; subst. clear H.
edestruct Lall.
right. left. reflexivity.
easy.
apply IHl.
intros.
apply Lall.
destruct H0.
left. easy.
right. right. easy.
apply H.
Qed.
(* for l[i]=x, swaps the position of qubits i and x in the n-qubit system *)
(* Requires: length l <= n *)
(* Requires: x ∈ l implies x < n *)
Lemma swap_list_unitary : forall n l, (length l <= n)%nat ->
(forall x, In x l -> x < n)%nat ->
WF_Unitary (swap_list n l).
Proof.
intros n l len Lall.
unfold swap_list.
apply swap_list_aux_unitary; try lia.
intros i j IN.
split.
- unfold zip_to in *.
apply in_combine_l in IN.
apply in_seq in IN.
lia.
- unfold zip_to in *.
apply in_combine_r in IN.
apply Lall.
apply IN.
Qed.
(*** Applying Unitaries to Systems ***)
(* Dummy matrices and superoperators *)
Definition dummy_mat {m n} : Matrix m n. exact Zero. Qed.
Definition dummy_so {m n} : Superoperator m n. exact (fun _ => dummy_mat). Qed.
Definition super_Zero {m n} : Superoperator m n := fun _ => Zero.
(* Might be nice to make this use dummy matrices at some point.
See ctrls_to_list_empty and denote_ctrls_empty, however *)
Fixpoint ctrls_to_list {W} (lb : list bool) (l : list nat) (g : Unitary W) {struct g}:
(nat * list bool * Square 2) :=
match g with
| ctrl g' => match l with
| n :: l' => let (res,u) := ctrls_to_list lb l' g' in
let (k,lb') := res in
(k,update_at lb' n true, u)
| _ => (O,[],Zero)
end
| bit_ctrl g' => match l with
| n :: l' => let (res,u) := ctrls_to_list lb l' g' in
let (k,lb') := res in
(k,update_at lb' n true, u)
| _ => (O,[],Zero)
end
| u => match l with
| k :: l' => (k,lb,⟦u⟧)
| _ => (O,[],Zero)
end
end.
Fixpoint ctrl_list_to_unitary_r (r : list bool) (u : Square 2) :
(Square (2^(length r + 1))) :=
match r with
| false :: r' => ctrl_list_to_unitary_r r' u ⊗ I 2
| true :: r' => ctrl_list_to_unitary_r r' u ⊗ ∣1⟩⟨1∣ .+ I _ ⊗ ∣0⟩⟨0∣
| [] => u
end.
Fixpoint ctrl_list_to_unitary (l r : list bool) (u : Square 2) :
(Square (2^(length l + length r + 1))) :=
match l with
| false :: l' => I 2 ⊗ ctrl_list_to_unitary l' r u
| true :: l' => ∣1⟩⟨1∣ ⊗ ctrl_list_to_unitary l' r u .+ ∣0⟩⟨0∣ ⊗ I _
| [] => ctrl_list_to_unitary_r r u
end.
Definition denote_ctrls {W} (n : nat) (g : Unitary W) (l : list nat) : Matrix (2^n) (2^n) :=
let (res, u) := ctrls_to_list (repeat false n) l g in
let (k, lb) := res in
let ll := firstn k lb in
let lr := rev (skipn (S k) lb) in
ctrl_list_to_unitary ll lr u.
(* Apply U to qubit k in an n-qubit system *)
(* Requires: k < n *)
(*
Definition apply_qubit_unitary {n} (U : Matrix 2 2) (k : nat)
: Superoperator (2^n) (2^n) := (super (I (2^k) ⊗ U ⊗ I (2^(n-k-1)))).
*)
(* New in-place version of apply_U *)
Definition apply_unitary {W} (n : nat) (U : Unitary W) (l : list nat) : Square (2^n) :=
match W with
| Qubit => let k := (hd O l) in
I (2^k) ⊗ ⟦U⟧ ⊗ I (2 ^ (n - k -1))
| _ => denote_ctrls n U l
end.
Definition apply_U {W} (n : nat) (U : Unitary W) (l : list nat)
: Superoperator (2^n) (2^n) := super (apply_unitary n U l).
(* In case we add other multi-qubit unitaries
Fixpoint apply_U {W} (n : nat) (U : Unitary W) (l : list nat)
: Superoperator (2^n) (2^n) :=
match U with
| _H => apply_to_first (apply_qubit_unitary hadamard) l
| _X => apply_to_first (apply_qubit_unitary σx) l
| _Y => apply_to_first (apply_qubit_unitary σy) l
| _Z => apply_to_first (apply_qubit_unitary σz) l
| _R_ ϕ => apply_to_first (apply_qubit_unitary (phase_shift ϕ)) l
| ctrl g => super (denote_ctrls n U l)
| bit_ctrl g =>
end.
*)
(***********************************)
(* Lemmas about applying unitaries *)
(***********************************)
Lemma ctrl_list_to_unitary_r_false : forall n (u : Matrix 2 2),
WF_Matrix u ->
ctrl_list_to_unitary_r (repeat false n) u = u ⊗ I (2^n).
Proof.
induction n; intros.
- simpl. Msimpl. reflexivity.
- intros.
simpl.
rewrite IHn; trivial.
setoid_rewrite (kron_assoc u (I (2^n)) (I 2)); auto with wf_db.
rewrite id_kron.
unify_pows_two.
reflexivity.
Qed.
Lemma ctrl_list_to_unitary_false : forall m n (u : Matrix 2 2),
WF_Matrix u ->
ctrl_list_to_unitary (repeat false m) (repeat false n) u = I (2^m) ⊗ u ⊗ I (2^n).
Proof.
induction m; intros.
- simpl. Msimpl. apply ctrl_list_to_unitary_r_false; easy.
- simpl in *.
rewrite IHm by easy.
repeat rewrite repeat_length.
progress restore_dims.
specialize (pow_gt_0 m) as Gm.
specialize (pow_gt_0 n) as Gn.
repeat rewrite <- kron_assoc; auto with wf_db; try lia.
restore_dims.
rewrite id_kron.
reflexivity.
Qed.
Lemma ctrls_to_list_empty : forall W lb u, @ctrls_to_list W lb [] u = (O, [], Zero).
Proof. destruct u; easy. Qed.
Lemma denote_ctrls_empty : forall W (n : nat) (u : Unitary W),
denote_ctrls n u [] = Zero.
Proof. destruct u; cbv; easy. Qed.
(*
Lemma denote_ctrls_ctrl_u : forall (u : Unitary Qubit), denote_ctrls 2 (ctrl u) [0%nat;1%nat] = (control (denote u)).
Proof.
intros.
dependent destruction u.
- unfold denote_ctrls; simpl; solve_matrix.
- unfold denote_ctrls; simpl; solve_matrix.
- unfold denote_ctrls; simpl; solve_matrix.
- unfold denote_ctrls; simpl; solve_matrix.
- unfold denote_ctrls; simpl; solve_matrix.
Qed.
Lemma denote_ctrls_ctrl_u' : forall (u : Unitary Qubit), denote_ctrls 2 (ctrl u) [1%nat;0%nat] = swap × (control (denote u)) × swap.
Proof.
intros.
dependent destruction u.
- unfold denote_ctrls; simpl; solve_matrix.
- unfold denote_ctrls; simpl; solve_matrix.
- unfold denote_ctrls; simpl; solve_matrix.
- unfold denote_ctrls; simpl; solve_matrix.
- unfold denote_ctrls; simpl; solve_matrix.
Qed.
*)
Lemma denote_ctrls_qubit : forall n (u : Unitary Qubit) k,
(k < n)%nat ->
denote_ctrls n u [k] = I (2^k) ⊗ ⟦u⟧ ⊗ I (2^(n-k-1)).
Proof.
intros n u k L.
remember Qubit as W.
induction u.
Opaque rev skipn.
1-5: unfold denote_ctrls; simpl;
rewrite firstn_repeat_le, skipn_repeat, rev_repeat by lia;
rewrite ctrl_list_to_unitary_false; auto with wf_db;
rewrite Nat.sub_succ_r, Nat.sub_1_r;
reflexivity.
1-2: inversion HeqW.
Qed.
Lemma ctrl_list_to_unitary_r_unitary : forall r (u : Square 2),
WF_Unitary u -> WF_Unitary (ctrl_list_to_unitary_r r u).
Proof.
intros r u Uu.
induction r; auto.
simpl.
destruct a.
- simpl.
assert (H : forall n (U : Square n), WF_Unitary U -> WF_Unitary (U ⊗ ∣1⟩⟨1∣ .+ I n ⊗ ∣0⟩⟨0∣)).
intros n U [WFU UU].
unfold WF_Unitary.
split; auto with wf_db.
Msimpl.
rewrite Mmult_plus_distr_r, Mmult_plus_distr_l.
rewrite Mmult_plus_distr_l.
Msimpl.
rewrite UU.
replace (∣0⟩⟨0∣ × ∣1⟩⟨1∣) with (@Zero 2 2) by solve_matrix.
replace (∣1⟩⟨1∣ × ∣0⟩⟨0∣) with (@Zero 2 2) by solve_matrix.
repeat rewrite kron_0_r.
rewrite Mplus_0_r, Mplus_0_l.
rewrite <- kron_plus_distr_l.
replace (∣1⟩⟨1∣ × ∣1⟩⟨1∣ .+ ∣0⟩⟨0∣ × ∣0⟩⟨0∣) with (I 2) by solve_matrix.
rewrite id_kron.
reflexivity.
specialize (H _ (ctrl_list_to_unitary_r r u)).
rewrite Nat.mul_comm in H.
apply H.
apply IHr.
- specialize (kron_unitary _ (I 2) IHr) as H.
rewrite Nat.mul_comm in H.
apply H.
apply id_unitary.
Qed.
Lemma ctrl_list_to_unitary_unitary : forall l r (u : Square 2), WF_Unitary u ->
WF_Unitary (ctrl_list_to_unitary l r u).
Proof.
intros l r u Uu.
induction l.
- simpl. apply ctrl_list_to_unitary_r_unitary. easy.
- simpl.
destruct a.
+ simpl.
assert (H : forall n (U : Square n), WF_Unitary U -> WF_Unitary (∣1⟩⟨1∣ ⊗ U .+ ∣0⟩⟨0∣ ⊗ (I n))).
intros n U [WFU UU].
unfold WF_Unitary.
split; auto with wf_db.
Msimpl.
rewrite Mmult_plus_distr_l, Mmult_plus_distr_r.
rewrite Mmult_plus_distr_r.
Msimpl.
rewrite UU.
replace (∣0⟩⟨0∣ × ∣1⟩⟨1∣) with (@Zero 2 2) by solve_matrix.
replace (∣1⟩⟨1∣ × ∣0⟩⟨0∣) with (@Zero 2 2) by solve_matrix.
repeat rewrite kron_0_l.
rewrite Mplus_0_r, Mplus_0_l.
rewrite <- kron_plus_distr_r.
replace (∣1⟩⟨1∣ × ∣1⟩⟨1∣ .+ ∣0⟩⟨0∣ × ∣0⟩⟨0∣) with (I 2) by solve_matrix.
rewrite id_kron.
reflexivity.
specialize (H _ (ctrl_list_to_unitary l r u)).
apply H.
apply IHl.
+ specialize (kron_unitary _ _ (id_unitary 2) IHl) as H.
apply H.
Qed.
Lemma ctrls_to_list_spec : forall W l (g : Unitary W) k lb lb' u,
(length l = ⟦W⟧)%nat ->
ctrls_to_list lb l g = (k, lb', u) ->