Skip to content

Conversation

ToastCheng
Copy link
Contributor

@ToastCheng ToastCheng commented Aug 25, 2025

  • Updated PauliString.mul and rmul to handle GateOperations
    that can be interpreted as Pauli strings (X2, Y2, Z**2, etc.)
  • Enhanced Gate._mul_with_qubits and _rmul_with_qubits to handle
    operations with single-item pauli expansions using PauliString

This fixes #7588.

Enhanced PauliString multiplication methods to handle GateOperations
that can be interpreted as Pauli strings (e.g. X**2) by using the
existing _try_interpret_as_pauli_string function.

Addressed part of quantumlib#7588, for the PauliString * GateOperation cases, e.g. `x*x*x**2`
@ToastCheng ToastCheng requested review from vtomole and a team as code owners August 25, 2025 13:50
@github-actions github-actions bot added the size: S 10< lines changed <50 label Aug 25, 2025
... to handle operations with single-item pauli expansions using
PauliString.

Addressed the GateOperation * GateOperation cases in quantumlib#7588.
@github-actions github-actions bot added size: M 50< lines changed <250 and removed size: S 10< lines changed <50 labels Aug 25, 2025
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.

Two quick comments

@github-actions github-actions bot added size: S 10< lines changed <50 and removed size: M 50< lines changed <250 labels Aug 26, 2025
...so that the PauliString construction logic in quantumlib#7588 is defined in a
single function "_try_interpret_as_pauli_string".
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.

Looking good. Couple more comments.

1. Remove cases that have been handled in _mul_with_qubits
2. Remove try catch block in _try_interpret_as_pauli_string (quantumlib#7588)
Copy link

codecov bot commented Aug 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.50%. Comparing base (fe0dc21) to head (87337dc).
⚠️ Report is 34 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #7603    +/-   ##
========================================
  Coverage   97.50%   97.50%            
========================================
  Files        1101     1103     +2     
  Lines       99396    99714   +318     
========================================
+ Hits        96913    97228   +315     
- Misses       2483     2486     +3     

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

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.

Nice, a couple more things I'd like you to try. Then also add the test cases from the linked issue to pauli_string_tests.

@github-actions github-actions bot added size: M 50< lines changed <250 and removed size: S 10< lines changed <50 labels Aug 27, 2025
@ToastCheng ToastCheng force-pushed the i7588-ps-impl branch 2 times, most recently from bd84d42 to 0579e62 Compare August 28, 2025 07:34
... by trying to first convert the operation to be multiplied to
a PauliString. With this change, the multiplication dunder function
in pauli operation could be removed.
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.

This is looking great. I think the high level structure works well, just a couple nits and I think we'll be ready to wrap it up.

Relax the type check from GateOperation to Operation.
...since it is unused now.
Given that _try_interpret_as_pauli_string also handles PauliString after relaxing
the type check to Operation, remove the PauliString type check here.
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.

One more nit and I think this looks good to go. Great work!

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. Fixes the problem, and makes code more flexible and removes the need for special cases.

cc @pavoljuhas or @NoureldinYosri for maintainer review.

@@ -614,13 +614,13 @@ def _as_pauli_mask(val: Iterable[cirq.PAULI_GATE_LIKE] | np.ndarray) -> np.ndarr


def _attempt_value_to_pauli_index(v: cirq.Operation) -> tuple[int, int] | None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

@ToastCheng Note that this could easily be expanded now to return a list[tuple[int, int]] if v has multiple qubits, and allow DensePauliString to multiply and commute with parity gates etc on LineQubits too. I think that would be a separate PR, but would be a straightforward followup.

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.

Multiplying Paulis and Gates sometimes works, sometimes fails
2 participants