-
Notifications
You must be signed in to change notification settings - Fork 24
/
FullGateSet.v
1293 lines (1184 loc) · 42.2 KB
/
FullGateSet.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 UnitaryListRepresentation.
Require Export QArith.
(* Other gate sets *)
Require Import IBMGateSet.
Require Import MappingGateSet.
Require Import RzQGateSet.
Require Import MappingConstraints.
Import Qreals. (* Coq version < 8.13.0 has Q2R defined in Qreals *)
(** This gate set is intended to be the "full" gate set that contains every gate
we could ever want. Optimizations are not defined directly over this set.
Instead, we define optimizations over specialized sets (e.g. RzQ, IBM) and
provide translations from this set to those sets.
The value of having a gate set that contains everything is that it removes
some opportunities for error in the OpenQASM -> SQIR parser. For instance,
the parser can directly translate "T" to the T gate in the full gate set
rather than the Rz(PI/4) gate in the RzQ gate set. This is even more important
for gates like CCX that have complicated translations to other gate sets.
To extend this gate set with new gates, you will need to modify the definitions
in this file and any optimizations defined over the full gate set (at
present, these only include definitions in Main.v). **)
Local Open Scope R_scope.
Local Open Scope Q_scope.
Ltac invert_WT :=
repeat match goal with
| H: uc_well_typed _ |- _ => inversion H; subst; clear H
end.
Ltac simpl_Reqb_and_Qeqb :=
repeat match goal with
| H : Reqb _ _ = true |- _ => apply Reqb_eq in H; rewrite H
| H : _ && _ = true |- _ => apply andb_prop in H as [? ?]
| H : Qeq_bool _ _ = true |- _ => apply Qeq_bool_iff in H;
apply RMicromega.Q2R_m in H; rewrite H
end.
Module FullGateSet <: GateSet.
Inductive Full_Unitary : nat -> Set :=
| U_I : Full_Unitary 1
| U_X : Full_Unitary 1
| U_Y : Full_Unitary 1
| U_Z : Full_Unitary 1
| U_H : Full_Unitary 1
| U_S : Full_Unitary 1
| U_T : Full_Unitary 1
| U_Sdg : Full_Unitary 1
| U_Tdg : Full_Unitary 1
| U_Rx (r : R) : Full_Unitary 1
| U_Ry (r : R) : Full_Unitary 1
| U_Rz (r : R) : Full_Unitary 1
| U_Rzq (q : Q) : Full_Unitary 1
| U_U1 (r : R) : Full_Unitary 1
| U_U2 (r : R) (r : R) : Full_Unitary 1
| U_U3 (r : R) (r : R) (r : R) : Full_Unitary 1
| U_CX : Full_Unitary 2
| U_CZ : Full_Unitary 2
| U_SWAP : Full_Unitary 2
| U_CCX : Full_Unitary 3
| U_CCZ : Full_Unitary 3.
Definition U := Full_Unitary.
(* Used for proofs -- not extracted to OCaml, so efficiency isn't a concern *)
Definition to_base {n dim} (u : U n) (qs : list nat) (pf : List.length qs = n) :=
match u with
| U_I => @SQIR.ID dim (List.nth O qs O)
| U_X => @SQIR.X dim (List.nth O qs O)
| U_Y => @SQIR.Y dim (List.nth O qs O)
| U_Z => @SQIR.Z dim (List.nth O qs O)
| U_H => @SQIR.H dim (List.nth O qs O)
| U_S => @SQIR.P dim (List.nth O qs O)
| U_T => @SQIR.T dim (List.nth O qs O)
| U_Sdg => @SQIR.PDAG dim (List.nth O qs O)
| U_Tdg => @SQIR.TDAG dim (List.nth O qs O)
| U_Rx r => @SQIR.Rx dim r (List.nth O qs O)
| U_Ry r => @SQIR.Ry dim r (List.nth O qs O)
| U_Rz r => @SQIR.Rz dim r (List.nth O qs O)
| U_Rzq q => @SQIR.Rz dim (Q2R q * PI)%R (List.nth O qs O)
| U_U1 r => @SQIR.U1 dim r (List.nth O qs O)
| U_U2 r1 r2 => @SQIR.U2 dim r1 r2 (List.nth O qs O)
| U_U3 r1 r2 r3 => @SQIR.U3 dim r1 r2 r3 (List.nth O qs O)
| U_CX => @SQIR.CNOT dim (List.nth O qs O) (List.nth (S O) qs O)
| U_CZ => @SQIR.CZ dim (List.nth O qs O) (List.nth (S O) qs O)
| U_SWAP => @SQIR.SWAP dim (List.nth O qs O) (List.nth (S O) qs O)
| U_CCX => @SQIR.CCX dim (List.nth O qs O) (List.nth (S O) qs O) (List.nth (S (S O)) qs O)
| U_CCZ => @SQIR.CCZ dim (List.nth O qs O) (List.nth (S O) qs O) (List.nth (S (S O)) qs O)
end.
Local Transparent SQIR.ID SQIR.X SQIR.Y SQIR.Z SQIR.H SQIR.Rx
SQIR.Ry SQIR.Rz SQIR.CNOT SQIR.SWAP.
Lemma to_base_only_uses_qs : forall {n} (dim : nat) (u : U n) (qs : list nat) (pf : List.length qs = n),
@only_uses _ dim (to_base u qs pf) qs.
Proof.
intros.
destruct u; simpl;
repeat constructor; apply nth_In; lia.
Qed.
Lemma to_base_WT : forall {n} (dim : nat) (u : U n) (qs : list nat) (pf : List.length qs = n),
@uc_well_typed _ dim (to_base u qs pf) <-> (bounded_list qs dim /\ List.NoDup qs).
Proof.
intros n dim u s pf.
unfold bounded_list.
split.
- intro H.
destruct u; invert_WT.
all: repeat (destruct s; simpl in *; try lia).
all: split.
all: repeat constructor; auto.
all: try (intros x [Hx | Hx]; subst; easy).
all: try (intros x [Hx | [Hx | Hx]]; subst; easy).
all: try (intros x [Hx | [Hx | [Hx | Hx]]]; subst; easy).
all: try (intro contra; destruct_In; auto).
- intros [H1 H2].
assert (aux1: (length s >= 2)%nat -> nth 0 s O <> nth 1 s O).
{ intro H. clear pf.
destruct s; [|destruct s; [|destruct s]]; simpl in H; try lia.
inversion H2; subst.
simpl.
intro contra.
contradict H4.
subst; constructor; auto.
inversion H2; subst.
simpl.
intro contra.
contradict H4.
subst; constructor; auto. }
assert (aux2: length s = 3%nat -> nth 0 s O <> nth 2 s O).
{ intro H. clear pf.
destruct s; [|destruct s; [|destruct s; [|destruct s]]]; simpl in H; try lia.
inversion H2; subst.
simpl.
intro contra.
contradict H4.
subst; right; constructor; auto. }
assert (aux3: length s = 3%nat -> nth 1 s O <> nth 2 s O).
{ intro H. clear pf.
destruct s; [|destruct s; [|destruct s; [|destruct s]]]; simpl in H; try lia.
inversion H2; subst.
simpl.
inversion H5; subst.
intro contra.
contradict H6.
subst; constructor; auto. }
destruct u; repeat constructor.
all: try apply H1.
all: try (apply nth_In; lia).
all: try (apply aux1; lia).
all: try (apply aux2; assumption).
all: try (apply aux3; assumption).
apply Nat.neq_sym.
apply aux1; lia.
Qed.
Lemma to_base_map_commutes : forall {n} (dim : nat) (u : U n) (qs : list nat) (pf : List.length qs = n) (f : nat -> nat) (pfm : List.length (map f qs) = n),
@to_base _ dim u (map f qs) pfm = map_qubits f (to_base u qs pf).
Proof.
intros n dim u qs pf f pfm.
destruct u; simpl.
all: repeat erewrite map_nth_In; try reflexivity; lia.
Qed.
Local Opaque SQIR.ID SQIR.X SQIR.Y SQIR.Z SQIR.H SQIR.Rx
SQIR.Ry SQIR.Rz SQIR.CNOT SQIR.SWAP.
Definition match_gate {n} (u u' : U n) : bool :=
match u, u' with
| U_I, U_I
| U_X, U_X
| U_Y, U_Y
| U_Z, U_Z
| U_H, U_H
| U_S, U_S
| U_T, U_T
| U_Sdg, U_Sdg
| U_Tdg, U_Tdg
| U_CX, U_CX
| U_CZ, U_CZ
| U_SWAP, U_SWAP
| U_CCX, U_CCX
| U_CCZ, U_CCZ => true
(* All rz gates are u1 gates, some u2 gates are H gates, etc.
We will not bother to check for these cases. If desired, you could
write an optimization pass that replaces gates with their equivalents. *)
| U_Rx r, U_Rx r'
| U_Ry r, U_Ry r'
| U_Rz r, U_Rz r'
| U_U1 r, U_U1 r' => Reqb r r'
| U_Rzq q, U_Rzq q' => Qeq_bool q q'
| U_U2 r1 r2, U_U2 r1' r2' => Reqb r1 r1' && Reqb r2 r2'
| U_U3 r1 r2 r3, U_U3 r1' r2' r3' => Reqb r1 r1' && Reqb r2 r2' && Reqb r3 r3'
| _, _ => false
end.
Lemma match_gate_refl : forall {n} (u : U n), match_gate u u = true.
Proof.
intros.
dependent destruction u; simpl; auto.
apply Reqb_eq; auto.
apply Reqb_eq; auto.
apply Reqb_eq; auto.
apply Qeq_bool_iff; reflexivity.
apply Reqb_eq; auto.
apply andb_true_iff.
split; apply Reqb_eq; auto.
apply andb_true_iff.
split; [apply andb_true_iff; split |]; apply Reqb_eq; auto.
Qed.
Lemma match_gate_implies_equiv : forall {n} dim (u u' : U n) (qs : list nat) (pf : List.length qs = n),
match_gate u u' = true -> uc_equiv (@to_base n dim u qs pf) (to_base u' qs pf).
Proof.
intros.
dependent destruction u; dependent destruction u'.
all: inversion H; simpl; simpl_Reqb_and_Qeqb; reflexivity.
Qed.
End FullGateSet.
Export FullGateSet.
Module FullList := UListProofs FullGateSet.
Definition full_ucom dim := ucom Full_Unitary dim.
Definition full_ucom_l dim := gate_list Full_Unitary dim.
(** Some useful gate decompositions **)
Lemma Cexp_plus_PI2 : forall x, Cexp (x + PI/2) = (Ci * Cexp x)%C.
Proof. intros. autorewrite with Cexp_db. lca. Qed.
Lemma Cexp_minus_PI2 : forall x, Cexp (x - PI/2) = (- Ci * Cexp x)%C.
Proof.
intros.
unfold Cexp.
replace (x - PI / 2)%R with (- (PI / 2 - x))%R by lra.
rewrite cos_neg, sin_neg.
rewrite cos_shift, sin_shift.
lca.
Qed.
(* All 4 cases in u3_to_rz can be solved by the same commands. *)
Ltac u3_to_rz_aux a :=
try rewrite Cexp_add;
try rewrite Cexp_plus_PI2;
try rewrite Cexp_minus_PI2;
unfold Cexp;
autorewrite with trig_db;
replace (sin a) with (sin (2 * (a/2))) by (apply f_equal; lra);
rewrite sin_2a;
apply c_proj_eq; simpl; field_simplify_eq; try nonzero;
change_to_sqr;
try rewrite sin_half_squared;
try rewrite cos_half_squared;
rewrite Rsqr_sqrt by lra;
lra.
Local Open Scope ucom_scope.
Local Close Scope Q_scope.
Local Open Scope R_scope.
Lemma u3_to_rz : forall dim a b c q,
@SQIR.U3 dim a b c q ≅ SQIR.Rz (c - (PI/2)) q ; SQIR.H q ; SQIR.Rz a q ; SQIR.H q ; SQIR.Rz (b + (PI/2)) q.
Proof.
intros.
unfold uc_cong; simpl.
exists (- (a / 2)).
autorewrite with eval_db.
bdestruct_all.
gridify.
rewrite <- Mscale_kron_dist_l.
rewrite <- Mscale_kron_dist_r.
apply f_equal2; try reflexivity.
apply f_equal2; try reflexivity.
unfold phase_shift, rotation, hadamard.
solve_matrix; u3_to_rz_aux a. (* all goals solved by ltac above *)
(* final case is ill-typed *)
Msimpl.
reflexivity.
Qed.
Lemma u2_to_rz : forall dim a b q,
@SQIR.U2 dim a b q ≡ SQIR.Rz (b - PI) q ; SQIR.H q ; SQIR.Rz a q.
Proof.
intros.
unfold uc_equiv; simpl.
autorewrite with eval_db.
gridify.
apply f_equal2; try reflexivity.
apply f_equal2; try reflexivity.
unfold phase_shift, rotation, hadamard.
solve_matrix;
try rewrite Cexp_add;
try rewrite Cexp_minus_PI;
replace (PI / 2 / 2) with (PI / 4) by lra;
autorewrite with eval_db. rewrite cos_PI4.
apply c_proj_eq; simpl; try R_field_simplify; try easy;
try apply sqrt2_neq_0.
rewrite sin_PI4.
apply c_proj_eq; simpl; try R_field_simplify; try easy;
try apply sqrt2_neq_0.
rewrite sin_PI4.
apply c_proj_eq; simpl; try R_field_simplify; try easy;
try apply sqrt2_neq_0.
rewrite cos_PI4.
apply c_proj_eq; simpl; try R_field_simplify; try easy;
try apply sqrt2_neq_0.
Qed.
Lemma rx_to_rz : forall dim a q,
@SQIR.Rx dim a q ≅ SQIR.H q ; SQIR.Rz a q ; SQIR.H q.
Proof.
intros dim a q.
assert (H: @Rx dim a q ≅ U3 a (- (PI / 2)) (PI / 2) q).
reflexivity.
rewrite H.
rewrite u3_to_rz.
replace (PI / 2 - PI / 2) with 0 by lra.
replace (- (PI / 2) + PI / 2) with 0 by lra.
apply uc_equiv_cong.
rewrite Rz_0_id.
bdestruct (q <? dim)%nat.
rewrite ID_equiv_SKIP by assumption.
rewrite SKIP_id_l, SKIP_id_r by assumption.
reflexivity.
unfold uc_equiv; simpl.
autorewrite with eval_db.
gridify.
Qed.
Lemma ry_to_rz : forall dim a q,
@SQIR.Ry dim a q ≅ SQIR.PDAG q ; SQIR.H q ; SQIR.Rz a q ; SQIR.H q ; SQIR.P q.
Proof.
intros dim a q.
assert (H: @Ry dim a q ≅ U3 a 0 0 q).
reflexivity.
rewrite H.
rewrite u3_to_rz.
replace (0 - PI / 2) with (- (PI / 2)) by lra.
replace (0 + PI / 2) with (PI / 2) by lra.
reflexivity.
Qed.
(** * Function to convert between gate sets **)
Local Open Scope Z_scope.
(* Effectively List.map, but tail recursive *)
Fixpoint change_gate_set' {dim : nat} {U1 U2 : nat -> Set}
(f : gate_app U1 dim -> gate_list U2 dim) (l : gate_list U1 dim)
(acc : gate_list U2 dim) : gate_list U2 dim :=
match l with
| [] => List.rev acc
(* technically ++ isn't tail recursive, but when the first argument (i.e. f g)
is small it shouldn't matter *)
| g :: t => change_gate_set' f t (List.rev (f g) ++ acc)
end.
Definition change_gate_set {dim : nat} {U1 U2 : nat -> Set}
(f : gate_app U1 dim -> gate_list U2 dim) (l : gate_list U1 dim)
: gate_list U2 dim :=
change_gate_set' f l [].
Lemma change_gate_set_nil : forall {dim U1 U2} f,
@change_gate_set dim U1 U2 f [] = [].
Proof. intros. reflexivity. Qed.
Lemma change_gate_set_cons : forall {dim U1 U2} f h t,
@change_gate_set dim U1 U2 f (h :: t) = f h ++ change_gate_set f t.
Proof.
intros.
assert (forall l acc, change_gate_set' f l acc = rev acc ++ change_gate_set' f l []).
{ induction l; intro acc; simpl.
rewrite app_nil_r.
reflexivity.
rewrite IHl.
rewrite (IHl (_ ++ _)).
repeat rewrite rev_app_distr.
repeat rewrite rev_involutive.
simpl.
rewrite app_assoc.
reflexivity. }
unfold change_gate_set.
simpl.
rewrite H.
rewrite rev_app_distr.
rewrite rev_involutive.
simpl.
reflexivity.
Qed.
Lemma change_gate_set_app : forall {dim U1 U2} f l1 l2,
@change_gate_set dim U1 U2 f (l1 ++ l2) = change_gate_set f l1 ++ change_gate_set f l2.
Proof.
intros.
induction l1.
reflexivity.
rewrite <- app_comm_cons.
rewrite 2 change_gate_set_cons.
rewrite IHl1.
rewrite app_assoc.
reflexivity.
Qed.
(** * IBM gate set **)
Definition full_to_IBM_u {dim} (g : gate_app Full_Unitary dim) : IBM_ucom_l dim :=
match g with
| App1 U_I m => [IBMGateSet.Rz 0 m]
| App1 U_X m => [IBMGateSet.X m]
| App1 U_Y m => [IBMGateSet.Y m]
| App1 U_Z m => [IBMGateSet.Z m]
| App1 U_H m => [IBMGateSet.H m]
| App1 U_S m => [IBMGateSet.P m]
| App1 U_T m => [IBMGateSet.T m]
| App1 U_Sdg m => [IBMGateSet.PDAG m]
| App1 U_Tdg m => [IBMGateSet.TDAG m]
| App1 (U_Rx r) m => [IBMGateSet.Rx r m]
| App1 (U_Ry r) m => [IBMGateSet.Ry r m]
| App1 (U_Rz r) m => [IBMGateSet.Rz r m]
| App1 (U_Rzq q) m => [IBMGateSet.Rz (Q2R q * PI) m]
| App1 (U_U1 r) m => [IBMGateSet.U1 r m]
| App1 (U_U2 r1 r2) m => [IBMGateSet.U2 r1 r2 m]
| App1 (U_U3 r1 r2 r3) m => [IBMGateSet.U3 r1 r2 r3 m]
| App2 U_CX m n => [IBMGateSet.CNOT m n]
| App2 U_CZ m n => IBMGateSet.CZ m n
| App2 U_SWAP m n => IBMGateSet.SWAP m n
| App3 U_CCX m n p => IBMGateSet.CCX m n p
| App3 U_CCZ m n p => IBMGateSet.CCZ m n p
| _ => [] (* unreachable *)
end.
Definition IBM_to_full_u {dim} (g : gate_app IBM_Unitary dim) : full_ucom_l dim :=
match g with
| App1 (UIBM_U1 a) m => [App1 (U_U1 a) m]
| App1 (UIBM_U2 a b) m => [App1 (U_U2 a b) m]
| App1 (UIBM_U3 a b c) m => [App1 (U_U3 a b c) m]
| App2 UIBM_CNOT m n => [App2 U_CX m n]
| _ => [] (* unreachable *)
end.
Definition full_to_IBM {dim} (l : full_ucom_l dim) : IBM_ucom_l dim :=
change_gate_set full_to_IBM_u l.
Definition IBM_to_full {dim} (l : IBM_ucom_l dim) : full_ucom_l dim :=
change_gate_set IBM_to_full_u l.
Lemma IBM_to_base1 : forall {dim} (u : IBM_Unitary 1) n,
IBMGateSet.to_base u [n] (one_elem_list n) ≡
FullList.list_to_ucom (IBM_to_full_u (@App1 _ dim u n)).
Proof.
intros dim u n.
dependent destruction u; simpl; rewrite SKIP_id_r; reflexivity.
Qed.
Lemma IBM_to_base2 : forall {dim} (u : IBM_Unitary 2) m n,
IBMGateSet.to_base u (m :: n :: []) (two_elem_list m n) ≡
FullList.list_to_ucom (IBM_to_full_u (@App2 _ dim u m n)).
Proof.
intros dim u m n.
dependent destruction u; simpl; rewrite SKIP_id_r; reflexivity.
Qed.
Lemma IBM_list_to_ucom : forall {dim} (l : IBM_ucom_l dim),
IBMList.list_to_ucom l ≡ FullList.list_to_ucom (IBM_to_full l).
Proof.
intros.
induction l.
reflexivity.
simpl.
unfold IBM_to_full.
rewrite change_gate_set_cons.
rewrite FullList.list_to_ucom_append.
destruct a.
rewrite IBM_to_base1, IHl.
reflexivity.
rewrite IBM_to_base2, IHl.
reflexivity.
dependent destruction i.
Qed.
Lemma IBM_to_full_equiv : forall {dim} (l l' : IBM_ucom_l dim),
IBMGateSet.IBMList.uc_equiv_l l l' ->
FullList.uc_equiv_l (IBM_to_full l) (IBM_to_full l').
Proof.
intros dim l l' H.
unfold FullList.uc_equiv_l.
unfold IBMGateSet.IBMList.uc_equiv_l in H.
rewrite 2 IBM_list_to_ucom in H.
assumption.
Qed.
Lemma IBM_to_full_cong : forall {dim} (l l' : IBM_ucom_l dim),
IBMGateSet.IBMList.uc_cong_l l l' ->
FullList.uc_cong_l (IBM_to_full l) (IBM_to_full l').
Proof.
intros dim l l' H.
unfold FullList.uc_equiv_l.
unfold IBMGateSet.IBMList.uc_cong_l in H.
unfold uc_cong in H.
rewrite 2 IBM_list_to_ucom in H.
assumption.
Qed.
Lemma IBM_to_full_inv : forall {dim} (l : full_ucom_l dim),
FullList.uc_equiv_l (IBM_to_full (full_to_IBM l)) l.
Proof.
intros dim l.
induction l.
reflexivity.
unfold full_to_IBM, IBM_to_full.
rewrite change_gate_set_cons.
rewrite change_gate_set_app.
rewrite IHl.
rewrite cons_to_app.
FullList.apply_app_congruence.
destruct a; dependent destruction f;
unfold change_gate_set; simpl; try reflexivity.
all: unfold FullList.uc_equiv_l; simpl;
repeat rewrite <- useq_assoc; reflexivity.
Qed.
Lemma full_to_IBM_WT : forall {dim} (l : full_ucom_l dim),
uc_well_typed_l l ->
uc_well_typed_l (full_to_IBM l).
Proof.
intros dim l WT.
unfold full_to_IBM.
induction WT.
- constructor. assumption.
- dependent destruction u; rewrite change_gate_set_cons;
simpl; try constructor; assumption.
- dependent destruction u; rewrite change_gate_set_cons;
simpl; repeat constructor; try assumption. lia.
- dependent destruction u; rewrite change_gate_set_cons;
simpl; repeat constructor; assumption.
Qed.
Lemma full_to_IBM_preserves_mapping : forall {dim} (l : full_ucom_l dim) (is_in_graph : nat -> nat -> bool),
respects_constraints_directed is_in_graph U_CX l ->
respects_constraints_directed is_in_graph UIBM_CNOT (full_to_IBM l).
Proof.
intros dim l is_in_graph H.
unfold full_to_IBM.
induction l.
constructor.
rewrite change_gate_set_cons.
inversion H; subst.
apply respects_constraints_directed_app; auto.
dependent destruction u; repeat constructor.
apply respects_constraints_directed_app; auto.
repeat constructor.
assumption.
Qed.
Lemma IBM_to_full_preserves_mapping : forall {dim} (l : IBM_ucom_l dim) (is_in_graph : nat -> nat -> bool),
respects_constraints_directed is_in_graph UIBM_CNOT l ->
respects_constraints_directed is_in_graph U_CX (IBM_to_full l).
Proof.
intros dim l is_in_graph H.
unfold IBM_to_full.
induction l.
constructor.
rewrite change_gate_set_cons.
inversion H; subst.
apply respects_constraints_directed_app; auto.
dependent destruction u; repeat constructor.
apply respects_constraints_directed_app; auto.
repeat constructor.
assumption.
Qed.
(** * RzQ gate set **)
(* A perfect real -> rational function DOES NOT exist because some real
numbers are irrational. However, I am going to assert such a function
as an axiom until we come up with a better solution. This is BAD and
maybe be a source of troubles in the future. We extract this as a function
that converts from floats to rationals (which is ok). -KH *)
Axiom R2Q : R -> Q.
Definition R2Q_PI x := R2Q (x / PI).
Axiom Q2R_R2Q_PI : forall r, (Q2R (R2Q_PI r) * PI)%R = r.
Definition Rx {dim} a q : RzQ_ucom_l dim := H q :: Rzq (R2Q_PI a) q :: H q :: [].
Definition Rz {dim} a q := @App1 _ dim (URzQ_Rz (R2Q_PI a)) q.
Definition Ry {dim} a q : RzQ_ucom_l dim :=
PDAG q :: H q :: Rzq (R2Q_PI a) q :: H q :: P q :: [].
Definition U1 {dim} a q := @Rz dim a q.
Definition U2 {dim} a b q : RzQ_ucom_l dim :=
Rzq (R2Q_PI (b - PI)) q :: H q :: Rzq (R2Q_PI a) q :: [].
Definition U3 {dim} a b c q : RzQ_ucom_l dim :=
Rzq (R2Q_PI (c - (PI/2))) q :: H q :: Rzq (R2Q_PI a) q ::
H q :: Rzq (R2Q_PI (b + (PI/2))) q :: [].
Definition full_to_RzQ_u {dim} (g : gate_app Full_Unitary dim) : RzQ_ucom_l dim :=
match g with
| App1 U_I m => [Rzq zero_Q m]
| App1 U_X m => [RzQGateSet.X m]
| App1 U_Y m => RzQGateSet.Y m
| App1 U_Z m => [RzQGateSet.Z m]
| App1 U_H m => [RzQGateSet.H m]
| App1 U_S m => [RzQGateSet.P m]
| App1 U_T m => [RzQGateSet.T m]
| App1 U_Sdg m => [RzQGateSet.PDAG m]
| App1 U_Tdg m => [RzQGateSet.TDAG m]
| App1 (U_Rx r) m => Rx r m
| App1 (U_Ry r) m => Ry r m
| App1 (U_Rz r) m => [Rz r m]
| App1 (U_Rzq q) m => [RzQGateSet.Rzq q m]
| App1 (U_U1 r) m => [U1 r m]
| App1 (U_U2 r1 r2) m => U2 r1 r2 m
| App1 (U_U3 r1 r2 r3) m => U3 r1 r2 r3 m
| App2 U_CX m n => [RzQGateSet.CNOT m n]
| App2 U_CZ m n => RzQGateSet.CZ m n
| App2 U_SWAP m n => RzQGateSet.SWAP m n
| App3 U_CCX m n p => RzQGateSet.CCX m n p
| App3 U_CCZ m n p => RzQGateSet.CCZ m n p
| _ => [] (* unreachable *)
end.
Definition RzQ_to_full_u {dim} (g : gate_app RzQ_Unitary dim) : full_ucom_l dim :=
match g with
| App1 URzQ_H m => [App1 U_H m]
| App1 URzQ_X m => [App1 U_X m]
| App1 (URzQ_Rz q) m => [App1 (U_Rzq q) m]
| App2 URzQ_CNOT m n => [App2 U_CX m n]
| _ => [] (* unreachable *)
end.
Definition full_to_RzQ {dim} (l : full_ucom_l dim) : RzQ_ucom_l dim :=
change_gate_set full_to_RzQ_u l.
Definition RzQ_to_full {dim} (l : RzQ_ucom_l dim) : full_ucom_l dim :=
change_gate_set RzQ_to_full_u l.
Lemma RzQ_to_base1 : forall {dim} (u : RzQ_Unitary 1) n,
RzQGateSet.to_base u [n] (one_elem_list n) ≡
FullList.list_to_ucom (RzQ_to_full_u (@App1 _ dim u n)).
Proof.
intros dim u n.
dependent destruction u; simpl; rewrite SKIP_id_r; reflexivity.
Qed.
Lemma RzQ_to_base2 : forall {dim} (u : RzQ_Unitary 2) m n,
RzQGateSet.to_base u (m :: n :: []) (two_elem_list m n) ≡
FullList.list_to_ucom (RzQ_to_full_u (@App2 _ dim u m n)).
Proof.
intros dim u m n.
dependent destruction u; simpl; rewrite SKIP_id_r; reflexivity.
Qed.
Lemma RzQ_list_to_ucom : forall {dim} (l : RzQ_ucom_l dim),
RzQList.list_to_ucom l ≡ FullList.list_to_ucom (RzQ_to_full l).
Proof.
intros.
induction l.
reflexivity.
simpl.
unfold RzQ_to_full.
rewrite change_gate_set_cons.
rewrite FullList.list_to_ucom_append.
destruct a.
rewrite RzQ_to_base1, IHl.
reflexivity.
rewrite RzQ_to_base2, IHl.
reflexivity.
dependent destruction r.
Qed.
Lemma RzQ_to_full_equiv : forall {dim} (l l' : RzQ_ucom_l dim),
RzQGateSet.RzQList.uc_equiv_l l l' ->
FullList.uc_equiv_l (RzQ_to_full l) (RzQ_to_full l').
Proof.
intros dim l l' H.
unfold FullList.uc_equiv_l.
unfold RzQGateSet.RzQList.uc_equiv_l in H.
rewrite 2 RzQ_list_to_ucom in H.
assumption.
Qed.
Lemma RzQ_to_full_cong : forall {dim} (l l' : RzQ_ucom_l dim),
RzQGateSet.RzQList.uc_cong_l l l' ->
FullList.uc_cong_l (RzQ_to_full l) (RzQ_to_full l').
Proof.
intros dim l l' H.
unfold FullList.uc_equiv_l.
unfold RzQGateSet.RzQList.uc_cong_l in H.
unfold uc_cong in H.
rewrite 2 RzQ_list_to_ucom in H.
assumption.
Qed.
Local Open Scope R.
Lemma Q2R_1_4_PI : forall {dim} q,
@SQIR.Rz dim (Q2R (1 / 4) * PI) q ≡ SQIR.Rz (PI / 4) q.
Proof.
intros dim q.
unfold Q2R; simpl.
autorewrite with R_db.
rewrite Rmult_comm.
reflexivity.
Qed.
Lemma Q2R_7_4_PI : forall {dim} q,
@SQIR.Rz dim (Q2R (7 / 4) * PI) q ≡ SQIR.Rz (- (PI / 4)) q.
Proof.
intros dim q.
unfold Q2R; simpl.
unfold uc_equiv; autorewrite with eval_db; try lia.
gridify.
replace (7 * / 4 * PI) with (2 * PI + - (PI / 4)) by lra.
rewrite <- phase_mul.
rewrite phase_2pi.
Msimpl.
reflexivity.
Qed.
Lemma Q2R_1_2_PI : forall {dim} q,
@SQIR.Rz dim (Q2R (1 / 2) * PI) q ≡ SQIR.Rz (PI / 2) q.
Proof.
intros dim q.
unfold Q2R; simpl.
autorewrite with R_db.
rewrite Rmult_comm.
reflexivity.
Qed.
Lemma Q2R_3_2_PI : forall {dim} q,
@SQIR.Rz dim (Q2R (3 / 2) * PI) q ≡ SQIR.Rz (- (PI / 2)) q.
Proof.
intros dim q.
unfold Q2R; simpl.
unfold uc_equiv; autorewrite with eval_db; try lia.
gridify.
replace (3 * / 2 * PI) with (2 * PI + - (PI / 2)) by lra.
rewrite <- phase_mul.
rewrite phase_2pi.
Msimpl.
reflexivity.
Qed.
Lemma Q2R_1_PI : Q2R 1 * PI = PI.
Proof. unfold Q2R; simpl. lra. Qed.
Lemma RzQ_to_full_inv : forall {dim} (l : full_ucom_l dim),
FullList.uc_cong_l (RzQ_to_full (full_to_RzQ l)) l.
Proof.
intros dim l.
induction l.
reflexivity.
unfold full_to_RzQ, RzQ_to_full.
rewrite change_gate_set_cons.
rewrite change_gate_set_app.
rewrite IHl.
rewrite cons_to_app.
FullList.apply_app_congruence_cong.
destruct a; dependent destruction f;
unfold change_gate_set; simpl; try reflexivity.
all: unfold FullList.uc_cong_l; simpl; repeat rewrite <- uc_cong_assoc.
all: unfold one_Q, half_Q, three_halves_Q, quarter_Q, seven_quarters_Q.
all: try (apply uc_equiv_cong;
repeat rewrite Q2R_1_2_PI; repeat rewrite Q2R_3_2_PI;
repeat rewrite Q2R_1_4_PI; repeat rewrite Q2R_7_4_PI;
try rewrite Q2R_1_PI; repeat rewrite Q2R_R2Q_PI; reflexivity).
(* U_I *)
unfold zero_Q.
rewrite RMicromega.Q2R_0, Rmult_0_l.
apply uc_equiv_cong.
rewrite Rz_0_id.
reflexivity.
(* U_Y *)
apply uc_equiv_cong.
rewrite Q2R_1_2_PI, Q2R_3_2_PI.
rewrite 2 SKIP_id_r.
unfold uc_equiv; simpl.
autorewrite with eval_db; try lia.
gridify.
do 2 (apply f_equal2; try reflexivity).
solve_matrix; autorewrite with Cexp_db; lca.
(* U_Rx *)
rewrite rx_to_rz.
apply uc_equiv_cong.
rewrite Q2R_R2Q_PI.
reflexivity.
(* U_Ry *)
rewrite ry_to_rz.
apply uc_equiv_cong.
rewrite Q2R_1_2_PI, Q2R_3_2_PI, Q2R_R2Q_PI.
reflexivity.
(* U_U2 *)
apply uc_equiv_cong.
rewrite 2 Q2R_R2Q_PI.
rewrite u2_to_rz.
reflexivity.
(* U_U3 *)
rewrite u3_to_rz.
apply uc_equiv_cong.
repeat rewrite Q2R_R2Q_PI.
reflexivity.
Qed.
Lemma full_to_RzQ_WT : forall {dim} (l : full_ucom_l dim),
uc_well_typed_l l ->
uc_well_typed_l (full_to_RzQ l).
Proof.
intros dim l WT.
unfold full_to_RzQ.
induction WT.
- constructor. assumption.
- dependent destruction u; rewrite change_gate_set_cons;
simpl; repeat constructor; assumption.
- dependent destruction u; rewrite change_gate_set_cons;
simpl; repeat constructor; try assumption. lia.
- dependent destruction u; rewrite change_gate_set_cons;
simpl; repeat constructor; assumption.
Qed.
Lemma full_to_RzQ_preserves_mapping : forall {dim} (l : full_ucom_l dim) (is_in_graph : nat -> nat -> bool),
respects_constraints_directed is_in_graph U_CX l ->
respects_constraints_directed is_in_graph URzQ_CNOT (full_to_RzQ l).
Proof.
intros dim l is_in_graph H.
unfold full_to_RzQ.
induction l.
constructor.
rewrite change_gate_set_cons.
inversion H; subst.
apply respects_constraints_directed_app; auto.
dependent destruction u; repeat constructor.
apply respects_constraints_directed_app; auto.
repeat constructor.
assumption.
Qed.
Lemma RzQ_to_full_preserves_mapping : forall {dim} (l : RzQ_ucom_l dim) (is_in_graph : nat -> nat -> bool),
respects_constraints_directed is_in_graph URzQ_CNOT l ->
respects_constraints_directed is_in_graph U_CX (RzQ_to_full l).
Proof.
intros dim l is_in_graph H.
unfold RzQ_to_full.
induction l.
constructor.
rewrite change_gate_set_cons.
inversion H; subst.
apply respects_constraints_directed_app; auto.
dependent destruction u; repeat constructor.
apply respects_constraints_directed_app; auto.
repeat constructor.
assumption.
Qed.
(** * Mapping gate set **)
Definition decompose_to_cnot_and_swap_u {dim} (g : gate_app Full_Unitary dim) : full_ucom_l dim :=
match g with
| App2 U_CZ m n => App1 U_H n :: App2 U_CX m n :: App1 U_H n :: []
| App3 U_CCX m n p => App1 U_H p :: App2 U_CX n p :: App1 U_Tdg p ::
App2 U_CX m p :: App1 U_T p :: App2 U_CX n p ::
App1 U_Tdg p :: App2 U_CX m p :: App2 U_CX m n ::
App1 U_Tdg n :: App2 U_CX m n :: App1 U_T m ::
App1 U_T n :: App1 U_T p :: App1 U_H p :: []
| App3 U_CCZ m n p => App2 U_CX n p :: App1 U_Tdg p ::
App2 U_CX m p :: App1 U_T p :: App2 U_CX n p ::
App1 U_Tdg p :: App2 U_CX m p :: App2 U_CX m n ::
App1 U_Tdg n :: App2 U_CX m n :: App1 U_T m ::
App1 U_T n :: App1 U_T p :: []
| g => [g]
end.
Definition full_to_map_u {dim} (g : gate_app Full_Unitary dim) : gate_app (Map_Unitary (Full_Unitary 1)) dim :=
match g with
| App1 u m => App1 (UMap_U u) m
| App2 U_CX m n => App2 UMap_CNOT m n
| App2 U_SWAP m n => App2 UMap_SWAP m n
| _ => App1 (UMap_U U_I) 0 (* unreachable *)
end.
Definition full_to_map {dim} (l : full_ucom_l dim) : map_ucom_l (Full_Unitary 1) dim :=
change_gate_set (fun g => map full_to_map_u (decompose_to_cnot_and_swap_u g)) l.
Definition map_to_full_u {dim} (g : gate_app (Map_Unitary (Full_Unitary 1)) dim) : full_ucom_l dim :=
match g with
| App1 (UMap_U u) m => [App1 u m]
| App2 UMap_CNOT m n => [App2 U_CX m n]
| App2 UMap_SWAP m n => [App2 U_SWAP m n]
| _ => [] (* unreachable *)
end.
Definition map_to_full {dim} (l : map_ucom_l (Full_Unitary 1) dim) : full_ucom_l dim :=
change_gate_set map_to_full_u l.
Lemma map_to_full_inv : forall {dim} (l : full_ucom_l dim),
FullList.uc_equiv_l (map_to_full (full_to_map l)) l.
Proof.
intros dim l.
induction l.
reflexivity.
unfold full_to_map, map_to_full.
rewrite change_gate_set_cons.
rewrite change_gate_set_app.
rewrite IHl.
rewrite cons_to_app.
FullList.apply_app_congruence.
destruct a; dependent destruction f;
unfold change_gate_set; simpl; try reflexivity.
all: unfold FullList.uc_equiv_l; simpl;
repeat rewrite <- useq_assoc; reflexivity.
Qed.
Lemma full_to_map_WT : forall {dim} (l : full_ucom_l dim),
uc_well_typed_l l ->
uc_well_typed_l (full_to_map l).
Proof.
intros dim l WT.
unfold full_to_map.
induction WT.
- constructor. assumption.
- dependent destruction u; rewrite change_gate_set_cons;
simpl; repeat constructor; assumption.
- dependent destruction u; rewrite change_gate_set_cons;
simpl; repeat constructor; assumption.
- dependent destruction u; rewrite change_gate_set_cons;
simpl; repeat constructor; assumption.
Qed.
Lemma map_to_full_preserves_mapping_undirected : forall {dim} (l : map_ucom_l (Full_Unitary 1) dim) (is_in_graph : nat -> nat -> bool),
respects_constraints_undirected is_in_graph l ->
respects_constraints_undirected is_in_graph (map_to_full l).
Proof.
intros dim l is_in_graph H.
unfold map_to_full.
induction l.
constructor.
rewrite change_gate_set_cons.
inversion H; subst.
apply respects_constraints_undirected_app; auto.
dependent destruction u; repeat constructor.
apply respects_constraints_undirected_app; auto.
dependent destruction u; constructor.
assumption. constructor. assumption. constructor.
Qed.
Lemma map_to_full_preserves_mapping_directed : forall {dim} (l : map_ucom_l (Full_Unitary 1) dim) (is_in_graph : nat -> nat -> bool),
respects_constraints_directed is_in_graph UMap_CNOT l ->
respects_constraints_directed is_in_graph U_CX (map_to_full l).
Proof.
intros dim l is_in_graph H.
unfold map_to_full.
induction l.
constructor.
rewrite change_gate_set_cons.
inversion H; subst.
apply respects_constraints_directed_app; auto.
dependent destruction u; repeat constructor.
apply respects_constraints_directed_app; auto.
repeat constructor.
assumption.
Qed.
Lemma full_to_map_preserves_mapping_undirected : forall {dim} (l : full_ucom_l dim) (is_in_graph : nat -> nat -> bool),
respects_constraints_undirected is_in_graph l ->
respects_constraints_undirected is_in_graph (full_to_map l).
Proof.
intros dim l is_in_graph H.
unfold full_to_map.
induction l.
constructor.
rewrite change_gate_set_cons.
inversion H; subst.
apply respects_constraints_undirected_app; auto.
dependent destruction u; repeat constructor.
apply respects_constraints_undirected_app; auto.
dependent destruction u; repeat apply res_und_app2; try assumption.
constructor.
constructor. constructor. assumption. constructor. constructor.
constructor.
Qed.
(** * Check that every gate in the program satisfies some predicate **)
Definition forall_gates {U : nat -> Set} {dim} (p : gate_app U dim -> Prop) (l : gate_list U dim) :=
forall g, In g l -> p g.
Lemma forall_gates_drop : forall {U : nat -> Set} {dim} (p : gate_app U dim -> Prop) (g : gate_app U dim) (l : gate_list U dim),
forall_gates p (g :: l) -> forall_gates p l.
Proof.
intros U dim p g l H.
unfold forall_gates in *.
intros g0 Hg0.
apply H.
right.
assumption.
Qed.
Lemma forall_gates_extend : forall {U : nat -> Set} {dim} (p : gate_app U dim -> Prop) (g : gate_app U dim) (l : gate_list U dim),
p g -> forall_gates p l -> forall_gates p (g :: l).
Proof.
intros U dim p g l Hg Hl.