Skip to content
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

Handle qudits in drop_terminal_measurements #6879

Merged
merged 14 commits into from
Jan 15, 2025

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Dec 24, 2024

Explicitly set the dimension of the X's and I's corresponding to the invert_mask in drop_terminal_measurements to avoid the ValueError that was raised when the invert_mask was applied to qudits.

Fixes #5994

Note the weird MatrixGate swaps the 0 and 1 dimensions on qudits, leaving other dimensions unchanged. This behavior is consistent with the invert_mask implementation at https://github.com/quantumlib/cirq/blob/3fefe2984a1203c0bf647c1ea84f4882b05f8477/cirq-core/cirq/sim/simulator.py#L823, which has been in place since 2020. If there's a DimensionSwapGate or something, we could use that here instead, but I didn't see one.

This allows drop_terminal_measurements to work for qudits, which failed on the `I` application, given invert_mask values are undefined for them anyway.
@daxfohl daxfohl requested review from vtomole and a team as code owners December 24, 2024 13:31
@daxfohl daxfohl requested a review from fdmalone December 24, 2024 13:31
@CirqBot CirqBot added the Size: XS <10 lines changed label Dec 24, 2024
@daxfohl daxfohl marked this pull request as draft December 29, 2024 21:48
… qubit was being removed completely if a terminal measurement was the only thing it contained.
@CirqBot CirqBot added the size: S 10< lines changed <50 label Dec 29, 2024
@daxfohl daxfohl changed the title Only apply X's (not I's) for invert_mask in drop_terminal_measurements Handle qudits in drop_terminal_measurements Dec 29, 2024
@daxfohl daxfohl marked this pull request as ready for review December 29, 2024 23:13
Copy link

codecov bot commented Jan 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.86%. Comparing base (0361a81) to head (91bb645).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6879      +/-   ##
==========================================
- Coverage   97.87%   97.86%   -0.01%     
==========================================
  Files        1084     1084              
  Lines       94420    94447      +27     
==========================================
+ Hits        92409    92435      +26     
- Misses       2011     2012       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@daxfohl
Copy link
Collaborator Author

daxfohl commented Jan 2, 2025

glitchy test again? It failed on cirq-core/cirq/experiments/z_phase_calibration_test.py::test_calibrate_z_phases_no_options on Ubuntu, but works fine on my Ubuntu instance. @mhucka

@mhucka
Copy link
Contributor

mhucka commented Jan 2, 2025

Yeah, I get that one too. I opened issue #6906 and will try to get to the bottom of it.

Copy link
Collaborator

@dstrain115 dstrain115 left a comment

Choose a reason for hiding this comment

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

It's a little ridiculous we don't have a proper qudit X gate, but I suppose that's not the fault of this PR. LGTM.

@daxfohl
Copy link
Collaborator Author

daxfohl commented Jan 14, 2025

@dstrain115 X and Z already generalize to qudits: #4919. X on qudits is equivalent to q = (q + 1) % dim. The existing (since 2019) implementation of invert_mask on a measurement doesn't behave like that on qudits though; instead it swaps the 0 and 1 states, leaving other states untouched. So that's what the behavior of the matrix gate here replicates.

I just remembered I had a PR that adapts gates to a subspace of a qudit: #4783. That would be a generalization of what we'd need here. To swap 0,1, cirq.DimensionAdapterGate(cirq.X, [(q.dim, (0, 1))]) would do the trick. I don't think this one use case calls for reviving that PR though.

@dstrain115
Copy link
Collaborator

@daxfohl Ah, ok. The definition I have seen of a qudit X gate is like X_0,1 which would swap the |0> and |1> subspaces. I am not sure there's a great standard for this since qudit operations are a less trod ground. Thanks for the flurry of changes lately!

@dstrain115 dstrain115 enabled auto-merge (squash) January 15, 2025 15:22
@daxfohl
Copy link
Collaborator Author

daxfohl commented Jan 15, 2025

@dstrain115 Yeah, there was some discussion on the original issue #3190, that both approaches were reasonable, but the group seemed to converge on the +1 mod dim approach. In particular this comment from @cduck: #3190 (comment). The way qudit X is implemented is equivalent to the old PlusGate that we had in tests everywhere, except it's an EigenGate and thus defined on non-integer exponents (and has interesting properties there, as shown in some of the tests).

The DimensionAdapterGate from the comment above could be used for the on(qudit.subspace[...]) approach (see the test_diagram test in that PR). @cduck also has a simpler but more targeted adapter SingleQuditSubspaceGate that's specific to the [0,1] subspace, from which a FlipGate that swaps the [0,1] subspace is defined in just a few lines: https://github.com/cduck/cirqtools/blob/master/cirqtools/qudit/common_gates.py#L120.

(I'm not advocating for pulling in any of those gates; just leaving this as an FYI in case it comes up in the future).

@dstrain115 dstrain115 merged commit 84adca9 into quantumlib:main Jan 15, 2025
37 checks passed
@daxfohl daxfohl deleted the fix-invert-mask-in-transformer branch January 16, 2025 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50 Size: XS <10 lines changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

drop_terminal_measurements fails for measurements on dimension>2 Qids
4 participants