-
Notifications
You must be signed in to change notification settings - Fork 13
/
qnumber.jl
410 lines (349 loc) · 10.1 KB
/
qnumber.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""
QNumber
Abstract type representing any expression involving operators.
"""
abstract type QNumber end
"""
QSym <: QNumber
Abstract type representing fundamental operator types.
"""
abstract type QSym <: QNumber end
# Generic hash fallback for interface -- this will be slow
function Base.hash(op::T, h::UInt) where T<:QSym
n = fieldcount(T)
if n == 3
# These three fields need to be defined for any QSym
return hash(T, hash(op.hilbert, hash(op.name, hash(op.aon, h))))
else
# If there are more we'll need to iterate through
h_ = copy(h)
for k = n:-1:4
if fieldname(typeof(op), k) !== :metadata
h_ = hash(getfield(op, k), h_)
end
end
return hash(T, hash(op.hilbert, hash(op.name, hash(op.aon, h_))))
end
end
"""
QTerm <: QNumber
Abstract type representing noncommutative expressions.
"""
abstract type QTerm <: QNumber end
Base.isless(a::QSym, b::QSym) = a.name < b.name
## Interface for SymbolicUtils
TermInterface.head(::QNumber) = :call
SymbolicUtils.istree(::QSym) = false
SymbolicUtils.istree(::QTerm) = true
SymbolicUtils.istree(::Type{T}) where {T<:QTerm} = true
# Symbolic type promotion
SymbolicUtils.promote_symtype(f, Ts::Type{<:QNumber}...) = promote_type(Ts...)
SymbolicUtils.promote_symtype(f, T::Type{<:QNumber}, Ts...) = T
SymbolicUtils.promote_symtype(f,T::Type{<:QNumber},S::Type{<:Number}) = T
SymbolicUtils.promote_symtype(f,T::Type{<:Number},S::Type{<:QNumber}) = S
SymbolicUtils.promote_symtype(f,T::Type{<:QNumber},S::Type{<:QNumber}) = promote_type(T,S)
SymbolicUtils.symtype(x::T) where T<:QNumber = T
# Standard simplify
function SymbolicUtils.simplify(x::QNumber;kwargs...)
avg = average(x)
avg_ = SymbolicUtils.simplify(avg;kwargs...)
return undo_average(avg_)
end
## End of interface
## Methods
import Base: *, +, -
const SNuN = Union{<:SymbolicUtils.Symbolic{<:Number}, <:Number}
Base.:~(a::QNumber, b::QNumber) = Symbolics.Equation(a, b)
## Multiplication
"""
QMul <: QTerm
Represent a multiplication involving [`QSym`](@ref) types.
Fields:
======
* arg_c: The commutative prefactor.
* args_nc: A vector containing all [`QSym`](@ref) types.
"""
struct QMul{M} <: QTerm
arg_c
args_nc::Vector{Any}
metadata::M
function QMul{M}(arg_c, args_nc, metadata) where {M}
if SymbolicUtils._isone(arg_c) && length(args_nc)==1
return args_nc[1]
elseif (0 in args_nc) || isequal(arg_c,0)
return 0
else
return new(arg_c, args_nc, metadata)
end
end
end
QMul(arg_c, args_nc; metadata::M=NO_METADATA) where {M} = QMul{M}(arg_c, args_nc, metadata)
Base.hash(q::QMul, h::UInt) = hash(QMul, hash(q.arg_c, SymbolicUtils.hashvec(q.args_nc, h)))
SymbolicUtils.operation(::QMul) = (*)
SymbolicUtils.arguments(a::QMul) = vcat(a.arg_c, a.args_nc)
function SymbolicUtils.similarterm(::QMul, ::typeof(*), args, symtype=nothing; metadata=NO_METADATA, exprhead=nothing)
args_c = filter(x->!(x isa QNumber), args)
args_nc = filter(x->x isa QNumber, args)
arg_c = *(args_c...)
isempty(args_nc) && return arg_c
return QMul(arg_c, args_nc; metadata)
end
SymbolicUtils.metadata(a::QMul) = a.metadata
function Base.adjoint(q::QMul)
args_nc = map(adjoint, q.args_nc)
reverse!(args_nc)
sort!(args_nc, by=acts_on)
return QMul(conj(q.arg_c), args_nc; q.metadata)
end
function Base.isequal(a::QMul, b::QMul)
isequal(a.arg_c, b.arg_c) || return false
length(a.args_nc)==length(b.args_nc) || return false
for (arg_a, arg_b) ∈ zip(a.args_nc, b.args_nc)
isequal(arg_a,arg_b) || return false
end
return true
end
function *(a::QSym,b::QSym)
check_hilbert(a, b)
args = [a,b]
sort!(args, by=acts_on)
QMul(1,args)
end
function *(a::QSym, b::SNuN)
SymbolicUtils._iszero(b) && return b
SymbolicUtils._isone(b) && return a
return QMul(b,[a])
end
*(b::SNuN, a::QNumber) = a*b
function *(a::QMul, b::SNuN)
SymbolicUtils._iszero(b) && return b
SymbolicUtils._isone(b) && return a
arg_c = a.arg_c * b
return QMul(arg_c, a.args_nc)
end
function *(a::QSym, b::QMul)
check_hilbert(a, b)
args_nc = vcat(a,b.args_nc)
sort!(args_nc, by=acts_on)
return merge_commutators(b.arg_c,args_nc)
end
function *(a::QMul, b::QSym)
check_hilbert(a, b)
args_nc = vcat(a.args_nc, b)
sort!(args_nc, by=acts_on)
return merge_commutators(a.arg_c,args_nc)
end
function *(a::QMul, b::QMul)
check_hilbert(a, b)
args_nc = vcat(a.args_nc, b.args_nc)
sort!(args_nc, by=acts_on)
arg_c = a.arg_c*b.arg_c
return merge_commutators(arg_c,args_nc)
end
Base.:/(a::QNumber, b::SNuN) = (1/b) * a
function merge_commutators(arg_c,args_nc)
#Added extra checks for 0 here
if isequal(arg_c,0) || 0 in args_nc
return 0
end
i = 1
was_merged = false
while i<length(args_nc)
if _ismergeable(args_nc[i], args_nc[i+1])
args_nc[i] = *(args_nc[i], args_nc[i+1])
iszero(args_nc[i]) && return 0
deleteat!(args_nc, i+1)
was_merged = true
end
i += 1
end
if was_merged
return *(arg_c, args_nc...)
else
return QMul(arg_c, args_nc)
end
end
_ismergeable(a,b) = isequal(acts_on(a),acts_on(b)) && ismergeable(a,b) && isequal(hilbert(a),hilbert(b))
ismergeable(a,b) = false
## Powers
function Base.:^(a::QNumber, n::Integer)
iszero(n) && return 1
isone(n) && return a
return *((a for i=1:n)...)
end
## Addition
"""
QAdd <: QTerm
Represent an addition involving [`QNumber`](@ref) and other types.
"""
struct QAdd <: QTerm
arguments::Vector{Any}
end
Base.hash(q::T, h::UInt) where T<:QAdd = hash(T, SymbolicUtils.hashvec(q.arguments, h))
function Base.isequal(a::QAdd,b::QAdd)
length(a.arguments)==length(b.arguments) || return false
for (arg_a,arg_b) ∈ zip(a.arguments, b.arguments)
isequal(arg_a, arg_b) || return false
end
return true
end
SymbolicUtils.operation(::QAdd) = (+)
SymbolicUtils.arguments(a::QAdd) = a.arguments
SymbolicUtils.similarterm(::QAdd, ::typeof(+), args; metadata=NO_METADATA, exprhead=nothing) = QAdd(args; metadata)
SymbolicUtils.metadata(q::QAdd) = q.metadata
Base.adjoint(q::QAdd) = QAdd(map(adjoint, q.arguments))
-(a::QNumber) = -1*a
-(a,b::QNumber) = a + (-b)
-(a::QNumber,b) = a + (-b)
-(a::QNumber,b::QNumber) = a + (-b)
function +(a::QNumber, b::SNuN)
SymbolicUtils._iszero(b) && return a
return QAdd([a,b])
end
+(a::SNuN,b::QNumber) = +(b,a)
function +(a::QAdd,b::SNuN)
SymbolicUtils._iszero(b) && return a
args = vcat(a.arguments, b)
return QAdd(args)
end
function +(a::QNumber, b::QNumber)
check_hilbert(a, b)
args = [a,b]
return QAdd(args)
end
function +(a::QAdd,b::QNumber)
check_hilbert(a, b)
args = vcat(a.arguments, b)
return QAdd(args)
end
function +(b::QNumber,a::QAdd)
check_hilbert(a, b)
args = vcat(a.arguments, b)
return QAdd(args)
end
function +(a::QAdd,b::QAdd)
check_hilbert(a, b)
args = vcat(a.arguments, b.arguments)
return QAdd(args)
end
function *(a::QAdd, b)
check_hilbert(a, b)
args = Any[a_ * b for a_ ∈ a.arguments]
flatten_adds!(args)
isempty(args) && return 0
q = QAdd(args)
return q
end
function *(a::QNumber, b::QAdd)
check_hilbert(a, b)
args = Any[a * b_ for b_ ∈ b.arguments]
flatten_adds!(args)
isempty(args) && return 0
q = QAdd(args)
return q
end
function *(a::QAdd, b::QAdd)
check_hilbert(a, b)
args = []
for a_ ∈ a.arguments, b_ ∈ b.arguments
push!(args, a_ * b_)
end
flatten_adds!(args)
isempty(args) && return 0
q = QAdd(args)
return q
end
function flatten_adds!(args)
i = 1
while i <= length(args)
if args[i] isa QAdd
append!(args,args[i].arguments)
deleteat!(args, i)
elseif SymbolicUtils._iszero(args[i]) || isequal(args[i],0)
deleteat!(args,i)
else
i += 1
end
end
return args
end
## Hilbert space checks
check_hilbert(a::QNumber,b::QNumber) = (hilbert(a) == hilbert(b)) || error("Incompatible Hilbert spaces $(hilbert(a)) and $(hilbert(b))!")
check_hilbert(x,y) = nothing
hilbert(a::QSym) = a.hilbert
hilbert(a::QMul) = hilbert(a.args_nc[1])
function hilbert(a::QAdd)
idx = findfirst(x->x isa QNumber, a.arguments)
idx === nothing && print(a)
hilbert(a.arguments[idx])
end
const AonType = Union{<:Int,<:ClusterAon}
"""
acts_on(op)
Shows on which Hilbert space `op` acts. For [`QSym`](@ref) types, this
returns an Integer, whereas for a `Term` it returns a `Vector{Int}`
whose entries specify all subspaces on which the expression acts.
"""
acts_on(op::QSym) = op.aon
function acts_on(q::QMul)
aon = AonType[]
for arg ∈ q.args_nc
aon_ = acts_on(arg)
aon_ ∈ aon || push!(aon, aon_)
end
return aon
end
function acts_on(q::QAdd)
aon = AonType[]
for arg ∈ q.arguments
append!(aon, acts_on(arg))
end
unique!(aon)
sort!(aon)
return aon
end
acts_on(x) = Int[]
Base.one(::T) where T<:QNumber = one(T)
Base.one(::Type{<:QNumber}) = 1
Base.isone(::QNumber) = false
Base.zero(::T) where T<:QNumber = zero(T)
Base.zero(::Type{<:QNumber}) = 0
Base.iszero(::QNumber) = false
"""
@qnumbers
Convenience macro for the construction of operators.
Examples
========
```
julia> h = FockSpace(:fock)
ℋ(fock)
julia> @qnumbers a::Destroy(h)
(a,)
julia> h = FockSpace(:one) ⊗ FockSpace(:two)
ℋ(one) ⊗ ℋ(two)
julia> @qnumbers b::Destroy(h,2)
(b,)
```
"""
macro qnumbers(qs...)
ex = Expr(:block)
qnames = []
for q in qs
@assert q isa Expr && q.head==:(::)
q_ = q.args[1]
@assert q_ isa Symbol
push!(qnames, q_)
f = q.args[2]
@assert f isa Expr && f.head==:call
op = _make_operator(q_, f.args...)
ex_ = Expr(:(=), esc(q_), op)
push!(ex.args, ex_)
end
push!(ex.args, Expr(:tuple, map(esc, qnames)...))
return ex
end
function _make_operator(name, T, h, args...)
name_ = Expr(:quote, name)
d = source_metadata(:qnumbers, name)
return Expr(:call, T, esc(h), name_, args..., Expr(:kw, :metadata, Expr(:quote, d)))
end