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

Add _apply_channel_ optimizations for reset and confusion #5917

Merged
merged 17 commits into from
Oct 13, 2022

Conversation

daxfohl
Copy link
Contributor

@daxfohl daxfohl commented Oct 12, 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.

@CirqBot CirqBot added the size: M 50< lines changed <250 label Oct 12, 2022
@daxfohl daxfohl marked this pull request as ready for review October 12, 2022 07:56
@daxfohl daxfohl requested review from a team, vtomole and cduck as code owners October 12, 2022 07:56
Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't looked at the details yet, but this is what I was hoping we would do! Thank you, Dax!

I have one concern. When we do things this way, i.e. when we have different code paths implementing the same operation to be employed under different circumstances, we should test that the code paths agree on a few test inputs. We do this for _unitary_ and _apply_unitary_ using this function. By analogy, we should have assert_has_consistent_apply_channel.

In fact, if you look at our present consistency checks for channels, there are surprisingly few. In fact, we have an issue - #4579 - about adding more (albeit the issue is about consistency between different representations such as Choi/Kraus/etc and doesn't mention _apply_channel_ explicitly).

How do you feel about adding a assert_has_consistent_apply_channel that would verify that _kraus_ and _apply_channel_ yield the same results? (I'd do it in a separate PR to be merged before this one so that we could make use of the new assert helper in here, but that's up to you of course.)

@daxfohl
Copy link
Contributor Author

daxfohl commented Oct 13, 2022

+1 I had the same concern. Thanks for the suggestion on the approach. I will try to do it as part of this PR. If it gets hairy I will open a separate PR for it.

@daxfohl
Copy link
Contributor Author

daxfohl commented Oct 13, 2022

Done. I have it calculate the superoperator from the Kraus, and separately calculating the superoperator by calling apply_channel on the identity tensor, and then comparing the results of each.

I also added an apply_channel for phase_damp.

Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

cirq-core/cirq/linalg/transformations.py Outdated Show resolved Hide resolved
cirq-core/cirq/linalg/transformations.py Outdated Show resolved Hide resolved
return 1

with pytest.raises(AssertionError):
cirq.testing.assert_has_consistent_apply_channel(NoKraus())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: I think it'd be nice to make this case work by having protocol.kraus compute the Kraus representation when _apply_channel_ is provided but _kraus_ isn't. Probably a good idea for a follow-up rather than this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added #5921

@viathor viathor added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Oct 13, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Oct 13, 2022
@CirqBot CirqBot merged commit 425bf8d into quantumlib:master Oct 13, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Oct 13, 2022
@daxfohl daxfohl deleted the apply-channel2 branch October 13, 2022 22:39
rht pushed a commit to rht/Cirq that referenced this pull request 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use _apply_channel_ when appropriate
3 participants