Skip to content

Commit 02658fd

Browse files
committed
extended methods for tensor and kron
1 parent 6fb83bc commit 02658fd

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/qobj/functions.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,15 @@ Quantum Object: type=Operator dims=[20, 20] size=(400, 400) ishermitian=
187187
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠦
188188
```
189189
"""
190-
function LinearAlgebra.kron(
190+
LinearAlgebra.kron(
191191
A::QuantumObject{<:AbstractArray{T1},OpType},
192192
B::QuantumObject{<:AbstractArray{T2},OpType},
193-
) where {T1,T2,OpType<:Union{KetQuantumObject,BraQuantumObject,OperatorQuantumObject}}
194-
return QuantumObject(kron(A.data, B.data), A.type, vcat(A.dims, B.dims))
193+
) where {T1,T2,OpType<:Union{KetQuantumObject,BraQuantumObject,OperatorQuantumObject}} =
194+
QuantumObject(kron(A.data, B.data), A.type, vcat(A.dims, B.dims))
195+
LinearAlgebra.kron(A::QuantumObject) = A
196+
function LinearAlgebra.kron(A::Vector{<:QuantumObject})
197+
@warn "`tensor(A)` or `kron(A)` with `A` is a `Vector` can hurt performance. Try to use `tensor(A...)` or `kron(A...)` instead."
198+
return kron(A...)
195199
end
196200

197201
@doc raw"""

src/qobj/synonyms.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Quantum Object: type=Operator dims=[2, 2, 2] size=(8, 8) ishermitian=tru
184184
1.0+0.0im ⋅ ⋅ ⋅ ⋅ ⋅
185185
```
186186
"""
187-
tensor(A::QuantumObject...) = kron(A...)
187+
tensor(A...) = kron(A...)
188188

189189
@doc raw"""
190190
⊗(A::QuantumObject, B::QuantumObject)

test/quantum_objects.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,24 @@
309309
@inferred a .^ 2
310310
@inferred a * a
311311
@inferred a * a'
312+
@inferred kron(a)
312313
@inferred kron(a, σx)
313314
@inferred kron(a, eye(2))
314315
end
315316
end
316317

318+
@testset "tensor" begin
319+
σx = sigmax()
320+
X3 = kron(σx, σx, σx)
321+
@test tensor(σx) == kron(σx)
322+
@test tensor(fill(σx, 3)...) == X3
323+
X_warn = @test_logs (
324+
:warn,
325+
"`tensor(A)` or `kron(A)` with `A` is a `Vector` can hurt performance. Try to use `tensor(A...)` or `kron(A...)` instead.",
326+
) tensor(fill(σx, 3))
327+
@test X_warn == X3
328+
end
329+
317330
@testset "projection" begin
318331
N = 10
319332
ψ = fock(N, 3)

0 commit comments

Comments
 (0)