-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add testing helper for consistent channel/mixture #5247
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a091c65
Add testing helper for consistent channel
dabacon 64b2c69
remove warning
dabacon 3a60ac2
Merge branch 'master' into cptp
dabacon 9434dd2
missed merge
dabacon 261da79
add consistent mixture
dabacon d3ecd2e
Merge branch 'master' into cptp
CirqBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2022 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Any | ||
|
||
import cirq | ||
|
||
|
||
def assert_consistent_channel(gate: Any, rtol: float = 1e-5, atol: float = 1e-8): | ||
assert cirq.has_kraus(gate), f"Given gate {gate!r} does not return True cirq.has_kraus." | ||
kraus_ops = cirq.kraus(gate) | ||
assert cirq.is_cptp(kraus_ops=kraus_ops, rtol=rtol, atol=atol), ( | ||
f"Kraus operators for {gate!r} did not sum to identity up to expected tolerances. " | ||
f"Summed to {sum(m.T.conj() @ m for m in kraus_ops)}" | ||
) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2022 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
import numpy as np | ||
import cirq | ||
|
||
|
||
def test_assert_consistent_channel_valid(): | ||
channel = cirq.KrausChannel(kraus_ops=(np.array([[0, 1], [0, 0]]), np.array([[1, 0], [0, 0]]))) | ||
cirq.testing.assert_consistent_channel(channel) | ||
|
||
|
||
def test_assert_consistent_channel_tolerances(): | ||
# This channel is off by 1e-5 from the identity matrix in the consistency condition. | ||
channel = cirq.KrausChannel( | ||
kraus_ops=(np.array([[0, np.sqrt(1 - 1e-5)], [0, 0]]), np.array([[1, 0], [0, 0]])) | ||
) | ||
# We are comparing to identity, so rtol is same as atol for non-zero entries. | ||
cirq.testing.assert_consistent_channel(channel, rtol=1e-5, atol=0) | ||
with pytest.raises(AssertionError): | ||
cirq.testing.assert_consistent_channel(channel, rtol=1e-6, atol=0) | ||
cirq.testing.assert_consistent_channel(channel, rtol=0, atol=1e-5) | ||
with pytest.raises(AssertionError): | ||
cirq.testing.assert_consistent_channel(channel, rtol=0, atol=1e-6) | ||
|
||
|
||
def test_assert_consistent_channel_invalid(): | ||
channel = cirq.KrausChannel(kraus_ops=(np.array([[1, 1], [0, 0]]), np.array([[1, 0], [0, 0]]))) | ||
with pytest.raises(AssertionError, match=r"cirq.KrausChannel.*2 1"): | ||
cirq.testing.assert_consistent_channel(channel) | ||
|
||
|
||
def test_assert_consistent_channel_not_kraus(): | ||
with pytest.raises(AssertionError, match="12.*has_kraus"): | ||
cirq.testing.assert_consistent_channel(12) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add tests for has_mixture and mixture.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is now done