Quantum Mechanics in Python Solutions

Author: Julie Butler

Date Created: September 9, 2024

Last Modified: September 9, 2024

Discrete Variables Example

# Import numpy for vectors and math functions
import numpy as np
# Create a dictionary where the keys are the ages and the values
# are the number of people in the room of that age
ages_dict = {14:1, 15:1, 16:3, 22:2, 24:2, 25:5}

Total Number: \(N = \sum_j N_j\)

# Compute the total number of people in the room by adding up the
# value for every dictionary key
total_number = 0
for i in ages_dict.keys():
    total_number += ages_dict[i]
print("Total Number of People:", total_number)
Total Number of People: 14

Probability: \(P(j) = \frac{N_j}{N}\)

# Make a dictionary for the probabilities by dividing the number of people
# in the room of a certain age by the total number of people in the room.
# Make that the value of the new dictionary and make the key the age.
ages_prob_dict = {}
for i in ages_dict.keys():
    ages_prob_dict[i] = ages_dict[i]/total_number
    print("Age:", i, "Probability:", np.round(ages_prob_dict[i],3))

Expectation Value: \(\langle j \rangle = \sum_j jP(j)\)

# Compute the expected age
expectation_value = 0
for i in ages_dict.keys():
    expectation_value += i*ages_prob_dict[i]
print("Expected Age:", expectation_value)

Expectation Value of a Function: \(\langle f(j) \rangle = \sum_j f(j)P(j)\)

# Define a function to return the insurance rate, I, for a given age, a
def I(a):
    return 5*a+7

# Find the expecation value of the insurance rate
expectation_value_insurance = 0
for i in ages_dict.keys():
    expectation_value_insurance += I(i)*ages_prob_dict[i]
print("Expected Insurance Rate:", expectation_value_insurance)
Expected Insurance Rate: 112.0

Standard Deviation: \(\sigma = \sqrt{\langle j^2\rangle - \langle j \rangle^2}\)

# Compute the expectation value of the ages squared
expectation_value_ages_squared = 0
for i in ages_dict.keys():
    expectation_value_ages_squared += (i**2)*ages_prob_dict[i]

# Use the prior expectation value along with the expected age to find
# the standard deviation
standard_deviation = np.sqrt(expectation_value_ages_squared-expectation_value**2)
print("Standard Deviation:", np.round(standard_deviation,3))
Standard Deviation: 4.309

Example 1

\[|\alpha\rangle = i|1\rangle - 2|2\rangle -i|3\rangle\] \[|\beta\rangle = i|1\rangle + 2|3\rangle = i|1\rangle + 0|2\rangle + 2|3\rangle\] In \(|\beta\rangle\), add a spot for the \(|2\rangle\) state with a coefficient of zero.

Ler’s convert \(|\alpha\rangle\) and \(|\beta\rangle\) into vectors where the first element is the coefficient of \(|1\rangle\), the second element is the coefficient of \(|2\rangle\), and the last elment is the coefficient of \(|3\rangle\).

\[|\alpha\rangle = \begin{bmatrix}i\\-2\\-i\end{bmatrix} \quad |\beta\rangle = \begin{bmatrix}i\\0\\2\end{bmatrix}\]

Now to find \(\langle\alpha|\) and \(\langle\beta|\) we take the complex conjugate transpose of \(|\alpha\rangle\) and \(|\beta\rangle\)

\[\langle\alpha| = (|\alpha\rangle^*)^T = \begin{bmatrix}-i&-2&i\end{bmatrix}\] \[\langle\beta| = (|\beta\rangle^*)^T = \begin{bmatrix}-i&0&2\end{bmatrix}\]

Now we need to check to see if each stae is noramlized by square rooting the inner product of each state with itself.

Magnitude of \(|\alpha\rangle\):

