class_docstr: Test the Gradient Descent optimizer
class_docstr_tok: ["Test","the","Gradient","Descent","optimizer"]
docstr: Tests that basic stochastic gradient descent takes gradient-descent steps correctly
  for multivariate functions and with higher dimensional inputs.
docstr_tok: ["Tests","that","basic","stochastic","gradient","descent","takes","gradient-descent","steps","correctly","for","multivariate","functions","and","with","higher","dimensional","inputs","."]
code: def test_gradient_descent_optimizer_multivar_multidim(self, tol):
      """Tests that basic stochastic gradient descent takes gradient-descent steps correctly
      for multivariate functions and with higher dimensional inputs."""
      stepsize = 0.1
      sgd_opt = GradientDescentOptimizer(stepsize)
  
      mvar_mdim_funcs = [
          lambda x: np.sin(x[0, 0])
          + np.cos(x[1, 0])
          - np.sin(x[0, 1])
          + x[1, 1],
          lambda x: np.exp(x[0, 0] / 3) * np.tanh(x[0, 1]),
          lambda x: np.sum([x_[0] ** 2 for x_ in x]),
      ]
      grad_mvar_mdim_funcs = [
          lambda x: (
              np.array(
                  [
                      [np.cos(x[0, 0]), -np.cos(x[0, 1])],
                      [-np.sin(x[1, 0]), 1.0],
                  ]
              ),
          ),
          lambda x: (
              np.array(
                  [
                      [
                          np.exp(x[0, 0] / 3) / 3 * np.tanh(x[0, 1]),
                          np.exp(x[0, 0] / 3)
                          * (1 - np.tanh(x[0, 1]) ** 2),
                      ],
                      [0.0, 0.0],
                  ]
              ),
          ),
          lambda x: (np.array([[2 * x_[0], 0.0] for x_ in x]),),
      ]
  
      x_vals = np.linspace(-10, 10, 16, endpoint=False)
  
      for gradf, f in zip(grad_mvar_mdim_funcs, mvar_mdim_funcs):
          for jdx in range(len(x_vals[:-3])):
              x_vec = x_vals[jdx : jdx + 4]
              x_vec_multidim = np.reshape(x_vec, (2, 2))
              x_new = sgd_opt.step(f, x_vec_multidim)
              x_correct = (
                  x_vec_multidim - gradf(x_vec_multidim)[0] * stepsize
              )
              x_new_flat = x_new.flatten()
              x_correct_flat = x_correct.flatten()
              assert np.allclose(x_new_flat, x_correct_flat, atol=tol)
  
file_path: pennylane/tests/optimize/test_gradient_descent.py
imports: import pytest
  import pennylane
  from pennylane import numpy
  from pennylane.optimize import GradientDescentOptimizer
label_desc: Use image node_id 7 for calling the TestGradientDescentOptimizer obj's underlying member method code with example usage: obj.test_gradient_descent_optimizer_multivar_multidim(tol) without return types