code: def _project_density_matrix_spectrum(rdm):
      """Project the estimator density matrix rdm with possibly negative eigenvalues onto the closest true density matrix in L2 norm"""
      # algorithm below eq. (16) in https://arxiv.org/pdf/1106.5458.pdf
      evs = qml.math.eigvalsh(rdm)[
          ::-1
      ]  # order from largest to smallest
      d = len(rdm)
      a = 0.0
      for i in range(d - 1, -1, -1):
          if evs[i] + a / (i + 1) > 0:
              break
          a += evs[i]
  
      lambdas = evs[: i + 1] + a / (i + 1)
      return lambdas[::-1]
  
file_path: pennylane/pennylane/shadows/classical_shadow.py
imports: import warnings
  from collections.abc import Iterable
  from string import ascii_letters
  import numpy
  import pennylane
label_desc: Use image node_id 1 for calling a global function with example usage: _project_density_matrix_spectrum(rdm) and returns: lambdas