Skip to content

Conversation

@ACE07-Sev
Copy link
Contributor

Closes #7650 .

@ACE07-Sev ACE07-Sev requested review from a team and vtomole as code owners November 13, 2025 11:02
@ACE07-Sev ACE07-Sev requested a review from viathor November 13, 2025 11:02
@github-actions github-actions bot added the size: L 250< lines changed <1000 label Nov 13, 2025
@codecov
Copy link

codecov bot commented Nov 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.57%. Comparing base (6871bdb) to head (146ac4c).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ACE07-Sev
Copy link
Contributor Author

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.

@pavoljuhas
Copy link
Collaborator

I need to check a few things with colleagues; I will have an update early next week.

Copy link
Collaborator

@pavoljuhas pavoljuhas left a 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
Copy link
Collaborator

@pavoljuhas pavoljuhas Dec 10, 2025

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?

Copy link
Contributor Author

@ACE07-Sev ACE07-Sev Dec 13, 2025

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"
        )

Comment on lines 50 to 51
unitary = np.zeros((num_rows, num_columns), dtype=np.complex128)
orthonormal_basis: list[NDArray[np.complex128]] = []
Copy link
Collaborator

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

Copy link
Contributor Author

@ACE07-Sev ACE07-Sev Dec 15, 2025

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.

Comment on lines +39 to +40
state = np.random.rand(2**N) + 1j * np.random.rand(2**N)
state /= np.linalg.norm(state)
Copy link
Collaborator

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

Copy link
Contributor Author

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.
@ACE07-Sev
Copy link
Contributor Author

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 _gram_schmidt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: L 250< lines changed <1000

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide features for MPS state preparation

3 participants