Use `_apply_channel_` when appropriate
Most (all?) our channels are specified via _kraus_ method that returns a sequence of Kraus operators. This is inefficient for channels such as Reset whose Kraus operators are sparse and whose action is easy to describe in terms of changes they make to the elements of the input density matrix. IOW, such channels should be implemented using _apply_channel_, not _kraus_.
Filing for discussion, identification of the offending channels and subsequent implementation changes.
Context: Review of #5851 (which adds yet another offending channel).
I created #5902 that improves the situation here a fair amount. Curious to see what more can be done!
Also I'd have to imagine that einsum has the optimizations from #5902. So targeted_left_multiply may already be optimal for sparse operator matrices.
Edit: I take it back, or at least I think I do: einsum seems to take the same amount of time no matter what's in the operator matrix.
Thank you for checking einsum performance on sparse vs dense matrices! Your result appears to confirm my concerns here.
For posterity and since the code is rather tricky, #5905 is also a large boost when the matrix is one-hot, which is the case for many Kraus components such as Reset, ConfusionChannel, PhaseDamp(1). However the flatnonzero check was too slow to include in the general case of that function. It should probably be added as an independent function for these custom _apply_channel_ to use as appropriate. Same with the code in #5902 (except that code is not tricky).
From cirq sync:
This should be done. This would probably involve two steps:
GateOperationshould forward the protocol to the underlying gates -- should be relatively easy to fix.- Go through all the operations and identify the ones where
_apply_channel_would be more efficient but is not already implemented. Go and implement_apply_channel_for these operations -- this would be more involved and a good learning opportunity.
@viathor I accidentally linked this to close instead of #5900. The most important channels have been addressed but IDK if you wanted to keep this open to investigate the others.
Yeah, let's keep this open until we've convinced ourselves that there are no more channels that should have _apply_channel_, but don't.