1import numpy as np
2import matplotlib.pyplot as plt
3
4plt.ion()
5pi = np.pi
6exp = np.exp
7cos = np.cos
8sin = np.sin
9i = 1j
10sx = np.matrix([[0,1],[1,0]])
11sy = np.matrix([[0,-i],[i,0]])
12sz = np.matrix([[1,0],[0,-1]])
13s0 = np.matrix([[1,0],[0,1]])
14
15def jones(th,phi,color='blue'):
16    J = [cos(th/2.),sin(th/2.)*exp(i*phi)]
17    #norm = np.sqrt(J[0]**2 + J[1]**2)
18    #J = [J[0]/norm,J[1]*exp(i*phi)/norm]
19    t = np.linspace(0,2*pi,100)
20    Ex,Ey = (J[0]*exp(i*t),J[1]*exp(i*t))
21    x0 = Ex[0].real
22    y0 = Ey[0].real
23    x1 = Ex[1].real
24    y1 = Ey[1].real
25    dx = x1-x0
26    dy = y1-y0
27    first_one = True
28    for x,y in zip(Ex.real,Ey.real):
29        plt.plot([x0,x],[y0,y],color=color)
30        x0,y0 = x,y
31        if first_one:
32            plt.arrow(x0,y0,dx,dy)
33            first_one = False
34        #plt.pause(.003)
35    #plt.plot(Ex.real,Ey.real)
36    plt.pause(.1)
37    J = np.matrix([J[0],J[1]]).T
38    a = [sin(th)*cos(phi),sin(th)*sin(phi),cos(th)]
39    rho = 0.5*(s0 + a[0]*sx + a[1]*sy + a[2]*sz)
40    return(J,a,rho)
41
42def clr():
43    plt.close('all')
44    plt.figure()
45    plt.plot([-1],[-1])
46    plt.plot([1],[1])
47    plt.xlim(-1,1)
48    plt.ylim(-1,1)
49    plt.axes().set_aspect('equal')
50    plt.grid('on')
51
52def vsphi():
53    for phi in np.linspace(0,pi,9):
54        jones(pi/4,phi)
55    for phi in np.linspace(0,pi,9):
56        jones(pi/8,phi)
57    for phi in np.linspace(0,pi,9):
58        jones(pi*3./8,phi)
59
60def vsth():
61    for th in np.linspace(0,pi/2,9):
62        jones(th,pi/2.)
63    for th in np.linspace(0,pi/2,9):
64        jones(th,pi/4.)
65    for th in np.linspace(0,pi/2,9):
66        jones(th,pi*3./4.)
67
68# This is the observable that measures the photon spin
69# (eigenvalue +1 for |L>, -1 for |R>)
70# It happens to be the same as Pauli sy
71szc = 0.5*np.matrix([[1,-i],[i,1]]) - 0.5*np.matrix([[1,i],[-i,1]])
72
73def photon():
74    print '----------------'
75    print '|H>'
76    J,a,rho = jones(0,0,'blue')
77    print 'J = ',J.T
78    print 'a = ',a
79    print 'rho = '
80    print rho
81    print 'spins'
82    print np.trace(sx*rho),np.trace(sy*rho),np.trace(sz*rho)
83    print '----------------'
84    print '|V>'
85    J,a,rho = jones(pi,0,'blue')
86    print 'J = ',J.T
87    print 'a = ',a
88    print 'rho = '
89    print rho
90    print 'spins'
91    print np.trace(sx*rho),np.trace(sy*rho),np.trace(sz*rho)
92    print '----------------'
93    print '|D> = |H>+|V>'
94    J,a,rho = jones(pi/2,0,'green')
95    print 'J = ',J.T
96    print 'a = ',a
97    print 'rho = '
98    print rho
99    print 'spins'
100    print np.trace(sx*rho),np.trace(sy*rho),np.trace(sz*rho)
101    print '----------------'
102    print '|A> = |H>-|V>'
103    J,a,rho = jones(pi/2,pi,'green')
104    print 'J = ',J.T
105    print 'a = ',a
106    print 'rho = '
107    print rho
108    print 'spins'
109    print np.trace(sx*rho),np.trace(sy*rho),np.trace(sz*rho)
110    print '----------------'
111    print '|L> = |H>+i|V>'
112    J,a,rho = jones(pi/2,pi/2,'red')
113    print 'J = ',J.T
114    print 'a = ',a
115    print 'rho = '
116    print rho
117    print 'spins'
118    print np.trace(sx*rho),np.trace(sy*rho),np.trace(sz*rho)
119    print '----------------'
120    print '|R> = |H>-i|V>'
121    J,a,rho = jones(pi/2,-pi/2,'red')
122    print 'J = ',J.T
123    print 'a = ',a
124    print 'rho = '
125    print rho
126    print 'spins'
127    print np.trace(sx*rho),np.trace(sy*rho),np.trace(sz*rho)
128    print '----------------'
129
130V = np.matrix([1,0]).T
131H = np.matrix([0,1]).T
132D = np.matrix([1,1]).T/np.sqrt(2.)
133A = np.matrix([1,-1]).T/np.sqrt(2.)
134L = np.matrix([1,i]).T/np.sqrt(2.)
135R = np.matrix([1,-i]).T/np.sqrt(2.)
136
137