Skip to content

Commit

Permalink
Fix: regression in identityoperator()+sparse_matrix due to the move t…
Browse files Browse the repository at this point in the history
…o Eye #109 (#110)

* Overloaded common operations between Eye and sparse operators

* tensor, unary minus, and bump version number

* forgot the cases where they are both IEye

* non-square tests

---------

Co-authored-by: Stefan Krastanov <[email protected]>
  • Loading branch information
SatyaBade12 and Krastanov authored Jun 22, 2023
1 parent 94ac58b commit 0d3dfb4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
15 changes: 15 additions & 0 deletions src/operators_sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
18 changes: 18 additions & 0 deletions test/test_operators_sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0d3dfb4

Please sign in to comment.