diff --git a/Project.toml b/Project.toml index 9b821571..1d9c1c45 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "QuantumOpticsBase" uuid = "4f57444f-1401-5e15-980d-4471b28d5678" -version = "0.4.5" +version = "0.4.6" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/operators_sparse.jl b/src/operators_sparse.jl index 7c12a240..173a57d3 100644 --- a/src/operators_sparse.jl +++ b/src/operators_sparse.jl @@ -85,3 +85,18 @@ mul!(result::DenseOpType{B1,B3},M::SparseOpType{B1,B2},b::DenseOpType{B2,B3},alp mul!(result::DenseOpType{B1,B3},a::DenseOpType{B1,B2},M::SparseOpType{B2,B3},alpha,beta) where {B1,B2,B3} = (gemm!(alpha,a.data,M.data,beta,result.data); result) mul!(result::Ket{B1},M::SparseOpPureType{B1,B2},b::Ket{B2},alpha,beta) where {B1,B2} = (gemv!(alpha,M.data,b.data,beta,result.data); result) mul!(result::Bra{B2},b::Bra{B1},M::SparseOpPureType{B1,B2},alpha,beta) where {B1,B2} = (gemv!(alpha,b.data,M.data,beta,result.data); result) + +# Ensure that Eye is not densified # TODO - some of this can still be special cased on Eye or lazy embed ++(op1::EyeOpType{BL,BR},op2::SparseOpType{BL,BR}) where {BL,BR} = sparse(op1) + op2 +-(op1::EyeOpType{BL,BR},op2::SparseOpType{BL,BR}) where {BL,BR} = sparse(op1) - op2 ++(op1::SparseOpType{BL,BR},op2::EyeOpType{BL,BR}) where {BL,BR} = sparse(op2) + op1 +-(op2::SparseOpType{BL,BR},op1::EyeOpType{BL,BR}) where {BL,BR} = op2 - sparse(op1) ++(op1::EyeOpType{BL,BR},op2::EyeOpType{BL,BR}) where {BL,BR} = sparse(op1) + sparse(op1) +-(op2::EyeOpType{BL,BR},op1::EyeOpType{BL,BR}) where {BL,BR} = sparse(op2) - sparse(op1) +-(op::EyeOpType) = -sparse(op) +*(op::EyeOpType,a::T) where {T<:Number} = a*sparse(op) +*(a::T,op::EyeOpType) where {T<:Number} = a*sparse(op) +/(op::EyeOpType,a::T) where {T<:Number} = sparse(op)/a +tensor(a::EyeOpType, b::SparseOpType) = tensor(sparse(a),b) +tensor(a::SparseOpType, b::EyeOpType) = tensor(a,sparse(b)) +tensor(a::EyeOpType, b::EyeOpType) = tensor(sparse(a),sparse(b)) \ No newline at end of file diff --git a/test/test_operators_sparse.jl b/test/test_operators_sparse.jl index 5e1c50cc..a14caf92 100644 --- a/test/test_operators_sparse.jl +++ b/test/test_operators_sparse.jl @@ -118,6 +118,24 @@ IEye = identityoperator(b_l) Icomp = identityoperator(b1a) ⊗ identityoperator(b2a) ⊗ identityoperator(b3a) @test IEye == Icomp +for _IEye in (identityoperator(b_l), identityoperator(b1a, b1b)) + for IEye in (_IEye, -_IEye, _IEye', dagger(_IEye)) + @test isa(2*IEye, SparseOpType) + @test isa(IEye*2, SparseOpType) + @test isa(IEye/2, SparseOpType) + @test isa(IEye+sparse(IEye), SparseOpType) + @test isa(sparse(IEye)+IEye, SparseOpType) + @test isa(sparse(IEye)-IEye, SparseOpType) + @test isa(IEye-sparse(IEye), SparseOpType) + @test isa(IEye+IEye, SparseOpType) + @test isa(IEye-IEye, SparseOpType) + @test isa(-IEye, SparseOpType) + @test isa(tensor(IEye, sparse(IEye)), SparseOpType) + @test isa(tensor(sparse(IEye), IEye), SparseOpType) + @test isa(tensor(IEye, IEye), SparseOpType) + end +end + # Test tr and normalize op = sparse(DenseOperator(GenericBasis(3), [1 3 2;5 2 2;-1 2 5])) @test 8 == tr(op)