Skip to content

Commit 7b67629

Browse files
committed
perf: Minor improvements.
1 parent f225de0 commit 7b67629

File tree

7 files changed

+75
-93
lines changed

7 files changed

+75
-93
lines changed

Appendix/A3_Swaps.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ Proof.
4949
rewrite control_decomp.
5050
rewrite Mmult_plus_distr_l.
5151
rewrite Mmult_plus_distr_r.
52-
repeat rewrite kron_mixed_product.
52+
repeat rewrite (@kron_mixed_product 2 2 2 4 4 4).
5353
repeat rewrite Mmult_1_l.
5454
repeat rewrite Mmult_1_r.
55-
rewrite swap_swap.
55+
rewrite swap_swap at 1.
5656
assert (swap × control (diag2 C1 c1) × swap = control (diag2 C1 c1)) by lma'.
57-
rewrite H.
57+
rewrite H at 1.
5858
all: solve_WF_matrix.
5959
Qed.
6060

Appendix/A5_ControlledUnitaries.v

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,10 @@ assert (U_block_decomp: exists (P0 P1 : Square 2), U = P0 ⊗ ∣0⟩⟨0∣ .+
245245
assert (swap_inverse_helper: swap × (swap × U × swap) × swap = U).
246246
{
247247
repeat rewrite <- Mmult_assoc.
248-
rewrite swap_swap.
249-
rewrite Mmult_1_l. 2: apply U_unitary.
250248
rewrite Mmult_assoc.
251-
(* TODO: Figure out why swap_swap doesn't work here *)
252-
lma'; solve_WF_matrix.
249+
rewrite swap_swap.
250+
rewrite swap_swap at 1.
251+
rewrite Mmult_1_l, Mmult_1_r; solve_WF_matrix.
253252
}
254253
rewrite swap_inverse_helper in SUS_block_decomp.
255254
rewrite SUS_block_decomp.

Appendix/A6_TensorProducts.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ assert (outer_prod_equiv : acgate U × (a ⊗ b ⊗ g) × (a ⊗ b ⊗ g)† ×
613613
rewrite <- Mmult_adjoint.
614614
assert (app_helper: acgate U × (a ⊗ b ⊗ g) = psi ⊗ phi). apply acU_app.
615615
rewrite app_helper at 1. clear app_helper.
616-
rewrite kron_adjoint.
616+
rewrite (@kron_adjoint 2 1 4 1).
617617
reflexivity.
618618
}
619619
(* trace out ac qubits *)

Appendix/A7_OtherProperties.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ rewrite <- acv3_id at 1.
285285
rewrite <- Mmult_1_r with (A := acgate V3). 2: apply WF_acgate. 2: assumption.
286286
assert (temp: WF_Unitary (bcgate V4)†). apply adjoint_unitary. apply bcgate_unitary. assumption.
287287
destruct temp as [WF_bcv4dag bcv4dag_inv].
288-
replace (2*2)%nat with 4%nat by lia.
288+
replace 8%nat with (2 * 4)%nat by lia.
289289
rewrite <- bcv4dag_inv.
290290
rewrite adjoint_involutive.
291291
repeat rewrite <- Mmult_assoc.

Helpers/GateHelpers.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,16 @@ apply (f_equal (fun f => swapbc × f)) in acgate_c.
586586
repeat rewrite <- Mmult_assoc in acgate_c.
587587
rewrite swapbc_inverse in acgate_c. rewrite Mmult_1_l in acgate_c. 2: apply WF_abgate; solve_WF_matrix.
588588
rewrite Mmult_plus_distr_l in acgate_c.
589-
unfold swapbc in acgate_c at 2. rewrite kron_mixed_product in acgate_c.
590-
unfold swapbc in acgate_c at 2. rewrite kron_mixed_product in acgate_c.
589+
unfold swapbc in acgate_c at 2. rewrite (@kron_mixed_product 2 2 2 4 4 4) in acgate_c.
590+
unfold swapbc in acgate_c at 2. rewrite (@kron_mixed_product 2 2 2 4 4 4) in acgate_c.
591591
rewrite Mmult_1_l in acgate_c. 2: solve_WF_matrix.
592592
rewrite Mmult_1_l in acgate_c. 2: solve_WF_matrix.
593593
apply (f_equal (fun f => f × swapbc)) in acgate_c.
594594
rewrite Mmult_assoc in acgate_c.
595595
rewrite swapbc_inverse in acgate_c at 1. rewrite Mmult_1_r in acgate_c. 2: apply WF_abgate; solve_WF_matrix.
596596
rewrite Mmult_plus_distr_r in acgate_c.
597-
unfold swapbc in acgate_c at 2. rewrite kron_mixed_product in acgate_c.
598-
unfold swapbc in acgate_c at 1. rewrite kron_mixed_product in acgate_c.
597+
unfold swapbc in acgate_c at 2. rewrite (@kron_mixed_product 2 2 2 4 4 4) in acgate_c.
598+
unfold swapbc in acgate_c at 1. rewrite (@kron_mixed_product 2 2 2 4 4 4) in acgate_c.
599599
rewrite Mmult_1_r in acgate_c. 2: solve_WF_matrix.
600600
rewrite Mmult_1_r in acgate_c. 2: solve_WF_matrix.
601601
assert (swapW0_unit: WF_Unitary (swap × W0 × swap)).
@@ -1229,10 +1229,10 @@ acgate U = ∣0⟩⟨0∣ ⊗ TL .+ ∣1⟩⟨1∣ ⊗ BR).
12291229
rewrite abs.
12301230
unfold swapbc.
12311231
rewrite Mmult_plus_distr_l.
1232-
repeat rewrite kron_mixed_product.
1232+
repeat rewrite (@kron_mixed_product 2 2 2 4 4 4).
12331233
repeat rewrite Mmult_1_l. 2,3: solve_WF_matrix.
12341234
rewrite Mmult_plus_distr_r.
1235-
repeat rewrite kron_mixed_product.
1235+
repeat rewrite (@kron_mixed_product 2 2 2 4 4 4).
12361236
repeat rewrite Mmult_1_r. 2,3: solve_WF_matrix.
12371237
reflexivity.
12381238
}
@@ -1256,7 +1256,7 @@ rewrite Mmult_assoc in zeropassthrough.
12561256
rewrite swapbc_3q in zeropassthrough. 2: solve_WF_matrix. 2: apply x_qubit. 2: apply y_qubit.
12571257
rewrite zeropassthrough at 1.
12581258
unfold swapbc.
1259-
rewrite kron_mixed_product.
1259+
rewrite (@kron_mixed_product 2 2 1 4 4 1).
12601260
rewrite Mmult_1_l. 2: solve_WF_matrix.
12611261
reflexivity.
12621262
Qed.

