import numpy as np
from qutip import *

"""
Explanation: Lab 2 - Quantum States, SOLUTIONS
Useful for working examples and problems with photon quantum states. You may notice some similarity to the Jones Calculus ;-)
End of explanation
"""


H = Qobj([[1],[0]])
V = Qobj([[0],[1]])
P45 = Qobj([[1/np.sqrt(2)],[1/np.sqrt(2)]])
M45 = Qobj([[1/np.sqrt(2)],[-1/np.sqrt(2)]])
R = Qobj([[1/np.sqrt(2)],[-1j/np.sqrt(2)]])
L = Qobj([[1/np.sqrt(2)],[1j/np.sqrt(2)]])

V

Hbra*V

Hbra = H.dag()

"""
Explanation: These are the polarization states:
End of explanation
"""


def HWP(theta):
    return Qobj([[np.cos(2*theta),np.sin(2*theta)],[np.sin(2*theta),-np.cos(2*theta)]]).tidyup()

def LP(theta):
    return Qobj([[np.cos(theta)**2,np.cos(theta)*np.sin(theta)],[np.sin(theta)*np.cos(theta),np.sin(theta)**2]]).tidyup()

def QWP(theta):
    return Qobj([[np.cos(theta)**2 + 1j*np.sin(theta)**2,
                 (1-1j)*np.sin(theta)*np.cos(theta)],
                [(1-1j)*np.sin(theta)*np.cos(theta),
                 np.sin(theta)**2 + 1j*np.cos(theta)**2]]).tidyup()

QWP(np.pi/4)

"""
Explanation: Devices:
HWP - Half-wave plate axis at $\theta$ to the horizontal
LP - Linear polarizer, axis at $\theta$
QWP - Quarter-wave plate, axis at $\theta$
Note, these are functions so you need to call them with a specific value of theta.
End of explanation
"""


H.dag()*H

"""
Explanation: Example 1) Check that the $|H\rangle$ state is normalized
End of explanation
"""


np.sin?

"""
Explanation: To show more information on an object, use the question mark after the function or object:
End of explanation
"""


psi = Qobj([[1+1j],[2-1j]])
psi

psi.dag()

psi.dag().dag()

"""
Explanation: Example 2) Converting from ket to bra:
End of explanation
"""


psi.dag()*psi

psi_norm = psi*np.sqrt(1/7)

psi_norm.dag() * psi_norm

"""
Explanation: the .dag() python method computes the "daggar" or the complex transpose.
1) Is psi normalized? If not, find the normalization constant and confirm that constant normalizes psi.
End of explanation
"""


V.dag()*V

"""
Explanation: 2) Verify that the $|V\rangle$ state is normalized
End of explanation
"""


H.dag()*V

L.dag()*R

P45.dag()*M45

"""
Explanation: 3) Verify that the $|H\rangle$ and $|V\rangle$ states are orthogonal. Repeat for the other pairs of states.
End of explanation
"""


psi = 1/np.sqrt(5)*H + 2/np.sqrt(5)*V
psi

psi2 = Qobj([[1/np.sqrt(5)],[2/np.sqrt(5)]])
psi2

H.dag()*psi

"""
Explanation: 4) Calculate the horizontal component $c_H$ of the state $\psi = \frac{1}{\sqrt{5}}|H\rangle + \frac{2}{\sqrt{5}}|V\rangle$
End of explanation
"""


(H.dag()*P45).norm()**2

np.conjugate(H.dag()*P45) * (H.dag()*P45)

"""
Explanation: 5) Verify Eq. (3.18), $P(H||45\rangle)=\frac{1}{2},$ (which states "The probability that a photon prepared in the +45 state will leave a PA_HV in the Horizontal state is one half.")
End of explanation
"""


HWP(np.radians(45)) * H

HWP(np.pi/4) * H == V

"""
Explanation: 6) Demonstrate that a half-wave plate at 45-degrees converts $|H\rangle$ to $|V\rangle$
End of explanation
"""


import matplotlib.pyplot as plt

plt.plot([1,2,3,2,3,4])

phi_list = np.linspace(0,8*np.pi,num=100)

plt.plot(phi,np.sin(phi))

sin_list = [np.sin(p) for p in phi] # notes 

"""
Explanation: 7) Re-create Figure 3.9 by plotting the probability P(+45) vs phase φ
End of explanation
"""


plt.plot(phi,sin_list)

def psi(phi):
    return 1/np.sqrt(2)*(H + np.exp(1j*phi)*V)

answer = [(M45.dag()*psi(phi)).norm()**2 for phi in phi_list]

plt.plot(phi_list/np.pi,answer,"-o")
plt.xlabel("$\phi$")
plt.ylabel("P(-45)")

"""
Explanation: add text
End of explanation
"""


