Skip to content

Commit

Permalink
Base.convert(::Type{Operator}, ::Operator) (#107)
Browse files Browse the repository at this point in the history
* 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
```
  • Loading branch information
Krastanov authored Jun 19, 2023
1 parent f98ec89 commit 0599959
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "QuantumOpticsBase"
uuid = "4f57444f-1401-5e15-980d-4471b28d5678"
version = "0.4.3"
version = "0.4.4"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
7 changes: 7 additions & 0 deletions src/operators_dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ Base.eltype(op::Operator) = eltype(op.data)
Base.eltype(::Type{T}) where {BL,BR,D,T<:Operator{BL,BR,D}} = eltype(D)
Base.size(op::Operator) = size(op.data)
Base.size(op::Operator, d::Int) = size(op.data, d)
function Base.convert(::Type{Operator{BL,BR,T}}, op::Operator{BL,BR,S}) where {BL,BR,T,S}
if T==S
return op
else
return Operator{BL,BR,T}(op.basis_l, op.basis_r, convert(T, op.data))
end
end

# Convert data to CuArray with cu(::Operator)
Adapt.adapt_structure(to, x::Operator) = Operator(x.basis_l, x.basis_r, Adapt.adapt(to, x.data))
Expand Down
5 changes: 5 additions & 0 deletions test/test_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,9 @@ Lop1 = LazyTensor(b1^2, b2^2, 2, sparse(randoperator(b1, b2)))
@test_throws ErrorException size(dense(Lop1), 0) # check for consistency
@test_throws ErrorException size(dense(Lop1), -1)

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

end # testset

2 comments on commit 0599959

@Krastanov
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/85887

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.4 -m "<description of version>" 0599959ad0abb4237a2ae6f9fcf75323ca1531ea
git push origin v0.4.4

Please sign in to comment.