diff --git a/CHANGELOG.md b/CHANGELOG.md index 49e8cbeef..b7902671f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Introduce `Lanczos` solver for `spectrum`. ([#476]) - Add Bloch-Redfield master equation solver. ([#473]) - Implement Bloch Sphere rendering and align style with qutip. ([#472], [#480]) +- Add `Base.copy` method for `AbstractQuantumObject`. ([#486]) ## [v0.31.1] Release date: 2025-05-16 @@ -236,3 +237,4 @@ Release date: 2024-11-13 [#473]: https://github.com/qutip/QuantumToolbox.jl/issues/473 [#476]: https://github.com/qutip/QuantumToolbox.jl/issues/476 [#480]: https://github.com/qutip/QuantumToolbox.jl/issues/480 +[#486]: https://github.com/qutip/QuantumToolbox.jl/issues/486 diff --git a/src/qobj/quantum_object_base.jl b/src/qobj/quantum_object_base.jl index 9fcbb0502..cf5fcd679 100644 --- a/src/qobj/quantum_object_base.jl +++ b/src/qobj/quantum_object_base.jl @@ -1,6 +1,6 @@ #= This file defines the AbstractQuantumObject structure, all the type structures for AbstractQuantumObject, and fundamental functions in Julia standard library: - - Base: show, length, size, eltype, getindex, setindex!, isequal, :(==), isapprox + - Base: show, length, size, copy, eltype, getindex, setindex!, isequal, :(==), isapprox =# export AbstractQuantumObject @@ -93,6 +93,8 @@ Optionally, you can specify an index (`idx`) to just get the corresponding dimen Base.size(A::AbstractQuantumObject) = size(A.data) Base.size(A::AbstractQuantumObject, idx::Int) = size(A.data, idx) +Base.copy(A::AbstractQuantumObject) = get_typename_wrapper(A)(copy(A.data), A.type, A.dimensions) + Base.getindex(A::AbstractQuantumObject, inds...) = getindex(A.data, inds...) Base.setindex!(A::AbstractQuantumObject, val, inds...) = setindex!(A.data, val, inds...) diff --git a/test/core-test/quantum_objects.jl b/test/core-test/quantum_objects.jl index 81afc501b..920f26970 100644 --- a/test/core-test/quantum_objects.jl +++ b/test/core-test/quantum_objects.jl @@ -186,9 +186,12 @@ a2 = Qobj(a) a3 = Qobj(a, type = SuperOperator()) a4 = to_sparse(a2) + a4_copy = copy(a4) + a4_copy[1] = rand(ComplexF64) @test isequal(a4, a2) == true @test isequal(a4, a3) == false @test a4 ≈ a2 + @test a4 != a4_copy @test real(a2).data == real(a) @test imag(a2).data == imag(a)