Skip to content

Commit f9c3a8c

Browse files
authored
Arithemtic between Operators and LazyOperators
Extend some of the current operation with LazyOperators to allow for smoother interface with e.g. LazyTensor. As an example I defined +(a::LazyTensor,LazyTensor) so that it returns LazySum. Note that tensor(a::Operator,b::LazyTensor) only behaves well if Operator does not contain a CompositeBasis. This could probably be fixed by specifying the type more explicitly and returning error if it does contain CompositeBasis. Other solutions are welcome.
1 parent b7684f7 commit f9c3a8c

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

src/LazyOperatorAritmetic.jl

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
function QuantumOpticsBase.:+(a::LazyTensor{B1,B2},b::LazyTensor{B1,B2}) where {B1,B2}
3+
LazySum(a,b)
4+
end
5+
6+
function QuantumOpticsBase.:-(a::LazyTensor{B1,B2},b::LazyTensor{B1,B2}) where {B1,B2}
7+
LazySum([1,-1],[a,b])
8+
end
9+
10+
function QuantumOpticsBase.:+(a::LazyTensor{B1,B2},b::Operator{B1,B2}) where {B1,B2}
11+
LazySum(a) + b
12+
end
13+
function QuantumOpticsBase.:+(a::Operator{B1,B2},b::LazyTensor{B1,B2}) where {B1,B2}
14+
+(b,a)
15+
end
16+
function QuantumOpticsBase.:+(a::LazyProduct{B1,B2},b::Operator{B1,B2}) where {B1,B2}
17+
LazySum(a) + b
18+
end
19+
function QuantumOpticsBase.:+(a::Operator{B1,B2},b::LazyProduct{B1,B2}) where {B1,B2}
20+
+(b,a)
21+
end
22+
function QuantumOpticsBase.:-(a::LazyProduct{B1,B2},b::LazyProduct{B1,B2}) where {B1,B2}
23+
LazySum(a) - b
24+
end
25+
function QuantumOpticsBase.:-(a::LazyProduct{B1,B2},b::LazyProduct{B1,B2}) where {B1,B2}
26+
a-LazySum(b)
27+
end
28+
function QuantumOpticsBase.:+(a::LazyProduct{B1,B2},b::LazyProduct{B1,B2}) where {B1,B2}
29+
LazySum(a) + LazySum(b)
30+
end
31+
function QuantumOpticsBase.:+(a::LazyProduct{B1,B2},b::LazyProduct{B1,B2}) where {B1,B2}
32+
+(b,a)
33+
end
34+
35+
function QuantumOpticsBase.:(a::LazyTensor,b::Operator)
36+
if isequal(b,identityoperator(basis(b)))
37+
btotal = basis(a) basis(b)
38+
LazyTensor(btotal,btotal,[a.indices...],(a.operators...,),a.factor)
39+
else
40+
a LazyTensor(b.basis_l,b.basis_r,[1],(b,),1)
41+
end
42+
end
43+
44+
function QuantumOpticsBase.:(a::Operator,b::LazyTensor)
45+
if isequal(a,identityoperator(basis(a)))
46+
btotal = basis(a) basis(b)
47+
LazyTensor(btotal,btotal,[b.indices...].+1 ,(b.operators...,),b.factor)
48+
else
49+
LazyTensor(a.basis_l,a.basis_r,[1],(a,),1) b
50+
end
51+
end
52+
53+
54+
55+
function QuantumOpticsBase.:(a::Operator,b::LazySum)
56+
btotal_l = a.basis_l b.basis_l
57+
btotal_r = a.basis_r b.basis_r
58+
ops = Vector{AbstractOperator}(undef,length(b.operators))
59+
for i in eachindex(ops)
60+
ops[i] = a b.operators[i]
61+
end
62+
LazySum(btotal_l,btotal_r,b.factors,ops)
63+
end
64+
function QuantumOpticsBase.:(a::LazySum,b::Operator)
65+
btotal_l = a.basis_l b.basis_l
66+
btotal_r = a.basis_r b.basis_r
67+
ops = Vector{AbstractOperator}(undef,length(a.operators))
68+
for i in eachindex(ops)
69+
ops[i] = a.operators[i] b
70+
end
71+
LazySum(btotal_l,btotal_r,a.factors,ops)
72+
end
73+
74+
75+
function QuantumOpticsBase.:(a::Operator,b::LazyProduct)
76+
btotal_l = a.basis_l b.basis_l
77+
btotal_r = a.basis_r b.basis_r
78+
ops = Vector{AbstractOperator}(undef,length(b.operators))
79+
for i in eachindex(ops)
80+
ops[i] = a b.operators[i]
81+
end
82+
LazyProduct(btotal_l,btotal_r,b.factor,ops)
83+
end
84+
function QuantumOpticsBase.:(a::LazyProduct,b::Operator)
85+
btotal_l = a.basis_l b.basis_l
86+
btotal_r = a.basis_r b.basis_r
87+
ops = Vector{AbstractOperator}(undef,length(a.operators))
88+
for i in eachindex(ops)
89+
ops[i] = a.operators[i] b
90+
end
91+
LazyProduct(btotal_l,btotal_r,a.factor,ops)
92+
end
93+
94+
function QuantumOpticsBase.:(a::AbstractOperator,b::LazyTensor)
95+
btotal_l = a.basis_l b.basis_l
96+
btotal_r = a.basis_r b.basis_r
97+
indices = (1,(b.indices .+ 1)...)
98+
ops = (a,b.operators...)
99+
LazyTensor(btotal_l,btotal_r,indices,ops,b.factor)
100+
end
101+
function QuantumOpticsBase.:(a::LazyTensor,b::AbstractOperator)
102+
btotal_l = a.basis_l b.basis_l
103+
btotal_r = a.basis_r b.basis_r
104+
indices = (a.indices...,length(a.indices)+1)
105+
ops = (a.operators...,b)
106+
LazyTensor(btotal_l,btotal_r,indices,ops,a.factor)
107+
end

0 commit comments

Comments
 (0)