Skip to content

Commit b45f9ba

Browse files
committed
Move to no parametric type on abstract quanutum objects
1 parent 86fbdeb commit b45f9ba

20 files changed

+71
-100
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "QuantumOpticsBase"
22
uuid = "4f57444f-1401-5e15-980d-4471b28d5678"
3-
version = "0.5.5"
3+
version = "0.6.0"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
@@ -25,7 +25,7 @@ FastGaussQuadrature = "0.5, 1"
2525
FillArrays = "0.13, 1"
2626
LRUCache = "1"
2727
LinearAlgebra = "1"
28-
QuantumInterface = "0.3.3"
28+
QuantumInterface = "0.4.0"
2929
Random = "1"
3030
RecursiveArrayTools = "3"
3131
SparseArrays = "1"

src/QuantumOpticsBase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import LinearAlgebra: mul!, rmul!
55
import RecursiveArrayTools
66

77
import QuantumInterface: dagger, directsum, , dm, embed, nsubsystems, expect, identityoperator, identitysuperoperator,
8-
permutesystems, projector, ptrace, reduced, tensor, , variance, apply!, basis, AbstractSuperOperator
8+
permutesystems, projector, ptrace, reduced, tensor, , variance, apply!, basis, basis_l, basis_r
99

1010
# index helpers
1111
import QuantumInterface: complement, remove, shiftremove, reducedindices!, check_indices, check_sortedindices, check_embed_indices

src/bases.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import QuantumInterface: Basis, basis, GenericBasis, CompositeBasis,
2-
equal_shape, equal_bases, IncompatibleBases, @samebases, samebases, check_samebases,
2+
equal_shape, IncompatibleBases, @samebases, samebases, check_samebases,
33
multiplicable, check_multiplicable, reduced, ptrace, permutesystems

src/operators.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Abstract type for operators with a data field.
99
This is an abstract type for operators that have a direct matrix representation
1010
stored in their `.data` field.
1111
"""
12-
abstract type DataOperator{BL,BR} <: AbstractOperator{BL,BR} end
12+
abstract type BLROperator{BL,BR} <: AbstractOperator end
13+
abstract type DataOperator{BL,BR} <: BLROperator{BL,BR} end
1314

1415

1516
# Common error messages
@@ -109,18 +110,18 @@ Expectation value of the given operator `op` for the specified `state`.
109110
110111
`state` can either be a (density) operator or a ket.
111112
"""
112-
expect(op::AbstractOperator{B,B}, state::Ket{B}) where B = dot(state.data, (op * state).data)
113+
expect(op::BLROperator{B,B}, state::Ket{B}) where B = dot(state.data, (op * state).data)
113114

114115
# TODO upstream this one
115116
# expect(op::AbstractOperator{B,B}, state::AbstractKet{B}) where B = norm(op * state) ^ 2
116117

117-
function expect(indices, op::AbstractOperator{B,B}, state::Ket{B2}) where {B,B2<:CompositeBasis}
118+
function expect(indices, op::BLROperator{B,B}, state::Ket{B2}) where {B,B2<:CompositeBasis}
118119
N = length(state.basis.shape)
119120
indices_ = complement(N, indices)
120121
expect(op, ptrace(state, indices_))
121122
end
122123

123-
expect(index::Integer, op::AbstractOperator{B,B}, state::Ket{B2}) where {B,B2<:CompositeBasis} = expect([index], op, state)
124+
expect(index::Integer, op::BLROperator{B,B}, state::Ket{B2}) where {B,B2<:CompositeBasis} = expect([index], op, state)
124125

125126
"""
126127
variance(op, state)
@@ -129,18 +130,18 @@ Variance of the given operator `op` for the specified `state`.
129130
130131
`state` can either be a (density) operator or a ket.
131132
"""
132-
function variance(op::AbstractOperator{B,B}, state::Ket{B}) where B
133+
function variance(op::BLROperator{B,B}, state::Ket{B}) where B
133134
x = op*state
134135
state.data'*(op*x).data - (state.data'*x.data)^2
135136
end
136137

