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 second derivative calculation of a scalar-valued QNode
docstr_tok: ["Test","second","derivative","calculation","of","a","scalar-valued","QNode"]
code: def test_second_derivative(
      self,
      dev,
      diff_method,
      grad_on_execution,
      device_vjp,
      interface,
      tol,
  ):
      """Test second derivative calculation of a scalar-valued QNode"""
      kwargs = {
          "diff_method": diff_method,
          "interface": interface,
          "grad_on_execution": grad_on_execution,
          "device_vjp": device_vjp,
          "max_diff": 2,
      }
  
      if diff_method == "adjoint":
          pytest.skip("Adjoint does not second derivative.")
      elif diff_method == "spsa":
          kwargs["sampler_rng"] = np.random.default_rng(SEED_FOR_SPSA)
          tol = TOL_FOR_SPSA
  
      @qnode(dev, **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)
      g = jax.grad(circuit)(x)
      g2 = jax.grad(lambda x: jax.numpy.sum(jax.grad(circuit)(x)))(x)
  
      a, b = x
  
      expected_res = np.cos(a) * np.cos(b)
      assert np.allclose(res, expected_res, atol=tol, rtol=0)
  
      expected_g = [-np.sin(a) * np.cos(b), -np.cos(a) * np.sin(b)]
      assert np.allclose(g, expected_g, atol=tol, rtol=0)
  
      expected_g2 = [
          -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(g2, expected_g2, atol=10e-2, rtol=0)
      else:
          assert np.allclose(g2, expected_g2, atol=tol, rtol=0)
  
imports: from itertools import product
  import numpy
  import pytest
  import pennylane
  from pennylane import qnode
  from pennylane.devices import DefaultQubit
label_desc: Use image node_id 1 for calling the TestQubitIntegrationHigherOrder obj's underlying member method code with example usage: obj.test_second_derivative(dev, diff_method, grad_on_execution, device_vjp, interface, tol) and returns: qml