-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add sparse_matrix() to PauliString and PauliSum #8127
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1078,6 +1078,40 @@ def test_pauli_sum_matrix() -> None: | |
| assert np.allclose(H3, paulisum.matrix([q[1], q[2], q[0]])) | ||
|
|
||
|
|
||
| def test_pauli_sum_sparse_matrix() -> None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend parameterizing this test case and passing in a variety of pauli sums.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dstrain115 when you get a chance, would you review & approve the PR if it's ready? |
||
| q = cirq.LineQubit.range(3) | ||
| paulisum = cirq.X(q[0]) * cirq.X(q[1]) + cirq.Z(q[0]) | ||
| H1 = np.array( | ||
|
mhucka marked this conversation as resolved.
Outdated
|
||
| [[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, -1.0, 0.0], [1.0, 0.0, 0.0, -1.0]] | ||
| ) | ||
| # Default qubit ordering. | ||
| assert np.allclose(H1, paulisum.sparse_matrix().toarray()) | ||
| # Explicit 2-qubit ordering (same as default). | ||
| assert np.allclose(H1, paulisum.sparse_matrix([q[0], q[1]]).toarray()) | ||
| # Reversed qubit ordering changes the matrix. | ||
| H2 = np.array( | ||
| [[1.0, 0.0, 0.0, 1.0], [0.0, -1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0], [1.0, 0.0, 0.0, -1.0]] | ||
| ) | ||
| assert np.allclose(H2, paulisum.sparse_matrix([q[1], q[0]]).toarray()) | ||
| # Adding an extra idle qubit expands to 8x8. | ||
| H3 = np.array( | ||
| [ | ||
| [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0], | ||
| [0.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0], | ||
| [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0], | ||
| [0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0], | ||
| [0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0], | ||
| [1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0], | ||
| [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0], | ||
| [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0], | ||
| ] | ||
| ) | ||
| assert np.allclose(H3, paulisum.sparse_matrix([q[1], q[2], q[0]]).toarray()) | ||
| # Empty PauliSum should return a zero sparse matrix. | ||
| empty = cirq.PauliSum.from_pauli_strings([]) | ||
| assert np.allclose(empty.sparse_matrix([q[0], q[1]]).toarray(), np.zeros((4, 4))) | ||
|
|
||
|
|
||
| def test_pauli_sum_repr() -> None: | ||
| q = cirq.LineQubit.range(2) | ||
| pstr1 = cirq.X(q[0]) * cirq.X(q[1]) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.