class_docstr: Tests for the QuantumMonteCarlo template
class_docstr_tok: ["Tests","for","the","QuantumMonteCarlo","template"]
docstr: Test that the QuantumMonteCarlo template can correctly estimate the expectation value
  following the example in the usage details
docstr_tok: ["Test","that","the","QuantumMonteCarlo","template","can","correctly","estimate","the","expectation","value","following","the","example","in","the","usage","details"]
code: def test_expected_value(self):
      """Test that the QuantumMonteCarlo template can correctly estimate the expectation value
      following the example in the usage details"""
      # pylint: disable=cell-var-from-loop
      m = 5
      M = 2**m
  
      xmax = np.pi
      xs = np.linspace(-xmax, xmax, M)
  
      probs = np.array([norm().pdf(x) for x in xs])
      probs /= np.sum(probs)
  
      func = lambda i: np.cos(xs[i]) ** 2
  
      estimates = []
  
      for n in range(4, 11):
          N = 2**n
  
          target_wires = range(m + 1)
          estimation_wires = range(m + 1, n + m + 1)
  
          dev = qml.device("default.qubit", wires=(n + m + 1))
  
          @qml.qnode(dev)
          def circuit():
              qml.QuantumMonteCarlo(
                  probs,
                  func,
                  target_wires=target_wires,
                  estimation_wires=estimation_wires,
              )
              return qml.probs(estimation_wires)
  
          phase_estimated = np.argmax(circuit()[: int(N / 2)]) / N
          mu_estimated = (1 - np.cos(np.pi * phase_estimated)) / 2
          estimates.append(mu_estimated)
  
      exact = 0.432332358381693654
  
      # Check that the error is monotonically decreasing
      for i in range(len(estimates) - 1):
          err1 = np.abs(estimates[i] - exact)
          err2 = np.abs(estimates[i + 1] - exact)
          assert err1 >= err2
  
      assert np.allclose(estimates[-1], exact, rtol=1e-3)
  
imports: import numpy
  import pytest
  from scipy.stats import norm
  import pennylane
  from pennylane.templates.subroutines.qmc import QuantumMonteCarlo, _make_V, _make_Z, func_to_unitary, make_Q, probs_to_unitary
  from pennylane.wires import Wires
label_desc: Use image node_id 7 for calling the TestQuantumMonteCarlo obj's underlying member method code with example usage: obj.test_expected_value() and returns: qml