Skip to content

Commit e21b0e5

Browse files
ytdHuangAdria Labay
authored andcommitted
Support zero and one for AbstractQuantumObject (#346)
* support `zero` and `one` for `AbstractQuantumObject` * update changelog * fix changelog
1 parent 839e6e7 commit e21b0e5

File tree

7 files changed

+43
-1
lines changed

7 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased](https://github.com/qutip/QuantumToolbox.jl/tree/main)
99

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

1213
## [v0.23.1]
1314
Release date: 2024-12-06
@@ -55,3 +56,5 @@ Release date: 2024-11-13
5556
[#335]: https://github.com/qutip/QuantumToolbox.jl/issues/335
5657
[#338]: https://github.com/qutip/QuantumToolbox.jl/issues/338
5758
[#339]: https://github.com/qutip/QuantumToolbox.jl/issues/339
59+
[#342]: https://github.com/qutip/QuantumToolbox.jl/issues/342
60+
[#346]: https://github.com/qutip/QuantumToolbox.jl/issues/346

docs/src/resources/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ SciMLOperators.isconstant
5252
## [Qobj arithmetic and attributes](@id doc-API:Qobj-arithmetic-and-attributes)
5353

5454
```@docs
55+
Base.zero
56+
Base.one
5557
Base.conj
5658
LinearAlgebra.transpose
5759
LinearAlgebra.adjoint

docs/src/users_guide/QuantumObject/QuantumObject_functions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Here is a table that summarizes all the supported linear algebra functions and a
1010

1111
| **Description** | **Function call** | **Synonyms** |
1212
|:----------------|:------------------|:-------------|
13+
| zero-like array | [`zero(Q)`](@ref zero) | - |
14+
| identity-like matrix | [`one(Q)`](@ref one) | - |
1315
| conjugate | [`conj(Q)`](@ref conj) | - |
1416
| transpose | [`transpose(Q)`](@ref transpose) | [`trans(Q)`](@ref trans) |
1517
| conjugate transposition | [`adjoint(Q)`](@ref adjoint) | [`Q'`](@ref adjoint), [`dag(Q)`](@ref dag) |

src/qobj/arithmetic_and_attributes.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ function LinearAlgebra.dot(
158158
return LinearAlgebra.dot(i.data, A.data, j.data)
159159
end
160160

161+
@doc raw"""
162+
zero(A::AbstractQuantumObject)
163+
164+
Return a similar [`AbstractQuantumObject`](@ref) with `dims` and `type` are same as `A`, but `data` is a zero-array.
165+
"""
166+
Base.zero(A::AbstractQuantumObject) = get_typename_wrapper(A)(zero(A.data), A.type, A.dims)
167+
168+
@doc raw"""
169+
one(A::AbstractQuantumObject)
170+
171+
Return a similar [`AbstractQuantumObject`](@ref) with `dims` and `type` are same as `A`, but `data` is an identity matrix.
172+
173+
Note that `A` must be [`Operator`](@ref) or [`SuperOperator`](@ref).
174+
"""
175+
Base.one(
176+
A::AbstractQuantumObject{DT,OpType},
177+
) where {DT,OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} =
178+
get_typename_wrapper(A)(one(A.data), A.type, A.dims)
179+
161180
@doc raw"""
162181
conj(A::AbstractQuantumObject)
163182

test/core-test/quantum_objects.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@
166166
@test (a2 + 2).data == a2.data + 2 * I
167167
@test a2 * 2 == 2 * a2
168168

169+
zero_like = zero(a2)
170+
iden_like = one(a3)
171+
zero_array = spzeros(ComplexF64, 100, 100)
172+
iden_array = sparse(1:100, 1:100, ones(ComplexF64, 100))
173+
@test zero_like == Qobj(zero_array, type = a2.type, dims = a2.dims)
174+
@test typeof(zero_like.data) == typeof(zero_array)
175+
@test iden_like == Qobj(iden_array, type = a3.type, dims = a3.dims)
176+
@test typeof(iden_like.data) == typeof(iden_array)
169177
@test trans(trans(a2)) == a2
170178
@test trans(a2).data == transpose(a2.data)
171179
@test adjoint(a2) trans(conj(a2))

test/core-test/quantum_objects_evo.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@
7474
@test (a2 + 2).data == a2.data + 2 * I
7575
@test a2 * 2 == 2 * a2
7676

77+
zero_like = zero(a2)
78+
iden_like = one(a3)
79+
zero_array = NullOperator(100)
80+
iden_array = IdentityOperator(100)
81+
@test zero_like == QobjEvo(zero_array, type = a2.type, dims = a2.dims)
82+
@test typeof(zero_like.data) == typeof(zero_array)
83+
@test iden_like == QobjEvo(iden_array, type = a3.type, dims = a3.dims)
84+
@test typeof(iden_like.data) == typeof(iden_array)
7785
@test trans(trans(a2)) == a2
7886
@test trans(a2).data == transpose(a2.data)
7987
# @test adjoint(a2) ≈ trans(conj(a2)) # Currently doesn't work

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if (GROUP == "All") || (GROUP == "Core")
3737
using QuantumToolbox
3838
import QuantumToolbox: position, momentum
3939
import Random: MersenneTwister
40-
import SciMLOperators: MatrixOperator
40+
import SciMLOperators: MatrixOperator, NullOperator, IdentityOperator
4141

4242
QuantumToolbox.about()
4343

0 commit comments

Comments
 (0)