module

public import SubentropyCertificates.FixedMarginalSpectrum
public import SubentropyCertificates.SpatialSignBlockDiagonal

/-! Bound the spatial sign norm when the second tensor factor is maximally mixed. -/
@[expose] public section
noncomputable section
set_option backward.isDefEq.respectTransparency false
open Matrix MeasureTheory Set
open scoped BigOperators ComplexOrder MatrixOrder Matrix.Norms.L2Operator Kronecker

namespace SubentropyCertificates.ConditionalContinuity
open SubentropyCertificates.BinarySpectra (densityMatrix)
open SubentropyCertificates.SpectralContinuity (maximallyMixed)

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

private theorem psd_norm_le_trace [Nonempty D] {T : Matrix D D ℂ} (hT : T.PosSemidef) :
    ‖T‖ ≤ T.trace.re := by
  have hm := CStarAlgebra.norm_mem_spectrum_of_nonneg T hT.nonneg
  rw [hT.isHermitian.spectrum_real_eq_range_eigenvalues] at hm
  obtain ⟨i, hi⟩ := hm
  have hs := congrArg Complex.re hT.isHermitian.trace_eq_sum_eigenvalues
  rw [← hi, hs]
  simpa using Finset.single_le_sum (s := Finset.univ) (fun j _ ↦ hT.eigenvalues_nonneg j)
    (Finset.mem_univ i)

/-- K of a state tensored with I/k has k identical positive diagonal blocks. Each has
trace 1/k, so this also bounds the operator norm of the full matrix. -/
theorem spatialSign_norm_tensor_maximallyMixed (C : Type*) [Fintype C] [DecidableEq C]
    [Nonempty C] {M : Matrix D D ℂ} (hM : M.PosSemidef) (ht : M.trace = 1) :
    ‖spatialSign (M ⊗ₖ densityMatrix (maximallyMixed C))‖ ≤ 1 / (Fintype.card C : ℝ) := by
  have : Nonempty D := by
    by_contra h
    have := not_nonempty_iff.mp h
    simp [Matrix.trace] at ht
  let c : ℝ := 1 / (Fintype.card C : ℝ)
  have hc : 0 ≤ c := by dsimp [c]; positivity
  let A := fun _ : C ↦ c • M
  have ha (x : C) : (A x).PosSemidef := hM.smul hc
  have he : M ⊗ₖ densityMatrix (maximallyMixed C) = blockDiagonal A := by
    change M ⊗ₖ diagonal (fun _ : C ↦ (c : ℂ)) = _
    rw [kroneckerMap_diagonal_right (· * ·) mul_zero]
    ext ⟨i,x⟩ ⟨j,y⟩
    by_cases h : x = y
    · subst y
      simp [A, Complex.real_smul, mul_comm]
    · simp [blockDiagonal_apply_ne _ _ _ h]
  let x₀ : C := Classical.choice inferInstance
  let T := blockSpatialSign A x₀
  have hb : blockSpatialSign A = fun _ : C ↦ T := rfl
  have hpsd : T.PosSemidef := by
    apply nonneg_iff_posSemidef.mp
    apply integral_nonneg_of_ae
    filter_upwards [ae_restrict_mem measurableSet_Ioi] with t ht'
    exact (blockSignKernel_order A ha x₀ ht').1
  have hR := blocks_posSemidef A ha
  have hn : blockDiagonal A ≠ 0 := by
    intro hz
    have h := congrArg Matrix.trace (he.trans hz)
    rw [Matrix.trace_kronecker, ht, one_mul, show (densityMatrix (maximallyMixed C)).trace = 1
      from (maximallyMixed C).2.2, trace_zero] at h
    exact one_ne_zero h
  have htr := spatialSign_trace_one hR hn
  rw [spatialSign_blockDiagonal A ha, hb, trace_blockDiagonal] at htr
  have htrace : T.trace.re = c := by
    have h : (Fintype.card C : ℂ) * T.trace = 1 := by
      simpa only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul] using htr
    have h' := congrArg Complex.re h
    apply (eq_div_iff (Nat.cast_ne_zero.mpr (Fintype.card_ne_zero (α := C)))).2
    simpa [mul_comm] using h'
  have hle : T ≤ c • 1 := by
    rw [← Algebra.algebraMap_eq_smul_one]
    apply (CStarAlgebra.norm_le_iff_le_algebraMap T hc hpsd.nonneg).1
    exact (psd_norm_le_trace hpsd).trans_eq htrace
  rw [he, spatialSign_blockDiagonal A ha, hb]
  let B := fun _ : C ↦ T
  change ‖blockDiagonal B‖ ≤ c
  apply (CStarAlgebra.norm_le_iff_le_algebraMap _ hc (blocks_posSemidef B (fun _ ↦ hpsd)).nonneg).2
  rw [Algebra.algebraMap_eq_smul_one]
  have h := (blocks_posSemidef (c • (1 : C → Matrix D D ℂ) - B)
    (fun _ ↦ (sub_nonneg.mpr hle).posSemidef)).nonneg
  rw [blockDiagonal_sub, blockDiagonal_smul, blockDiagonal_one] at h
  exact sub_nonneg.mp h

end SubentropyCertificates.ConditionalContinuity
