class_docstr: Tests for the hamiltonian_expand transform
class_docstr_tok: ["Tests","for","the","hamiltonian_expand","transform"]
docstr: Tests that the hamiltonian_expand tape transform is differentiable with the Autograd interface
docstr_tok: ["Tests","that","the","hamiltonian_expand","tape","transform","is","differentiable","with","the","Autograd","interface"]
code: def test_hamiltonian_dif_autograd(self, tol):
      """Tests that the hamiltonian_expand tape transform is differentiable with the Autograd interface"""
  
      H = qml.Hamiltonian(
          [-0.2, 0.5, 1],
          [qml.PauliX(1), qml.PauliZ(1) @ qml.PauliY(2), qml.PauliZ(0)],
      )
  
      var = pnp.array(
          [0.1, 0.67, 0.3, 0.4, -0.5, 0.7, -0.2, 0.5, 1.0],
          requires_grad=True,
      )
      output = 0.42294409781940356
      output2 = [
          9.68883500e-02,
          -2.90832724e-01,
          -1.04448033e-01,
          -1.94289029e-09,
          3.50307411e-01,
          -3.41123470e-01,
          0.0,
          -0.43657,
          0.64123,
      ]
  
      with AnnotatedQueue() as q:
          for _ in range(2):
              qml.RX(np.array(0), wires=0)
              qml.RX(np.array(0), wires=1)
              qml.RX(np.array(0), wires=2)
              qml.CNOT(wires=[0, 1])
              qml.CNOT(wires=[1, 2])
              qml.CNOT(wires=[2, 0])
  
          qml.expval(H)
  
      tape = QuantumScript.from_queue(q)
  
      def cost(x):
          new_tape = tape.bind_new_parameters(x, list(range(9)))
          tapes, fn = hamiltonian_expand(new_tape)
          res = qml.execute(tapes, dev, qml.gradients.param_shift)
          return fn(res)
  
      assert np.isclose(cost(var), output)
  
      grad = qml.grad(cost)(var)
      assert len(grad) == len(output2)
      for g, o in zip(grad, output2):
          assert np.allclose(g, o, atol=tol)
  
imports: import numpy
  import pytest
  import pennylane
  from pennylane import numpy
  from pennylane.queuing import AnnotatedQueue
  from pennylane.tape import QuantumScript
  from pennylane.transforms import hamiltonian_expand, sum_expand
label_desc: Use image node_id 9 for calling the TestHamiltonianExpand obj's underlying member method code with example usage: obj.test_hamiltonian_dif_autograd(tol) and returns: fn