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 scalar-valued QNode
docstr_tok: ["Test","hessian","calculation","of","a","scalar-valued","QNode"]
code: def test_hessian(
      self, dev_name, diff_method, grad_on_execution, interface, tol
  ):
      """Test hessian calculation of a scalar-valued QNode"""
      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.RY(x[0], wires=0)
          qml.RX(x[1], wires=0)
          return qml.expval(qml.PauliZ(0))
  
      x = jax.numpy.array([1.0, 2.0])
      res = circuit(x)
  
      a, b = x
  
      expected_res = np.cos(a) * np.cos(b)
      assert np.allclose(res, expected_res, atol=tol, rtol=0)
  
      grad_fn = jax.grad(circuit)
      g = grad_fn(x)
  
      expected_g = [-np.sin(a) * np.cos(b), -np.cos(a) * np.sin(b)]
      assert np.allclose(g, expected_g, atol=tol, rtol=0)
  
      hess = jax.jacobian(grad_fn)(x)
  
      expected_hess = [
          [-np.cos(a) * np.cos(b), np.sin(a) * np.sin(b)],
          [np.sin(a) * np.sin(b), -np.cos(a) * np.cos(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 2 for calling the TestQubitIntegrationHigherOrder obj's underlying member method code with example usage: obj.test_hessian(dev_name, diff_method, grad_on_execution, interface, tol) and returns: qml