Skip to content

Commit f84426b

Browse files
AmitRotemAmit Rotem
andauthored
Mul adjoint and lazy sum (#85)
* allocate concrete type operator in * * tests * fix test * Robust version of _parent * Add test with sparse sparse mul --------- Co-authored-by: Amit Rotem <[email protected]>
1 parent b7684f7 commit f84426b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/operators_dense.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ Base.isapprox(x::DataOperator, y::DataOperator; kwargs...) = false
8989
*(a::Operator, b::Number) = Operator(a.basis_l, a.basis_r, b*a.data)
9090
*(a::Number, b::Operator) = Operator(b.basis_l, b.basis_r, a*b.data)
9191
function *(op1::AbstractOperator{B1,B2}, op2::Operator{B2,B3,T}) where {B1,B2,B3,T}
92-
result = Operator{B1,B3,T}(op1.basis_l, op2.basis_r, similar(op2.data,length(op1.basis_l),length(op2.basis_r)))
92+
result = Operator{B1,B3}(op1.basis_l, op2.basis_r, similar(_parent(op2.data),promote_type(eltype(op1),eltype(op2)),length(op1.basis_l),length(op2.basis_r)))
9393
mul!(result,op1,op2)
9494
return result
9595
end
9696
function *(op1::Operator{B1,B2,T}, op2::AbstractOperator{B2,B3}) where {B1,B2,B3,T}
97-
result = Operator{B1,B3,T}(op1.basis_l, op2.basis_r, similar(op1.data,length(op1.basis_l),length(op2.basis_r)))
97+
result = Operator{B1,B3}(op1.basis_l, op2.basis_r, similar(_parent(op1.data),promote_type(eltype(op1),eltype(op2)),length(op1.basis_l),length(op2.basis_r)))
9898
mul!(result,op1,op2)
9999
return result
100100
end
@@ -109,6 +109,10 @@ function *(psi::Bra{BL,T}, op::AbstractOperator{BL,BR}) where {BL,BR,T}
109109
return result
110110
end
111111

112+
_parent(x::T, x_parent::T) where T = x
113+
_parent(x, x_parent) = _parent(x_parent, parent(x_parent))
114+
_parent(x) = _parent(x, parent(x))
115+
112116
/(a::Operator, b::Number) = Operator(a.basis_l, a.basis_r, a.data ./ b)
113117

114118
dagger(x::Operator) = Operator(x.basis_r,x.basis_l,adjoint(x.data))

test/test_operators_lazysum.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ xbra1 = Bra(b_l, rand(ComplexF64, length(b_l)))
102102
@test 1e-11 > D((op1+op2)*(x1+0.3*x2), (op1_+op2_)*(x1+0.3*x2))
103103
@test 1e-12 > D(dagger(x1)*dagger(0.3*op2), dagger(x1)*dagger(0.3*op2_))
104104

105+
## multiplication with Operator of AbstractMatrix
106+
LSop = LazySum(randoperator(b1a^2)) # AbstractOperator
107+
LSop_s = LazySum(sparse(randoperator(b1a^2)))
108+
hermitian_op = Operator(basis(LSop), Hermitian(randn(ComplexF64,length(basis(LSop)),length(basis(LSop))))) # Hermitian
109+
symmetric_op = Operator(basis(LSop), Symmetric(randn(ComplexF64,length(basis(LSop)),length(basis(LSop))))) # Symmetric
110+
adjoint_op = randoperator(basis(LSop))' # Adjoint
111+
real_op = Operator(basis(LSop), real(adjoint_op.data)) # real
112+
ops_tuple = (symmetric_op,hermitian_op,adjoint_op)
113+
### Test
114+
@test ops_tuple.*(LSop,) == dense.(ops_tuple).*(LSop,)
115+
@test (LSop,).*ops_tuple == (LSop,).*dense.(ops_tuple)
116+
### test with sparse
117+
@test all(isapprox.(sparse.(ops_tuple).*(LSop,) , dense.(ops_tuple).*(LSop,), atol=1e-13))
118+
@test all(isapprox.((LSop,).*sparse.(ops_tuple) , (LSop,).*dense.(ops_tuple), atol=1e-13))
119+
@test all(isapprox.(dense.(sparse.(ops_tuple).*(LSop_s,)) , dense.(ops_tuple).*(LSop_s,), atol=1e-13))
120+
@test all(isapprox.(dense.((LSop_s,).*sparse.(ops_tuple)) , (LSop_s,).*dense.(ops_tuple), atol=1e-13))
121+
### test real valued op with AbstractOperator
122+
@test isapprox(LSop*real_op*LSop , LSop*Operator(basis(real_op), complex.(real_op.data))*LSop, atol=1e-13)
123+
105124
# Test division
106125
@test 1e-14 > D(op1/7, op1_/7)
107126

0 commit comments

Comments
 (0)