docstr: Test step() function with the single-input qnode.
docstr_tok: ["Test","step","(",")","function","with","the","single-input","qnode","."]
code: def test_step_from_single_input(self, finite_diff_step, seed):
      """Test step() function with the single-input qnode."""
      regularization = 1e-3
      stepsize = 1e-2
      opt = qml.QNSPSAOptimizer(
          stepsize=stepsize,
          regularization=regularization,
          finite_diff_step=finite_diff_step,
          resamplings=1,
          blocking=False,
          history_length=5,
          seed=seed,
      )
      # target opt is used to reproduce the random sampling result
      target_opt = deepcopy(opt)
  
      qnode, params_shape = get_single_input_qnode()
      params = np.random.rand(*params_shape)
  
      new_params_res = opt.step(qnode, params)
  
      _, grad_dirs = target_opt._get_spsa_grad_tapes(
          qnode, [params], {}
      )
      _, tensor_dirs = target_opt._get_tensor_tapes(qnode, [params], {})
  
      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]
      )[0]
  
      metric_tensor_expected = get_metric_from_single_input_qnode(
          params, finite_diff_step, tensor_dirs
      )
      # regularize raw metric tensor
      identity = np.identity(metric_tensor_expected.shape[0])
      avg_metric_tensor = 0.5 * (identity + metric_tensor_expected)
      tensor_reg = np.real(
          sqrtm(np.matmul(avg_metric_tensor, avg_metric_tensor))
      )
      tensor_reg = (tensor_reg + regularization * identity) / (
          1 + regularization
      )
  
      inv_metric_tensor = np.linalg.inv(tensor_reg)
      new_params_expected = params - stepsize * np.matmul(
          inv_metric_tensor, grad_expected
      )
      assert np.allclose(new_params_res, new_params_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 4 for calling the TestQNSPSAOptimizer obj's underlying member method code with example usage: obj.test_step_from_single_input(finite_diff_step, seed) without return types