module

public import SubentropyCertificates.InteriorEigenbasis
public import SubentropyCertificates.ClassicalInteriorEquality

/-! Theorem 13: all maximizing density-matrix pairs at strictly interior radii. -/
@[expose] public section
noncomputable section
set_option backward.isDefEq.respectTransparency false
open Matrix Set
open scoped BigOperators ComplexOrder MatrixOrder

namespace SubentropyCertificates.SpectralContinuity
open D5.S3.Quantum.Foundation.FiniteStateChannel
open D5.S3.Quantum.Information.InputInformationBalance (IsPure)
open D5.S3.Quantum.Information.PartialTraceMutualInformation
open D5.S3.Quantum.Subentropy.Basic
open D5.S3.Quantum.Subentropy.DensityKernel
open SubentropyCertificates.ClassicalCurve
open SubentropyCertificates.Continuity
open SubentropyCertificates.BinarySpectra (densityMatrix)
open SubentropyCertificates.MixedQubit (traceDistance)
open SubentropyCertificates.Subadditivity (subentropy_zero_iff_pure subentropy_of_spectrum)

variable {D : Type*} [Fintype D] [DecidableEq D] {m : ℕ}

private theorem native_spectral_distance (ρ σ : DensityState D) :
    totalVariation (density_posSemidef ρ).isHermitian.eigenvalues
      (density_posSemidef σ).isHermitian.eigenvalues ≤ traceDistance ρ σ := by
  let e : Fin (Fintype.card D) ≃ D := Fintype.equivOfCardEq (Fintype.card_fin _)
  apply LE.le.trans _ (ordered_spectrum_totalVariation_le ρ σ)
  apply le_of_eq
  exact congrArg (fun x : ℝ ↦ x / 2)
    (e.symm.sum_comp (fun j ↦ |orderedSpectrum ρ j - orderedSpectrum σ j|))

private theorem conjugate_pure_projection (ρ : DensityState D) (hρ : IsPure ρ)
    (U : Matrix.unitaryGroup D ℂ) :
    let P := Unitary.conjStarAlgAut ℂ _ U (densityMatrix ρ)
    P.IsHermitian ∧ P*P = P ∧ P.trace = 1 := by
  refine ⟨((density_posSemidef ρ).mul_mul_conjTranspose_same (U : Matrix D D ℂ)).isHermitian,
    ?_, ?_⟩
  · rw [← map_mul, (pure_density_projection ρ hρ).1]
  · rw [Unitary.conjStarAlgAut_apply, Matrix.trace_mul_comm, ← Matrix.mul_assoc]
    simpa using (show (densityMatrix ρ).trace = 1 from ρ.2.2)

private theorem interior_necessity (hn : Fintype.card D = m + 2) {δ : ℝ}
    (h0 : 0 < δ) (hu : δ < uniformError m) (ρ σ : DensityState D)
    (hd : traceDistance ρ σ ≤ δ) (hg : subentropy σ - subentropy ρ = phi m δ) :
    IsPure ρ ∧ densityMatrix σ = extremalMatrix m δ (densityMatrix ρ) := by
  let p := (density_posSemidef σ).isHermitian.eigenvalues
  let q := (density_posSemidef ρ).isHermitian.eigenvalues
  have hp : p ∈ probabilitySimplex D := spectral_prob (density_posSemidef σ) σ.2.2
  have hq : q ∈ probabilitySimplex D := spectral_prob (density_posSemidef ρ) ρ.2.2
  have hsσ : subentropy σ = classicalSubentropy p :=
    subentropy_of_spectrum (density_posSemidef σ).isHermitian
  have hsρ : subentropy ρ = classicalSubentropy q :=
    subentropy_of_spectrum (density_posSemidef ρ).isHermitian
  rw [hsσ, hsρ] at hg
  have ht := native_spectral_distance σ ρ
  rw [traceDistance_comm σ ρ] at ht
  obtain ⟨hz, shape⟩ := classical_interior_equality m hn p q hp hq h0 hu (ht.trans hd) hg
  have pure := (subentropy_zero_iff_pure ρ).mp (hsρ.trans hz)
  have hδ : δ ∈ Icc (0 : ℝ) 1 := ⟨h0.le, hu.le.trans (uniformError_mem m).2⟩
  obtain ⟨e, U, hσ⟩ := extremal_spectral_basis σ hn hδ shape
  let P := Unitary.conjStarAlgAut ℂ _ U (densityMatrix (coordinatePure e))
  obtain ⟨hpH, hp2, hpt⟩ := conjugate_pure_projection (coordinatePure e)
    (extremal_diagonal_pair e hδ).1 U
  have hS : densityMatrix σ = extremalMatrix m δ P := by
    rw [hσ, extremalDiagonal_matrix, extremalMatrix_conj]
  have he := interior_projection_alignment m hu ρ σ pure P hpH hp2 hpt hS hd
  exact ⟨pure, he.symm ▸ hS⟩