\[\sqrt{\langle\alpha|\alpha\rangle} = \sqrt{\begin{bmatrix}-i&-2&i\end{bmatrix}\begin{bmatrix}i\\-2\\-i\end{bmatrix}} = \sqrt{-i(i) -2(2) +i(-i)} = \sqrt{-(-1) + 4 -(-1)} = \sqrt{6}\]

Not 1 so \(|\alpha\rangle\) is not normalized. Normalize \(|\alpha\rangle\) by dividing every element of \(|\alpha\rangle\) and \(\langle\alpha|\) by its magnitude.

\[|\alpha\rangle = \begin{bmatrix} i/\sqrt{6} \\ -2/\sqrt{6} \\ -i/\sqrt{6}\end{bmatrix} \quad \langle\alpha| = \begin{bmatrix} -i/\sqrt{6} & -2/\sqrt{6} & i/\sqrt{6}\end{bmatrix} \]

Now let’s see of \(|\beta\rangle\) is normlized and if it is not then we will normalize it.

Magnitude of \(|\beta\rangle\):

\[\sqrt{\langle\beta|\beta\rangle} = \sqrt{\begin{bmatrix}-i&0&2\end{bmatrix} \begin{bmatrix}i\\0\\2\end{bmatrix}} = \sqrt{i(-i) + 0(0) + 2(2)} = \sqrt{-(-1) +4} = \sqrt{5}\]

Not 1 so not normalized. Then:

