docstr: Test that the QNSPSA gradient estimation is correct by comparing the optimizer result
  to the analytical result.
docstr_tok: ["Test","that","the","QNSPSA","gradient","estimation","is","correct","by","comparing","the","optimizer","result","to","the","analytical","result","."]
code: def test_gradient_from_single_input(self, finite_diff_step, seed):
      """Test that the QNSPSA gradient estimation is correct by comparing the optimizer result
      to the analytical result."""
      opt = qml.QNSPSAOptimizer(
          stepsize=1e-3,
          regularization=1e-3,
          finite_diff_step=finite_diff_step,
          resamplings=1,
          blocking=True,
          history_length=5,
          seed=seed,
      )
      qnode, params_shape = get_single_input_qnode()
      params = np.random.rand(*params_shape)
  
      # gradient result from QNSPSAOptimizer
      grad_tapes, grad_dirs = opt._get_spsa_grad_tapes(
          qnode, [params], {}
      )
      raw_results = qml.execute(grad_tapes, qnode.device, None)
      grad_res = opt._post_process_grad(raw_results, grad_dirs)[0]
  
      # gradient computed analytically
      qnode_finite_diff = get_grad_finite_diff(
          params[0], finite_diff_step, grad_dirs[0][0]
      )
      grad_expected = (
          qnode_finite_diff / (2 * finite_diff_step) * grad_dirs[0]
      )
      assert np.allclose(grad_res, grad_expected)
  
file_path: pennylane/tests/optimize/test_qnspsa.py
imports: from copy import deepcopy
  import pytest
  from scipy.linalg import sqrtm
  import pennylane
  from pennylane import numpy
label_desc: Use image node_id 1 for calling the TestQNSPSAOptimizer obj's underlying member method code with example usage: obj.test_gradient_from_single_input(finite_diff_step, seed) without return types