class_docstr: Test the Rotoselect (gradient-free) optimizer
class_docstr_tok: ["Test","the","Rotoselect","(","gradient-free",")","optimizer"]
docstr: Tests that rotoselect optimizer finds the optimal generators and parameters for the
  VQE circuit defined in `this rotoselect tutorial
  <https://pennylane.ai/qml/demos/tutorial_rotoselect.html>`_.
docstr_tok: ["Tests","that","rotoselect","optimizer","finds","the","optimal","generators","and","parameters","for","the","VQE","circuit","defined","in","`","this","rotoselect","tutorial","<","https",":","\/\/pennylane.ai\/qml\/demos\/tutorial_rotoselect.html",">","`","_","."]
code: def test_rotoselect_optimizer(self, x_start, generators, tol):
      """Tests that rotoselect optimizer finds the optimal generators and parameters for the
      VQE circuit defined in `this rotoselect tutorial
      <https://pennylane.ai/qml/demos/tutorial_rotoselect.html>`_."""
  
      # the optimal generators for the 2-qubit VQE circuit
      # H = 0.5 * Y_2 + 0.8 * Z_1 - 0.2 * X_1
      rotoselect_opt = RotoselectOptimizer()
      optimal_generators = [qml.RY, qml.RX]
      possible_generators = [qml.RX, qml.RY, qml.RZ]
      rotoselect_opt.possible_generators = possible_generators
  
      dev = qml.device("default.qubit", shots=None, wires=2)
  
      def ansatz(params, generators):
          generators[0](params[0], wires=0)
          generators[1](params[1], wires=1)
          qml.CNOT(wires=[0, 1])
  
      @qml.qnode(dev)
      def circuit_1(
          params, generators=None
      ):  # generators will be passed as a keyword arg
          ansatz(params, generators)
          return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliY(1))
  
      @qml.qnode(dev)
      def circuit_2(
          params, generators=None
      ):  # generators will be passed as a keyword arg
          ansatz(params, generators)
          return qml.expval(qml.PauliX(0))
  
      def cost_fn(params, generators):
          Z_1, Y_2 = circuit_1(params, generators=generators)
          X_1 = circuit_2(params, generators=generators)
          return 0.5 * Y_2 + 0.8 * Z_1 - 0.2 * X_1
  
      f_best_gen = lambda x: cost_fn(x, optimal_generators)
      optimal_x_start = x_start.copy()
  
      # after four steps the optimzer should find the optimal generators/x_start values
      for _ in range(4):
          x_start, generators = rotoselect_opt.step(
              cost_fn, x_start, generators
          )
          optimal_x_start = self.rotosolve_step(
              f_best_gen, optimal_x_start
          )
  
      assert np.allclose(x_start, optimal_x_start, atol=tol)
      assert generators == optimal_generators
  
file_path: pennylane/tests/optimize/test_rotoselect.py
imports: import itertools
  import numpy
  import pytest
  import pennylane
  from pennylane import numpy
  from pennylane.optimize import RotoselectOptimizer
label_desc: Use image node_id 2 for calling the TestRotoselectOptimizer obj's underlying member method code with example usage: obj.test_rotoselect_optimizer(x_start, generators, tol) and returns: qml,  qml, qml, unknown