Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: regression in identityoperator()+sparse_matrix due to the move to Eye #109 #110

Merged
merged 5 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
15 changes: 15 additions & 0 deletions test/test_operators_sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@ IEye = identityoperator(b_l)
@test sparse(IEye) == I
Icomp = identityoperator(b1a) ⊗ identityoperator(b2a) ⊗ identityoperator(b3a)
@test IEye == Icomp
for IEye in (IEye, -IEye, IEye, dagger(IEye))
Krastanov marked this conversation as resolved.
Show resolved Hide resolved
@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

# Test tr and normalize
op = sparse(DenseOperator(GenericBasis(3), [1 3 2;5 2 2;-1 2 5]))
Expand Down