3636
3737for op in (:(+ ), :(- ), :(* ))
3838 @eval begin
39- function LinearAlgebra. $op (
40- A:: QuantumObject{<:AbstractArray{T1},OpType} ,
41- B:: QuantumObject{<:AbstractArray{T2},OpType} ,
42- ) where {T1,T2,OpType<: QuantumObjectType }
43- A. dims != B. dims &&
44- throw (DimensionMismatch (" The two quantum objects are not of the same Hilbert dimension." ))
45- return QuantumObject ($ (op)(A. data, B. data), A. type, A. dims)
39+ function LinearAlgebra. $op (A:: AbstractQuantumObject , B:: AbstractQuantumObject )
40+ check_dims (A, B)
41+ QType = promote_op_type (A, B)
42+ return QType ($ (op)(A. data, B. data), A. type, A. dims)
4643 end
47- LinearAlgebra.$ op (A:: QuantumObject{<:AbstractArray{T}} ) where {T} = QuantumObject ($ (op)(A. data), A. type, A. dims)
44+ LinearAlgebra.$ op (A:: AbstractQuantumObject ) = get_typename_wrapper (A) ($ (op)(A. data), A. type, A. dims)
4845
49- LinearAlgebra.$ op (n:: T1 , A:: QuantumObject{<:AbstractArray{T2}} ) where {T1 <: Number ,T2 } =
50- QuantumObject ($ (op)(n * I, A. data), A. type, A. dims)
51- LinearAlgebra.$ op (A:: QuantumObject{<:AbstractArray{T1}} , n:: T2 ) where {T1,T2 <: Number } =
52- QuantumObject ($ (op)(A. data, n * I), A. type, A. dims)
46+ LinearAlgebra.$ op (n:: T , A:: AbstractQuantumObject ) where {T <: Number } =
47+ get_typename_wrapper (A) ($ (op)(n * I, A. data), A. type, A. dims)
48+ LinearAlgebra.$ op (A:: AbstractQuantumObject , n:: T ) where {T <: Number } =
49+ get_typename_wrapper (A) ($ (op)(A. data, n * I), A. type, A. dims)
5350 end
5451end
5552
5653function LinearAlgebra.:(* )(
57- A:: QuantumObject{<:AbstractArray{T1} ,OperatorQuantumObject} ,
58- B:: QuantumObject{<:AbstractArray{T2} ,KetQuantumObject} ,
59- ) where {T1,T2 }
60- A . dims != B . dims && throw ( DimensionMismatch ( " The two quantum objects are not of the same Hilbert dimension. " ) )
54+ A:: AbstractQuantumObject{DT1 ,OperatorQuantumObject} ,
55+ B:: QuantumObject{DT2 ,KetQuantumObject} ,
56+ ) where {DT1,DT2 }
57+ check_dims (A, B )
6158 return QuantumObject (A. data * B. data, Ket, A. dims)
6259end
6360function LinearAlgebra.:(* )(
64- A:: QuantumObject{<:AbstractArray{T1} ,BraQuantumObject} ,
65- B:: QuantumObject{<:AbstractArray{T2} ,OperatorQuantumObject} ,
66- ) where {T1,T2 }
67- A . dims != B . dims && throw ( DimensionMismatch ( " The two quantum objects are not of the same Hilbert dimension. " ) )
61+ A:: QuantumObject{DT1 ,BraQuantumObject} ,
62+ B:: AbstractQuantumObject{DT2 ,OperatorQuantumObject} ,
63+ ) where {DT1,DT2 }
64+ check_dims (A, B )
6865 return QuantumObject (A. data * B. data, Bra, A. dims)
6966end
7067function LinearAlgebra.:(* )(
71- A:: QuantumObject{<:AbstractArray{T1} ,KetQuantumObject} ,
72- B:: QuantumObject{<:AbstractArray{T2} ,BraQuantumObject} ,
73- ) where {T1,T2 }
74- A . dims != B . dims && throw ( DimensionMismatch ( " The two quantum objects are not of the same Hilbert dimension. " ) )
68+ A:: QuantumObject{DT1 ,KetQuantumObject} ,
69+ B:: QuantumObject{DT2 ,BraQuantumObject} ,
70+ ) where {DT1,DT2 }
71+ check_dims (A, B )
7572 return QuantumObject (A. data * B. data, Operator, A. dims)
7673end
7774function LinearAlgebra.:(* )(
78- A:: QuantumObject{<:AbstractArray{T1} ,BraQuantumObject} ,
79- B:: QuantumObject{<:AbstractArray{T2} ,KetQuantumObject} ,
80- ) where {T1,T2 }
81- A . dims != B . dims && throw ( DimensionMismatch ( " The two quantum objects are not of the same Hilbert dimension. " ) )
75+ A:: QuantumObject{DT1 ,BraQuantumObject} ,
76+ B:: QuantumObject{DT2 ,KetQuantumObject} ,
77+ ) where {DT1,DT2 }
78+ check_dims (A, B )
8279 return A. data * B. data
8380end
8481function LinearAlgebra.:(* )(
85- A:: QuantumObject{<:AbstractArray{T1} ,SuperOperatorQuantumObject} ,
86- B:: QuantumObject{<:AbstractArray{T2} ,OperatorQuantumObject} ,
87- ) where {T1,T2 }
88- A . dims != B . dims && throw ( DimensionMismatch ( " The two quantum objects are not of the same Hilbert dimension. " ) )
82+ A:: AbstractQuantumObject{DT1 ,SuperOperatorQuantumObject} ,
83+ B:: QuantumObject{DT2 ,OperatorQuantumObject} ,
84+ ) where {DT1,DT2 }
85+ check_dims (A, B )
8986 return QuantumObject (vec2mat (A. data * mat2vec (B. data)), Operator, A. dims)
9087end
9188function LinearAlgebra.:(* )(
92- A:: QuantumObject{<:AbstractArray{T1} ,OperatorBraQuantumObject} ,
93- B:: QuantumObject{<:AbstractArray{T2} ,OperatorKetQuantumObject} ,
94- ) where {T1,T2 }
95- A . dims != B . dims && throw ( DimensionMismatch ( " The two quantum objects are not of the same Hilbert dimension. " ) )
89+ A:: QuantumObject{DT1 ,OperatorBraQuantumObject} ,
90+ B:: QuantumObject{DT2 ,OperatorKetQuantumObject} ,
91+ ) where {DT1,DT2 }
92+ check_dims (A, B )
9693 return A. data * B. data
9794end
9895function LinearAlgebra.:(* )(
99- A:: QuantumObject{<:AbstractArray{T1} ,SuperOperatorQuantumObject} ,
100- B:: QuantumObject{<:AbstractArray{T2} ,OperatorKetQuantumObject} ,
101- ) where {T1,T2 }
102- A . dims != B . dims && throw ( DimensionMismatch ( " The two quantum objects are not of the same Hilbert dimension. " ) )
96+ A:: AbstractQuantumObject{DT1 ,SuperOperatorQuantumObject} ,
97+ B:: QuantumObject{DT2 ,OperatorKetQuantumObject} ,
98+ ) where {DT1,DT2 }
99+ check_dims (A, B )
103100 return QuantumObject (A. data * B. data, OperatorKet, A. dims)
104101end
105102function LinearAlgebra.:(* )(
106103 A:: QuantumObject{<:AbstractArray{T1},OperatorBraQuantumObject} ,
107- B:: QuantumObject {<:AbstractArray{T2},SuperOperatorQuantumObject} ,
104+ B:: AbstractQuantumObject {<:AbstractArray{T2},SuperOperatorQuantumObject} ,
108105) where {T1,T2}
109- A . dims != B . dims && throw ( DimensionMismatch ( " The two quantum objects are not of the same Hilbert dimension. " ) )
106+ check_dims (A, B )
110107 return QuantumObject (A. data * B. data, OperatorBra, A. dims)
111108end
112109
113- LinearAlgebra.:(^ )(A:: QuantumObject{<:AbstractArray{T}} , n:: T1 ) where {T,T1<: Number } =
114- QuantumObject (^ (A. data, n), A. type, A. dims)
115- LinearAlgebra.:(/ )(A:: QuantumObject{<:AbstractArray{T}} , n:: T1 ) where {T,T1<: Number } =
116- QuantumObject (/ (A. data, n), A. type, A. dims)
110+ LinearAlgebra.:(^ )(A:: QuantumObject{DT} , n:: T ) where {DT,T<: Number } = QuantumObject (^ (A. data, n), A. type, A. dims)
111+ LinearAlgebra.:(/ )(A:: AbstractQuantumObject{DT} , n:: T ) where {DT,T<: Number } =
112+ get_typename_wrapper (A)(A. data / n, A. type, A. dims)
117113
118114@doc raw """
119115 dot(A::QuantumObject, B::QuantumObject)
@@ -125,93 +121,91 @@ Note that `A` and `B` should be [`Ket`](@ref) or [`OperatorKet`](@ref)
125121`A ⋅ B` (where `⋅` can be typed by tab-completing `\c dot` in the REPL) is a synonym for `dot(A, B)`
126122"""
127123function LinearAlgebra. dot (
128- A:: QuantumObject{<:AbstractArray{T1} ,OpType} ,
129- B:: QuantumObject{<:AbstractArray{T2} ,OpType} ,
130- ) where {T1 <: Number ,T2 <: Number ,OpType<: Union{KetQuantumObject,OperatorKetQuantumObject} }
124+ A:: QuantumObject{DT1 ,OpType} ,
125+ B:: QuantumObject{DT2 ,OpType} ,
126+ ) where {DT1,DT2 ,OpType<: Union{KetQuantumObject,OperatorKetQuantumObject} }
131127 A. dims != B. dims && throw (DimensionMismatch (" The quantum objects are not of the same Hilbert dimension." ))
132128 return LinearAlgebra. dot (A. data, B. data)
133129end
134130
135131@doc raw """
136- dot(i::QuantumObject, A::QuantumObject j::QuantumObject)
132+ dot(i::QuantumObject, A::AbstractQuantumObject j::QuantumObject)
137133
138- Compute the generalized dot product `dot(i, A*j)` between three [`QuantumObject`](@ref): `` \l angle i | \h at{A} | j \r angle``
134+ Compute the generalized dot product `dot(i, A*j)` between a [`AbstractQuantumObject`](@ref) and two [` QuantumObject`](@ref) (`i` and `j`), namely `` \l angle i | \h at{A} | j \r angle``.
139135
140136Supports the following inputs:
141137- `A` is in the type of [`Operator`](@ref), with `i` and `j` are both [`Ket`](@ref).
142138- `A` is in the type of [`SuperOperator`](@ref), with `i` and `j` are both [`OperatorKet`](@ref)
143139"""
144140function LinearAlgebra. dot (
145- i:: QuantumObject{<:AbstractArray{T1} ,KetQuantumObject} ,
146- A:: QuantumObject{<:AbstractArray{T2} ,OperatorQuantumObject} ,
147- j:: QuantumObject{<:AbstractArray{T3} ,KetQuantumObject} ,
148- ) where {T1 <: Number ,T2 <: Number ,T3 <: Number }
141+ i:: QuantumObject{DT1 ,KetQuantumObject} ,
142+ A:: AbstractQuantumObject{DT2 ,OperatorQuantumObject} ,
143+ j:: QuantumObject{DT3 ,KetQuantumObject} ,
144+ ) where {DT1,DT2,DT3 }
149145 ((i. dims != A. dims) || (A. dims != j. dims)) &&
150146 throw (DimensionMismatch (" The quantum objects are not of the same Hilbert dimension." ))
151147 return LinearAlgebra. dot (i. data, A. data, j. data)
152148end
153149function LinearAlgebra. dot (
154- i:: QuantumObject{<:AbstractArray{T1} ,OperatorKetQuantumObject} ,
155- A:: QuantumObject{<:AbstractArray{T2} ,SuperOperatorQuantumObject} ,
156- j:: QuantumObject{<:AbstractArray{T3} ,OperatorKetQuantumObject} ,
157- ) where {T1 <: Number ,T2 <: Number ,T3 <: Number }
150+ i:: QuantumObject{DT1 ,OperatorKetQuantumObject} ,
151+ A:: AbstractQuantumObject{DT2 ,SuperOperatorQuantumObject} ,
152+ j:: QuantumObject{DT3 ,OperatorKetQuantumObject} ,
153+ ) where {DT1,DT2,DT3 }
158154 ((i. dims != A. dims) || (A. dims != j. dims)) &&
159155 throw (DimensionMismatch (" The quantum objects are not of the same Hilbert dimension." ))
160156 return LinearAlgebra. dot (i. data, A. data, j. data)
161157end
162158
163159@doc raw """
164- conj(A::QuantumObject )
160+ conj(A::AbstractQuantumObject )
165161
166- Return the element-wise complex conjugation of the [`QuantumObject `](@ref).
162+ Return the element-wise complex conjugation of the [`AbstractQuantumObject `](@ref).
167163"""
168- Base. conj (A:: QuantumObject{<:AbstractArray{T}} ) where {T} = QuantumObject (conj (A. data), A. type, A. dims)
164+ Base. conj (A:: AbstractQuantumObject ) = get_typename_wrapper (A) (conj (A. data), A. type, A. dims)
169165
170166@doc raw """
171- transpose(A::QuantumObject )
167+ transpose(A::AbstractQuantumObject )
172168
173- Lazy matrix transpose of the [`QuantumObject `](@ref).
169+ Lazy matrix transpose of the [`AbstractQuantumObject `](@ref).
174170"""
175171LinearAlgebra. transpose (
176- A:: QuantumObject{<:AbstractArray{T} ,OpType} ,
177- ) where {T ,OpType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } =
178- QuantumObject (transpose (A. data), A. type, A. dims)
172+ A:: AbstractQuantumObject{DT ,OpType} ,
173+ ) where {DT ,OpType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } =
174+ get_typename_wrapper (A) (transpose (A. data), A. type, A. dims)
179175
180176@doc raw """
181177 A'
182- adjoint(A::QuantumObject )
178+ adjoint(A::AbstractQuantumObject )
183179
184- Lazy adjoint (conjugate transposition) of the [`QuantumObject `](@ref)
180+ Lazy adjoint (conjugate transposition) of the [`AbstractQuantumObject `](@ref)
185181
186182Note that `A'` is a synonym for `adjoint(A)`
187183"""
188184LinearAlgebra. adjoint (
189- A:: QuantumObject{<:AbstractArray{T},OpType} ,
190- ) where {T,OpType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } =
191- QuantumObject (adjoint (A. data), A. type, A. dims)
192- LinearAlgebra. adjoint (A:: QuantumObject{<:AbstractArray{T},KetQuantumObject} ) where {T} =
193- QuantumObject (adjoint (A. data), Bra, A. dims)
194- LinearAlgebra. adjoint (A:: QuantumObject{<:AbstractArray{T},BraQuantumObject} ) where {T} =
195- QuantumObject (adjoint (A. data), Ket, A. dims)
196- LinearAlgebra. adjoint (A:: QuantumObject{<:AbstractArray{T},OperatorKetQuantumObject} ) where {T} =
185+ A:: AbstractQuantumObject{DT,OpType} ,
186+ ) where {DT,OpType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } =
187+ get_typename_wrapper (A)(adjoint (A. data), A. type, A. dims)
188+ LinearAlgebra. adjoint (A:: QuantumObject{DT,KetQuantumObject} ) where {DT} = QuantumObject (adjoint (A. data), Bra, A. dims)
189+ LinearAlgebra. adjoint (A:: QuantumObject{DT,BraQuantumObject} ) where {DT} = QuantumObject (adjoint (A. data), Ket, A. dims)
190+ LinearAlgebra. adjoint (A:: QuantumObject{DT,OperatorKetQuantumObject} ) where {DT} =
197191 QuantumObject (adjoint (A. data), OperatorBra, A. dims)
198- LinearAlgebra. adjoint (A:: QuantumObject{<:AbstractArray{T} ,OperatorBraQuantumObject} ) where {T } =
192+ LinearAlgebra. adjoint (A:: QuantumObject{DT ,OperatorBraQuantumObject} ) where {DT } =
199193 QuantumObject (adjoint (A. data), OperatorKet, A. dims)
200194
201195@doc raw """
202- inv(A::QuantumObject )
196+ inv(A::AbstractQuantumObject )
203197
204- Matrix inverse of the [`QuantumObject `](@ref)
198+ Matrix inverse of the [`AbstractQuantumObject `](@ref). If `A` is a [`QuantumObjectEvolution`](@ref), the inverse is computed at the last computed time.
205199"""
206200LinearAlgebra. inv (
207- A:: QuantumObject{<:AbstractArray{T} ,OpType} ,
208- ) where {T ,OpType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } =
201+ A:: AbstractQuantumObject{DT ,OpType} ,
202+ ) where {DT ,OpType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } =
209203 QuantumObject (sparse (inv (Matrix (A. data))), A. type, A. dims)
210204
211205LinearAlgebra. Hermitian (
212- A:: QuantumObject{<:AbstractArray{T} ,OpType} ,
206+ A:: QuantumObject{DT ,OpType} ,
213207 uplo:: Symbol = :U ,
214- ) where {T ,OpType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } =
208+ ) where {DT ,OpType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } =
215209 QuantumObject (Hermitian (A. data, uplo), A. type, A. dims)
216210
217211@doc raw """
@@ -436,8 +430,8 @@ Matrix sine of [`QuantumObject`](@ref), defined as
436430Note that this function only supports for [`Operator`](@ref) and [`SuperOperator`](@ref)
437431"""
438432LinearAlgebra. sin (
439- A:: QuantumObject{<:AbstractMatrix{T} ,ObjType} ,
440- ) where {T ,ObjType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } = (exp (1im * A) - exp (- 1im * A)) / 2im
433+ A:: QuantumObject{DT ,ObjType} ,
434+ ) where {DT ,ObjType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } = (exp (1im * A) - exp (- 1im * A)) / 2im
441435
442436@doc raw """
443437 cos(A::QuantumObject)
@@ -449,8 +443,8 @@ Matrix cosine of [`QuantumObject`](@ref), defined as
449443Note that this function only supports for [`Operator`](@ref) and [`SuperOperator`](@ref)
450444"""
451445LinearAlgebra. cos (
452- A:: QuantumObject{<:AbstractMatrix{T} ,ObjType} ,
453- ) where {T ,ObjType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } = (exp (1im * A) + exp (- 1im * A)) / 2
446+ A:: QuantumObject{DT ,ObjType} ,
447+ ) where {DT ,ObjType<: Union{OperatorQuantumObject,SuperOperatorQuantumObject} } = (exp (1im * A) + exp (- 1im * A)) / 2
454448
455449@doc raw """
456450 diag(A::QuantumObject, k::Int=0)
@@ -659,11 +653,11 @@ tidyup!(A::AbstractArray{T}, tol::T2 = 1e-14) where {T,T2<:Real} =
659653 @. A = T (abs (real (A)) > tol) * real (A) + 1im * T (abs (imag (A)) > tol) * imag (A)
660654
661655@doc raw """
662- get_data(A::QuantumObject )
656+ get_data(A::AbstractQuantumObject )
663657
664- Returns the data of a QuantumObject .
658+ Returns the data of a [`AbstractQuantumObject`](@ref) .
665659"""
666- get_data (A:: QuantumObject ) = A. data
660+ get_data (A:: AbstractQuantumObject ) = A. data
667661
668662@doc raw """
669663 get_coherence(ψ::QuantumObject)
0 commit comments