-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMatrix.lean
More file actions
422 lines (340 loc) · 13.6 KB
/
Copy pathMatrix.lean
File metadata and controls
422 lines (340 loc) · 13.6 KB
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
import Mathlib.Data.Complex.Basic
import Mathlib.Data.Matrix.Mul
import Mathlib.LinearAlgebra.Matrix.ConjTranspose
import Mathlib.LinearAlgebra.Matrix.Kronecker
import Mathlib.LinearAlgebra.Matrix.Trace
import Mathlib.LinearAlgebra.UnitaryGroup
import Mathlib.Logic.Equiv.Fin.Basic
import Mathlib.Tactic.FinCases
import Mathlib.Tactic.NormNum
/-!
# Complex Matrices
Linear algebra primitives for the quantum development: finite-dimensional
complex matrices, state vectors, adjoints, traces, Kronecker products,
projections, and normalized-vector predicates.
-/
open scoped BigOperators
namespace QuantumComputing
abbrev Matrix (m n : ℕ) := _root_.Matrix (Fin m) (Fin n) ℂ
abbrev Vector (n : ℕ) := Matrix n 1
abbrev Square (n : ℕ) := Matrix n n
namespace Matrix
variable {m n p : ℕ}
noncomputable abbrev adjoint (A : Matrix m n) : Matrix n m :=
_root_.Matrix.conjTranspose A
noncomputable abbrev mul (A : Matrix m n) (B : Matrix n p) : Matrix m p :=
A * B
@[simp]
theorem mul_add (A : Matrix m n) (B C : Matrix n p) :
mul A (B + C) = mul A B + mul A C := by
simp [mul, _root_.Matrix.mul_add]
@[simp]
theorem add_mul (A B : Matrix m n) (C : Matrix n p) :
mul (A + B) C = mul A C + mul B C := by
simp [mul, _root_.Matrix.add_mul]
@[simp]
theorem mul_zero (A : Matrix m n) : mul A (0 : Matrix n p) = 0 := by
simp [mul]
@[simp]
theorem zero_mul (A : Matrix n p) : mul (0 : Matrix m n) A = 0 := by
simp [mul]
@[simp]
theorem mul_one (A : Matrix m n) : mul A (1 : Square n) = A := by
simp [mul]
@[simp]
theorem one_mul (A : Matrix m n) : mul (1 : Square m) A = A := by
simp [mul]
@[simp]
theorem mul_smul (A : Matrix m n) (a : ℂ) (B : Matrix n p) :
mul A (a • B) = a • mul A B := by
simp [mul]
@[simp]
theorem smul_mul (a : ℂ) (A : Matrix m n) (B : Matrix n p) :
mul (a • A) B = a • mul A B := by
simp [mul]
noncomputable abbrev trace (A : Square n) : ℂ :=
_root_.Matrix.trace A
noncomputable def proj (s : Vector n) : Square n :=
mul s (adjoint s)
noncomputable def kron {q : ℕ} (A : Matrix m n) (B : Matrix p q) : Matrix (m * p) (n * q) :=
_root_.Matrix.reindex finProdFinEquiv finProdFinEquiv (_root_.Matrix.kronecker A B)
@[simp]
theorem adjoint_mul (A : Matrix m n) (B : Matrix n p) :
adjoint (mul A B) = mul (adjoint B) (adjoint A) := by
simp [adjoint, mul]
@[simp]
theorem adjoint_zero : adjoint (0 : Matrix m n) = 0 := by
simp [adjoint]
@[simp]
theorem adjoint_one : adjoint (1 : Square n) = 1 := by
simp [adjoint]
@[simp]
theorem adjoint_adjoint (A : Matrix m n) : adjoint (adjoint A) = A := by
ext i j
simp [adjoint]
theorem adjoint_inj {A B : Matrix m n} (h : adjoint A = adjoint B) : A = B := by
rw [← adjoint_adjoint A, h, adjoint_adjoint]
@[simp]
theorem adjoint_add (A B : Matrix m n) : adjoint (A + B) = adjoint A + adjoint B := by
simp [adjoint]
@[simp]
theorem adjoint_neg (A : Matrix m n) : adjoint (-A) = -adjoint A := by
simp [adjoint]
@[simp]
theorem adjoint_sub (A B : Matrix m n) : adjoint (A - B) = adjoint A - adjoint B := by
simp [sub_eq_add_neg]
@[simp]
theorem adjoint_smul (a : ℂ) (A : Matrix m n) : adjoint (a • A) = star a • adjoint A := by
simp [adjoint]
@[simp]
theorem adjoint_apply (A : Matrix m n) (i : Fin n) (j : Fin m) :
adjoint A i j = star (A j i) :=
rfl
theorem trace_smul (a : ℂ) (A : Square n) : trace (a • A) = a * trace A := by
simp [trace, _root_.Matrix.trace, Finset.mul_sum]
@[simp]
theorem trace_zero : trace (0 : Square n) = 0 := by
simp [trace]
@[simp]
theorem trace_add (A B : Square n) : trace (A + B) = trace A + trace B := by
simp [trace, _root_.Matrix.trace, Finset.sum_add_distrib]
theorem trace_mul_comm (A : Matrix m n) (B : Matrix n m) :
trace (mul A B) = trace (mul B A) := by
simpa [trace, mul] using _root_.Matrix.trace_mul_comm A B
theorem trace_adjoint (A : Square n) : trace (adjoint A) = star (trace A) := by
simp [trace, adjoint, _root_.Matrix.trace]
@[simp]
theorem adjoint_proj (s : Vector n) : adjoint (proj s) = proj s := by
simp [proj]
theorem proj_add (s t : Vector n) :
proj (s + t) =
proj s + mul s (adjoint t) + mul t (adjoint s) + proj t := by
ext i j
simp [proj, mul, adjoint, _root_.Matrix.mul_apply]
ring
@[simp]
theorem kron_apply {q : ℕ} (A : Matrix m n) (B : Matrix p q)
(i : Fin m) (k : Fin p) (j : Fin n) (l : Fin q) :
kron A B (finProdFinEquiv (i, k)) (finProdFinEquiv (j, l)) = A i j * B k l := by
simp [kron]
@[simp]
theorem kron_zero_left {q : ℕ} (B : Matrix p q) :
kron (0 : Matrix m n) B = 0 := by
ext i j
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [kron]
@[simp]
theorem kron_zero_right {q : ℕ} (A : Matrix m n) :
kron A (0 : Matrix p q) = 0 := by
ext i j
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [kron]
theorem kron_add_left {q : ℕ} (A B : Matrix m n) (C : Matrix p q) :
kron (A + B) C = kron A C + kron B C := by
ext i j
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [kron, _root_.add_mul]
theorem kron_add_right {q : ℕ} (A : Matrix m n) (B C : Matrix p q) :
kron A (B + C) = kron A B + kron A C := by
ext i j
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [kron, _root_.mul_add]
theorem kron_sub_left {q : ℕ} (A B : Matrix m n) (C : Matrix p q) :
kron (A - B) C = kron A C - kron B C := by
ext i j
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [kron, sub_eq_add_neg, _root_.add_mul]
theorem kron_sub_right {q : ℕ} (A : Matrix m n) (B C : Matrix p q) :
kron A (B - C) = kron A B - kron A C := by
ext i j
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [kron, sub_eq_add_neg, _root_.mul_add]
theorem kron_smul_left {q : ℕ} (a : ℂ) (A : Matrix m n) (B : Matrix p q) :
kron (a • A) B = a • kron A B := by
ext i j
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [kron, mul_assoc]
theorem kron_smul_right {q : ℕ} (a : ℂ) (A : Matrix m n) (B : Matrix p q) :
kron A (a • B) = a • kron A B := by
ext i j
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [kron, mul_left_comm]
@[simp]
theorem adjoint_kron {q : ℕ} (A : Matrix m n) (B : Matrix p q) :
adjoint (kron A B) = kron (adjoint A) (adjoint B) := by
ext i j
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [adjoint, kron, _root_.Matrix.kronecker]
@[simp]
theorem kron_mul {q r s : ℕ} (A : Matrix m n) (B : Matrix p q)
(C : Matrix n r) (D : Matrix q s) :
mul (kron A B) (kron C D) = kron (mul A C) (mul B D) := by
ext i j
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [mul, kron, _root_.Matrix.mul_kronecker_mul]
theorem kron_mul_assoc {r s t u : ℕ} (A : Matrix m n) (B : Matrix p q)
(C : Matrix n r) (D : Matrix q s) (E : Matrix r t) (F : Matrix s u) :
mul (mul (kron A B) (kron C D)) (kron E F) = kron (mul (mul A C) E) (mul (mul B D) F) := by
simp [mul_assoc, kron_mul]
@[simp]
theorem proj_kron (s : Vector m) (t : Vector n) :
proj (kron s t) = kron (proj s) (proj t) := by
rw [proj, adjoint_kron, kron_mul]
rfl
theorem proj_add_kron (s t : Vector m) (u v : Vector n) :
proj (kron s u + kron t v) =
kron (proj s) (proj u) + kron (mul s (adjoint t)) (mul u (adjoint v)) +
kron (mul t (adjoint s)) (mul v (adjoint u)) + kron (proj t) (proj v) := by
rw [proj_add, proj_kron, proj_kron]
rw [adjoint_kron, adjoint_kron, kron_mul, kron_mul]
@[simp]
theorem kron_one_one : kron (1 : Square m) (1 : Square n) = (1 : Square (m * n)) := by
ext i j
rw [← finProdFinEquiv.apply_symm_apply i, ← finProdFinEquiv.apply_symm_apply j]
rcases finProdFinEquiv.symm i with ⟨i₁, i₂⟩
rcases finProdFinEquiv.symm j with ⟨j₁, j₂⟩
simp [kron, _root_.Matrix.one_apply, Prod.ext_iff]
theorem trace_kron (A : Square m) (B : Square n) :
trace (kron A B) = trace A * trace B := by
calc
trace (kron A B) = ∑ x : Fin m × Fin n, A x.1 x.1 * B x.2 x.2 := by
rw [trace, _root_.Matrix.trace]
symm
exact Fintype.sum_equiv finProdFinEquiv
(fun x : Fin m × Fin n => A x.1 x.1 * B x.2 x.2)
(fun x : Fin (m * n) => kron A B x x)
(by intro x; simp [kron])
_ = trace A * trace B := by
rw [trace, trace, _root_.Matrix.trace, _root_.Matrix.trace]
rw [← Finset.univ_product_univ]
rw [Finset.sum_product]
rw [Finset.sum_comm]
simp [Finset.mul_sum, mul_comm]
theorem trace_outer_eq_inner (s t : Vector n) :
trace (mul s (adjoint t)) = (mul (adjoint t) s) 0 0 := by
rw [trace_mul_comm]
simp [trace, mul, _root_.Matrix.trace]
theorem trace_proj (s : Vector n) : trace (proj s) = (mul (adjoint s) s) 0 0 := by
simp [proj, trace_outer_eq_inner]
def isNormal (A : Square n) : Prop :=
mul (adjoint A) A = mul A (adjoint A)
def isUnitary (A : Square n) : Prop :=
A ∈ _root_.Matrix.unitaryGroup (Fin n) ℂ
theorem isUnitary_iff_adjoint_mul_self (A : Square n) :
isUnitary A ↔ mul (adjoint A) A = 1 := by
simpa [isUnitary, adjoint, mul, _root_.Matrix.star_eq_conjTranspose] using
(_root_.Matrix.mem_unitaryGroup_iff' (A := A))
theorem isUnitary_iff_mul_adjoint_self (A : Square n) :
isUnitary A ↔ mul A (adjoint A) = 1 := by
simpa [isUnitary, adjoint, mul, _root_.Matrix.star_eq_conjTranspose] using
(_root_.Matrix.mem_unitaryGroup_iff (A := A))
@[simp]
theorem isUnitary_one : isUnitary (1 : Square n) := by
rw [isUnitary_iff_adjoint_mul_self]
simp [mul]
theorem isUnitary_mul {A B : Square n}
(hA : isUnitary A) (hB : isUnitary B) : isUnitary (mul A B) := by
rw [isUnitary_iff_adjoint_mul_self]
rw [adjoint_mul]
rw [isUnitary_iff_adjoint_mul_self] at hA hB
have hAroot : adjoint A * A = 1 := by simpa [mul] using hA
have hBroot : adjoint B * B = 1 := by simpa [mul] using hB
change (adjoint B * adjoint A) * (A * B) = 1
rw [_root_.Matrix.mul_assoc]
rw [← _root_.Matrix.mul_assoc (adjoint A) A B]
rw [hAroot]
simp [hBroot]
theorem isUnitary_preserve_inner {U : Square n} (hU : isUnitary U) (v w : Vector n) :
mul (adjoint (mul U v)) (mul U w) = mul (adjoint v) w := by
rw [adjoint_mul]
rw [isUnitary_iff_adjoint_mul_self] at hU
have hUroot : adjoint U * U = 1 := by simpa [mul] using hU
change (adjoint v * adjoint U) * (U * w) = adjoint v * w
rw [_root_.Matrix.mul_assoc]
rw [← _root_.Matrix.mul_assoc (adjoint U) U w]
rw [hUroot]
simp
theorem isUnitary_kron {A : Square m} {B : Square n}
(hA : isUnitary A) (hB : isUnitary B) : isUnitary (kron A B) := by
rw [isUnitary_iff_adjoint_mul_self]
rw [adjoint_kron, kron_mul]
rw [isUnitary_iff_adjoint_mul_self] at hA hB
simp [hA, hB]
end Matrix
postfix:max "†" => Matrix.adjoint
infixl:70 " ⬝ " => Matrix.mul
infixl:75 " ⊗ " => Matrix.kron
notation "Tr(" A ")" => Matrix.trace A
notation "I" n => (1 : Square n)
namespace Vector
variable {n : ℕ}
/-- A state vector is normalized when its inner product with itself is `1`. -/
def IsNormalized (s : Vector n) : Prop :=
s† ⬝ s = 1
theorem isNormalized_kron {m n : ℕ} {s : Vector m} {t : Vector n}
(hs : IsNormalized s) (ht : IsNormalized t) : IsNormalized (s ⊗ t) := by
rw [IsNormalized, Matrix.adjoint_kron, Matrix.kron_mul]
have hsroot : s† ⬝ s = (1 : Square 1) := by simpa [IsNormalized] using hs
have htroot : t† ⬝ t = (1 : Square 1) := by simpa [IsNormalized] using ht
rw [hsroot, htroot]
simp
theorem not_isNormalized_zero (n : ℕ) : ¬ IsNormalized (0 : Vector n) := by
intro h
have hscalar := congrFun (congrFun h 0) 0
norm_num [IsNormalized, Matrix.mul, Matrix.adjoint, _root_.Matrix.mul_apply] at hscalar
def basis (i : Fin n) : Vector n :=
fun j _ => if j = i then 1 else 0
@[simp]
theorem basis_apply (i j : Fin n) : basis i j 0 = if j = i then 1 else 0 :=
rfl
theorem basis_apply_ne {i j : Fin n} (h : j ≠ i) : basis i j 0 = 0 := by
simp [basis, h]
theorem basis_isNormalized (i : Fin n) : IsNormalized (basis i) := by
rw [IsNormalized]
ext j k
fin_cases j
fin_cases k
simp [Matrix.mul, Matrix.adjoint, basis, _root_.Matrix.mul_apply]
end Vector
namespace Matrix
variable {m n p : ℕ}
theorem proj_mul_proj_of_isNormalized {s : Vector n} (hs : Vector.IsNormalized s) :
mul (proj s) (proj s) = proj s := by
change (s * adjoint s) * (s * adjoint s) = s * adjoint s
rw [_root_.Matrix.mul_assoc]
rw [← _root_.Matrix.mul_assoc (adjoint s) s (adjoint s)]
have hsroot : adjoint s * s = 1 := by simpa [Vector.IsNormalized, mul] using hs
rw [hsroot]
simp
theorem trace_proj_of_isNormalized {s : Vector n} (hs : Vector.IsNormalized s) :
trace (proj s) = 1 := by
rw [proj]
rw [trace_mul_comm]
rw [Vector.IsNormalized] at hs
rw [hs]
simp [trace]
theorem isUnitary_mul_isNormalized {A : Square n} {s : Vector n}
(hA : isUnitary A) (hs : Vector.IsNormalized s) :
Vector.IsNormalized (mul A s) := by
rw [Vector.IsNormalized] at hs ⊢
rw [adjoint_mul]
rw [isUnitary_iff_adjoint_mul_self] at hA
have hAroot : adjoint A * A = 1 := by simpa [mul] using hA
have hsroot : adjoint s * s = 1 := by simpa [mul] using hs
change (adjoint s * adjoint A) * (A * s) = 1
rw [_root_.Matrix.mul_assoc]
rw [← _root_.Matrix.mul_assoc (adjoint A) A s]
rw [hAroot]
simpa using hsroot
end Matrix
end QuantumComputing