Helpers/SwapHelpers.v

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Require Import QuantumLib.Quantum.
33
Require Import WFHelpers.
44
Require Import MatrixHelpers.
55

6-
Definition swapab := swap ⊗ I 2.
7-
Definition swapbc := I 2 ⊗ swap.
8-
Definition swapac := swapab × swapbc × swapab.
6+
Definition swapab : Square 8 := swap ⊗ I 2.
7+
Definition swapbc : Square 8 := I 2 ⊗ swap.
8+
Definition swapac : Square 8 := swapab × swapbc × swapab.
99

1010
#[global] Hint Unfold swapab swapbc swapac : M_db.
1111

@@ -28,68 +28,57 @@ Qed.
2828

2929
Lemma swapab_unitary : WF_Unitary swapab.
3030
Proof.
31-
autounfold with M_db; auto with unit_db.
31+
solve_WF_matrix.
3232
Qed.
3333

3434
Lemma swapbc_unitary : WF_Unitary swapbc.
3535
Proof.
36-
autounfold with M_db; auto with unit_db.
36+
solve_WF_matrix.
3737
Qed.
3838

3939
Lemma swapac_unitary : WF_Unitary swapac.
4040
Proof.
41-
apply Mmult_unitary.
42-
apply Mmult_unitary.
43-
apply swapab_unitary.
44-
apply swapbc_unitary.
45-
apply swapab_unitary.
41+
solve_WF_matrix.
4642
Qed.
4743

