class_docstr: Tests that ensure various qubit circuits integrate correctly when computing higher-order derivatives
class_docstr_tok: ["Tests","that","ensure","various","qubit","circuits","integrate","correctly","when","computing","higher-order","derivatives"]
docstr: Test hessian calculation of a vector valued QNode with post-processing
docstr_tok: ["Test","hessian","calculation","of","a","vector","valued","QNode","with","post-processing"]
code: def test_hessian_vector_valued_postprocessing(
      self, dev_name, diff_method, interface, grad_on_execution, tol
  ):
      """Test hessian calculation of a vector valued QNode with post-processing"""
      gradient_kwargs = {}
      if diff_method == "adjoint":
          pytest.skip("Adjoint does not support second derivative.")
      elif diff_method == "spsa":
          gradient_kwargs = {
              "h": H_FOR_SPSA,
              "num_directions": 20,
              "sampler_rng": np.random.default_rng(SEED_FOR_SPSA),
          }
          tol = TOL_FOR_SPSA
  
      num_wires = 1
  
      if diff_method == "hadamard":
          num_wires = 3
  
      dev = qml.device(dev_name, wires=num_wires)
  
      @qnode(
          dev,
          diff_method=diff_method,
          interface=interface,
          grad_on_execution=grad_on_execution,
          max_diff=2,
          **gradient_kwargs,
      )
      def circuit(x):
          qml.RX(x[0], wires=0)
          qml.RY(x[1], wires=0)
          return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(0))
  
      def cost_fn(x):
          return x @ jax.numpy.array(circuit(x))
  
      x = jax.numpy.array([0.76, -0.87])
      res = cost_fn(x)
  
      a, b = x
  
      expected_res = x @ jax.numpy.array(
          [np.cos(a) * np.cos(b), np.cos(a) * np.cos(b)]
      )
      assert np.allclose(res, expected_res, atol=tol, rtol=0)
  
      grad_fn = jax.grad(cost_fn)
      g = grad_fn(x)
  
      expected_g = [
          np.cos(b) * (np.cos(a) - (a + b) * np.sin(a)),
          np.cos(a) * (np.cos(b) - (a + b) * np.sin(b)),
      ]
      assert np.allclose(g, expected_g, atol=tol, rtol=0)
      hess = jax.jacobian(grad_fn)(x)
  
      expected_hess = [
          [
              -(np.cos(b) * ((a + b) * np.cos(a) + 2 * np.sin(a))),
              -(np.cos(b) * np.sin(a))
              + (-np.cos(a) + (a + b) * np.sin(a)) * np.sin(b),
          ],
          [
              -(np.cos(b) * np.sin(a))
              + (-np.cos(a) + (a + b) * np.sin(a)) * np.sin(b),
              -(np.cos(a) * ((a + b) * np.cos(b) + 2 * np.sin(b))),
          ],
      ]
  
      if diff_method == "finite-diff":
          assert np.allclose(hess, expected_hess, atol=10e-2, rtol=0)
      else:
          assert np.allclose(hess, expected_hess, atol=tol, rtol=0)
  
imports: import pytest
  import pennylane
  from pennylane import numpy
  from pennylane import qnode
  from pennylane.tape import QuantumScript
label_desc: Use image node_id 4 for calling the TestQubitIntegrationHigherOrder obj's underlying member method code with example usage: obj.test_hessian_vector_valued_postprocessing(dev_name, diff_method, interface, grad_on_execution, tol) and returns: qml,  qml, unknown