class_docstr: Test the Rademacher distribution sampler.
class_docstr_tok: ["Test","the","Rademacher","distribution","sampler","."]
docstr: Test that the mean and variance of many produced samples are
  close to the theoretical values.
docstr_tok: ["Test","that","the","mean","and","variance","of","many","produced","samples","are","close","to","the","theoretical","values","."]
code: def test_mean_and_var(self, ids, num, N):
      """Test that the mean and variance of many produced samples are
      close to the theoretical values."""
      rng = np.random.default_rng(42)
      ids_mask = np.zeros(num, dtype=bool)
      ids_mask[ids] = True
      outputs = [
          _rademacher_sampler(ids, num, rng=rng) for _ in range(N)
      ]
      # Test that the mean of non-zero entries is approximately right
      assert np.allclose(
          np.mean(outputs, axis=0)[ids_mask], 0, atol=4 / np.sqrt(N)
      )
      # Test that the variance of non-zero entries is approximately right
      assert np.allclose(
          np.var(outputs, axis=0)[ids_mask], 1, atol=4 / N
      )
      # Test that the mean of zero entries is exactly 0, because all entries should be
      assert np.allclose(
          np.mean(outputs, axis=0)[~ids_mask], 0, atol=1e-8
      )
      # Test that the variance of zero entries is exactly 0, because all entries are the same
      assert np.allclose(
          np.var(outputs, axis=0)[~ids_mask], 0, atol=1e-8
      )
  
imports: import numpy
  import pytest
  import pennylane
  from pennylane import numpy
  from pennylane.devices import DefaultQubitLegacy
  from pennylane.gradients import spsa_grad
  from pennylane.gradients.spsa_gradient import _rademacher_sampler
  from pennylane.operation import AnyWires, Observable
label_desc: Use image node_id 5 for calling the TestRademacherSampler obj's underlying member method code with example usage: obj.test_mean_and_var(ids, num, N) without return types