4844
#[export] Hint Resolve swapab_unitary swapbc_unitary swapac_unitary : unit_db.
4945

5046
Lemma swapab_inverse : swapab × swapab = I 8.
5147
Proof.
5248
unfold swapab.
53-
rewrite kron_mixed_product, swap_swap.
54-
Msimpl_light.
55-
rewrite id_kron.
56-
replace (2 * 2 * 2)%nat with 8%nat by lia.
49+
rewrite (@kron_mixed_product 4 4 4 2 2 2).
50+
rewrite swap_swap at 1.
51+
rewrite Mmult_1_l, id_kron.
5752
reflexivity.
53+
apply WF_I.
5854
Qed.
5955

6056
Lemma swapbc_inverse : swapbc × swapbc = I 8.
6157
Proof.
6258
unfold swapbc.
63-
rewrite kron_mixed_product, swap_swap.
64-
Msimpl_light.
65-
rewrite id_kron.
66-
replace (2 * (2 * 2))%nat with 8%nat by lia.
59+
rewrite (@kron_mixed_product 2 2 2 4 4 4).
60+
rewrite swap_swap at 1.
61+
rewrite Mmult_1_l, id_kron.
6762
reflexivity.
63+
apply WF_I.
6864
Qed.
6965

7066
Lemma swapac_inverse : swapac × swapac = I 8.
7167
Proof.
72-
apply mat_equiv_eq.
73-
apply WF_mult. apply WF_swapac. apply WF_swapac.
74-
apply WF_I.
7568
unfold swapac.
7669
repeat rewrite Mmult_assoc.
77-
rewrite <- Mmult_assoc with (A := swapab) (B := swapab) (C:= swapbc × swapab).
78-
rewrite <- Mmult_assoc with (A := swapbc) (B := swapab × swapab) (C:= swapbc × swapab).
79-
rewrite swapab_inverse.
80-
rewrite Mmult_1_r. 2: apply WF_swapbc.
81-
rewrite <- Mmult_assoc with (A := swapbc) (B:= swapbc) (C:=swapab).
82-
rewrite <- Mmult_assoc with (A := swapab) (B:= swapbc × swapbc) (C:=swapab).
83-
rewrite swapbc_inverse.
84-
rewrite Mmult_1_r. 2: apply WF_swapab.
85-
rewrite <- swapab_inverse.
86-
apply mat_equiv_refl.
70+
rewrite <- Mmult_assoc with (A := swapab) (B := swapab).
71+
rewrite swapab_inverse, Mmult_1_l.
72+
rewrite <- Mmult_assoc with (A := swapbc).
73+
rewrite swapbc_inverse, Mmult_1_l.
74+
exact swapab_inverse.
75+
all: solve_WF_matrix.
8776
Qed.
8877

8978
Lemma swapab_hermitian : swapab† = swapab.
9079
Proof.
9180
unfold swapab.
92-
rewrite kron_adjoint.
81+
rewrite (@kron_adjoint 4 4 2 2).
9382
rewrite swap_hermitian, id_adjoint_eq.
9483
reflexivity.
9584
Qed.
@@ -116,7 +105,7 @@ WF_Matrix a -> WF_Matrix b -> WF_Matrix c ->
116105
Proof.
117106
intros.
118107
unfold swapab.
119-
rewrite kron_mixed_product.
108+
rewrite (@kron_mixed_product 4 4 1 2 2 1).
120109
rewrite Mmult_1_l. 2: assumption.
121110
rewrite swap_2q. 2,3: assumption.
122111
reflexivity.
@@ -128,26 +117,21 @@ Lemma swapab_3gate : forall (A B C : Square 2),
128117
Proof.
129118
intros.
130119
unfold swapab.
131-
rewrite kron_mixed_product.
132-
rewrite Mmult_1_l. 2: assumption.
133-
rewrite kron_mixed_product.
134-
rewrite Mmult_1_r. 2: assumption.
135-
rewrite swap_2gate. 2: assumption. 2: assumption.
136-
reflexivity.
120+
do 2 rewrite (@kron_mixed_product 4 4 4 2 2 2).
121+
rewrite swap_2gate, Mmult_1_l, Mmult_1_r.
122+
all: solve_WF_matrix.
137123
Qed.
138124

139125
Lemma swapbc_3q : forall (a b c : Vector 2),
140126
WF_Matrix a -> WF_Matrix b -> WF_Matrix c ->
141127
swapbc × (a ⊗ b ⊗ c) = (a ⊗ c ⊗ b).
142128
Proof.
143-
intros.
144-
unfold swapbc.
145-
rewrite kron_assoc. 2,3,4: assumption.
146-
rewrite kron_mixed_product.
147-
rewrite Mmult_1_l. 2: assumption.
148-
rewrite swap_2q. 2,3: assumption.
149-
rewrite kron_assoc. 2,3,4: assumption.
150-
reflexivity.
129+
intros.
130+
unfold swapbc.
131+
repeat rewrite kron_assoc.
132+
rewrite (@kron_mixed_product 2 2 1 4 4 1).
133+
rewrite swap_2q, Mmult_1_l.
134+
all: solve_WF_matrix.
151135
Qed.
152136

153137
Lemma swapbc_3gate : forall (A B C : Square 2),
@@ -193,7 +177,7 @@ Qed.
193177
Lemma swapbc_sa: swapbc = (swapbc) †.
194178
Proof.
195179
unfold swapbc.
196-
rewrite kron_adjoint.
180+
rewrite (@kron_adjoint 2 2 4 4).
197181
rewrite id_adjoint_eq.
198182
rewrite swap_hermitian.
199183
reflexivity.

Main.v

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,24 @@ Proof.
13741374
rewrite <- kron_plus_distr_l.
13751375
unfold beta, beta_perp.
13761376
rewrite a8; auto.
1377+
assert (forall {n}, I 2 ⊗ I n = I n .⊕ I n).
1378+
{
1379+
intro n.
1380+
rewrite <- Mplus01, kron_plus_distr_r, <- (direct_sum_decomp _ _ 0 0).
1381+
all: solve_WF_matrix.
1382+
}
1383+
unfold ccu.
1384+
rewrite kron_assoc.
1385+
rewrite id_kron.
1386+
rewrite H2, control_direct_sum.
1387+
rewrite direct_sum_simplify.
1388+
split. reflexivity.
1389+
rewrite <- id_kron.
1390+
rewrite H2, control_direct_sum.
1391+
rewrite direct_sum_simplify.
1392+
split. reflexivity.
13771393
lma'.
1394+
all: solve_WF_matrix.
13781395
}
13791396
Qed.
13801397

