-
Notifications
You must be signed in to change notification settings - Fork 1.2k
MPS Synthesis #7752
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?
MPS Synthesis #7752
Conversation
- Fixed `_parser.py` formatting issues.
- Added testers for `cp`, `c3x`, `c4x`, `csx`, and `c3sqrtx` gates. - Fixed u0 gate. - Removed `r`, `ryy`, `cu2`, and `iswap` as they are not part of qelib 1.
- Fixes gate definition for `id` to address git-blame note. - Fixes gate definition for `p` to use ZPowGate. - Adds rccx and rc3x using `cirq.MatrixGate`. - Adds missing testers for cry and crz.
- Fixed failed CIs.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7752 +/- ##
========================================
Coverage 99.57% 99.57%
========================================
Files 1102 1105 +3
Lines 98638 98778 +140
========================================
+ Hits 98218 98358 +140
Misses 420 420 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I added single qubit state prep manually, it should be removed when cirq gets state preparation added. I'll be sure to remind in the next cirq cynq. |
|
I need to check a few things with colleagues; I will have an update early next week. |
pavoljuhas
left a comment
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 see inline comments. Otherwise LGTM, thank you for contributing this.
PS: I'd like to loop in @tanujkhattar for more comments after we converge on the initial review.
| derived from the input matrix. If a column is (approximately) zero, it | ||
| is replaced with a random vector. | ||
| """ | ||
| num_rows, num_columns = matrix.shape |
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.
The only caller of this function passes a square matrix.
Can we require the matrix to be square here?
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.
Okay if I add this for safety? I'll add it for now, and later before merge I can remove it if you don't want it.
if matrix.shape[0] != matrix.shape[1]:
raise ValueError(
"Input matrix is expected to be square. "
f"Got shape {matrix.shape} instead. "
"If this is not an error, please file an issue at "
"https://github.com/quantumlib/Cirq/issues"
)| unitary = np.zeros((num_rows, num_columns), dtype=np.complex128) | ||
| orthonormal_basis: list[NDArray[np.complex128]] = [] |
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 rewrite using scipy.linalg.org and scipy.linalg.null_space
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.
I'm trying something like that but noticed a slight drop in fidelity.
# Current
Fidelity for N=5: 0.9763405705141897
Fidelity for N=8: 0.9376726644863744
Fidelity for N=10: 0.8896167855158122
Fidelity for N=11: 0.8809997802239913
# Requested
Fidelity for N=5: 0.9625124988989704
Fidelity for N=8: 0.8941036354519807
Fidelity for N=10: 0.8705322861238998
Fidelity for N=11: 0.8701612775480855
I followed the Gram Schmidt decomposition based on scipy's doc to get started for this. If I use scipy.linalg.qr directly (couldn't get SVD decomposition approaches like orth and null_space to work), it gets slightly worse than this, so I feel it'd be better to keep as is to get those slight extra fidelity. I can try other implementations, perhaps I'm missing something obvious.
If others have a recommended approach, would appreciate guidance on that as well.
| state = np.random.rand(2**N) + 1j * np.random.rand(2**N) | ||
| state /= np.linalg.norm(state) |
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 reuse cirq.testing.random_superposition instead
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.
I checked, and random_superposition can have volume-law states which are hard to approximate with vanilla MPS (I'll add the sweeping I mentioned before soon, not in this PR though). Is it alright that for this test I keep it as is since the one I have there produces area-law states?
…s internal module only. - Added assertion to `_gram_schmidt` to raise an error should `matrix` not be square to catch edge cases not known at the time of changing this code. - Changed class name to `MPSSequential`. - Added class docstring. - Removed `convention` from class attributes. - Added indentation to docstring sections where needed. - Removed `logger` uses. - Added `mps_circuit_from_statevector` to `__init__.py` for ease of use. - Used `cirq.testing.random_superposition` in testers where possible, except `test_compile_area_law_states` which requires strictly area-law entangled states as part of the test. - Rewrote `test_compile_trivial_state_with_mps_pass` to avoid using qasm and avoid inter-module dependency. - Used `np.testing.assert_allclose` for about exact matching assertions.
|
Pushed commit that addressed as many comments as I could. 4 comments remain which need your kind insight. Main comment remaining is converging on optimal/preferred implementation for |
Closes #7650 .