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

Use _apply_channel_ when appropriate #5901

Open
viathor opened this issue Oct 1, 2022 · 7 comments · Fixed by #5917
Open

Use _apply_channel_ when appropriate #5901

viathor opened this issue Oct 1, 2022 · 7 comments · Fixed by #5917
Labels
area/channels good for learning For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues" good part time project A meaty non-urgent issue with a substantial amount of work to be done. kind/design-issue A conversation around design triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@viathor
Copy link
Collaborator

viathor commented Oct 1, 2022

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).

@viathor viathor added kind/design-issue A conversation around design triage/discuss Needs decision / discussion, bring these up during Cirq Cynque area/channels labels Oct 1, 2022
@daxfohl
Copy link
Collaborator

daxfohl commented Oct 2, 2022

I created #5902 that improves the situation here a fair amount. Curious to see what more can be done!

@daxfohl
Copy link
Collaborator

daxfohl commented Oct 3, 2022

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.

@viathor
Copy link
Collaborator Author

viathor commented Oct 7, 2022

Thank you for checking einsum performance on sparse vs dense matrices! Your result appears to confirm my concerns here.

@daxfohl
Copy link
Collaborator

daxfohl commented Oct 8, 2022

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).

@tanujkhattar tanujkhattar added good part time project A meaty non-urgent issue with a substantial amount of work to be done. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on good for learning For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues" and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Oct 12, 2022
@tanujkhattar
Copy link
Collaborator

From cirq sync:

This should be done. This would probably involve two steps:

  1. GateOperation should forward the protocol to the underlying gates -- should be relatively easy to fix.
  2. 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.

CirqBot pushed a commit that referenced this issue Oct 13, 2022
Confusion matrix and reset channels both can be viewed as starting with a zero DM tensor and then copying (adding) in scaled slices from the original DM tensor. Thus we make a helper function that does this and add `_apply_channel_` optimizations to those gates.

Fixes #5901.

Starts #5900 though I haven't looked at all gates.

Also starts #4579 but there's likely more to do there as well. I didn't add the new test function to the primary test suite because creating superoperators is likely computationally expensive (granted most if not all gates that use this would be three or fewer qubits, which is still cheap), and the test not relevant for most gates.
@daxfohl
Copy link
Collaborator

daxfohl commented Oct 13, 2022

@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.

@viathor
Copy link
Collaborator Author

viathor commented Oct 14, 2022

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.

@viathor viathor reopened this Oct 14, 2022
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
…#5917)

Confusion matrix and reset channels both can be viewed as starting with a zero DM tensor and then copying (adding) in scaled slices from the original DM tensor. Thus we make a helper function that does this and add `_apply_channel_` optimizations to those gates.

Fixes quantumlib#5901.

Starts quantumlib#5900 though I haven't looked at all gates.

Also starts quantumlib#4579 but there's likely more to do there as well. I didn't add the new test function to the primary test suite because creating superoperators is likely computationally expensive (granted most if not all gates that use this would be three or fewer qubits, which is still cheap), and the test not relevant for most gates.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
…#5917)

Confusion matrix and reset channels both can be viewed as starting with a zero DM tensor and then copying (adding) in scaled slices from the original DM tensor. Thus we make a helper function that does this and add `_apply_channel_` optimizations to those gates.

Fixes quantumlib#5901.

Starts quantumlib#5900 though I haven't looked at all gates.

Also starts quantumlib#4579 but there's likely more to do there as well. I didn't add the new test function to the primary test suite because creating superoperators is likely computationally expensive (granted most if not all gates that use this would be three or fewer qubits, which is still cheap), and the test not relevant for most gates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/channels good for learning For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues" good part time project A meaty non-urgent issue with a substantial amount of work to be done. kind/design-issue A conversation around design triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants