class_docstr: Tests for entropies in ClassicalShadow class
class_docstr_tok: ["Tests","for","entropies","in","ClassicalShadow","class"]
docstr: Test entropies match roughly with exact solution for a non-constant distribution using other PennyLane functionalities
docstr_tok: ["Test","entropies","match","roughly","with","exact","solution","for","a","non-constant","distribution","using","other","PennyLane","functionalities"]
code: def test_non_constant_distribution(
      self,
  ):
      """Test entropies match roughly with exact solution for a non-constant distribution using other PennyLane functionalities"""
      n_wires = 4
      # exact solution
      dev_exact = qml.device(
          "default.qubit", wires=range(n_wires), shots=None
      )
      dev = qml.device(
          "default.qubit", wires=range(n_wires), shots=100000
      )
  
      @qml.qnode(dev_exact)
      def qnode_exact(x):
          for i in range(n_wires):
              qml.RY(x[i], wires=i)
  
          for i in range(n_wires - 1):
              qml.CNOT((i, i + 1))
  
          return qml.state()
  
      # classical shadow qnode
      @qml.qnode(dev)
      def qnode(x):
          for i in range(n_wires):
              qml.RY(x[i], wires=i)
  
          for i in range(n_wires - 1):
              qml.CNOT((i, i + 1))
  
          return qml.classical_shadow(wires=dev.wires)
  
      x = np.arange(n_wires, requires_grad=True)
  
      bitstrings, recipes = qnode(x)
      shadow = ClassicalShadow(bitstrings, recipes)
  
      # Check for the correct entropies for all possible 2-site reduced density matrix (rdm)
      for rdm_wires in [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]:
          # this is intentionally not done in a parametrize loop because this would re-execute the quantum function
  
          # exact solution
          rdm = qml.qinfo.reduced_dm(qnode_exact, wires=rdm_wires)(x)
          evs = qml.math.eigvalsh(rdm)
  
          evs = evs[np.where(evs > 0)]
  
          exact_2 = -np.log(np.trace(rdm @ rdm))
  
          alpha = 1.5
          exact_alpha = qml.math.log(qml.math.sum(evs**alpha)) / (
              1 - alpha
          )
  
          exact_vn = qml.math.entr(evs)
          exact = [exact_vn, exact_alpha, exact_2]
  
          # shadow estimate
          entropies = [
              shadow.entropy(wires=rdm_wires, alpha=alpha)
              for alpha in [1, 1.5, 2]
          ]
  
          assert np.allclose(entropies, exact, atol=1e-1)
  
file_path: pennylane/tests/shadow/test_shadow_entropies.py
imports: import pytest
  import pennylane
  import pennylane.numpy
  from pennylane.shadows import ClassicalShadow
  from pennylane.shadows.classical_shadow import _project_density_matrix_spectrum
label_desc: Use image node_id 2 for calling the TestShadowEntropies obj's underlying member method code with example usage: obj.test_non_constant_distribution() and returns: qml, qml