{-# OPTIONS --safe --cubical-compatible --guardedness --exact-split --double-check #-}
module ALMA.Physics.Quantum.AmplitudeComp where
open import Data.Rational using (ℚ; 0ℚ; 1ℚ; _+_; _*_; _-_; -_; _/_)
open import Data.Rational.Properties using
(+-assoc; +-comm; +-identityʳ; +-inverseʳ; *-assoc; *-comm; *-identityˡ
; *-distribˡ-+; *-distribʳ-+; neg-distrib-+; neg-distribˡ-*; neg-distribʳ-*)
open import Data.Integer.Base using (ℤ; 1ℤ) renaming (_+_ to _ℤ+_)
open import Data.Product.Base using (_,_)
open import Relation.Binary.PropositionalEquality using
(_≡_; refl; sym; trans; cong; cong₂; module ≡-Reasoning)
open import ALMA.Physics.Quantum.Gaussian using
(ℂ; re; conj; norm²; _+ᶜ_; _*ᶜ_; norm²-mul; norm²-conj)
open import ALMA.Physics.RationalKit using
(neg-involutive; neg-neg-mul; sq+; +interchange; flat4; sub-+-assoc; *ʳ-sub; *ˡ-sub)
-ᶜ_ : ℂ → ℂ
-ᶜ_ (a , b) = (- a , - b)
private
perm4 : ∀ a b c d → a + (b + (c + d)) ≡ a + (c + (d + b))
perm4 a b c d = let open ≡-Reasoning in begin
a + (b + (c + d))
≡⟨ sym (+-assoc a b (c + d)) ⟩
(a + b) + (c + d)
≡⟨ +interchange a b c d ⟩
(a + c) + (b + d)
≡⟨ cong ((a + c) +_) (+-comm b d) ⟩
(a + c) + (d + b)
≡⟨ +-assoc a c (d + b) ⟩
a + (c + (d + b)) ∎
gather-t : ∀ P Q R S t →
((P + Q) + t) + ((R + S) + (- t)) ≡ (P + R) + (Q + S)
gather-t P Q R S t = let open ≡-Reasoning in begin
((P + Q) + t) + ((R + S) + (- t))
≡⟨ +interchange (P + Q) t (R + S) (- t) ⟩
((P + Q) + (R + S)) + (t + (- t))
≡⟨ cong (((P + Q) + (R + S)) +_) (+-inverseʳ t) ⟩
((P + Q) + (R + S)) + 0ℚ
≡⟨ +-identityʳ ((P + Q) + (R + S)) ⟩
(P + Q) + (R + S)
≡⟨ +interchange P Q R S ⟩
(P + R) + (Q + S) ∎
private
*ᶜ-comm : ∀ x y → x *ᶜ y ≡ y *ᶜ x
*ᶜ-comm (a , b) (c , d) = cong₂ _,_ real imag
where
real = let open ≡-Reasoning in begin
a * c - b * d
≡⟨ cong₂ _-_ (*-comm a c) (*-comm b d) ⟩
c * a - d * b ∎
imag = let open ≡-Reasoning in begin
a * d + b * c
≡⟨ cong₂ _+_ (*-comm a d) (*-comm b c) ⟩
d * a + c * b
≡⟨ +-comm (d * a) (c * b) ⟩
c * b + d * a ∎
*ᶜ-assoc : ∀ x y z → (x *ᶜ y) *ᶜ z ≡ x *ᶜ (y *ᶜ z)
*ᶜ-assoc (a , b) (c , d) (e , f) = cong₂ _,_ real imag
where
r1 = (a * c) * e ; r2 = - ((b * d) * e)
r3 = - ((a * d) * f) ; r4 = - ((b * c) * f)
realR : a * (c * e - d * f) - b * (c * f + d * e)
≡ r1 + (r3 + (r4 + r2))
realR = let open ≡-Reasoning in begin
a * (c * e - d * f) - b * (c * f + d * e)
≡⟨ cong₂ _-_ (*ˡ-sub a (c * e) (d * f))
(*-distribˡ-+ b (c * f) (d * e)) ⟩
(a * (c * e) - a * (d * f)) - (b * (c * f) + b * (d * e))
≡⟨ sub-+-assoc (a * (c * e) - a * (d * f)) (b * (c * f))
(b * (d * e)) ⟩
((a * (c * e) - a * (d * f)) - b * (c * f)) - b * (d * e)
≡⟨ flat4 (a * (c * e)) (- (a * (d * f))) (- (b * (c * f)))
(- (b * (d * e))) ⟩
a * (c * e) + (- (a * (d * f))
+ (- (b * (c * f)) + - (b * (d * e))))
≡⟨ cong₂ _+_ (sym (*-assoc a c e))
(cong₂ _+_ (cong -_ (sym (*-assoc a d f)))
(cong₂ _+_ (cong -_ (sym (*-assoc b c f)))
(cong -_ (sym (*-assoc b d e))))) ⟩
r1 + (r3 + (r4 + r2)) ∎
real = let open ≡-Reasoning in begin
(a * c - b * d) * e - (a * d + b * c) * f
≡⟨ cong₂ _-_ (*ʳ-sub (a * c) (b * d) e)
(*-distribʳ-+ f (a * d) (b * c)) ⟩
((a * c) * e - (b * d) * e) - ((a * d) * f + (b * c) * f)
≡⟨ sub-+-assoc ((a * c) * e - (b * d) * e) ((a * d) * f)
((b * c) * f) ⟩
(((a * c) * e - (b * d) * e) - (a * d) * f) - (b * c) * f
≡⟨ flat4 r1 r2 r3 r4 ⟩
r1 + (r2 + (r3 + r4))
≡⟨ perm4 r1 r2 r3 r4 ⟩
r1 + (r3 + (r4 + r2))
≡⟨ sym realR ⟩
a * (c * e - d * f) - b * (c * f + d * e) ∎
t1 = (a * c) * f ; t2 = - ((b * d) * f)
t3 = (a * d) * e ; t4 = (b * c) * e
imagR : a * (c * f + d * e) + b * (c * e - d * f)
≡ t1 + (t3 + (t4 + t2))
imagR = let open ≡-Reasoning in begin
a * (c * f + d * e) + b * (c * e - d * f)
≡⟨ cong₂ _+_ (*-distribˡ-+ a (c * f) (d * e))
(*ˡ-sub b (c * e) (d * f)) ⟩
(a * (c * f) + a * (d * e)) + (b * (c * e) - b * (d * f))
≡⟨ cong₂ _+_
(cong₂ _+_ (sym (*-assoc a c f)) (sym (*-assoc a d e)))
(cong₂ _+_ (sym (*-assoc b c e))
(cong -_ (sym (*-assoc b d f)))) ⟩
(t1 + t3) + (t4 + t2)
≡⟨ sym (+-assoc (t1 + t3) t4 t2) ⟩
((t1 + t3) + t4) + t2
≡⟨ flat4 t1 t3 t4 t2 ⟩
t1 + (t3 + (t4 + t2)) ∎
imag = let open ≡-Reasoning in begin
(a * c - b * d) * f + (a * d + b * c) * e
≡⟨ cong₂ _+_ (*ʳ-sub (a * c) (b * d) f)
(*-distribʳ-+ e (a * d) (b * c)) ⟩
((a * c) * f - (b * d) * f) + ((a * d) * e + (b * c) * e)
≡⟨ sym (+-assoc ((a * c) * f - (b * d) * f) ((a * d) * e)
((b * c) * e)) ⟩
(((a * c) * f - (b * d) * f) + (a * d) * e) + (b * c) * e
≡⟨ flat4 t1 t2 t3 t4 ⟩
t1 + (t2 + (t3 + t4))
≡⟨ perm4 t1 t2 t3 t4 ⟩
t1 + (t3 + (t4 + t2))
≡⟨ sym imagR ⟩
a * (c * f + d * e) + b * (c * e - d * f) ∎
conj-invol : ∀ z → conj (conj z) ≡ z
conj-invol (a , b) = cong (a ,_) (neg-involutive b)
conj-neg : ∀ z → conj (-ᶜ z) ≡ -ᶜ conj z
conj-neg (a , b) = refl
conj-mul : ∀ x y → conj (x *ᶜ y) ≡ conj x *ᶜ conj y
conj-mul (a , b) (c , d) = cong₂ _,_ (sym real) (sym imag)
where
real = let open ≡-Reasoning in begin
a * c - (- b) * (- d)
≡⟨ cong (λ z → a * c - z) (let open ≡-Reasoning in begin
(- b) * (- d)
≡⟨ sym (neg-distribˡ-* b (- d)) ⟩
- (b * (- d))
≡⟨ cong -_ (sym (neg-distribʳ-* b d)) ⟩
- (- (b * d))
≡⟨ neg-involutive (b * d) ⟩
b * d ∎) ⟩
a * c - b * d ∎
imag = let open ≡-Reasoning in begin
a * (- d) + (- b) * c
≡⟨ cong₂ _+_ (sym (neg-distribʳ-* a d))
(sym (neg-distribˡ-* b c)) ⟩
- (a * d) + - (b * c)
≡⟨ sym (neg-distrib-+ (a * d) (b * c)) ⟩
- (a * d + b * c) ∎
neg-mulˡ : ∀ x y → (-ᶜ x) *ᶜ y ≡ -ᶜ (x *ᶜ y)
neg-mulˡ (a , b) (c , d) = cong₂ _,_ real imag
where
real = let open ≡-Reasoning in begin
(- a) * c - (- b) * d
≡⟨ cong₂ _-_ (sym (neg-distribˡ-* a c))
(sym (neg-distribˡ-* b d)) ⟩
- (a * c) - - (b * d)
≡⟨ cong (λ z → - (a * c) + z) (neg-involutive (b * d)) ⟩
- (a * c) + b * d
≡⟨ cong (λ z → - (a * c) + z) (sym (neg-involutive (b * d))) ⟩
- (a * c) + - (- (b * d))
≡⟨ sym (neg-distrib-+ (a * c) (- (b * d))) ⟩
- (a * c - b * d) ∎
imag = let open ≡-Reasoning in begin
(- a) * d + (- b) * c
≡⟨ cong₂ _+_ (sym (neg-distribˡ-* a d))
(sym (neg-distribˡ-* b c)) ⟩
- (a * d) + - (b * c)
≡⟨ sym (neg-distrib-+ (a * d) (b * c)) ⟩
- (a * d + b * c) ∎
neg-mulʳ : ∀ x y → x *ᶜ (-ᶜ y) ≡ -ᶜ (x *ᶜ y)
neg-mulʳ x y = let open ≡-Reasoning in begin
x *ᶜ (-ᶜ y)
≡⟨ *ᶜ-comm x (-ᶜ y) ⟩
(-ᶜ y) *ᶜ x
≡⟨ neg-mulˡ y x ⟩
-ᶜ (y *ᶜ x)
≡⟨ cong -ᶜ_ (*ᶜ-comm y x) ⟩
-ᶜ (x *ᶜ y) ∎
re-neg : ∀ z → re (-ᶜ z) ≡ - re z
re-neg (a , b) = refl
norm²-neg : ∀ z → norm² (-ᶜ z) ≡ norm² z
norm²-neg (a , b) =
cong₂ _+_ (neg-neg-mul a a) (neg-neg-mul b b)
cross : ℂ → ℂ → ℚ
cross x y = re (conj x *ᶜ y) + re (conj x *ᶜ y)
private
re-overlap : ∀ (a b d e : ℚ) →
re (conj (a , b) *ᶜ (d , e)) ≡ a * d + b * e
re-overlap a b d e = let open ≡-Reasoning in begin
(a * d) - ((- b) * e)
≡⟨ cong (λ z → (a * d) - z) (sym (neg-distribˡ-* b e)) ⟩
(a * d) - (- (b * e))
≡⟨ cong (λ z → (a * d) + z) (neg-involutive (b * e)) ⟩
a * d + b * e ∎
norm²-add : ∀ (x y : ℂ) →
norm² (x +ᶜ y) ≡ norm² x + norm² y + cross x y
norm²-add (a , b) (d , e) = let open ≡-Reasoning in begin
(a + d) * (a + d) + (b + e) * (b + e)
≡⟨ cong₂ _+_ (sq+ a d) (sq+ b e) ⟩
(a² + (d² + P)) + (b² + (e² + Q))
≡⟨ +interchange a² (d² + P) b² (e² + Q) ⟩
(a² + b²) + ((d² + P) + (e² + Q))
≡⟨ cong ((a² + b²) +_) (+interchange d² P e² Q) ⟩
(a² + b²) + ((d² + e²) + (P + Q))
≡⟨ cong (λ z → (a² + b²) + ((d² + e²) + z)) swap ⟩
(a² + b²) + ((d² + e²) + cross (a , b) (d , e))
≡⟨ sym (+-assoc (a² + b²) (d² + e²) (cross (a , b) (d , e))) ⟩
(a² + b²) + (d² + e²) + cross (a , b) (d , e) ∎
where
a² = a * a ; d² = d * d ; b² = b * b ; e² = e * e
P = a * d + a * d ; Q = b * e + b * e
ovl = re (conj (a , b) *ᶜ (d , e))
swap = let open ≡-Reasoning in begin
P + Q
≡⟨ +interchange (a * d) (a * d) (b * e) (b * e) ⟩
(a * d + b * e) + (a * d + b * e)
≡⟨ sym (cong₂ _+_ (re-overlap a b d e) (re-overlap a b d e)) ⟩
ovl + ovl ∎
private
cross-opposite : ∀ (u v α β : ℂ) →
let x = u *ᶜ α
y = v *ᶜ β
x' = (-ᶜ conj v) *ᶜ α
y' = conj u *ᶜ β
in cross x' y' ≡ - cross x y
cross-opposite u v α β = let open ≡-Reasoning in begin
re B + re B
≡⟨ cong₂ _+_ (cong re B≡-A) (cong re B≡-A) ⟩
re (-ᶜ A) + re (-ᶜ A)
≡⟨ cong₂ _+_ (re-neg A) (re-neg A) ⟩
(- re A) + (- re A)
≡⟨ sym (neg-distrib-+ (re A) (re A)) ⟩
- (re A + re A) ∎
where
x = u *ᶜ α
y = v *ᶜ β
x' = (-ᶜ conj v) *ᶜ α
y' = conj u *ᶜ β
A = conj x *ᶜ y
B = conj x' *ᶜ y'
w = conj u *ᶜ v
F = conj α *ᶜ w
flip : conj (u *ᶜ α) ≡ conj α *ᶜ conj u
flip = let open ≡-Reasoning in begin
conj (u *ᶜ α)
≡⟨ conj-mul u α ⟩
conj u *ᶜ conj α
≡⟨ *ᶜ-comm (conj u) (conj α) ⟩
conj α *ᶜ conj u ∎
A≡ : A ≡ F *ᶜ β
A≡ = let open ≡-Reasoning in begin
conj (u *ᶜ α) *ᶜ (v *ᶜ β)
≡⟨ cong (_*ᶜ (v *ᶜ β)) flip ⟩
(conj α *ᶜ conj u) *ᶜ (v *ᶜ β)
≡⟨ *ᶜ-assoc (conj α) (conj u) (v *ᶜ β) ⟩
conj α *ᶜ (conj u *ᶜ (v *ᶜ β))
≡⟨ cong (conj α *ᶜ_) (sym (*ᶜ-assoc (conj u) v β)) ⟩
conj α *ᶜ ((conj u *ᶜ v) *ᶜ β)
≡⟨ sym (*ᶜ-assoc (conj α) w β) ⟩
F *ᶜ β ∎
conjx'≡ : conj x' ≡ conj α *ᶜ (-ᶜ v)
conjx'≡ = let open ≡-Reasoning in begin
conj ((-ᶜ conj v) *ᶜ α)
≡⟨ conj-mul (-ᶜ conj v) α ⟩
conj (-ᶜ conj v) *ᶜ conj α
≡⟨ *ᶜ-comm (conj (-ᶜ conj v)) (conj α) ⟩
conj α *ᶜ conj (-ᶜ conj v)
≡⟨ cong (conj α *ᶜ_) (conj-neg (conj v)) ⟩
conj α *ᶜ (-ᶜ conj (conj v))
≡⟨ cong (conj α *ᶜ_) (cong -ᶜ_ (conj-invol v)) ⟩
conj α *ᶜ (-ᶜ v) ∎
mid≡ : (-ᶜ v) *ᶜ conj u ≡ -ᶜ w
mid≡ = let open ≡-Reasoning in begin
(-ᶜ v) *ᶜ conj u
≡⟨ neg-mulˡ v (conj u) ⟩
-ᶜ (v *ᶜ conj u)
≡⟨ cong -ᶜ_ (*ᶜ-comm v (conj u)) ⟩
-ᶜ (conj u *ᶜ v) ∎
B≡-A : B ≡ -ᶜ A
B≡-A = let open ≡-Reasoning in begin
B
≡⟨ cong (_*ᶜ y') conjx'≡ ⟩
(conj α *ᶜ (-ᶜ v)) *ᶜ (conj u *ᶜ β)
≡⟨ *ᶜ-assoc (conj α) (-ᶜ v) (conj u *ᶜ β) ⟩
conj α *ᶜ ((-ᶜ v) *ᶜ (conj u *ᶜ β))
≡⟨ cong (conj α *ᶜ_) (sym (*ᶜ-assoc (-ᶜ v) (conj u) β)) ⟩
conj α *ᶜ (((-ᶜ v) *ᶜ conj u) *ᶜ β)
≡⟨ cong (conj α *ᶜ_) (cong (_*ᶜ β) mid≡) ⟩
conj α *ᶜ ((-ᶜ w) *ᶜ β)
≡⟨ cong (conj α *ᶜ_) (neg-mulˡ w β) ⟩
conj α *ᶜ (-ᶜ (w *ᶜ β))
≡⟨ neg-mulʳ (conj α) (w *ᶜ β) ⟩
-ᶜ (conj α *ᶜ (w *ᶜ β))
≡⟨ cong -ᶜ_ (sym (*ᶜ-assoc (conj α) w β)) ⟩
-ᶜ ((conj α *ᶜ w) *ᶜ β)
≡⟨ cong -ᶜ_ (sym A≡) ⟩
-ᶜ A ∎
unitary2-pres : ∀ (u v α β : ℂ) →
norm² u + norm² v ≡ 1ℚ →
norm² (u *ᶜ α +ᶜ v *ᶜ β)
+ norm² ((-ᶜ conj v) *ᶜ α +ᶜ conj u *ᶜ β)
≡ norm² α + norm² β
unitary2-pres u v α β h = let open ≡-Reasoning in begin
norm² γ0 + norm² γ1
≡⟨ cong₂ _+_ (norm²-add x y) (norm²-add x' y') ⟩
(Dxy + cross x y) + (Dx'y' + cross x' y')
≡⟨ cong (λ c → (Dxy + cross x y) + (Dx'y' + c))
(cross-opposite u v α β) ⟩
(Dxy + cross x y) + (Dx'y' + (- cross x y))
≡⟨ cong₂ (λ p q → (p + cross x y) + (q + (- cross x y))) dxy dx'y' ⟩
((Uαα + Vββ) + cross x y) + ((Vαα + Uββ) + (- cross x y))
≡⟨ gather-t Uαα Vββ Vαα Uββ (cross x y) ⟩
(Uαα + Vαα) + (Vββ + Uββ)
≡⟨ cong (λ s → (Uαα + Vαα) + s) (+-comm Vββ Uββ) ⟩
(Uαα + Vαα) + (Uββ + Vββ)
≡⟨ cong₂ _+_ (sym (*-distribʳ-+ (norm² α) (norm² u) (norm² v)))
(sym (*-distribʳ-+ (norm² β) (norm² u) (norm² v))) ⟩
(norm² u + norm² v) * norm² α
+ (norm² u + norm² v) * norm² β
≡⟨ cong₂ _+_ (cong (_* norm² α) h) (cong (_* norm² β) h) ⟩
1ℚ * norm² α + 1ℚ * norm² β
≡⟨ cong₂ _+_ (*-identityˡ (norm² α)) (*-identityˡ (norm² β)) ⟩
norm² α + norm² β ∎
where
x = u *ᶜ α
y = v *ᶜ β
x' = (-ᶜ conj v) *ᶜ α
y' = conj u *ᶜ β
γ0 = x +ᶜ y
γ1 = x' +ᶜ y'
Uαα = norm² u * norm² α
Vββ = norm² v * norm² β
Vαα = norm² v * norm² α
Uββ = norm² u * norm² β
Dxy = norm² x + norm² y
Dx'y' = norm² x' + norm² y'
diagx : norm² x ≡ Uαα
diagx = let open ≡-Reasoning in begin
norm² (u *ᶜ α) ≡⟨ norm²-mul u α ⟩ norm² u * norm² α ∎
diagy : norm² y ≡ Vββ
diagy = let open ≡-Reasoning in begin
norm² (v *ᶜ β) ≡⟨ norm²-mul v β ⟩ norm² v * norm² β ∎
diagx' : norm² x' ≡ Vαα
diagx' = let open ≡-Reasoning in begin
norm² ((-ᶜ conj v) *ᶜ α)
≡⟨ norm²-mul (-ᶜ conj v) α ⟩
norm² (-ᶜ conj v) * norm² α
≡⟨ cong (_* norm² α) (norm²-neg (conj v)) ⟩
norm² (conj v) * norm² α
≡⟨ cong (_* norm² α) (norm²-conj v) ⟩
norm² v * norm² α ∎
diagy' : norm² y' ≡ Uββ
diagy' = let open ≡-Reasoning in begin
norm² (conj u *ᶜ β)
≡⟨ norm²-mul (conj u) β ⟩
norm² (conj u) * norm² β
≡⟨ cong (_* norm² β) (norm²-conj u) ⟩
norm² u * norm² β ∎
dxy : Dxy ≡ Uαα + Vββ
dxy = cong₂ _+_ diagx diagy
dx'y' : Dx'y' ≡ Vαα + Uββ
dx'y' = cong₂ _+_ diagx' diagy'
private
i3 i4 : ℤ
i3 = (1ℤ ℤ+ 1ℤ) ℤ+ 1ℤ
i4 = i3 ℤ+ 1ℤ
c₃₄₅ s₃₄₅ : ℚ
c₃₄₅ = i3 / 5
s₃₄₅ = i4 / 5
u₃₄₅ v₃₄₅ : ℂ
u₃₄₅ = (c₃₄₅ , 0ℚ)
v₃₄₅ = (0ℚ , s₃₄₅)
pythagoras-345 : norm² u₃₄₅ + norm² v₃₄₅ ≡ 1ℚ
pythagoras-345 = refl
unitary2-345 : ∀ (α β : ℂ) →
norm² (u₃₄₅ *ᶜ α +ᶜ v₃₄₅ *ᶜ β)
+ norm² ((-ᶜ conj v₃₄₅) *ᶜ α +ᶜ conj u₃₄₅ *ᶜ β)
≡ norm² α + norm² β
unitary2-345 α β = unitary2-pres u₃₄₅ v₃₄₅ α β pythagoras-345