|
| 1 | +######################### |
| 2 | +# Spin Operator Methods # |
| 3 | +######################### |
| 4 | + |
| 5 | +immutable HalfSpin{S} |
| 6 | + function HalfSpin() |
| 7 | + S > 0 ? new() : error("Spin must be positive") |
| 8 | + end |
| 9 | +end |
| 10 | + |
| 11 | +# The below function `spin` takes in a value and converts its to a type parameter |
| 12 | +# i.e., from value domain to type domain making it type unstable. |
| 13 | + |
| 14 | +spin(j::Integer) = j > 0 ? HalfSpin{j}() : error("Spin must be positive") |
| 15 | + |
| 16 | +function spin(j::Float64) |
| 17 | + if (j > 0) && isinteger(j) |
| 18 | + spin(int(j)) |
| 19 | + elseif (j > 0) && isinteger(2*j) |
| 20 | + spin(int(2*j)) |
| 21 | + else |
| 22 | + error("Spin must be positive and a multiple of 1/2.") |
| 23 | + end |
| 24 | +end |
| 25 | + |
| 26 | +spin_value{S}(::HalfSpin{S}) = S/2 |
| 27 | + |
| 28 | +##################### |
| 29 | +# Auxiliary methods # |
| 30 | +##################### |
| 31 | + |
| 32 | +function mat_coeffs_1(j::Float64) |
| 33 | + m = [j-1:-1:-j] |
| 34 | + N = length(m)+1 |
| 35 | + m_coeffs = [sqrt(j*(j+1.0) - (x+1.0)*x) for x in m] |
| 36 | + return spdiagm(m_coeffs,1,N,N) |
| 37 | +end |
| 38 | + |
| 39 | +function mat_coeffs_2(j::Float64) |
| 40 | + m = [j:-1:-j] |
| 41 | + N = length(m) |
| 42 | + return spdiagm(m,0,N,N) |
| 43 | +end |
| 44 | + |
| 45 | +function spin_h1(k::HalfSpin) |
| 46 | + j = spin_value(k) |
| 47 | + return mat_coeffs_1(j) |
| 48 | +end |
| 49 | + |
| 50 | +function spin_h2(k::HalfSpin) |
| 51 | + j = spin_value(k) |
| 52 | + return mat_coeffs_2(j) |
| 53 | +end |
| 54 | + |
| 55 | +################################ |
| 56 | +# Jx, Jy, Jz, Jp, Jm operators # |
| 57 | +################################ |
| 58 | + |
| 59 | +spin_Jx(j) = QuArray(0.5*(spin_h1(spin(j))+spin_h1(spin(j))')) |
| 60 | +spin_Jy(j) = QuArray(-0.5*im*(spin_h1(spin(j))-spin_h1(spin(j))')) |
| 61 | +spin_Jz(j) = QuArray(spin_h2(spin(j))) |
| 62 | +spin_Jp(j) = QuArray(spin_h1(spin(j))) |
| 63 | +spin_Jm(j) = QuArray(spin_h1(spin(j))') |
| 64 | + |
| 65 | +############################ |
| 66 | +# Pauli spin 1/2 matrices # |
| 67 | +############################ |
| 68 | + |
| 69 | +const sigma_x = 2.0 * spin_Jx(1/2) |
| 70 | +const sigma_y = 2.0 * spin_Jy(1/2) |
| 71 | +const sigma_z = 2.0 * spin_Jz(1/2) |
| 72 | + |
| 73 | +# TODO : unicode sigma_y, sigma_z |
| 74 | +const σₓ = sigma_x |
| 75 | + |
| 76 | +export spin_Jx, |
| 77 | + spin_Jy, |
| 78 | + spin_Jz, |
| 79 | + spin_Jp, |
| 80 | + spin_Jm, |
| 81 | + sigma_x, |
| 82 | + sigma_y, |
| 83 | + sigma_z, |
| 84 | + σₓ |
0 commit comments