/-- Every aligned pair has actual distance δ and signed subentropy gap Φ_d(δ). -/
theorem aligned_pair_values (hn : Fintype.card D = m + 2) {δ : ℝ} (hδ : δ ∈ Icc (0 : ℝ) 1)
    (ρ σ : DensityState D) (hρ : IsPure ρ)
    (hs : densityMatrix σ = extremalMatrix m δ (densityMatrix ρ)) :
    traceDistance ρ σ = δ ∧ subentropy σ - subentropy ρ = phi m δ := by
  obtain ⟨e, U, hr⟩ := pure_spectral_basis ρ hn hρ
  have hσ : densityMatrix σ =
      Unitary.conjStarAlgAut ℂ _ U (densityMatrix (extremalDiagonal e δ hδ)) := by
    rw [hs, hr, extremalDiagonal_matrix, extremalMatrix_conj]
  obtain ⟨_, _, dist, gap⟩ := extremal_diagonal_pair e hδ
  exact ⟨(traceDistance_comm ρ σ).trans ((traceDistance_eq_of_unitary _ _ _ _ U hσ hr).trans dist),
    by rwa [subentropy_eq_of_unitary σ _ U hσ, subentropy_eq_of_unitary ρ _ U hr]⟩

/-- A pair in the closed trace-distance ball attains the interior maximum iff, up to
exchange, it consists of a pure projection and the state with weight 1-δ on that
projection and weight δ/(d-1) on its orthogonal complement. -/
theorem quantum_modulus_interior_eq_iff (hn : Fintype.card D = m + 2) {δ : ℝ}
    (h0 : 0 < δ) (hu : δ < uniformError m) (ρ σ : DensityState D) :
    (traceDistance ρ σ ≤ δ ∧ |subentropy ρ - subentropy σ| = sSup (quantumDifferences D δ)) ↔
      (IsPure ρ ∧ densityMatrix σ = extremalMatrix m δ (densityMatrix ρ)) ∨
      (IsPure σ ∧ densityMatrix ρ = extremalMatrix m δ (densityMatrix σ)) := by
  have : Nonempty D := Fintype.card_pos_iff.mp (by omega)
  rw [quantum_modulus_supremum D h0.le, hn]
  change (_ ∧ _ = phi m (min δ (uniformError m))) ↔ _
  rw [min_eq_left hu.le]
  have hδ : δ ∈ Icc (0 : ℝ) 1 := ⟨h0.le, hu.le.trans (uniformError_mem m).2⟩
  have hpos := phi_pos m h0 hu.le
  constructor
  · rintro ⟨hd, hg⟩
    rcases le_total (subentropy ρ) (subentropy σ) with h | h
    · rw [abs_of_nonpos (sub_nonpos.mpr h), neg_sub] at hg
      exact Or.inl (interior_necessity hn h0 hu ρ σ hd hg)
    · rw [abs_of_nonneg (sub_nonneg.mpr h)] at hg
      rw [traceDistance_comm] at hd
      exact Or.inr (interior_necessity hn h0 hu σ ρ hd hg)
  · rintro (⟨hr, hs⟩ | ⟨hs, hr⟩)
    · obtain ⟨hd, hg⟩ := aligned_pair_values hn hδ ρ σ hr hs
      exact ⟨hd.le, by rw [abs_sub_comm, hg, abs_of_nonneg hpos.le]⟩
    · obtain ⟨hd, hg⟩ := aligned_pair_values hn hδ σ ρ hs hr
      exact ⟨(traceDistance_comm ρ σ).le.trans hd.le, by rw [hg, abs_of_nonneg hpos.le]⟩

end SubentropyCertificates.SpectralContinuity