137-
function variance(indices, op::AbstractOperator{B,B}, state::Ket{BC}) where {B,BC<:CompositeBasis}
138+
function variance(indices, op::BLROperator{B,B}, state::Ket{BC}) where {B,BC<:CompositeBasis}
138139
N = length(state.basis.shape)
139140
indices_ = complement(N, indices)
140141
variance(op, ptrace(state, indices_))
141142
end
142143

143-
variance(index::Integer, op::AbstractOperator{B,B}, state::Ket{BC}) where {B,BC<:CompositeBasis} = variance([index], op, state)
144+
variance(index::Integer, op::BLROperator{B,B}, state::Ket{BC}) where {B,BC<:CompositeBasis} = variance([index], op, state)
144145

145146
# Helper functions to check validity of arguments
146147
function check_ptrace_arguments(a::AbstractOperator, indices)

src/operators_dense.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Operator(qets::AbstractVector{<:Ket}) = Operator(first(qets).basis, GenericBasis
2828
Operator(basis_r::Basis,qets::AbstractVector{<:Ket}) = Operator(first(qets).basis, basis_r, qets)
2929
Operator(basis_l::BL,basis_r::BR,qets::AbstractVector{<:Ket}) where {BL,BR} = Operator{BL,BR}(basis_l, basis_r, reduce(hcat, getfield.(qets, :data)))
3030

31+
basis_l(op::Operator) = op.basis_l
32+
basis_r(op::Operator) = op.basis_r
33+
3134
QuantumInterface.traceout!(s::QuantumOpticsBase.Operator, i) = QuantumInterface.ptrace(s,i)
3235

3336
Base.zero(op::Operator) = Operator(op.basis_l,op.basis_r,zero(op.data))
@@ -98,22 +101,22 @@ Base.isapprox(x::DataOperator, y::DataOperator; kwargs...) = false
98101
*(a::Operator{B1, B2, T}, b::DataOperator{B2, B3}) where {B1, B2, B3, T} = error("no `*` method defined for DataOperator subtype $(typeof(b))") # defined to avoid method ambiguity
99102
*(a::Operator, b::Number) = Operator(a.basis_l, a.basis_r, b*a.data)
100103
*(a::Number, b::Operator) = Operator(b.basis_l, b.basis_r, a*b.data)
101-
function *(op1::AbstractOperator{B1,B2}, op2::Operator{B2,B3,T}) where {B1,B2,B3,T}
104+
function *(op1::BLROperator{B1,B2}, op2::Operator{B2,B3,T}) where {B1,B2,B3,T}
102105
result = Operator{B1,B3}(op1.basis_l, op2.basis_r, similar(_parent(op2.data),promote_type(eltype(op1),eltype(op2)),length(op1.basis_l),length(op2.basis_r)))
103106
mul!(result,op1,op2)
104107
return result
105108
end
106-
function *(op1::Operator{B1,B2,T}, op2::AbstractOperator{B2,B3}) where {B1,B2,B3,T}
109+
function *(op1::Operator{B1,B2,T}, op2::BLROperator{B2,B3}) where {B1,B2,B3,T}
107110
result = Operator{B1,B3}(op1.basis_l, op2.basis_r, similar(_parent(op1.data),promote_type(eltype(op1),eltype(op2)),length(op1.basis_l),length(op2.basis_r)))
108111
mul!(result,op1,op2)
109112
return result
110113
end
111-
function *(op::AbstractOperator{BL,BR}, psi::Ket{BR,T}) where {BL,BR,T}
114+
function *(op::BLROperator{BL,BR}, psi::Ket{BR,T}) where {BL,BR,T}
112115
result = Ket{BL,T}(op.basis_l,similar(psi.data,length(op.basis_l)))
113116
mul!(result,op,psi)
114117
return result
115118
end
116-
function *(psi::Bra{BL,T}, op::AbstractOperator{BL,BR}) where {BL,BR,T}
119+
function *(psi::Bra{BL,T}, op::BLROperator{BL,BR}) where {BL,BR,T}
117120
result = Bra{BR,T}(op.basis_r, similar(psi.data,length(op.basis_r)))
118121
mul!(result,psi,op)
119122
return result
@@ -388,7 +391,7 @@ mul!(result::Bra{B2},a::Bra{B1},b::Operator{B1,B2},alpha,beta) where {B1,B2} = (
388391
rmul!(op::Operator, x) = (rmul!(op.data, x); op)
389392

390393
# Multiplication for Operators in terms of their gemv! implementation
391-
function mul!(result::Operator{B1,B3},M::AbstractOperator{B1,B2},b::Operator{B2,B3},alpha,beta) where {B1,B2,B3}
394+
function mul!(result::Operator{B1,B3},M::BLROperator{B1,B2},b::Operator{B2,B3},alpha,beta) where {B1,B2,B3}
392395
for i=1:size(b.data, 2)
393396
bket = Ket(b.basis_l, b.data[:,i])
394397
resultket = Ket(M.basis_l, result.data[:,i])
@@ -398,7 +401,7 @@ function mul!(result::Operator{B1,B3},M::AbstractOperator{B1,B2},b::Operator{B2,
398401
return result
399402
end
400403

401-
function mul!(result::Operator{B1,B3},b::Operator{B1,B2},M::AbstractOperator{B2,B3},alpha,beta) where {B1,B2,B3}
404+
function mul!(result::Operator{B1,B3},b::Operator{B1,B2},M::BLROperator{B2,B3},alpha,beta) where {B1,B2,B3}
402405
for i=1:size(b.data, 1)
403406
bbra = Bra(b.basis_r, vec(b.data[i,:]))
404407
resultbra = Bra(M.basis_r, vec(result.data[i,:]))
@@ -469,4 +472,4 @@ Base.similar(x::Operator, t) = typeof(x)(x.basis_l, x.basis_r, copy(x.data))
469472
RecursiveArrayTools.recursivecopy!(dest::Operator{Bl,Br,A},src::Operator{Bl,Br,A}) where {Bl,Br,A} = copyto!(dest,src) # ODE in-place equations
470473
RecursiveArrayTools.recursivecopy(x::Operator) = copy(x)
471474
RecursiveArrayTools.recursivecopy(x::AbstractArray{T}) where {T<:Operator} = copy(x)
472-
RecursiveArrayTools.recursivefill!(x::Operator, a) = fill!(x, a)
475+
RecursiveArrayTools.recursivefill!(x::Operator, a) = fill!(x, a)

src/operators_lazyproduct.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ function LazyProduct(operators::T, factor::F=1) where {T,F}
3434
LazyProduct{BL,BR,F,T,KTL,BTR}(operators, ket_l, bra_r, factor)
3535
end
3636

37-
38-
37+
basis_l(op::LazyProduct) = op.basis_l
38+
basis_r(op::LazyProduct) = op.basis_r
3939

4040
LazyProduct(operators::Vector{T}, factor=1) where T<:AbstractOperator = LazyProduct((operators...,), factor)
4141
LazyProduct(operators::AbstractOperator...) = LazyProduct((operators...,))

src/operators_lazysum.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ end
1212
"""
1313
Abstract class for all Lazy type operators ([`LazySum`](@ref), [`LazyProduct`](@ref), and [`LazyTensor`](@ref))
1414
"""
15-
abstract type LazyOperator{BL,BR} <: AbstractOperator{BL,BR} end
15+
abstract type LazyOperator{BL,BR} <: BLROperator{BL,BR} end
1616

1717
"""
1818
LazySum([Tf,] [factors,] operators)
@@ -49,6 +49,9 @@ mutable struct LazySum{BL,BR,F,T} <: LazyOperator{BL,BR}
4949
end
5050
end
5151

52+
basis_l(op::LazySum) = op.basis_l
53+
basis_r(op::LazySum) = op.basis_r
54+
5255
LazySum(::Type{Tf}, basis_l::Basis, basis_r::Basis) where Tf = LazySum(basis_l,basis_r,Tf[],())
5356
LazySum(basis_l::Basis, basis_r::Basis) = LazySum(ComplexF64, basis_l, basis_r)
5457

src/operators_lazytensor.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ LazyTensor(op::T, factor) where {T<:LazyTensor} = LazyTensor(op.basis_l, op.basi
5656
LazyTensor(basis_l::CompositeBasis, basis_r::CompositeBasis, index::Integer, operator::T, factor=one(eltype(operator))) where T<:AbstractOperator = LazyTensor(basis_l, basis_r, [index], (operator,), factor)
5757
LazyTensor(basis::Basis, index, operators, factor=_default_factor(operators)) = LazyTensor(basis, basis, index, operators, factor)
5858

59+
basis_l(op::LazyTensor) = op.basis_l
60+
basis_r(op::LazyTensor) = op.basis_r
61+
5962
Base.copy(x::LazyTensor) = LazyTensor(x.basis_l, x.basis_r, copy(x.indices), Tuple(copy(op) for op in x.operators), x.factor)
6063
function Base.eltype(x::LazyTensor)
6164
F = eltype(x.factor)
@@ -112,14 +115,14 @@ function -(a::T1,b::T2) where {T1 <: single_dataoperator{B1,B2},T2 <: single_dat
112115
LazySum(a) - LazySum(b)
113116
end
114117

115-
function tensor(a::LazyTensor{B1,B2},b::AbstractOperator{B3,B4}) where {B1,B2,B3,B4}
118+
function tensor(a::LazyTensor{B1,B2},b::BLROperator{B3,B4}) where {B1,B2,B3,B4}
116119
if B3 <: CompositeBasis || B4 <: CompositeBasis
117120
throw(ArgumentError("tensor(a::LazyTensor{B1,B2},b::AbstractOperator{B3,B4}) is not implemented for B3 or B4 being CompositeBasis unless b is identityoperator "))
118121
else
119122
a LazyTensor(b.basis_l,b.basis_r,[1],(b,),1)
120123
end
121124
end
122-
function tensor(a::AbstractOperator{B1,B2},b::LazyTensor{B3,B4}) where {B1,B2,B3,B4}
125+
function tensor(a::BLROperator{B1,B2},b::LazyTensor{B3,B4}) where {B1,B2,B3,B4}
123126
if B1 <: CompositeBasis || B2 <: CompositeBasis
124127
throw(ArgumentError("tensor(a::AbstractOperator{B1,B2},b::LazyTensor{B3,B4}) is not implemented for B1 or B2 being CompositeBasis unless b is identityoperator "))
125128
else

src/particle.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ end
280280
281281
Abstract type for all implementations of FFT operators.
282282
"""
283-
abstract type FFTOperator{BL, BR, T} <: AbstractOperator{BL,BR} end
283+
abstract type FFTOperator{BL, BR, T} <: BLROperator{BL,BR} end
284284

285285
Base.eltype(x::FFTOperator) = promote_type(eltype(x.mul_before), eltype(x.mul_after))
286286

@@ -310,6 +310,9 @@ struct FFTOperators{BL,BR,T,P1,P2,P3,P4} <: FFTOperator{BL, BR, T}
310310
end
311311
end
312312

313+
basis_l(op::FFTOperators) = op.basis_l
314+
basis_r(op::FFTOperators) = op.basis_r
315+
313316
"""
314317
FFTKets
315318

src/spinors.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,15 @@ end
155155
156156
Lazy implementation of `directsum`
157157
"""
158-
mutable struct LazyDirectSum{BL,BR,T} <: AbstractOperator{BL,BR}
158+
mutable struct LazyDirectSum{BL,BR,T} <: BLROperator{BL,BR}
159159
basis_l::BL
160160
basis_r::BR
161161
operators::T
162162
end
163163

164+
basis_l(op::LazyDirectSum) = op.basis_l
165+
basis_r(op::LazyDirectSum) = op.basis_r
166+
164167
# Methods
165168
LazyDirectSum(op1::AbstractOperator, op2::AbstractOperator) = LazyDirectSum(directsum(op1.basis_l,op2.basis_l),directsum(op1.basis_r,op2.basis_r),(op1,op2))
166169
LazyDirectSum(op1::LazyDirectSum, op2::AbstractOperator) = LazyDirectSum(directsum(op1.basis_l,op2.basis_l),directsum(op1.basis_r,op2.basis_r),(op1.operators...,op2))

0 commit comments

Comments
 (0)