class_docstr: Tests that ensure various qubit circuits integrate correctly
class_docstr_tok: ["Tests","that","ensure","various","qubit","circuits","integrate","correctly"]
docstr: Test that the returned gradient value for two chained qubit QNodes
  is correct.
docstr_tok: ["Test","that","the","returned","gradient","value","for","two","chained","qubit","QNodes","is","correct","."]
code: def test_chained_gradient_value(
      self,
      interface,
      dev,
      diff_method,
      grad_on_execution,
      device_vjp,
      tol,
  ):
      """Test that the returned gradient value for two chained qubit QNodes
      is correct."""
      kwargs = dict(
          interface=interface,
          diff_method=diff_method,
          grad_on_execution=grad_on_execution,
          device_vjp=device_vjp,
      )
  
      if diff_method == "spsa":
          kwargs["sampler_rng"] = np.random.default_rng(SEED_FOR_SPSA)
          tol = TOL_FOR_SPSA
      dev1 = qml.device("default.qubit")
  
      @qnode(dev1, **kwargs)
      def circuit1(a, b, c):
          qml.RX(a, wires=0)
          qml.RX(b, wires=1)
          qml.RX(c, wires=2)
          qml.CNOT(wires=[0, 1])
          qml.CNOT(wires=[1, 2])
          return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliY(2))
  
      dev2 = dev
  
      @qnode(
          dev2,
          interface=interface,
          diff_method=diff_method,
          grad_on_execution=grad_on_execution,
      )
      def circuit2(data, weights):
          qml.RX(data[0], wires=0)
          qml.RX(data[1], wires=1)
          qml.CNOT(wires=[0, 1])
          qml.RZ(weights[0], wires=0)
          qml.RZ(weights[1], wires=1)
          qml.CNOT(wires=[0, 1])
          return qml.expval(qml.PauliX(0) @ qml.PauliY(1))
  
      def cost(a, b, c, weights):
          return circuit2(circuit1(a, b, c), weights)
  
      grad_fn = qml.grad(cost)
  
      # Set the first parameter of circuit1 as non-differentiable.
      a = np.array(0.4, requires_grad=False)
  
      # The remaining free parameters are all differentiable.
      b = np.array(0.5, requires_grad=True)
      c = np.array(0.1, requires_grad=True)
      weights = np.array([0.2, 0.3], requires_grad=True)
  
      res = grad_fn(a, b, c, weights)
  
      # Output should have shape [dcost/db, dcost/dc, dcost/dw],
      # where b,c are scalars, and w is a vector of length 2.
      assert len(res) == 3
      assert res[0].shape == tuple()  # scalar
      assert res[1].shape == tuple()  # scalar
      assert res[2].shape == (2,)  # vector
  
      cacbsc = np.cos(a) * np.cos(b) * np.sin(c)
  
      expected = np.array(
          [
              # analytic expression for dcost/db
              -np.cos(a)
              * np.sin(b)
              * np.sin(c)
              * np.cos(cacbsc)
              * np.sin(weights[0])
              * np.sin(np.cos(a)),
              # analytic expression for dcost/dc
              np.cos(a)
              * np.cos(b)
              * np.cos(c)
              * np.cos(cacbsc)
              * np.sin(weights[0])
              * np.sin(np.cos(a)),
              # analytic expression for dcost/dw[0]
              np.sin(cacbsc) * np.cos(weights[0]) * np.sin(np.cos(a)),
              # analytic expression for dcost/dw[1]
              0,
          ]
      )
  
      # np.hstack 'flattens' the ragged gradient array allowing it
      # to be compared with the expected result
      assert np.allclose(np.hstack(res), expected, atol=tol, rtol=0)
  
      if diff_method != "backprop":
          # Check that the gradient was computed
          # for all parameters in circuit2
          assert circuit2.qtape.trainable_params == [0, 1, 2, 3]
  
          # Check that the parameter-shift rule was not applied
          # to the first parameter of circuit1.
          assert circuit1.qtape.trainable_params == [1, 2]
  
imports: import autograd
  import autograd.numpy
  import pytest
  import pennylane
  from pennylane import numpy
  from pennylane import qnode
  from pennylane.devices import DefaultQubit
label_desc: Use image node_id 6 for calling the TestQubitIntegration obj's underlying member method code with example usage: obj.test_chained_gradient_value(interface, dev, diff_method, grad_on_execution, device_vjp, tol) and returns: qml,  qml, qml, circuit2