Zulip Chat Archive

Stream: new members

Topic: Prove that a matrix is Hermitian


Yunong Shi (Dec 12 2023 at 19:08):

Hi I am very new to Lean. I am trying to prove that a concrete matrix to be Hermitian. Here are the definitions and my proof so far:

import Mathlib.Data.Matrix.Basic
import Mathlib.Data.Complex.Basic
import Mathlib.LinearAlgebra.Matrix.Hermitian
def PauliX : Matrix (Fin 2) (Fin 2)  :=
  ![![0, 1], ![1, 0]]

example paulix_is_hermitian: ( i j: Fin 2, star (PauliX j i) = PauliX i j) := by
  intros i j
  fin_cases i; fin_cases j
  all_goals simp [PauliX, star, Matrix.vecCons]
  rfl
  rfl
  ext
  simp

Currently, my first goal is:

j: Fin 2
 (Fin.cons (Fin.cons 0 (Fin.cons 1 ![])) (Fin.cons (Fin.cons 1 (Fin.cons 0 ![])) ![]) j 1).re =
  (Fin.cons 1 (Fin.cons 0 ![]) j).re

I admit that I used the help of chatGPT to get here. I am confused that:

  1. I used fin_cases i; fin_cases j, but why there is still j in the goal?
  2. How do I proceed?
    Thanks!

Kyle Miller (Dec 12 2023 at 19:13):

fin_cases i; fin_cases j should be fin_cases i <;> fin_cases j. Maybe ChatGPT is confused about Lean 3 vs Lean 4.

The ; ends up meaning "do fin_cases j on the first goal produced by fin_cases i"

Yunong Shi (Dec 12 2023 at 19:17):

Got it. Thanks very much!

Here is my new proof:

example paulix_is_hermitian: ( i j: Fin 2, star (PauliX j i) = PauliX i j) := by
  intros i j
  fin_cases i <;> fin_cases j
  all_goals simp [PauliX, star, Matrix.vecCons]
  rfl
  rfl
  rfl
  rfl

However, I still get the error:

failed to infer binder type when the resulting type of a declaration is explicitly provided, all holes (e.g., `_`) in the header are resolved before the declaration body is processed

I find all types are clear in the proof, what is wrong?

Eric Wieser (Dec 12 2023 at 19:30):

Note that ![![0, 1], ![1, 0]] is not a matrix, and some lemmas won't fire as a result

Eric Wieser (Dec 12 2023 at 19:30):

The correct notation is !![0, 1; 1, 0]

Eric Wieser (Dec 12 2023 at 19:31):

Your issue here is that the syntax for example is wrong. example foo : True := sorry means example (foo : _) : true := sorry

Eric Wieser (Dec 12 2023 at 19:33):

Which is to say: examples don't have names. If you want to give it a name, you should use theorem

Eric Wieser (Dec 12 2023 at 19:33):

It's unfortunate that lean parses this in such a confusing way

Yunong Shi (Dec 12 2023 at 19:35):

Thanks! @Eric Wieser It resolves all the problem!

Yunong Shi (Dec 12 2023 at 19:47):

Now I am trying to prove a 2nd version of the theorem, which leverages the "IsHermitian" in mathlib.

theorem PauliX_is_still_hermitian: PauliX.IsHermitian := by
  ext
  all_goals simp

Currently, I am having this goal:

ix: Fin 2
 (PauliX x i).re = (PauliX i x).re

Since i✝, x✝ are Fin 2, I want to fin_cases them. However, I cannot type them in the editor (the editor won't recognize them). How do I proceed?

llllvvuu (Dec 12 2023 at 19:51):

i didn't test, but most likely you want to ext i x or something

Jireh Loreaux (Dec 12 2023 at 19:52):

I'm curious. Does just rfl alone (no ext) work?

Kyle Miller (Dec 12 2023 at 19:52):

Or avoid unfolding the definition and start by rewriting with docs#Matrix.IsHermitian.ext_iff

Yunong Shi (Dec 12 2023 at 20:08):

Jireh Loreaux said:

I'm curious. Does just rfl alone (no ext) work?

unfortunately, it doesn't. It says type not match.

Jireh Loreaux (Dec 12 2023 at 20:22):

Sorry, what I asked before was nonsense. Thanks for checking though!

Jireh Loreaux (Dec 12 2023 at 20:32):

You may want to write some simp lemmas for PauliX, like this:

import Mathlib.Data.Matrix.Basic
import Mathlib.Data.Complex.Basic
import Mathlib.LinearAlgebra.Matrix.Hermitian

def PauliX : Matrix (Fin 2) (Fin 2)  :=
  !![0, 1; 1, 0]

@[simp] lemma PauliX₁₁ : PauliX 0 0 = 0 := rfl
@[simp] lemma PauliX₁₂ : PauliX 0 1 = 1 := rfl
@[simp] lemma PauliX₂₁ : PauliX 1 0 = 1 := rfl
@[simp] lemma PauliX₂₂ : PauliX 1 1 = 0 := rfl

example : PauliX.IsHermitian := by
  apply Matrix.IsHermitian.ext
  intro i j
  fin_cases i <;> fin_cases j <;> simp

which makes the proof quite simple.

Kyle Miller (Dec 12 2023 at 20:34):

You can also pass PauliX as a simp lemma rather than writing those four lemmas:

example : PauliX.IsHermitian := by
  apply Matrix.IsHermitian.ext
  intro i j
  fin_cases i <;> fin_cases j <;> simp [PauliX]

Yunong Shi (Dec 12 2023 at 21:30):

Those are very helpful. Thanks! @Jireh Loreaux @Kyle Miller


Last updated: Dec 20 2023 at 11:08 UTC