\[|\beta\rangle = \begin{bmatrix}i/\sqrt{5}\\0\\2/\sqrt{5}\end{bmatrix} \quad \langle\beta| = \begin{bmatrix}-i//\sqrt{5}&0&2/\sqrt{5}\end{bmatrix}\]

Remember that we always want to ensure that our wavefunctions are normaluzed after we find the ket and bra because the coefficients are related to the probability of the wavefunction collapsing into each of the basis states.

The below code cell shows the same calculations as the above text.

# Create our basis, where state1 is |1>, state2 is |2>, and
# state3 is |3>
state1 = np.array([1,0,0])
state2 = np.array([0,1,0])
state3 = np.array([0,0,1])

# Create the ket states by multiplying each basis state by the
# corresponding (and given) coefficient
alpha_ket = 1j*state1 - 2*state2 - 1j*state3
beta_ket = 1j*state1 + 2*state3

# Find the magnitude of both alpha and beta
alpha_mag = np.linalg.norm(alpha_ket)
beta_mag = np.linalg.norm(beta_ket)

# Divide alpha and beta by their magnitudes to generate the
# normalized ket states
alpha_ket /= alpha_mag
beta_ket /= beta_mag

# Find the bra states by taking the adjoint (transpose and 
# complex conjugate) of the ket states
alpha_bra = alpha_ket.T.conj()
beta_bra = beta_ket.T.conj()

print("Alpha Bra:", np.round(alpha_bra,3))
print("Beta Bra:", np.round(beta_bra,3))
Alpha Bra: [ 0.   -0.408j -0.816-0.j     0.   +0.408j]
Beta Bra: [0.   -0.447j 0.   -0.j    0.894-0.j   ]

Now to compute \(\langle\alpha|\beta\rangle\) and \(\langle\beta|\alpha\rangle\). Here we will want the states in their vector form. Make sure to use normalized states.

\[\langle\alpha|\beta\rangle = \begin{bmatrix}-i/\sqrt{6}&-2/\sqrt{6}&i/\sqrt{6}\end{bmatrix}\begin{bmatrix}i/\sqrt{5}\\0\\2/\sqrt{5}\end{bmatrix} = \frac{-i}{\sqrt{6}}(\frac{i}{\sqrt{5}}) + \frac{-2}{\sqrt{6}}(0) + \frac{i}{\sqrt{6}}(\frac{2}{\sqrt{5}}) = \frac{1}{\sqrt{30}} + \frac{2i}{\sqrt{30}} = \frac{1+2i}{\sqrt{30}}\]

\[\langle\beta|\alpha\rangle = \begin{bmatrix}-i/\sqrt{5}&0&2/\sqrt{5}\end{bmatrix}\begin{bmatrix}i/\sqrt{6}\\-2/\sqrt{6}\\-i/\sqrt{6}\end{bmatrix} = \frac{-1}{\sqrt{5}}(\frac{i}{\sqrt{6}}) + 0(\frac{-2}{\sqrt{6}}) + \frac{2}{\sqrt{5}}(\frac{-i}{\sqrt{6}}) = \frac{1}{\sqrt{30}} + \frac{-2i}{\sqrt{30}} = \frac{1-2i}{\sqrt{30}}\]

Furthermore from the above results note that \(\langle\beta|\alpha\rangle = \langle\alpha|\beta\rangle^*\). This is a general finding for all inner products!

The below code cell shows the same calculations as the above text.

# Find the inner products using the np.dot function
alpha_beta = np.dot(alpha_bra, beta_ket)
beta_alpha = np.dot(beta_bra, alpha_ket)

print("Alpha Bra, Beta Ket:", np.round(alpha_beta,3))
print("Beta Bra, Alpha Ket:", np.round(beta_alpha,3))

# Test to see if the given relation is true
print(alpha_beta == beta_alpha.conj())
Alpha Bra, Beta Ket: (0.183+0.365j)
Beta Bra, Alpha Ket: (0.183-0.365j)
True

Example 2

# hbar, omega, lambda, and mu are positive constants, so just set them
# equal to 1. Note that lambda is a reserved word in Python so use lamb 
# instead.
hbar = omega = lamb = mu = 1
# Define the Hamiltonian matrix as well as the operator matrices A and B
H = hbar*omega*np.array([[1,0,0],[0,2,0],[0,0,2]])
A = lamb*np.array([[0,1,0],[1,0,0],[0,0,2]])
B = mu*np.array([[2,0,0],[0,0,1],[0,1,0]])

The possible energies of the system are the eigenvalues of the Hamiltonian, so using an eigenvalue solver we get \(E_1 = \hbar\omega\), \(E_2 = 2\hbar\omega\), and \(E_3 = 2\hbar\omega\).

# Use the Numpy eig function to get the eigenvalues of the Hamiltonian
# (which are the energies of the system) and the eigenvectors of the 
# Hamiltonian (which are the computational basis state of the system)
energies, states = np.linalg.eig(H)
print("Energies:", energies)
Energies: [1. 2. 2.]

To find a we need to find \(\langle\psi|\) and then take the inner product.

\[|\psi\rangle = a\begin{bmatrix}-i\\3\\2i\end{bmatrix} \longrightarrow \langle\psi| = a^*\begin{bmatrix}i&3&-2i\end{bmatrix}\]

We want to find a so the following is true: \[\langle\psi|\psi\rangle = 1\] \[a^*\begin{bmatrix}i&3&-2i\end{bmatrix}a\begin{bmatrix}-i\\3\\2i\end{bmatrix} = 1\]

Gather all scalars and note that \(aa^* = |a|^2\)

\[|a|^2\begin{bmatrix}i&3&-2i\end{bmatrix}\begin{bmatrix}-i\\3\\2i\end{bmatrix} = 1\]

\[|a|^2(i(-i) + 3(3) + -2i(2i)) = 1\]

\[|a|^2(1+9+4) = 1\]

\[|a|^2(14) = 1\]

\[|a|^2 = \frac{1}{14}\]

\[a = \sqrt{\frac{1}{14}}\]

# Define the ket psi, find its magnitude, and then normalize psi
psi = np.array([-1j,3,2j])
a = np.linalg.norm(psi)
print("a:", np.round(1/a,3))
psi /= a

# Ensure that the computed value of a is the same from the hand-solved
# problem
print(np.round(1/a,3) == np.round(np.sqrt(1/14),3))
a: 0.267
True

So our normaluized wavefunction is:

\[|\psi\rangle = \frac{1}{\sqrt{14}}\begin{bmatrix}-1\\3\\2i\end{bmatrix}\]

with the corresponding bra being

\[\langle\psi| = \frac{1}{\sqrt{14}}\begin{bmatrix}i&3&-2i\end{bmatrix}\]

Now let’s find \(\langle\psi|A|\psi\rangle\)

\[\langle\psi|A|\psi\rangle = \frac{1}{\sqrt{14}}\begin{bmatrix}i&3&-2i\end{bmatrix}\lambda\begin{bmatrix}0&1&0\\1&0&0\\0&0&2\end{bmatrix}\frac{1}{\sqrt{14}}\begin{bmatrix}-1\\3\\2i\end{bmatrix}\]

First gather all the scalars together

\[\langle\psi|A|\psi\rangle = \frac{\lambda}{4}\begin{bmatrix}i&3&-2i\end{bmatrix}\begin{bmatrix}0&1&0\\1&0&0\\0&0&2\end{bmatrix}\begin{bmatrix}-1\\3\\2i\end{bmatrix}\]

Multiply the matrix by the last vector using Wolfram Alpha

\[\langle\psi|A|\psi\rangle = \frac{\lambda}{4}\begin{bmatrix}i&3&-2i\end{bmatrix}\begin{bmatrix}3\\-i\\4i\end{bmatrix}\]

Now take the dot product of the two remaining vectors and simplify

\[\langle\psi|A|\psi\rangle = \frac{\lambda}{14}(i(3) + 3(-i) -2i(4i)) = \frac{\lambda}{14}(8) = \frac{8\lambda}{14} = \frac{4\lambda}{7}\]

Now for \(\langle\psi|B|\psi\rangle\)

\[\langle\psi|B|\psi\rangle = \frac{1}{\sqrt{14}}\begin{bmatrix}i&3&-2i\end{bmatrix}\mu\begin{bmatrix}2&0&0\\0&0&1\\0&1&0\end{bmatrix}\frac{1}{\sqrt{14}}\begin{bmatrix}-1\\3\\2i\end{bmatrix} = \frac{\mu}{14}\begin{bmatrix}i&3&-2i\end{bmatrix}\begin{bmatrix}2&0&0\\0&0&1\\0&1&0\end{bmatrix}\begin{bmatrix}-1\\3\\2i\end{bmatrix}\]

\[\langle\psi|B|\psi\rangle = \frac{\mu}{14}\begin{bmatrix}i&3&-2i\end{bmatrix}\begin{bmatrix}-2i\\2i\\3\end{bmatrix} = \frac{\mu}{14}(i(-2i)+3(2i)-2i(3)) = \frac{2\mu}{14} = \frac{\mu}{7}\]

Both \(\langle\psi|A|\psi\rangle\) and \(\langle\psi|B|\psi\rangle\) are expectation values meaning they are average results of many observations but not necessarily a possible value for a measurement.

# Find the corresponding bra state by taking the adjoint
psi_bra = psi.T.conj()

# Do the multiplication to find the expectation values, print
# the results, and then ensure that the results are the same as
# when solved by hand.
expectation_A = psi_bra@A@psi
expectation_B = psi_bra@B@psi

print("Expectation Value of A:", np.round(expectation_A,3))
print("Expectation Value of B:", np.round(expectation_B,3))

print(np.round(expectation_A,3) == np.round((4*lamb)/7,3))
print(np.round(expectation_B,3) == np.round(mu/7,3))
Expectation Value of A: (0.571+0j)
Expectation Value of B: (0.143+0j)
True
True

Finally, the eigenvectors of H are \(\begin{bmatrix}1\\0\\0\end{bmatrix}\), \(\begin{bmatrix}0\\1\\0\end{bmatrix}\), and \(\begin{bmatrix}0\\0\\1\end{bmatrix}\) with the first corresponding to the eigenvalue/energy \(\hbar\omega\) and teh second two corresponding to the eigenvalue/energy \(2\hbar\omega\).

The eigenvectors of our Hamiltonian form our computational basis so let’s rewrite \(|\psi\rangle\) using these eigenvectors

\[|\psi\rangle = \frac{1}{\sqrt{14}}\begin{bmatrix}-i\\3\\2i\end{bmatrix} = \frac{-i}{\sqrt{14}}\begin{bmatrix}1\\0\\0\end{bmatrix} + \frac{3}{\sqrt{14}}\begin{bmatrix}0\\1\\-\end{bmatrix}+\frac{2i}{\sqrt{14}}\begin{bmatrix}0\\0\\1\end{bmatrix}\]

Since the coefficient in from of the eigenvector that corresponds to the \(\hbar\omega\) energy state is \(c = \frac{-i}{\sqrt{14}}\), then the probability of measuring the energy of the system and getting \(\hbar\omega\) is \(P(\hbar\omega) = |c|^2 = |\frac{-i}{\sqrt{14}}|^2 = \frac{-i}{\sqrt{14}}\frac{i}{\sqrt{14}} = \frac{1}{14}\)

# \hbar\omega corresponds to the first energy in the eneriges list, so
# extract it and the corresponding state. See the documentation for the
# eig function for why its the column
energy = energies[0]
state = states[:,0]
# Find the bra of the extracted state
state_bra = state.T.conj()

# Get the cofficient that corresponds the the portion of psi which comes
# from the selected state
coefficient = np.dot(state_bra,psi)

# Use the coefficient to get the probability, print it, and make sure it
# is the same as the hand-solved method
probability = np.real(coefficient*coefficient.conj())

print(r"Probability of $\hbar\omega$:", np.round(probability,3))
print(np.round(probability,3) == np.round(1/14,3))
Probability of $\hbar\omega$: 0.071
True

Example 3

\[|\psi\rangle = \frac{1}{\sqrt{6}}\begin{bmatrix}i+i\\2\end{bmatrix}\]

Check to see if the wavefunction is normalized \[\langle\psi| = \frac{1}{\sqrt{6}}\begin{bmatrix}1-i&2\end{bmatrix}\]

\[\langle\psi|\psi\rangle = \frac{1}{\sqrt{6}}\begin{bmatrix}1-i&2\end{bmatrix}\frac{1}{\sqrt{6}}\begin{bmatrix}i+i\\2\end{bmatrix} = \frac{1}{6}\begin{bmatrix}1-i&2\end{bmatrix}\begin{bmatrix}i+i\\2\end{bmatrix} = \frac{1}{6}((1-i)(i+i) + 2(2)) = \frac{1}{6}(1-i+i-(i^2)+4) = \frac{1}{6}(1+1+4) = \frac{6}{6} = 1\]

The wavefunction is normalized since the inner product is 1.

From the slides we know that \[S_x = \frac{\hbar}{2}\begin{bmatrix}0&1\\1&0\end{bmatrix} \quad S_z = \frac{\hbar}{2}\begin{bmatrix}1&0\\0&-1\end{bmatrix}\]

Let’s start by finding the eigenvalue and eigenvector pairs for both matrices. Find with Wolfram or Python and ensure that the wavefunctions are normalized.

\[S_x: \frac{\hbar}{2}\longrightarrow\begin{bmatrix}1/\sqrt{2}\\1/\sqrt{2}\end{bmatrix} \quad\quad \frac{-\hbar}{2} \longrightarrow \begin{bmatrix} -1/\sqrt{2}\\1/\sqrt{2}\end{bmatrix}\] \[S_x: \frac{\hbar}{2}\longrightarrow\begin{bmatrix}1\\0\end{bmatrix} \quad\quad \frac{-\hbar}{2} \longrightarrow \begin{bmatrix} 0\\1\end{bmatrix}\]

# Define the state psi
psi = (1/np.sqrt(6))*np.array([1+1j,2])

# Define S_x and S_z from the slides
Sx = (hbar/2)*np.array([[0,1],[1,0]])
Sz = (hbar/2)*np.array([[1,0],[0,-1]])

# Get the eigenvalues and eigenvectors using the eig function
# The eigenvalues are the possible observables and the 
# eigenvectors are the states that psi could collapse into
# once measured
x_observables, x_states = np.linalg.eig(Sx)
z_observables, z_states = np.linalg.eig(Sz)

# Print the eigenvalues to find which order they are in
print("Possible Measurements for $S_x$:", x_observables)
print("Possible Measurements for $S_z$:", z_observables)

# Extract the state corresponding to measuring \hbar/2 in the x direction
# and then the state corresponding to measuring -\hbar/2 in the x direction
x_positive_state = x_states[:,0]
x_negative_state = x_states[:,1]

# Extract the state corresponding to measuring \hbar/2 in the z direction
# and then the state corresponding to measuring -\hbar/2 in the z direction
z_positive_state = z_states[:,0]
z_negative_state = z_states[:,1]
Possible Measurements for $S_x$: [ 0.5 -0.5]
Possible Measurements for $S_z$: [ 0.5 -0.5]

Now we need to use the fact that if we measure a specific eigenvale of an operator then our wavefunction has collapsed into the state represented by the eigenvector.

\(S_x\): Let \(|+\rangle = \begin{bmatrix}1/\sqrt{2}\\1/\sqrt{2}\end{bmatrix}\) and \(|-\rangle = \begin{bmatrix}-1/\sqrt{2}\\1/\sqrt{2}\end{bmatrix}\) where \(|+\rangle\) corresponds to the eigenvalye \(\frac{\hbar}{2}\) and \(|-\rangle\) corresponds to the eigenvalue \(\frac{-\hbar}{2}\).

Therefore the probability of measuring \(S_x\) and getting \(\frac{\hbar}{2}\) is:

\[P(\frac{\hbar}{2}) = |\langle + | \psi \rangle |^2 = |\begin{bmatrix}1/\sqrt{2}&1/\sqrt{2}\end{bmatrix}\frac{1}{\sqrt{6}}\begin{bmatrix}1+i\\2\end{bmatrix}|^2 = |\frac{1}{\sqrt{6}}(\frac{1}{\sqrt{2}}(1+i) + \frac{1}{\sqrt{2}}(2))|^2 = |\frac{1}{\sqrt{6}} (\frac{1}{\sqrt{2}} + \frac{i}{\sqrt{2}} + \frac{2}{\sqrt{2}})|^2\]

\[ = |\frac{1}{\sqrt{6}}|\frac{3}{\sqrt{2}} + \frac{i}{\sqrt{2}})|^2 = |\frac{3}{\sqrt{12}} + \frac{i}{\sqrt{12}}|^2 = \frac{9}{12} + \frac{1}{12} = \frac{10}{12} = \frac{5}{6}\]

