Skip to content

Commit 6a0001c

Browse files
akirakyleKrastanov
andauthored
Remove access of .data field on abstract types (#57)
--------- Co-authored-by: Stefan Krastanov <[email protected]> Co-authored-by: Stefan Krastanov <[email protected]>
1 parent f9171c6 commit 6a0001c

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# News
22

3-
## dev
3+
## v0.4.0 - dev
44

5+
- **(breaking)** Move methods which access a `.data` field but are defined only on an abstract types to QuantumOpticsBase
56
- Declare `mutual_information`, without any implemented methods.
67

78
## v0.3.10 - 2025-04-21

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "QuantumInterface"
22
uuid = "5717a53b-5d69-4fa3-b976-0bf2f97ca1e5"
33
authors = ["QuantumInterface.jl contributors"]
4-
version = "0.3.10"
4+
version = "0.4.0"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/QuantumInterface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module QuantumInterface
22

3-
import Base: ==, +, -, *, /, ^, length, one, exp, conj, conj!, transpose, copy
3+
import Base: ==, +, -, *, /, ^, length, one, exp, conj, conj!, transpose
44
import LinearAlgebra: tr, ishermitian, norm, normalize, normalize!
55
import Base: show, summary
66
import SparseArrays: sparse, spzeros, AbstractSparseMatrix # TODO move to an extension

src/julia_base.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
# Common error messages
2-
arithmetic_unary_error(funcname, x::AbstractOperator) = throw(ArgumentError("$funcname is not defined for this type of operator: $(typeof(x)).\nTry to convert to another operator type first with e.g. dense() or sparse()."))
3-
arithmetic_binary_error(funcname, a::AbstractOperator, b::AbstractOperator) = throw(ArgumentError("$funcname is not defined for this combination of types of operators: $(typeof(a)), $(typeof(b)).\nTry to convert to a common operator type first with e.g. dense() or sparse()."))
4-
addnumbererror() = throw(ArgumentError("Can't add or subtract a number and an operator. You probably want 'op + identityoperator(op)*x'."))
2+
# We use these error messages instead of:
3+
# - MethodErrors, which are not very informative about the fact that there might very well be a convenient conversion to the desired type
4+
# - error hints, which are a bit too cumbersome to register for so many different types and functions
5+
# TODO use error hints instead of these custom error messages
6+
arithmetic_unary_error(funcname, x::AbstractOperator) = throw(ArgumentError("$funcname is not defined for this type of operator: $(typeof(x)).\nYou can try to convert to another operator type first, e.g. with `dense()` or `sparse()` from `QuantumOptics` or other type conversions from other packages."))
7+
arithmetic_binary_error(funcname, a::AbstractOperator, b::AbstractOperator) = throw(ArgumentError("$funcname is not defined for this combination of types of operators: $(typeof(a)), $(typeof(b)).\nYou can try to convert to another (common) operator type first, e.g. with `dense()` or `sparse()` from `QuantumOptics` or other type conversions from other packages."))
8+
addnumbererror() = throw(ArgumentError("Can't add or subtract a number and an operator. You probably want `op + identityoperator(op)*x`."))
59

610

711
##
812
# States
913
##
1014

11-
-(a::T) where {T<:StateVector} = T(a.basis, -a.data) # FIXME issue #12
15+
-(a::StateVector) = arithmetic_unary_error("Negation", a)
1216
*(a::StateVector, b::Number) = b*a
13-
copy(a::T) where {T<:StateVector} = T(a.basis, copy(a.data)) # FIXME issue #12
1417
length(a::StateVector) = length(a.basis)::Int # FIXME issue #12
1518
basis(a::StateVector) = a.basis # FIXME issue #12
1619
directsum(x::StateVector...) = reduce(directsum, x)
1720

1821
# Array-like functions
19-
Base.size(x::StateVector) = size(x.data) # FIXME issue #12
20-
@inline Base.axes(x::StateVector) = axes(x.data) # FIXME issue #12
22+
Base.size(x::StateVector) = length(basis(x))
2123
Base.ndims(x::StateVector) = 1
2224
Base.ndims(::Type{<:StateVector}) = 1
23-
Base.eltype(x::StateVector) = eltype(x.data) # FIXME issue #12
2425

2526
# Broadcasting
2627
Base.broadcastable(x::StateVector) = x

src/julia_linalg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tr(x::AbstractOperator) = arithmetic_unary_error("Trace", x)
1717
1818
Norm of the given bra or ket state.
1919
"""
20-
norm(x::StateVector) = norm(x.data) # FIXME issue #12
20+
norm(x::StateVector) = arithmetic_unary_error("norm", x)
2121

2222
"""
2323
normalize(x::StateVector)
@@ -31,7 +31,7 @@ normalize(x::StateVector) = x/norm(x)
3131
3232
In-place normalization of the given bra or ket so that `norm(x)` is one.
3333
"""
34-
normalize!(x::StateVector) = (normalize!(x.data); x) # FIXME issue #12
34+
normalize!(x::StateVector) = arithmetic_unary_error("normalize!", x)
3535

3636
"""
3737
normalize(op)

0 commit comments

Comments
 (0)