+
Spin Matrices and Linear Algebra Functions
Specialized IPython/Jupyter Notebook material developed by Michael BurnsKaurin  Published March 8, 2022
DOI: 10.1119/PICUP.Exercise.spins
In quantum mechanics, the spin operators for spin components in the *x*, *y*, and *z* directions are represented by matrices whose eigenvectors and eigenvalues are the spin states in that direction and the value of their spin components. These exercises show how to use linear algebra functions in the numpy library to solve spin matrices, to show that the results have the expected properties such as orthogonality, and to calculate measurement probabilities using the complex square of inner products.
Subject Area  Quantum Mechanics 

Level  Advanced 
Specialized Implementation  IPython/Jupyter Notebook 
Learning Objectives 
After completing these exercises, the student will be able to
 set up a numpy array that represents a spin matrix [**Exercises 1 and 4**];
 use a function to find the eigenvalues and eigenvectors of a matrix [**Exercises 1 and 4**];
 print out the eigenvalues and eigenvectors [**Exercises 1 and 4**];
 discuss the choices made during normalization of eigenvectors [**Exercise 2**];
 use functions to calculate inner products and complex conjugates [**Exercise 3**]; and
 compare the results of calculations with experimental measurements [**Exercise 3**].

Time to Complete  120 min 
Exercises are also imbedded in the ipython notebook.
## Exercise 1 ##
The code below shows how to set up a numpy array to represent the spin operator
$$ S_z = \frac{\hbar}{2} \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} $$
(the factor of $\frac{\hbar}{2}$ is left out for simplicity in the code), then find and print the eigenvalues and the eigenvectors (eigenvalues will then need multiplication by $\frac{\hbar}{2}$).
Write code to do the same for the operator for the *x* direction and for the operator for the *y* direction,
$$ S_x = \frac{\hbar}{2} \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} $$
and
$$ S_y = \frac{\hbar}{2} \begin{pmatrix} 0 & i \\ i & 0 \end{pmatrix} .$$
*Note*: Numpy uses "j" as the symbol for the square root of 1. To put the square root of 1 into the array, use "1.j".
*Note*: The code for printing the eigenvectors is strange, because the eigenvectors are in columns of the array, not in the rows indicated by the innermost square brackets. This array is twodimensional since it is an array of arrays. The notation **[j,i]** would mean "give me element *j* of the array that is element *i* of the overall array", in other words row *i* and column *j*. A **a:b** instead of a number is an example of "slicing", which is a way to access a range of elements all at once, in that case elements a through b. Since no numbers are given with the **:**, it means "start at the beginning" and "end at the end". The **[:,i]** therefore means "give me element *i* of each array that is an element of the overall array", which gives column *i*. (The notation **[i]**, however, would give row i.)
```
import numpy as np
S_z = np.array([[1,0],[0,1]])
print(S_z)
z_eval,z_evec = np.linalg.eigh(S_z)
print(z_eval)
print(z_evec)
for i in [0,1]:
print("for eigenvalue",z_eval[i],"eigenvector is",z_evec[:,i])
```
with output
[[ 1 0]
[ 0 1]]
[1. 1.]
[[0. 1.]
[1. 0.]]
for eigenvalue 1.0 eigenvector is [0. 1.]
for eigenvalue 1.0 eigenvector is [1. 0.]
## Exercise 2 ##
If the results do not match the book, then explain why both the code and the book are correct.
## Exercise 3 ##
Look up the function in **np.linalg** to do an inner product. Use that function to check that the eigenvectors you found in Exercise 3 for the *x* direction are orthogonal to each other. Repeat for the *y* direction.
You will also need a function that returns a complex conjugate, to find the complex square of the inner product to check the measurement probabilities. Find the complex square of the inner product of one of the *x* eigenvectors with one of the *y* eigenvectors.
Discuss the results with reference to the experimental result that a particle prepared in an *x* state will have 50% probability of then being measured in one of the *y* states.
## Exercise 4 ##
For a spin1 system, the spin operators in the $S_z$ basis are
$$ S_x = \frac{\hbar}{\sqrt{2}} \begin{pmatrix} 0 & 1 & 0\\
1 & 0 & 1\\
0 & 1 & 0
\end{pmatrix} $$
$$ S_y = \frac{\hbar}{\sqrt{2}} \begin{pmatrix} 0 & i & 0\\
i & 0 & i\\
0 & i & 0
\end{pmatrix} $$
$$ S_z = \hbar \begin{pmatrix} 1 & 0 & 0\\
0 & 0 & 0\\
0 & 0 & 1
\end{pmatrix} $$
with all having eigenvalues $\hbar$, 0, and $\hbar$. Sample results of experiments using two successive SternGerlach magnets (again, as simulated with the **Spins** simulation found [here](https://physics.weber.edu/schroeder/software/Spins.html) or [here](https://sites.science.oregonstate.edu/~mcintyre/ph425/spins/index.html)) are
 a particle prepared in the 1 state of *z* will subsequently have a 25% probability of being measured in the 1 state of *y*,
 a particle prepared in the 0 state of *z* will subsequently have a 50% probability of being measured in the 1 state of *y*, and
 a particle prepared in the 0 state of *z* will subsequently have a 0% probability of being measured in the 0 state of *y*.
Write code to find the eigenvectors and eigenvalues of the spin matrices, check for orthogonality for the eigenvectors for each direction, and check that complex squares of inner products give the measurement probabilities as found by using the **Spins** program. Discuss the results.
Download Options
Share a Variation
Did you have to edit this material to fit your needs? Share your changes by
Creating a Variation
Credits and Licensing
Michael BurnsKaurin, "Spin Matrices and Linear Algebra Functions," Published in the PICUP Collection, March 2022, https://doi.org/10.1119/PICUP.Exercise.spins.
DOI: 10.1119/PICUP.Exercise.spins
The instructor materials are ©2022 Michael BurnsKaurin.
The exercises are released under a Creative Commons AttributionNonCommercialShareAlike 4.0 license