class_docstr: Testing differentiable ZNE
class_docstr_tok: ["Testing","differentiable","ZNE"]
docstr: Testing that the mitigated qnode can be differentiated and returns the correct gradient in torch
docstr_tok: ["Testing","that","the","mitigated","qnode","can","be","differentiated","and","returns","the","correct","gradient","in","torch"]
code: def test_diffability_torch(self, interface):
      """Testing that the mitigated qnode can be differentiated and returns the correct gradient in torch"""
      import torch
  
      qnode_noisy = qml.QNode(qfunc, dev_noisy, interface=interface)
      qnode_ideal = qml.QNode(qfunc, dev_ideal, interface=interface)
  
      scale_factors = [1.0, 2.0, 3.0]
  
      mitigated_qnode = mitigate_with_zne(
          qnode_noisy,
          scale_factors,
          fold_global,
          richardson_extrapolate,
      )
  
      theta = torch.tensor([np.pi / 4, np.pi / 4], requires_grad=True)
  
      res = mitigated_qnode(theta)
  
      assert qml.math.allclose(res, out_ideal, atol=1e-2)
      res.backward()
      grad = theta.grad
      theta0 = torch.tensor([np.pi / 4, np.pi / 4], requires_grad=True)
      res_ideal = qnode_ideal(theta0)
      res_ideal.backward()
      grad_ideal = theta0.grad
      assert qml.math.allclose(grad_ideal, grad_ideal_0)
      assert qml.math.allclose(grad, grad_ideal, atol=1e-2)
  
imports: from functools import partial
  import pytest
  from packaging import version
  import pennylane
  from pennylane import numpy
  from pennylane.tape import QuantumScript
  from pennylane.transforms import mitigate_with_zne, richardson_extrapolate, fold_global
label_desc: Use image node_id 6 for calling the TestDifferentiableZNE obj's underlying member method code with example usage: obj.test_diffability_torch(interface) without return types