-
Notifications
You must be signed in to change notification settings - Fork 11
/
quantum.jl
349 lines (299 loc) · 9.42 KB
/
quantum.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
module quantum
using ..interaction, ..system
using QuantumOptics, LinearAlgebra
try
eval(Expr(:toplevel,:(import Optim)))
global optimize = Optim.optimize
catch e
if typeof(e) == ArgumentError
println("Optim package not available. (Needed for calculation of squeezing parameter)")
else
rethrow(e)
end
end
export Hamiltonian, JumpOperators
# Define Spin 1/2 operators
spinbasis = SpinBasis(1//2)
sigmax_ = sigmax(spinbasis)
sigmay_ = sigmay(spinbasis)
sigmaz_ = sigmaz(spinbasis)
sigmap_ = sigmap(spinbasis)
sigmam_ = sigmam(spinbasis)
I_spin = identityoperator(spinbasis)
"""
quantum.basis(x)
Get basis of the given System.
"""
basis(x::Spin) = spinbasis
basis(x::SpinCollection) = CompositeBasis([basis(s) for s=x.spins]...)
basis(N::Int) = CompositeBasis([spinbasis for s=1:N]...)
basis(x::CavityMode) = FockBasis(x.cutoff)
basis(x::CavitySpinCollection) = compose(basis(x.cavity), basis(x.spincollection))
"""
quantum.blochstate(phi, theta[, N=1])
Product state of `N` single spin Bloch states.
All spins have the same azimuthal angle `phi` and polar angle `theta`.
"""
function blochstate(phi::Vector{T1}, theta::Vector{T2}) where {T1<:Real, T2<:Real}
N = length(phi)
@assert length(theta)==N
state_g = basisstate(spinbasis, 1)
state_e = basisstate(spinbasis, 2)
states = [cos(theta[k]/2)*state_g + exp(1im*phi[k])*sin(theta[k]/2)*state_e for k=1:N]
return reduce(tensor, states)
# if spinnumber>1
# return reduce(tensor, [state for i=1:spinnumber])
# else
# return state
# end
end
function blochstate(phi::Real, theta::Real, N::Int=1)
state_g = basisstate(spinbasis, 1)
state_e = basisstate(spinbasis, 2)
state = cos(theta/2)*state_g + exp(1im*phi)*sin(theta/2)*state_e
if N>1
return reduce(tensor, [state for i=1:N])
else
return state
end
end
"""
quantum.dim(state)
Number of spins described by this state.
"""
function dim(ρ::AbstractOperator)
return length(ρ.basis_l.bases)
end
"""
quantum.Hamiltonian(S)
Hamiltonian of the given System.
"""
function Hamiltonian(S::system.SpinCollection)
spins = S.spins
N = length(spins)
b = basis(S)
H = SparseOperator(b)
for i=1:N
if S.spins[i].delta != 0.
H += 0.5*S.spins[i].delta * embed(b, i, sigmaz_)
end
end
for i=1:N, j=1:N
if i==j
continue
end
sigmap_i = embed(b, i, sigmap_)
sigmam_j = embed(b, j, sigmam_)
H += interaction.Omega(spins[i].position, spins[j].position, S.polarizations[i], S.polarizations[j], S.gammas[i], S.gammas[j])*sigmap_i*sigmam_j
end
return H
end
function Hamiltonian(S::system.CavityMode)
b = basis(S)
H = SparseOperator(S.delta*number(b) + S.eta*(create(b) + destroy(b)))
return H
end
function Hamiltonian(S::system.CavitySpinCollection)
b = basis(S)
bs = basis(S.spincollection)
bc = basis(S.cavity)
Hs = Hamiltonian(S.spincollection)
Hc = Hamiltonian(S.cavity)
Ic = identityoperator(basis(S.cavity))
H = embed(b, 1, Hc) + tensor(Ic, Hs)
a = SparseOperator(destroy(bc))
at = SparseOperator(create(bc))
for i=1:length(S.spincollection.spins)
if S.g[i] != 0.
H += S.g[i]*(tensor(a, embed(bs, i, sigmap_)) + tensor(at, embed(bs, i, sigmam_)))
end
end
return H
end
"""
quantum.JumpOperators(S)
Jump operators of the given system.
"""
function JumpOperators(S::system.SpinCollection)
J = SparseOperator[embed(basis(S), i, sigmam_) for i=1:length(S.spins)]
Γ = interaction.GammaMatrix(S)
return Γ, J
end
JumpOperators(S::system.CavityMode) = (Float64[2*S.kappa], SparseOperator[SparseOperator(destroy(basis(S)))])
function JumpOperators(S::system.CavitySpinCollection)
Γs, Js = JumpOperators(S.spincollection)
Γc, Jc = JumpOperators(S.cavity)
Ns = length(Js)
Nc = length(Jc)
N = Ns + Nc
Γ = zeros(Float64, N, N)
Γ[1:Nc, 1:Nc] = Γc
Γ[Nc+1:end, Nc+1:end] = Γs
Ic = identityoperator(basis(S.cavity))
J = SparseOperator[embed(basis(S), 1, Jc[1])]
for j in Js
push!(J, tensor(Ic, j))
end
return Γ, J
end
"""
quantum.JumpOperators_diagonal(S)
Jump operators of the given system. (diagonalized)
Diagonalized means that the Gamma matrix is diagonalized and
the jump operators are changed accordingly.
"""
function JumpOperators_diagonal(S::system.SpinCollection)
spins = S.spins
N = length(spins)
b = basis(S)
Γ = zeros(Float64, N, N)
for i=1:N, j=1:N
Γ[i,j] = interaction.Gamma(spins[i].position, spins[j].position, S.polarizations[i], S.polarizations[j], S.gammas[i], S.gammas[j])
end
λ, M = eig(Γ)
J = Any[]
for i=1:N
op = Operator(b)
for j=1:N
op += M[j,i]*embed(b, j, sigmam_)
end
push!(J, sqrt(λ[i])*op)
end
return J
end
"""
quantum.timeevolution_diagonal(T, S, state0[; fout])
Master equation time evolution. (diagonalized)
Diagonalized means that the Gamma matrix is diagonalized and
the jump operators are changed accordingly.
# Arguments
* `T`: Points of time for which output will be generated.
* `S`: System
* `ρ₀`: Initial density operator.
* `fout` (optional): Function with signature fout(t, state) that is called
whenever output should be generated.
"""
function timeevolution_diagonal(T, S::system.System, ρ₀::Union{StateVector, DenseOperator}; fout=nothing, kwargs...)
H = Hamiltonian(S)
J = JumpOperators_diagonal(S)
Hnh = H - 0.5im*sum([dagger(J[i])*J[i] for i=1:length(J)])
return QuantumOptics.timeevolution.master_nh(T, ρ₀, Hnh, J; fout=fout, kwargs...)
end
"""
quantum.timeevolution(T, S, state0[; fout])
Master equation time evolution.
Diagonalized means that the Gamma matrix is diagonalized and
the jump operators are changed accordingly.
# Arguments
* `T`: Points of time for which output will be generated.
* `S`: System
* `ρ₀`: Initial density operator.
* `fout` (optional): Function with signature fout(t, state) that is called
whenever output should be generated.
"""
function timeevolution(T, S::system.System, ρ₀::Union{StateVector, DenseOperator}; fout=nothing, kwargs...)
b = basis(S)
H = Hamiltonian(S)
Γ, J = JumpOperators(S)
return QuantumOptics.timeevolution.master_h(T, ρ₀, H, J; fout=fout, rates=Γ, kwargs...)
end
"""
meanfield.rotate(axis, angles, state)
Rotations on the Bloch sphere for the given density operator.
# Arguments
* `axis`: Rotation axis.
* `angles`: Rotation angle(s).
* `ρ`: Density operator that should be rotated.
"""
function rotate(axis::Vector{T1}, angles::Vector{T2}, ρ::DenseOperator) where {T1<:Real, T2<:Real}
N = dim(ρ)
@assert length(axis)==3
@assert length(angles)==N
basis = ρ.basis_l
n = axis/norm(axis)
for i=1:N
nσ = n[1]*sigmax_ + n[2]*sigmay_ + n[3]*sigmaz_
α = angles[i]
R = I_spin*cos(α/2) - 1im*nσ*sin(α/2)
R_ = embed(basis, i, R)
ρ = R_*ρ*dagger(R_)
end
return ρ
end
rotate(axis::Vector{T}, angle::Real, ρ::DenseOperator) where {T<:Real} = rotate(axis, ones(Float64, dim(ρ))*angle, ρ)
"""
quantum.squeeze_sx(χT, ρ₀)
Spin squeezing along sx.
# Arguments
* `χT`: Squeezing strength.
* `ρ₀`: Operator that should be squeezed.
"""
function squeeze_sx(χT::Real, ρ₀::DenseOperator)
N = dim(ρ₀)
basis = ρ₀.basis_l
totaloperator(op::SparseOperator) = sum([embed(basis, i, op) for i=1:N])/N
sigmax_total = totaloperator(sigmax_)
H = χT*sigmax_total^2
T = [0.,1.]
t, states = QuantumOptics.timeevolution.master(T, ρ₀, H, [])
return states[end]
end
"""
quantum.squeeze(axis, χT, ρ₀)
Spin squeezing along an arbitrary axis.
# Arguments
* `axis`: Squeezing axis.
* `χT`: Squeezing strength.
* `ρ₀`: Operator that should be squeezed.
"""
function squeeze(axis::Vector{T}, χT::Real, ρ₀::DenseOperator) where T<:Real
@assert length(axis)==3
axis = axis/norm(axis)
N = dim(ρ₀)
basis = ρ₀.basis_l
totaloperator(op::SparseOperator) = sum([embed(basis, i, op) for i=1:N])/N
σ = map(totaloperator, [sigmax_, sigmay_, sigmaz_])
σn = sum([axis[i]*σ[i] for i=1:3])
H = χT*σn^2
tout, states = QuantumOptics.timeevolution.master([0,1], ρ₀, H, [])
return states[end]
end
"""
quantum.orthogonal_vectors(n)
Create 3 orthonormal vectors where one is in the given direction `n`.
"""
function orthogonal_vectors(n::Vector{Float64})
@assert length(n)==3
n = n/norm(n)
v = (n[1]<n[2] ? [1.,0.,0.] : [0.,1.,0.])
e1 = v - dot(n,v)*n
e1 = e1/norm(e1)
e2 = cross(n, e1)
e2 = e2/norm(e2)
return e1, e2
end
"""
quantum.variance(op, state)
Variance of the operator for the given state.
"""
variance(op::AbstractOperator, state) = (expect(op^2, state) - expect(op, state)^2)
"""
quantum.squeezingparameter(ρ)
Calculate squeezing parameter for the given state.
"""
function squeezingparameter(ρ::DenseOperator)
N = dim(ρ)
basis = ρ.basis_l
totaloperator(op::SparseOperator) = sum([embed(basis, i, op) for i=1:N])/N
S = map(totaloperator, [sigmax_, sigmay_, sigmaz_])
n = real([expect(s, ρ) for s=S])
e1, e2 = orthogonal_vectors(n)
function f(phi)
nphi = cos(phi)*e1 + sin(phi)*e2
Sphi = sum([nphi[i]*S[i] for i=1:3])
return real(variance(Sphi, ρ))
end
varSmin = Optim.optimize(f, 0., 2*pi).f_minimum
return sqrt(N*varSmin)/norm(n)
end
end # module