Then the probability of measuring \(S_x\) and getting \(\frac{-\hbar}{2}\) is:

\[P(\frac{-\hbar}{2}) = |\langle - |\psi\rangle|^2 = |\begin{bmatrix}-1/\sqrt{2}&1/\sqrt{2}\end{bmatrix}\frac{1}{\sqrt{6}}\begin{bmatrix}1+i\\2\end{bmatrix} = |\frac{1}{\sqrt{6}} (\frac{-1}{\sqrt{2}}(1+i) + \frac{2}{\sqrt{2}})|^2 = |\frac{1}{\sqrt{6}}(\frac{-1}{\sqrt{2}} - \frac{i}{\sqrt{2}} + \frac{2}{\sqrt{2}})|^2\]

\[= |\frac{1}{\sqrt{6}}(\frac{1}{\sqrt{2}} - \frac{i}{\sqrt{2}})|^2 = |\frac{1}{\sqrt{12}} - \frac{i}{\sqrt{12}}|^2 = \frac{1}{12} + \frac{1}{12} = \frac{2}{12} = \frac{1}{6}\]

ote that \(P(\frac{\hbar}{2}) + P(\frac{-\hbar}{2}) =1\) which is required since \(\frac{\hbar}{2}\) and \(\frac{-\hbar}{2}\) are the only ossible outcomes of a measurement \(\longrightarrow\) they are the only eigenvalues of \(S_x\).

