class_docstr: Test the core functionality of the VQE module
class_docstr_tok: ["Test","the","core","functionality","of","the","VQE","module"]
docstr: Test that the gradient of ExpvalCost is accessible and correct when using observable
  optimization and the Torch interface.
docstr_tok: ["Test","that","the","gradient","of","ExpvalCost","is","accessible","and","correct","when","using","observable","optimization","and","the","Torch","interface","."]
code: def test_optimize_grad_torch(self):
      """Test that the gradient of ExpvalCost is accessible and correct when using observable
      optimization and the Torch interface."""
      import torch
  
      dev = qml.device("default.qubit", wires=4)
      hamiltonian = big_hamiltonian
  
      cost = catch_warn_ExpvalCost(
          qml.templates.StronglyEntanglingLayers,
          hamiltonian,
          dev,
          optimize=True,
          interface="torch",
      )
  
      np.random.seed(1967)
      shape = qml.templates.StronglyEntanglingLayers.shape(
          n_layers=2, n_wires=4
      )
      w = np.random.uniform(low=0, high=2 * np.pi, size=shape)
      w = torch.tensor(w, requires_grad=True)
  
      res = cost(w)
      res.backward()
      dc = w.grad.detach().numpy()
  
      assert np.allclose(dc, big_hamiltonian_grad)
  
imports: import copy
  import numpy
  import pytest
  import pennylane
  from pennylane import numpy
label_desc: Use image node_id 13 for calling the TestVQE obj's underlying member method code with example usage: obj.test_optimize_grad_torch() without return types