Skip to content

feat: add PauliStringSum container#1270

Open
CleanDev-Fix wants to merge 8 commits into
amazon-braket:mainfrom
CleanDev-Fix:codex/paulistring-sums-1256
Open

feat: add PauliStringSum container#1270
CleanDev-Fix wants to merge 8 commits into
amazon-braket:mainfrom
CleanDev-Fix:codex/paulistring-sums-1256

Conversation

@CleanDev-Fix

Copy link
Copy Markdown

Summary

  • add a weighted PauliStringSum container for Pauli string arithmetic
  • support list conversion, conversion to/from circuit Sum observables, indexing, membership, scalar operations, and multiplication by PauliString
  • support reverse subtraction and order-preserving left/right multiplication by PauliString
  • add commutation helpers for sum/sum and sum/string checks

Closes #1256.

Tests

  • uv run --extra test pytest test/unit_tests/braket/quantum_information test/unit_tests/braket/program_sets/test_circuit_binding.py -q -n 0
  • uv run --extra test pytest test/unit_tests/braket/quantum_information/test_pauli_string.py test/unit_tests/braket/quantum_information/test_pauli_string_sum.py -q -n 0
  • uv run --with tox tox -e linters_check

@CleanDev-Fix CleanDev-Fix requested a review from a team as a code owner June 3, 2026 20:06
@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3fb2d0e) to head (0033248).

Additional details and impacted files
@@            Coverage Diff             @@
##              main     #1270    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          170       171     +1     
  Lines        11011     11180   +169     
  Branches      1413      1439    +26     
==========================================
+ Hits         11011     11180   +169     

☔ View full report in Codecov by Harness.
📢 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.

Comment on lines +41 to +44
@classmethod
def from_list(cls, terms: Iterable[tuple[numbers.Number, str | PauliString]]) -> PauliStringSum:
"""Builds a ``PauliStringSum`` from a list of weighted Pauli strings."""
return cls(terms)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unnecessary if the constructor already does this

for _, right in other_sum.terms
)

def is_self_commuting(self) -> bool:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can be a property, and the value determined on instantiation (instead of computing each time). Also, rename to all_terms_commute

observables.append(coefficient * pauli.to_unsigned_observable(include_trivial=True))
return Sum(observables)

def commutes_with(self, other: PauliStringSum | PauliString | str) -> bool:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And if all_terms_commute is determined at instantiation, this can be short circuited to False if all_terms_commute is False for either PauliSum.

)

@classmethod
def from_sum(cls, observable_sum: Sum) -> PauliStringSum:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: Move up right under __init__

_OBSERVABLE_TO_FACTOR = {I: "I", X: "X", Y: "Y", Z: "Z"}


class PauliStringSum:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PauliSum is more concise

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should add utilities here to, so we can add PauliStrings together, check commutativity, etc.

@CleanDev-Fix CleanDev-Fix force-pushed the codex/paulistring-sums-1256 branch from 12f08c2 to fdcfd25 Compare June 9, 2026 00:04

@speller26 speller26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for adding the tests; can you also expand PauliString to allow addition and commutation checking?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow for Sums of PauliStrings

2 participants