From e754148481e5fe4d69d276cf00068303b8c20cbd Mon Sep 17 00:00:00 2001 From: Yi-Te Huang <44385685+ytdHuang@users.noreply.github.com> Date: Wed, 11 Dec 2024 17:08:32 +0900 Subject: [PATCH] Support `zero` and `one` for `AbstractQuantumObject` (#346) * support `zero` and `one` for `AbstractQuantumObject` * update changelog * fix changelog --- CHANGELOG.md | 3 +++ docs/src/resources/api.md | 2 ++ .../QuantumObject/QuantumObject_functions.md | 2 ++ src/qobj/arithmetic_and_attributes.jl | 19 +++++++++++++++++++ test/core-test/quantum_objects.jl | 8 ++++++++ test/core-test/quantum_objects_evo.jl | 8 ++++++++ test/runtests.jl | 2 +- 7 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94fb28ee..21d5d665 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/docs/src/resources/api.md b/docs/src/resources/api.md index d20fc680..5e7c0027 100644 --- a/docs/src/resources/api.md +++ b/docs/src/resources/api.md @@ -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 diff --git a/docs/src/users_guide/QuantumObject/QuantumObject_functions.md b/docs/src/users_guide/QuantumObject/QuantumObject_functions.md index 698017dc..ac2c6269 100644 --- a/docs/src/users_guide/QuantumObject/QuantumObject_functions.md +++ b/docs/src/users_guide/QuantumObject/QuantumObject_functions.md @@ -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) | diff --git a/src/qobj/arithmetic_and_attributes.jl b/src/qobj/arithmetic_and_attributes.jl index 5a9c7d98..6a106148 100644 --- a/src/qobj/arithmetic_and_attributes.jl +++ b/src/qobj/arithmetic_and_attributes.jl @@ -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) diff --git a/test/core-test/quantum_objects.jl b/test/core-test/quantum_objects.jl index 36e720b5..6d26ac4c 100644 --- a/test/core-test/quantum_objects.jl +++ b/test/core-test/quantum_objects.jl @@ -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)) diff --git a/test/core-test/quantum_objects_evo.jl b/test/core-test/quantum_objects_evo.jl index a10416a8..5910b0e5 100644 --- a/test/core-test/quantum_objects_evo.jl +++ b/test/core-test/quantum_objects_evo.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 20f72b05..43b4fc75 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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()