class_docstr: Test the default gaussian device. The test ensures that the device is properly
  applying gaussian operations and calculating the correct observables.
class_docstr_tok: ["Test","the","default","gaussian","device",".","The","test","ensures","that","the","device","is","properly","applying","gaussian","operations","and","calculating","the","correct","observables","."]
docstr: Test that expectation values are calculated correctly
docstr_tok: ["Test","that","expectation","values","are","calculated","correctly"]
code: def test_expectation(self, tol):
      """Test that expectation values are calculated correctly"""
  
      dev = qml.device("default.gaussian", wires=1, hbar=hbar)
  
      # test correct mean for <n> of a displaced thermal state
      nbar = 0.5431
      alpha = 0.324 - 0.59j
      dev.apply("ThermalState", wires=Wires([0]), par=[nbar])
      dev.apply("Displacement", wires=Wires([0]), par=[alpha, 0])
      mean = dev.expval("NumberOperator", Wires([0]), [])
      assert mean == pytest.approx(np.abs(alpha) ** 2 + nbar, abs=tol)
  
      # test correct mean for Homodyne P measurement
      alpha = 0.324 - 0.59j
      dev.apply("CoherentState", wires=Wires([0]), par=[alpha])
      mean = dev.expval("QuadP", Wires([0]), [])
      assert mean == pytest.approx(
          alpha.imag * np.sqrt(2 * hbar), abs=tol
      )
  
      # test correct mean for Homodyne measurement
      mean = dev.expval("QuadOperator", Wires([0]), [np.pi / 2])
      assert mean == pytest.approx(
          alpha.imag * np.sqrt(2 * hbar), abs=tol
      )
  
      # test correct mean for number state expectation |<n|alpha>|^2
      # on a coherent state
      for n in range(3):
          mean = dev.expval(
              "FockStateProjector", Wires([0]), [np.array([n])]
          )
          expected = (
              np.abs(
                  np.exp(-np.abs(alpha) ** 2 / 2)
                  * alpha**n
                  / np.sqrt(fac(n))
              )
              ** 2
          )
          assert mean == pytest.approx(expected, abs=tol)
  
      # test correct mean for number state expectation |<n|S(r)>|^2
      # on a squeezed state
      n = 1
      r = 0.4523
      dev.apply("SqueezedState", wires=Wires([0]), par=[r, 0])
      mean = dev.expval(
          "FockStateProjector", Wires([0]), [np.array([2 * n])]
      )
      expected = (
          np.abs(
              np.sqrt(fac(2 * n))
              / (2**n * fac(n))
              * (-np.tanh(r)) ** n
              / np.sqrt(np.cosh(r))
          )
          ** 2
      )
      assert mean == pytest.approx(expected, abs=tol)
  
imports: import numpy
  import numpy.random
  import pytest
  from scipy.linalg import block_diag
  from scipy.special import factorial
  import pennylane
  from pennylane import DeviceError
  from pennylane.devices.default_gaussian import fock_prob, rotation, squeezing, quadratic_phase, beamsplitter, two_mode_squeezing, controlled_addition, controlled_phase, vacuum_state, coherent_state, squeezed_state, displaced_squeezed_state, thermal_state
  from pennylane.wires import Wires
label_desc: Use image node_id 8 for calling the TestDefaultGaussianDevice obj's underlying member method code with example usage: obj.test_expectation(tol) without return types