Skip to content

Commit 826a03d

Browse files
authored
Implement Base.copy for AbstractQuantumObject (#486)
1 parent 64ec15d commit 826a03d

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Introduce `Lanczos` solver for `spectrum`. ([#476])
1111
- Add Bloch-Redfield master equation solver. ([#473])
1212
- Implement Bloch Sphere rendering and align style with qutip. ([#472], [#480])
13+
- Add `Base.copy` method for `AbstractQuantumObject`. ([#486])
1314

1415
## [v0.31.1]
1516
Release date: 2025-05-16
@@ -236,3 +237,4 @@ Release date: 2024-11-13
236237
[#473]: https://github.com/qutip/QuantumToolbox.jl/issues/473
237238
[#476]: https://github.com/qutip/QuantumToolbox.jl/issues/476
238239
[#480]: https://github.com/qutip/QuantumToolbox.jl/issues/480
240+
[#486]: https://github.com/qutip/QuantumToolbox.jl/issues/486

src/qobj/quantum_object_base.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#=
22
This file defines the AbstractQuantumObject structure, all the type structures for AbstractQuantumObject, and fundamental functions in Julia standard library:
3-
- Base: show, length, size, eltype, getindex, setindex!, isequal, :(==), isapprox
3+
- Base: show, length, size, copy, eltype, getindex, setindex!, isequal, :(==), isapprox
44
=#
55

66
export AbstractQuantumObject
@@ -93,6 +93,8 @@ Optionally, you can specify an index (`idx`) to just get the corresponding dimen
9393
Base.size(A::AbstractQuantumObject) = size(A.data)
9494
Base.size(A::AbstractQuantumObject, idx::Int) = size(A.data, idx)
9595

96+
Base.copy(A::AbstractQuantumObject) = get_typename_wrapper(A)(copy(A.data), A.type, A.dimensions)
97+
9698
Base.getindex(A::AbstractQuantumObject, inds...) = getindex(A.data, inds...)
9799
Base.setindex!(A::AbstractQuantumObject, val, inds...) = setindex!(A.data, val, inds...)
98100

test/core-test/quantum_objects.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,12 @@
186186
a2 = Qobj(a)
187187
a3 = Qobj(a, type = SuperOperator())
188188
a4 = to_sparse(a2)
189+
a4_copy = copy(a4)
190+
a4_copy[1] = rand(ComplexF64)
189191
@test isequal(a4, a2) == true
190192
@test isequal(a4, a3) == false
191193
@test a4 a2
194+
@test a4 != a4_copy
192195

193196
@test real(a2).data == real(a)
194197
@test imag(a2).data == imag(a)

0 commit comments

Comments
 (0)