Skip to content

Commit 0599959

Browse files
authored
Base.convert(::Type{Operator}, ::Operator) (#107)
* Base.convert(::Type{Operator}, ::Operator) * do not convert if the type is already the same This is expected by the convert api, e.g. ``` julia> a = [1] 1-element Vector{Int64}: 1 julia> b = convert(Vector{Int},a) 1-element Vector{Int64}: 1 julia> a[1] = 2; b 1-element Vector{Int64}: 2 ```
1 parent f98ec89 commit 0599959

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
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.4.3"
3+
version = "0.4.4"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/operators_dense.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ Base.eltype(op::Operator) = eltype(op.data)
3232
Base.eltype(::Type{T}) where {BL,BR,D,T<:Operator{BL,BR,D}} = eltype(D)
3333
Base.size(op::Operator) = size(op.data)
3434
Base.size(op::Operator, d::Int) = size(op.data, d)
35+
function Base.convert(::Type{Operator{BL,BR,T}}, op::Operator{BL,BR,S}) where {BL,BR,T,S}
36+
if T==S
37+
return op
38+
else
39+
return Operator{BL,BR,T}(op.basis_l, op.basis_r, convert(T, op.data))
40+
end
41+
end
3542

3643
# Convert data to CuArray with cu(::Operator)
3744
Adapt.adapt_structure(to, x::Operator) = Operator(x.basis_l, x.basis_r, Adapt.adapt(to, x.data))

test/test_operators.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,9 @@ Lop1 = LazyTensor(b1^2, b2^2, 2, sparse(randoperator(b1, b2)))
140140
@test_throws ErrorException size(dense(Lop1), 0) # check for consistency
141141
@test_throws ErrorException size(dense(Lop1), -1)
142142

143+
# issue 106 | https://github.com/qojulia/QuantumOpticsBase.jl/issues/106
144+
a = destroy(FockBasis(5))
145+
@test dagger(a^2) == dagger(a)^2
146+
@test convert(Base._return_type(*, Tuple{typeof(a'), typeof(a')}), a') == a'
147+
143148
end # testset

0 commit comments

Comments
 (0)