Skip to content

Conversation

0x177
Copy link
Contributor

@0x177 0x177 commented Aug 28, 2025

Fixes #5915

Largely a direct implementation of the idea of @daxfohl in #5915.

@0x177 0x177 requested review from a team and vtomole as code owners August 28, 2025 10:01
@0x177 0x177 requested a review from senecameeks August 28, 2025 10:01
Copy link

google-cla bot commented Aug 28, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added the size: M 50< lines changed <250 label Aug 28, 2025
@0x177
Copy link
Contributor Author

0x177 commented Aug 28, 2025

i appear to have missed handling unused qubits

Copy link
Collaborator

@daxfohl daxfohl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good so far, just a couple points

Copy link

codecov bot commented Aug 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.50%. Comparing base (9672295) to head (5382b11).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7610   +/-   ##
=======================================
  Coverage   97.50%   97.50%           
=======================================
  Files        1103     1103           
  Lines       99709    99732   +23     
=======================================
+ Hits        97222    97246   +24     
+ Misses       2487     2486    -1     

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

@daxfohl
Copy link
Collaborator

daxfohl commented Aug 29, 2025

You can use check/format-incremental --apply to autoformat, and check/pylint-changed-files to see if there are linting errors locally (but there's no auto-fix flag on that one).

Copy link
Collaborator

@daxfohl daxfohl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm % a couple nits.

@0x177
Copy link
Contributor Author

0x177 commented Aug 29, 2025

there is one more issue that I am not sure how to handle properly: in the case of classical control, if the qubit_order has 2 qubits, but the circuit only uses 1, the function will fail because the circuit_like will not take the unused qubit into account.

@daxfohl
Copy link
Collaborator

daxfohl commented Aug 29, 2025

there is one more issue that I am not sure how to handle properly: in the case of classical control, if the qubit_order has 2 qubits, but the circuit only uses 1, the function will fail because the circuit_like will not take the unused qubit into account.

Hmm, so what happens? Does the simulation output tensor include a dimension for the 2nd qubit even though it's not in the circuit? I'd say that sounds like a bug in the simulate method, not a bug here. I think it's fine to leave that case unhandled. If anyone encounters that problem, then we can figure out how they expect it to be handled at that point.

@0x177
Copy link
Contributor Author

0x177 commented Aug 29, 2025

there is one more issue that I am not sure how to handle properly: in the case of classical control, if the qubit_order has 2 qubits, but the circuit only uses 1, the function will fail because the circuit_like will not take the unused qubit into account.

Hmm, so what happens? Does the simulation output tensor include a dimension for the 2nd qubit even though it's not in the circuit? I'd say that sounds like a bug in the simulate method, not a bug here. I think it's fine to leave that case unhandled. If anyone encounters that problem, then we can figure out how they expect it to be handled at that point.

yes, something like that. It eventually results in a reshapring error

Copy link
Collaborator

@daxfohl daxfohl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm cc @pavoljuhas

- access protocol functions from the `protocols` module
- eliminate single-use variables
- remove unnecessary blank lines
- use np.diag to construct the expected density matrix
- remove test of matrix shape; it is already checked in assert_allclose
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.

LGTM with a couple of tweaks in 2828ba9 and 2940ee8

@pavoljuhas pavoljuhas enabled auto-merge September 2, 2025 18:23
@pavoljuhas pavoljuhas added this pull request to the merge queue Sep 2, 2025
Merged via the queue into quantumlib:main with commit 7f3bab4 Sep 2, 2025
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: M 50< lines changed <250

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make cirq.final_density_matrix work on classical controls

3 participants