@@ -1825,9 +1842,9 @@ Proof.
18251842
rewrite <- a12 with (U := V4).
18261843
repeat rewrite Mmult_adjoint.
18271844
repeat rewrite <- Mmult_assoc.
1828-
(* PERF: Why does it lag here? *)
1829-
repeat rewrite swapab_hermitian at 1.
1830-
rewrite swapab_inverse at 1.
1845+
restore_dims.
1846+
repeat rewrite swapab_hermitian.
1847+
rewrite swapab_inverse.
18311848
rewrite Mmult_1_l.
18321849
repeat rewrite Mmult_assoc.
18331850
rewrite <- Mmult_assoc with (A := swapab).
@@ -1888,8 +1905,9 @@ Proof.
18881905
rewrite <- a12 with (U := V4).
18891906
repeat rewrite Mmult_adjoint.
18901907
repeat rewrite <- Mmult_assoc.
1891-
repeat rewrite swapab_hermitian at 1.
1892-
rewrite swapab_inverse at 1.
1908+
restore_dims.
1909+
repeat rewrite swapab_hermitian.
1910+
rewrite swapab_inverse.
18931911
rewrite Mmult_1_l.
18941912
repeat rewrite Mmult_assoc.
18951913
rewrite <- Mmult_assoc with (A := swapab).
@@ -2323,7 +2341,8 @@ Proof.
23232341
{
23242342
repeat rewrite Mmult_assoc.
23252343
repeat rewrite <- Mmult_assoc with (A := swapab) (B := swapab).
2326-
repeat rewrite swapab_inverse at 1.
2344+
restore_dims.
2345+
repeat rewrite swapab_inverse.
23272346
repeat rewrite Mmult_1_l.
23282347
rewrite H1 at 1; clear H1.
23292348
repeat rewrite <- Mmult_assoc.
@@ -2334,7 +2353,7 @@ Proof.
23342353
do 2 rewrite a12 in H2.
23352354
unfold swapab in H2.
23362355
rewrite Mmult_assoc with (A := bcgate U1) in H2.
2337-
rewrite kron_mixed_product in H2.
2356+
rewrite (@kron_mixed_product 4 4 1 2 2 1) in H2.
23382357
rewrite Mmult_1_l in H2.
23392358
rewrite a10 in H2.
23402359
rewrite H2 at 1; clear H2.
@@ -2346,23 +2365,13 @@ Proof.
23462365
rewrite <- Mmult_assoc with (A := swapab).
23472366
rewrite swapab_inverse at 1.
23482367
unfold swapab.
2349-
rewrite kron_mixed_product.
2368+
rewrite (@kron_mixed_product 4 4 1 2 2 1).
23502369
rewrite a10.
23512370
Msimpl_light.
23522371
all: solve_WF_matrix.
23532372
}
23542373
assert (exists (Q0 Q1 : Square 2), U4† = ∣0⟩⟨0∣ ⊗ Q0 .+ ∣1⟩⟨1∣ ⊗ Q1 /\ WF_Unitary Q0 /\ WF_Unitary Q1).
23552374
{
2356-
(* TODO(Kyle): Use the one in the refactoring PR!! *)
2357-
assert (inner_product_kron : forall {m n} (u : Vector m) (v : Vector n),
2358-
⟨u ⊗ v, u ⊗ v⟩ = ⟨u, u⟩ * ⟨v, v⟩).
2359-
{
2360-
intros.
2361-
unfold inner_product.
2362-
rewrite (@kron_adjoint m 1 n 1).
2363-
rewrite (@kron_mixed_product 1 m 1 1 n 1).
2364-
unfold kron; reflexivity.
2365-
}
23662375
assert (⟨ x ⊗ ∣0⟩, x ⊗ ∣0⟩ ⟩ = C1).
23672376
{
23682377
rewrite inner_product_kron.
@@ -2389,7 +2398,7 @@ Proof.
23892398
unfold bcgate in H3.
23902399
rewrite kron_assoc in H3.
23912400
rewrite Mmult_assoc in H3.
2392-
repeat rewrite kron_mixed_product in H3.
2401+
repeat rewrite (@kron_mixed_product 2 2 1 4 4 1) in H3.
23932402
rewrite Mmult_1_l in H3.
23942403
symmetry.
23952404
all: solve_WF_matrix.
@@ -2914,16 +2923,6 @@ Proof.
29142923
assert (exists (P0 P1 : Square 2),
29152924
V1 = ∣0⟩⟨0∣ ⊗ P0 .+ ∣1⟩⟨1∣ ⊗ P1 /\ WF_Unitary P0 /\ WF_Unitary P1).
29162925
{
2917-
(* TODO(Kyle): Use the one in the refactoring PR!! *)
2918-
assert (inner_product_kron : forall {m n} (u : Vector m) (v : Vector n),
2919-
⟨u ⊗ v, u ⊗ v⟩ = ⟨u, u⟩ * ⟨v, v⟩).
2920-
{
2921-
intros.
2922-
unfold inner_product.
2923-
rewrite (@kron_adjoint m 1 n 1).
2924-
rewrite (@kron_mixed_product 1 m 1 1 n 1).
2925-
unfold kron; reflexivity.
2926-
}
29272926
assert (⟨ x ⊗ ∣0⟩, x ⊗ ∣0⟩ ⟩ = C1).
29282927
{
29292928
rewrite inner_product_kron.

0 commit comments

Comments
 (0)