# qml.qchem.obs.spin2¶

spin2(electrons, orbitals, mapping='jordan_wigner', wires=None)[source]

Computes the total spin operator $$\hat{S}^2$$.

The total spin operator $$\hat{S}^2$$ is given by

$\hat{S}^2 = \frac{3}{4}N + \sum_{ \bm{\alpha}, \bm{\beta}, \bm{\gamma}, \bm{\delta} } \langle \bm{\alpha}, \bm{\beta} \vert \hat{s}_1 \cdot \hat{s}_2 \vert \bm{\gamma}, \bm{\delta} \rangle ~ \hat{c}_\bm{\alpha}^\dagger \hat{c}_\bm{\beta}^\dagger \hat{c}_\bm{\gamma} \hat{c}_\bm{\delta},$

where the two-particle matrix elements are computed as,

$\begin{split}\langle \bm{\alpha}, \bm{\beta} \vert \hat{s}_1 \cdot \hat{s}_2 \vert \bm{\gamma}, \bm{\delta} \rangle = && \delta_{\alpha,\delta} \delta_{\beta,\gamma} \\ && \times \left( \frac{1}{2} \delta_{s_{z_\alpha}, s_{z_\delta}+1} \delta_{s_{z_\beta}, s_{z_\gamma}-1} + \frac{1}{2} \delta_{s_{z_\alpha}, s_{z_\delta}-1} \delta_{s_{z_\beta}, s_{z_\gamma}+1} + s_{z_\alpha} s_{z_\beta} \delta_{s_{z_\alpha}, s_{z_\delta}} \delta_{s_{z_\beta}, s_{z_\gamma}} \right).\end{split}$

In the equations above $$N$$ is the number of electrons, $$\alpha$$ refer to the quantum numbers of the spatial wave function and $$s_{z_\alpha}$$ is the spin projection of the single-particle state $$\vert \bm{\alpha} \rangle \equiv \vert \alpha, s_{z_\alpha} \rangle$$. The operators $$\hat{c}^\dagger$$ and $$\hat{c}$$ are the particle creation and annihilation operators, respectively.

Parameters
• electrons (int) – Number of electrons. If an active space is defined, this is the number of active electrons.

• orbitals (int) – Number of spin orbitals. If an active space is defined, this is the number of active spin-orbitals.

• mapping (str) – Specifies the transformation to map the fermionic operator to the Pauli basis. Input values can be 'jordan_wigner' or 'bravyi_kitaev'.

• wires (Wires, list, tuple, dict) – Custom wire mapping used to convert the qubit operator to an observable measurable in a PennyLane ansatz. For types Wires/list/tuple, each item in the iterable represents a wire label corresponding to the qubit number equal to its index. For type dict, only int-keyed dict (for qubit-to-wire conversion) is accepted. If None, will use identity map (e.g. 0->0, 1->1, …).

Returns

the total spin observable $$\hat{S}^2$$

Return type

pennylane.Hamiltonian

Example

>>> electrons = 2
>>> orbitals = 4
>>> S2 = spin2(electrons, orbitals, mapping="jordan_wigner")
>>> print(S2)
(0.75) [I0]
+ (0.375) [Z1]
+ (-0.375) [Z0 Z1]
+ (0.125) [Z0 Z2]
+ (0.375) [Z0]
+ (-0.125) [Z0 Z3]
+ (-0.125) [Z1 Z2]
+ (0.125) [Z1 Z3]
+ (0.375) [Z2]
+ (0.375) [Z3]
+ (-0.375) [Z2 Z3]
+ (0.125) [Y0 X1 Y2 X3]
+ (0.125) [Y0 Y1 X2 X3]
+ (0.125) [Y0 Y1 Y2 Y3]
+ (-0.125) [Y0 X1 X2 Y3]
+ (-0.125) [X0 Y1 Y2 X3]
+ (0.125) [X0 X1 X2 X3]
+ (0.125) [X0 X1 Y2 Y3]
+ (0.125) [X0 Y1 X2 Y3]

>>> S2 = spin2(electrons, orbitals, mapping="jordan_wigner", wires=['w0','w1','w2','w3'])
>>> print(S2)
(0.75) [Iw0]
+ (0.375) [Zw1]
+ (-0.375) [Zw0 Zw1]
+ (0.125) [Zw0 Zw2]
+ (0.375) [Zw0]
+ (-0.125) [Zw0 Zw3]
+ (-0.125) [Zw1 Zw2]
+ (0.125) [Zw1 Zw3]
+ (0.375) [Zw2]
+ (0.375) [Zw3]
+ (-0.375) [Zw2 Zw3]
+ (0.125) [Yw0 Xw1 Yw2 Xw3]
+ (0.125) [Yw0 Yw1 Xw2 Xw3]
+ (0.125) [Yw0 Yw1 Yw2 Yw3]
+ (-0.125) [Yw0 Xw1 Xw2 Yw3]
+ (-0.125) [Xw0 Yw1 Yw2 Xw3]
+ (0.125) [Xw0 Xw1 Xw2 Xw3]
+ (0.125) [Xw0 Xw1 Yw2 Yw3]
+ (0.125) [Xw0 Yw1 Xw2 Yw3]