class_docstr: Tests that the template defines the correct decomposition.
class_docstr_tok: ["Tests","that","the","template","defines","the","correct","decomposition","."]
docstr: Tests that the QPE works correctly for a single operator
docstr_tok: ["Tests","that","the","QPE","works","correctly","for","a","single","operator"]
code: def test_phase_estimated_single_ops(self, param):
      """Tests that the QPE works correctly for a single operator"""
      # pylint: disable=cell-var-from-loop
  
      unitary = qml.RX(param, wires=[0])
  
      # Analytical eigenvectors and phase of the unitary
      eig_vec = np.array([-1 / np.sqrt(2), 1 / np.sqrt(2)])
      phase = param / (4 * np.pi)
  
      estimates = []
      wire_range = range(3, 11)
  
      for wires in wire_range:
          dev = qml.device("default.qubit", wires=wires)
  
          estimation_wires = range(1, wires - 1)
          target_wires = [0]
  
          tape = qml.tape.QuantumScript(
              [
                  qml.StatePrep(eig_vec, wires=target_wires),
                  qml.QuantumPhaseEstimation(
                      unitary, estimation_wires=estimation_wires
                  ),
              ],
              [qml.probs(estimation_wires)],
          )
  
          tapes, _ = dev.preprocess()[0]([tape])
          res = dev.execute(tapes)[0].flatten()
          assert len(tapes) == 1
  
          estimate = np.argmax(res) / 2 ** (wires - 2)
          estimates.append(estimate)
  
      # Check that the error is monotonically decreasing
      for i in range(len(estimates) - 1):
          err1 = np.abs(estimates[i] - phase)
          err2 = np.abs(estimates[i + 1] - phase)
          assert err1 >= err2
  
      # This is a large error, but we'd need to push the qubit number up more to get it lower
      assert np.allclose(estimates[-1], phase, rtol=1e-2)
  
file_path: pennylane/tests/templates/test_subroutines/test_qpe.py
imports: import pytest
  import numpy
  from scipy.stats import unitary_group
  import pennylane
label_desc: Use image node_id 4 for calling the TestDecomposition obj's underlying member method code with example usage: obj.test_phase_estimated_single_ops(param) without return types