code: def test_spin2(electrons, orbitals, coeffs_ref, ops_ref):
      r"""Tests the correctness of the total spin observable :math:`\hat{S}^2`
      built by the function `'spin2'`.
      """
      s2 = qchem.spin.spin2(electrons, orbitals)
      s2_ref = qml.Hamiltonian(coeffs_ref, ops_ref)
      assert s2.compare(s2_ref)
  
      enable_new_opmath()
      s2_pl_op = qchem.spin.spin2(electrons, orbitals)
      disable_new_opmath()
  
      wire_order = s2_ref.wires
      assert not isinstance(s2_pl_op, qml.Hamiltonian)
      assert np.allclose(
          qml.matrix(s2_pl_op, wire_order=wire_order),
          qml.matrix(s2_ref, wire_order=wire_order),
      )
  
file_path: pennylane/tests/qchem/test_spin.py
imports: import pytest
  import pennylane
  from pennylane import Identity, PauliX, PauliY, PauliZ
  from pennylane import numpy
  from pennylane import qchem
  from pennylane.operation import enable_new_opmath, disable_new_opmath
label_desc: Use image node_id 2 for calling a global function with example usage: test_spin2(electrons, orbitals, coeffs_ref, ops_ref) without return types