Do the same process for \(S_z\) where \(|0\rangle = \begin{bmatrix}1\\0\end{bmatrix}\) and \(|1\rangle = \begin{bmatrix}0\\1\end{bmatrix}\)

\[P(\frac{\hbar}{2}) = |\langle 0 |\psi\rangle|^2 = |\begin{bmatrix} 1 & 0 \end{bmatrix}\frac{1}{\sqrt{6}}\begin{bmatrix}1+i\\2\end{bmatrix}|^2 = |\frac{1}{\sqrt{6}}(1(1+i))|^2 = |\frac{1}{\sqrt{6}}(1+i)|^2 = |\frac{1}{\sqrt{6}} + \frac{i}{\sqrt{6}}|^2 = \frac{1}{6} + \frac{1}{6} = \frac{2}{6} = \frac{1}{3}\]

\[P(\frac{-\hbar}{2}) = |\langle 1 | \psi \rangle|^2 = |\begin{bmatrix} 0 & 1 \end{bmatrix}\frac{1}{\sqrt{6}}\begin{bmatrix}1+i\\2\end{bmatrix}|^2 = |\frac{1}{\sqrt{6}}(1)(2)|^2 = |\frac{2}{\sqrt{6}}|^2 = \frac{4}{6} = \frac{2}{3}\]

