Skip to content

Commit

Permalink
Support zero and one for AbstractQuantumObject (qutip#346)
Browse files Browse the repository at this point in the history
* support `zero` and `one` for `AbstractQuantumObject`

* update changelog

* fix changelog
  • Loading branch information
ytdHuang authored Dec 11, 2024
1 parent 5e108e1 commit e754148
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://github.com/qutip/QuantumToolbox.jl/tree/main)

- Improve the construction of `QobjEvo`. ([#338], [#339])
- Support `Base.zero` and `Base.one` for `AbstractQuantumObject`. ([#342], [#346])

## [v0.23.1]
Release date: 2024-12-06
Expand Down Expand Up @@ -55,3 +56,5 @@ Release date: 2024-11-13
[#335]: https://github.com/qutip/QuantumToolbox.jl/issues/335
[#338]: https://github.com/qutip/QuantumToolbox.jl/issues/338
[#339]: https://github.com/qutip/QuantumToolbox.jl/issues/339
[#342]: https://github.com/qutip/QuantumToolbox.jl/issues/342
[#346]: https://github.com/qutip/QuantumToolbox.jl/issues/346
2 changes: 2 additions & 0 deletions docs/src/resources/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ SciMLOperators.isconstant
## [Qobj arithmetic and attributes](@id doc-API:Qobj-arithmetic-and-attributes)

```@docs
Base.zero
Base.one
Base.conj
LinearAlgebra.transpose
LinearAlgebra.adjoint
Expand Down
2 changes: 2 additions & 0 deletions docs/src/users_guide/QuantumObject/QuantumObject_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Here is a table that summarizes all the supported linear algebra functions and a

| **Description** | **Function call** | **Synonyms** |
|:----------------|:------------------|:-------------|
| zero-like array | [`zero(Q)`](@ref zero) | - |
| identity-like matrix | [`one(Q)`](@ref one) | - |
| conjugate | [`conj(Q)`](@ref conj) | - |
| transpose | [`transpose(Q)`](@ref transpose) | [`trans(Q)`](@ref trans) |
| conjugate transposition | [`adjoint(Q)`](@ref adjoint) | [`Q'`](@ref adjoint), [`dag(Q)`](@ref dag) |
Expand Down
19 changes: 19 additions & 0 deletions src/qobj/arithmetic_and_attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ function LinearAlgebra.dot(
return LinearAlgebra.dot(i.data, A.data, j.data)
end

@doc raw"""
zero(A::AbstractQuantumObject)
Return a similar [`AbstractQuantumObject`](@ref) with `dims` and `type` are same as `A`, but `data` is a zero-array.
"""
Base.zero(A::AbstractQuantumObject) = get_typename_wrapper(A)(zero(A.data), A.type, A.dims)

@doc raw"""
one(A::AbstractQuantumObject)
Return a similar [`AbstractQuantumObject`](@ref) with `dims` and `type` are same as `A`, but `data` is an identity matrix.
Note that `A` must be [`Operator`](@ref) or [`SuperOperator`](@ref).
"""
Base.one(
A::AbstractQuantumObject{DT,OpType},
) where {DT,OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} =
get_typename_wrapper(A)(one(A.data), A.type, A.dims)

@doc raw"""
conj(A::AbstractQuantumObject)
Expand Down
8 changes: 8 additions & 0 deletions test/core-test/quantum_objects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@
@test (a2 + 2).data == a2.data + 2 * I
@test a2 * 2 == 2 * a2

zero_like = zero(a2)
iden_like = one(a3)
zero_array = spzeros(ComplexF64, 100, 100)
iden_array = sparse(1:100, 1:100, ones(ComplexF64, 100))
@test zero_like == Qobj(zero_array, type = a2.type, dims = a2.dims)
@test typeof(zero_like.data) == typeof(zero_array)
@test iden_like == Qobj(iden_array, type = a3.type, dims = a3.dims)
@test typeof(iden_like.data) == typeof(iden_array)
@test trans(trans(a2)) == a2
@test trans(a2).data == transpose(a2.data)
@test adjoint(a2) trans(conj(a2))
Expand Down
8 changes: 8 additions & 0 deletions test/core-test/quantum_objects_evo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@
@test (a2 + 2).data == a2.data + 2 * I
@test a2 * 2 == 2 * a2

zero_like = zero(a2)
iden_like = one(a3)
zero_array = NullOperator(100)
iden_array = IdentityOperator(100)
@test zero_like == QobjEvo(zero_array, type = a2.type, dims = a2.dims)
@test typeof(zero_like.data) == typeof(zero_array)
@test iden_like == QobjEvo(iden_array, type = a3.type, dims = a3.dims)
@test typeof(iden_like.data) == typeof(iden_array)
@test trans(trans(a2)) == a2
@test trans(a2).data == transpose(a2.data)
# @test adjoint(a2) ≈ trans(conj(a2)) # Currently doesn't work
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if (GROUP == "All") || (GROUP == "Core")
using QuantumToolbox
import QuantumToolbox: position, momentum
import Random: MersenneTwister
import SciMLOperators: MatrixOperator
import SciMLOperators: MatrixOperator, NullOperator, IdentityOperator

QuantumToolbox.about()

Expand Down

0 comments on commit e754148

Please sign in to comment.