You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Moving all abstract functionality to QuantumInterface.jl (#100)
* avoid piracy of SparseArrays.permutedims(::AbstractSparseMatrix, ...)
use the new _permutedims in operators_sparse
* new abstract ParticleBasis to avoid piracy of QuantumInterface.Basis
* move all operations on abstract types to QuantumInterface.jl
* document the purposeful piracy of identityoperator
* test with QuantumInterface v0.2.0
* bump version number
Copy file name to clipboardExpand all lines: src/operators.jl
+1-231Lines changed: 1 addition & 231 deletions
Original file line number
Diff line number
Diff line change
@@ -13,94 +13,7 @@ abstract type DataOperator{BL,BR} <: AbstractOperator{BL,BR} end
13
13
14
14
15
15
# Common error messages
16
-
arithmetic_unary_error(funcname, x::AbstractOperator) =throw(ArgumentError("$funcname is not defined for this type of operator: $(typeof(x)).\nTry to convert to another operator type first with e.g. dense() or sparse()."))
17
-
arithmetic_binary_error(funcname, a::AbstractOperator, b::AbstractOperator) =throw(ArgumentError("$funcname is not defined for this combination of types of operators: $(typeof(a)), $(typeof(b)).\nTry to convert to a common operator type first with e.g. dense() or sparse()."))
18
-
addnumbererror() =throw(ArgumentError("Can't add or subtract a number and an operator. You probably want 'op + identityoperator(op)*x'."))
ptrace(a::AbstractOperator, index) =arithmetic_unary_error("Partial trace", a)
247
-
248
-
"""
249
-
normalize(op)
250
-
251
-
Return the normalized operator so that its `tr(op)` is one.
252
-
"""
253
-
normalize(op::AbstractOperator) = op/tr(op)
254
-
255
-
"""
256
-
normalize!(op)
257
-
258
-
In-place normalization of the given operator so that its `tr(x)` is one.
259
-
"""
260
-
normalize!(op::AbstractOperator) =throw(ArgumentError("normalize! is not defined for this type of operator: $(typeof(op)).\n You may have to fall back to the non-inplace version 'normalize()'."))
261
-
262
105
"""
263
106
expect(op, state)
264
107
@@ -267,30 +110,15 @@ Expectation value of the given operator `op` for the specified `state`.
267
110
`state` can either be a (density) operator or a ket.
268
111
"""
269
112
expect(op::AbstractOperator{B,B}, state::Ket{B}) where B =dot(state.data, (op * state).data)
270
-
expect(op::AbstractOperator{B1,B2}, state::AbstractOperator{B2,B2}) where {B1,B2} =tr(op*state)
271
113
272
-
"""
273
-
expect(index, op, state)
274
-
275
-
If an `index` is given, it assumes that `op` is defined in the subsystem specified by this number.
276
-
"""
277
-
functionexpect(indices, op::AbstractOperator{B1,B2}, state::AbstractOperator{B3,B3}) where {B1,B2,B3<:CompositeBasis}
278
-
N =length(state.basis_l.shape)
279
-
indices_ =complement(N, indices)
280
-
expect(op, ptrace(state, indices_))
281
-
end
282
114
functionexpect(indices, op::AbstractOperator{B,B}, state::Ket{B2}) where {B,B2<:CompositeBasis}
283
115
N =length(state.basis.shape)
284
116
indices_ =complement(N, indices)
285
117
expect(op, ptrace(state, indices_))
286
118
end
287
119
288
-
expect(index::Integer, op::AbstractOperator{B1,B2}, state::AbstractOperator{B3,B3}) where {B1,B2,B3<:CompositeBasis} =expect([index], op, state)
289
120
expect(index::Integer, op::AbstractOperator{B,B}, state::Ket{B2}) where {B,B2<:CompositeBasis} =expect([index], op, state)
290
121
291
-
expect(op::AbstractOperator, states::Vector) = [expect(op, state) for state=states]
292
-
expect(indices, op::AbstractOperator, states::Vector) = [expect(indices, op, state) for state=states]
293
-
294
122
"""
295
123
variance(op, state)
296
124
@@ -302,60 +130,14 @@ function variance(op::AbstractOperator{B,B}, state::Ket{B}) where B
302
130
x = op*state
303
131
state.data'*(op*x).data - (state.data'*x.data)^2
304
132
end
305
-
functionvariance(op::AbstractOperator{B,B}, state::AbstractOperator{B,B}) where B
306
-
expect(op*op, state) -expect(op, state)^2
307
-
end
308
-
309
-
"""
310
-
variance(index, op, state)
311
133
312
-
If an `index` is given, it assumes that `op` is defined in the subsystem specified by this number
313
-
"""
314
-
functionvariance(indices, op::AbstractOperator{B,B}, state::AbstractOperator{BC,BC}) where {B,BC<:CompositeBasis}
315
-
N =length(state.basis_l.shape)
316
-
indices_ =complement(N, indices)
317
-
variance(op, ptrace(state, indices_))
318
-
end
319
134
functionvariance(indices, op::AbstractOperator{B,B}, state::Ket{BC}) where {B,BC<:CompositeBasis}
320
135
N =length(state.basis.shape)
321
136
indices_ =complement(N, indices)
322
137
variance(op, ptrace(state, indices_))
323
138
end
324
139
325
-
variance(index::Integer, op::AbstractOperator{B,B}, state::AbstractOperator{BC,BC}) where {B,BC<:CompositeBasis} =variance([index], op, state)
326
140
variance(index::Integer, op::AbstractOperator{B,B}, state::Ket{BC}) where {B,BC<:CompositeBasis} =variance([index], op, state)
327
-
variance(op::AbstractOperator, states::Vector) = [variance(op, state) for state=states]
328
-
variance(indices, op::AbstractOperator, states::Vector) = [variance(indices, op, state) for state=states]
329
-
330
-
331
-
"""
332
-
exp(op::AbstractOperator)
333
-
334
-
Operator exponential.
335
-
"""
336
-
exp(op::AbstractOperator) =throw(ArgumentError("exp() is not defined for this type of operator: $(typeof(op)).\nTry to convert to dense operator first with dense()."))
337
-
338
-
permutesystems(a::AbstractOperator, perm) =arithmetic_unary_error("Permutations of subsystems", a)
Return an identityoperator in the given bases. One can optionally specify the container
347
-
type which has to a subtype of [`AbstractOperator`](@ref) as well as the number type
348
-
to be used in the identity matrix.
349
-
"""
350
-
identityoperator(::Type{T}, ::Type{S}, b1::Basis, b2::Basis) where {T<:AbstractOperator,S} =throw(ArgumentError("Identity operator not defined for operator type $T."))
351
-
identityoperator(::Type{T}, ::Type{S}, b::Basis) where {T<:AbstractOperator,S} =identityoperator(T,S,b,b)
352
-
identityoperator(::Type{T}, bases::Basis...) where T<:AbstractOperator=identityoperator(T,eltype(T),bases...)
353
-
identityoperator(op::T) where {T<:AbstractOperator} =identityoperator(T, op.basis_l, op.basis_r)
354
-
355
-
# Catch case where eltype cannot be inferred from type; this is a bit hacky
356
-
identityoperator(::Type{T}, ::Type{Any}, b1::Basis, b2::Basis) where T<:AbstractOperator=identityoperator(T, ComplexF64, b1, b2)
identityoperator(b::Basis) =identityoperator(ComplexF64, b)
71
+
identityoperator(::Type{T}, b1::Basis, b2::Basis) where T<:Number=identityoperator(DataOperator, T, b1, b2) #XXX This is purposeful type piracy over QuantumInterface, that hardcodes the use of QuantumOpticsBase.DataOperator in identityoperator. Also necessary for backward compatibility.
0 commit comments