Note again that \(P(\frac{\hbar}{2}) + P(\frac{-\hbar}{2}) =1\) as expected.

# Compute the coefficients for each of the above for states with the wavefunction psi
coef_x_positive = np.dot(x_positive_state.T.conj(), psi)
coef_x_negative = np.dot(x_negative_state.T.conj(), psi)
coef_z_positive = np.dot(z_positive_state.T.conj(), psi)
coef_z_negative = np.dot(z_negative_state.T.conj(), psi)

# Convert each of the coefficients into a probability and then print the results
prob_x_positive = coef_x_positive*coef_x_positive.conj()
prob_x_negative = coef_x_negative*coef_x_negative.conj()
prob_z_positive = coef_z_positive*coef_z_positive.conj()
prob_z_negative = coef_z_negative*coef_z_negative.conj()

print("Probability of measuring $S_x$ and getting $\frac{\hbar}{2}$:", np.round(prob_x_positive,3))
print("Probability of measuring $S_x$ and getting $\frac{-\hbar}{2}$:", np.round(prob_x_negative,3))
print("Probability of measuring $S_z$ and getting $\frac{\hbar}{2}$:", np.round(prob_z_positive,3))
print("Probability of measuring $S_z$ and getting $\frac{-\hbar}{2}$:", np.round(prob_z_negative,3))
Probability of measuring $S_x$ and getting $rac{\hbar}{2}$: (0.833+0j)
Probability of measuring $S_x$ and getting $rac{-\hbar}{2}$: (0.167+0j)
Probability of measuring $S_z$ and getting $rac{\hbar}{2}$: (0.333+0j)
Probability of measuring $S_z$ and getting $rac{-\hbar}{2}$: (0.667+0j)
# Compare the Python and by hand results
print(np.round(prob_x_positive,3) == np.round(5/6,3))
print(np.round(prob_x_negative,3) == np.round(1/6,3))
print(np.round(prob_z_positive,3) == np.round(1/3,3))
print(np.round(prob_z_negative,3) == np.round(2/3,3))
True
True
True
True

