-
Notifications
You must be signed in to change notification settings - Fork 2
Add expectation and trace to tableau python bindings #176
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
+105
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import math | ||
|
|
||
| import pytest | ||
|
|
||
| from ppvm import GeneralizedTableau | ||
|
|
||
|
|
||
| def _bell() -> GeneralizedTableau: | ||
| tab = GeneralizedTableau(2) | ||
| tab.h(0) | ||
| tab.cnot(0, 1) | ||
| return tab | ||
|
|
||
|
|
||
| def test_expectation_z_on_zero_state(): | ||
| tab = GeneralizedTableau(1) | ||
| assert tab.expectation("Z") == pytest.approx(1.0, abs=1e-10) | ||
| assert tab.expectation("X") == pytest.approx(0.0, abs=1e-10) | ||
| assert tab.expectation("I") == pytest.approx(1.0, abs=1e-10) | ||
|
|
||
|
|
||
| def test_expectation_x_on_plus_state(): | ||
| tab = GeneralizedTableau(1) | ||
| tab.h(0) | ||
| assert tab.expectation("X") == pytest.approx(1.0, abs=1e-10) | ||
| assert tab.expectation("Z") == pytest.approx(0.0, abs=1e-10) | ||
|
|
||
|
|
||
| def test_bell_state_pauli_expectations(): | ||
| tab = _bell() | ||
| assert tab.expectation("II") == pytest.approx(1.0, abs=1e-10) | ||
| assert tab.expectation("ZZ") == pytest.approx(1.0, abs=1e-10) | ||
| assert tab.expectation("XX") == pytest.approx(1.0, abs=1e-10) | ||
| assert tab.expectation("YY") == pytest.approx(-1.0, abs=1e-10) | ||
| assert tab.expectation("IZ") == pytest.approx(0.0, abs=1e-10) | ||
| assert tab.expectation("ZI") == pytest.approx(0.0, abs=1e-10) | ||
|
|
||
|
|
||
| def test_ry_rotation_z_expectation_is_cos_theta(): | ||
| tab = GeneralizedTableau(1) | ||
| theta = math.pi / 3 | ||
| tab.ry(0, theta) | ||
| assert tab.expectation("Z") == pytest.approx(math.cos(theta), abs=1e-10) | ||
| assert tab.expectation("X") == pytest.approx(math.sin(theta), abs=1e-10) | ||
|
|
||
|
|
||
| def test_trace_z_or_identity_pattern_on_bell(): | ||
| tab = _bell() | ||
| # ⟨II⟩ + ⟨IZ⟩ + ⟨ZI⟩ + ⟨ZZ⟩ = 1 + 0 + 0 + 1 = 2 | ||
| assert tab.trace("Z?{2}") == pytest.approx(2.0, abs=1e-10) | ||
|
|
||
|
|
||
| def test_trace_y_or_identity_pattern_on_bell_is_zero(): | ||
| tab = _bell() | ||
| # ⟨II⟩ + ⟨IY⟩ + ⟨YI⟩ + ⟨YY⟩ = 1 + 0 + 0 + (-1) = 0 | ||
| assert tab.trace("Y?{2}") == pytest.approx(0.0, abs=1e-10) | ||
|
|
||
|
|
||
| def test_trace_positional_pattern_matches_single_pauli(): | ||
| tab = _bell() | ||
| assert tab.trace("Z0Z1") == pytest.approx(tab.expectation("ZZ"), abs=1e-10) | ||
|
david-pl marked this conversation as resolved.
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.