|
1 | 1 | # 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`.")) |
5 | 9 |
|
6 | 10 |
|
7 | 11 | ## |
8 | 12 | # States |
9 | 13 | ## |
10 | 14 |
|
11 | | --(a::T) where {T<:StateVector} = T(a.basis, -a.data) # FIXME issue #12 |
| 15 | +-(a::StateVector) = arithmetic_unary_error("Negation", a) |
12 | 16 | *(a::StateVector, b::Number) = b*a |
13 | | -copy(a::T) where {T<:StateVector} = T(a.basis, copy(a.data)) # FIXME issue #12 |
14 | 17 | length(a::StateVector) = length(a.basis)::Int # FIXME issue #12 |
15 | 18 | basis(a::StateVector) = a.basis # FIXME issue #12 |
16 | 19 | directsum(x::StateVector...) = reduce(directsum, x) |
17 | 20 |
|
18 | 21 | # 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)) |
21 | 23 | Base.ndims(x::StateVector) = 1 |
22 | 24 | Base.ndims(::Type{<:StateVector}) = 1 |
23 | | -Base.eltype(x::StateVector) = eltype(x.data) # FIXME issue #12 |
24 | 25 |
|
25 | 26 | # Broadcasting |
26 | 27 | Base.broadcastable(x::StateVector) = x |
|
0 commit comments