Example 4

\[|\psi\rangle = A\begin{bmatrix}3i\\4\end{bmatrix}\]

Based on in class example \(A = \frac{1}{5}\)

\[|\psi\rangle = \frac{1}{5}\begin{bmatrix}3i\\4\end{bmatrix}\]

\[S_x = \frac{\hbar}{2}\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \quad S_y = \frac{\hbar}{2}\begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix} \quad S_z = \frac{\hbar}{2}\begin{bmatrix}1 & 0 \\ 0 & -1 \end{bmatrix}\]

# Define psi, find its magnitude, and normalize psi by dividing it by the magnitude
psi = np.array([3j,4])
A = np.linalg.norm(psi)
print("A:", 1/A)
psi /= A

# Define Sy (Sx and Sy defined previously)
Sy = (hbar/2)*np.array([[0,-1j],[1j,0]])

# Compute the bra of the state psi
psi_bra = psi.T.conj()
A: 0.2

Now compute the expectation values

\[\langle\psi|S_x|\psi\rangle = \frac{1}{5}\begin{bmatrix}-3i&4\end{bmatrix}\frac{\hbar}{2}\begin{bmatrix}0&1\\1&0\end{bmatrix}\frac{1}{5}\begin{bmatrix}3i\\4\end{bmatrix} = \frac{\hbar}{50}\begin{bmatrix}-3i&4\end{bmatrix}\begin{bmatrix}0&1\\1&0\end{bmatrix}\begin{bmatrix}3i\\4\end{bmatrix} = \frac{\hbar}{50}\begin{bmatrix}-3i&4\end{bmatrix}\begin{bmatrix}4\\3i\end{bmatrix} = \frac{\hbar}{50}(-3i(4) + 4(3i)) = 0\] \[\langle\psi|S_y|\psi\rangle = \frac{1}{5}\begin{bmatrix}-3i&4\end{bmatrix}\frac{\hbar}{2}\begin{bmatrix}0&-i\\i&0\end{bmatrix}\frac{1}{5}\begin{bmatrix}3i\\4\end{bmatrix} = \frac{\hbar}{50}\begin{bmatrix}-3i&4\end{bmatrix}\begin{bmatrix}0&-i\\i&0\end{bmatrix}\begin{bmatrix}3i\\4\end{bmatrix} = \frac{\hbar}{50}\begin{bmatrix}-3i&4\end{bmatrix}\begin{bmatrix}-4i\\-3\end{bmatrix} = \frac{\hbar}{50}(-3i(-4i)+4(-3)) \] \[= \frac{\hbar}{50}(-12+-12) = \frac{-24\hbar}{50} = \frac{-12\hbar}{25}\]

