class_docstr: Tests that ensure various qubit circuits integrate correctly when computing higher-order derivatives
class_docstr_tok: ["Tests","that","ensure","various","qubit","circuits","integrate","correctly","when","computing","higher-order","derivatives"]
docstr: Test that the variance of a projector is correctly returned
docstr_tok: ["Test","that","the","variance","of","a","projector","is","correctly","returned"]
code: def test_projector(
      self,
      state,
      dev,
      diff_method,
      grad_on_execution,
      device_vjp,
      interface,
      tol,
  ):
      """Test that the variance of a projector is correctly returned"""
      gradient_kwargs = {}
      if diff_method == "adjoint":
          pytest.skip(
              "adjoint supports all expvals or only diagonal measurements."
          )
      if diff_method == "hadamard":
          pytest.skip("Hadamard does not support var.")
      elif diff_method == "spsa":
          gradient_kwargs = {
              "h": H_FOR_SPSA,
              "sampler_rng": np.random.default_rng(SEED_FOR_SPSA),
          }
          tol = TOL_FOR_SPSA
  
      P = jax.numpy.array(state)
      x, y = 0.765, -0.654
  
      @qnode(
          dev,
          diff_method=diff_method,
          interface=interface,
          grad_on_execution=grad_on_execution,
          device_vjp=device_vjp,
          **gradient_kwargs,
      )
      def circuit(x, y):
          qml.RX(x, wires=0)
          qml.RY(y, wires=1)
          qml.CNOT(wires=[0, 1])
          return qml.var(qml.Projector(P, wires=0) @ qml.PauliX(1))
  
      res = circuit(x, y)
      expected = (
          0.25
          * np.sin(x / 2) ** 2
          * (3 + np.cos(2 * y) + 2 * np.cos(x) * np.sin(y) ** 2)
      )
      assert np.allclose(res, expected, atol=tol, rtol=0)
  
      res = jax.grad(circuit, argnums=[0, 1])(x, y)
      expected = np.array(
          [
              0.5
              * np.sin(x)
              * (
                  np.cos(x / 2) ** 2
                  + np.cos(2 * y) * np.sin(x / 2) ** 2
              ),
              -2 * np.cos(y) * np.sin(x / 2) ** 4 * np.sin(y),
          ]
      )
      assert np.allclose(res, expected, atol=tol, rtol=0)
  
imports: from itertools import product
  import numpy
  import pytest
  import pennylane
  from pennylane import qnode
  from pennylane.devices import DefaultQubit
label_desc: Use image node_id 7 for calling the TestQubitIntegrationHigherOrder obj's underlying member method code with example usage: obj.test_projector(state, dev, diff_method, grad_on_execution, device_vjp, interface, tol) and returns: qml