Skip to content

feat: add PauliStringSum class for sums of weighted PauliStrings#1291

Open
tanishqbagriaresearch-hue wants to merge 21 commits into
amazon-braket:mainfrom
tanishqbagriaresearch-hue:pauli-string-sum
Open

feat: add PauliStringSum class for sums of weighted PauliStrings#1291
tanishqbagriaresearch-hue wants to merge 21 commits into
amazon-braket:mainfrom
tanishqbagriaresearch-hue:pauli-string-sum

Conversation

@tanishqbagriaresearch-hue

Copy link
Copy Markdown

Summary

Fixes #1256

This PR introduces a new PauliStringSum class that represents a linear combination (weighted sum) of PauliString objects, enabling richer algebraic operations on Pauli operators.

Changes

New File: src/braket/quantum_information/pauli_string_sum.py

  • Added PauliStringSum class to represent sums of weighted PauliString objects
  • Supports addition and subtraction of PauliString objects with optional scalar weights
  • Supports scalar (float) multiplication via __mul__ and __rmul__
  • Supports multiplication by PauliString (element-wise Pauli product) via __mul__ and __rmul__
  • Supports single-item index access (__getitem__) and in checks (__contains__)
  • Provides to_list() method for conversion to list of (float, str) tuples
  • Provides from_list() class method for constructing from list of (float, str) tuples
  • Implements commutes_with() for checking commutativity with another PauliString
  • Implements is_self_commuting() to check if all terms in the sum mutually commute

Bug Fix: PauliString.__mul__

  • Fixed PauliString.__mul__ to return NotImplemented for non-PauliString types, allowing Python to correctly fall back to PauliStringSum.__rmul__ when multiplying a PauliString by a PauliStringSum

Testing

  • Added 46 unit tests, all passing, covering:
    • Addition and subtraction of PauliStrings
    • Scalar multiplication
    • PauliString multiplication
    • Index access and in checks
    • to_list() / from_list() round-trips
    • commutes_with() and is_self_commuting() checks
    • Edge cases and error handling

Merge Checklist

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow for Sums of PauliStrings

4 participants