\[\langle \psi | S_z | \psi \rangle = \frac{1}{5}\begin{bmatrix}-3i&4\end{bmatrix}\frac{\hbar}{2}\begin{bmatrix}1&0\\0&-1\end{bmatrix}\frac{1}{5}\begin{bmatrix}3i\\4\end{bmatrix} = \frac{\hbar}{50}\begin{bmatrix}-3i&4\end{bmatrix}\begin{bmatrix}1&0\\0&-1\end{bmatrix}\begin{bmatrix}3i\\4\end{bmatrix} \] \[= \frac{\hbar}{50}\begin{bmatrix}-3i&4\end{bmatrix}\begin{bmatrix}3i\\-4\end{bmatrix} = \frac{\hbar}{50}(-3i(3i) + 4(-4)) = \frac{\hbar}{50}(9-16) = \frac{-7\hbar}{50}\]

# Find all of the expectation values, and then print the results
expectation_Sx = psi_bra@Sx@psi
expectation_Sy = psi_bra@Sy@psi
expectation_Sz = psi_bra@Sz@psi

print("Expectation Sx:", np.round(expectation_Sx,3))
print("Expectation Sy:", np.round(expectation_Sy,3))
print("Expectation Sz:", np.round(expectation_Sz,3))
Expectation Sx: -0j
Expectation Sy: (-0.48+0j)
Expectation Sz: (-0.14+0j)
# Compare the Python and hand calculated results
print(np.round(expectation_Sx,2) == 0)
print(np.round(expectation_Sy,2) == -24*hbar/50)
print(np.round(expectation_Sz,2) == -7*hbar/50)
True
True
True