The identityoperator function returns a sparse array. As the action of the identity matrix on states and operators is known, however, we could make this lazy. This may not appear to be a large change, but it could potentially have a large impact on the implementation of LazyTensor and its mul! methods.
I'm not set on actually doing is, it's just an idea that I thought worth discussing.
We could, for example, use FillArrays to implement this. They export an Eye function which lazily represents a matrix that is zero except on the diagonal with corresponding getindex methods and such. We could just use that as .data field when constructing identityoperator.
using FillArrays
id = Eye{ComplexF64}(4,3)
id isa AbstractMatrix # true
Alternatively, if we don't want to add this dependency, we could also implement it ourselves (it's not super complicated), which would also give us more control over e.g. the mul! implementations and such. I'd prefer not duplicating work, however.
The big advantage I see in implementing this is that it might greatly simplify the implementation of LazyTensor. If identity operators are efficient and lazy, then we could remove the entire concept of indices from it. This will also simplify the underlying mul! methods.
The disadvantage I is that this may have a negative impact on LazyTensor performance. Without further dispatches on the Eye matrix above, for example, we'd be doing a ton of unnecessary multiplication operations. Ultimately the question whether this impacts performance negatively can only be answered if we do the implementation first, so I'm not sure if we want to invest in this.
One option I see for now is to just change the implementation of identityoperator to return a lazy Eye (or add a new method such as lazy_identityoperator) and leave the LazyTensor implementation untouched.
The
identityoperatorfunction returns a sparse array. As the action of the identity matrix on states and operators is known, however, we could make this lazy. This may not appear to be a large change, but it could potentially have a large impact on the implementation ofLazyTensorand itsmul!methods.I'm not set on actually doing is, it's just an idea that I thought worth discussing.
We could, for example, use FillArrays to implement this. They export an
Eyefunction which lazily represents a matrix that is zero except on the diagonal with correspondinggetindexmethods and such. We could just use that as.datafield when constructingidentityoperator.Alternatively, if we don't want to add this dependency, we could also implement it ourselves (it's not super complicated), which would also give us more control over e.g. the
mul!implementations and such. I'd prefer not duplicating work, however.The big advantage I see in implementing this is that it might greatly simplify the implementation of
LazyTensor. If identity operators are efficient and lazy, then we could remove the entire concept ofindicesfrom it. This will also simplify the underlyingmul!methods.The disadvantage I is that this may have a negative impact on
LazyTensorperformance. Without further dispatches on theEyematrix above, for example, we'd be doing a ton of unnecessary multiplication operations. Ultimately the question whether this impacts performance negatively can only be answered if we do the implementation first, so I'm not sure if we want to invest in this.One option I see for now is to just change the implementation of
identityoperatorto return a lazyEye(or add a new method such aslazy_identityoperator) and leave theLazyTensorimplementation untouched.