From 1da85cf2de743e52ef85e3fce913db62093eaa96 Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Tue, 15 Apr 2025 15:15:35 -0700 Subject: [PATCH 1/7] Fix: Allow subtraction of IdentityGate in PauliSum.__sub__ --- cirq-core/cirq/ops/linear_combinations.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cirq-core/cirq/ops/linear_combinations.py b/cirq-core/cirq/ops/linear_combinations.py index 0d08f2e7def..eb4215f675c 100644 --- a/cirq-core/cirq/ops/linear_combinations.py +++ b/cirq-core/cirq/ops/linear_combinations.py @@ -798,7 +798,10 @@ def __isub__(self, other): def __sub__(self, other): if not isinstance(other, (numbers.Complex, PauliString, PauliSum)): - return NotImplemented + if hasattr(other, 'gate') and isinstance(other.gate, identity.IdentityGate): + other = PauliString(other) + else: + return NotImplemented result = self.copy() result -= other return result From 6b9ec8b60a55861766edd676133a550ec9bbf5ca Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Wed, 16 Apr 2025 10:36:47 -0700 Subject: [PATCH 2/7] Manually resolve conflict and complete cherry-pick --- cirq-core/cirq/ops/pauli_string_test.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cirq-core/cirq/ops/pauli_string_test.py b/cirq-core/cirq/ops/pauli_string_test.py index 13231df9409..cbc852522e0 100644 --- a/cirq-core/cirq/ops/pauli_string_test.py +++ b/cirq-core/cirq/ops/pauli_string_test.py @@ -2137,3 +2137,25 @@ def test_resolve(resolve_fn): pst = cirq.PauliString({q: 'x'}, coefficient=t) ps1 = cirq.PauliString({q: 'x'}, coefficient=1j) assert resolve_fn(pst, {'t': 1j}) == ps1 + +def test_pauli_sum_identity_operations(): + q = cirq.LineQubit(0) + paulis = tuple(cirq.PauliString(p) for p in (cirq.I(q), cirq.X(q), cirq.Y(q), cirq.Z(q))) + + def add(x, y): + return x + y + + def sub(x, y): + return x - y + + def addm(x, y): + return x + (-1.0) * y + + for p1 in paulis: + for p2 in paulis: + for op in (add, sub, addm): + try: + _ = op(p1, p2) + except Exception as e: + import pytest + pytest.fail(f"Operation {p1.gate}{getattr(op, 'sym', op.__name__)}{p2.gate} raised {e}") From 7bae7dd6381ec61bc2680ad755282a36b2422e2f Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Wed, 16 Apr 2025 16:46:44 -0700 Subject: [PATCH 3/7] Add comprehensive tests for Pauli operator addition and subtraction, including identity cases (except I+I) --- cirq-core/cirq/ops/pauli_string_test.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/cirq-core/cirq/ops/pauli_string_test.py b/cirq-core/cirq/ops/pauli_string_test.py index cbc852522e0..78b5bcf7456 100644 --- a/cirq-core/cirq/ops/pauli_string_test.py +++ b/cirq-core/cirq/ops/pauli_string_test.py @@ -2138,24 +2138,12 @@ def test_resolve(resolve_fn): ps1 = cirq.PauliString({q: 'x'}, coefficient=1j) assert resolve_fn(pst, {'t': 1j}) == ps1 -def test_pauli_sum_identity_operations(): +def test_pauli_ops_identity_gate_operation(): q = cirq.LineQubit(0) - paulis = tuple(cirq.PauliString(p) for p in (cirq.I(q), cirq.X(q), cirq.Y(q), cirq.Z(q))) - - def add(x, y): - return x + y - - def sub(x, y): - return x - y - - def addm(x, y): - return x + (-1.0) * y - + paulis = (cirq.I(q), cirq.X(q), cirq.Y(q), cirq.Z(q)) for p1 in paulis: for p2 in paulis: - for op in (add, sub, addm): - try: - _ = op(p1, p2) - except Exception as e: - import pytest - pytest.fail(f"Operation {p1.gate}{getattr(op, 'sym', op.__name__)}{p2.gate} raised {e}") + # TODO: Support addition and subtraction of identity gate operations + if p1 == cirq.I(q) and p2 == cirq.I(q): continue + assert isinstance(p1 + p2, (cirq.PauliSum, cirq.PauliString)), f"Addition failed for {p1} + {p2}" + assert isinstance(p1 - p2, (cirq.PauliSum, cirq.PauliString)), f"Subtraction failed for {p1} - {p2}" \ No newline at end of file From 7df8392170e1e184113f1d51557ca76deada4d63 Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Thu, 17 Apr 2025 13:15:06 -0700 Subject: [PATCH 4/7] Fixed linting erorrs --- cirq-core/cirq/ops/pauli_string_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cirq-core/cirq/ops/pauli_string_test.py b/cirq-core/cirq/ops/pauli_string_test.py index 78b5bcf7456..c0bc8ecf43e 100644 --- a/cirq-core/cirq/ops/pauli_string_test.py +++ b/cirq-core/cirq/ops/pauli_string_test.py @@ -2138,6 +2138,7 @@ def test_resolve(resolve_fn): ps1 = cirq.PauliString({q: 'x'}, coefficient=1j) assert resolve_fn(pst, {'t': 1j}) == ps1 + def test_pauli_ops_identity_gate_operation(): q = cirq.LineQubit(0) paulis = (cirq.I(q), cirq.X(q), cirq.Y(q), cirq.Z(q)) @@ -2146,4 +2147,4 @@ def test_pauli_ops_identity_gate_operation(): # TODO: Support addition and subtraction of identity gate operations if p1 == cirq.I(q) and p2 == cirq.I(q): continue assert isinstance(p1 + p2, (cirq.PauliSum, cirq.PauliString)), f"Addition failed for {p1} + {p2}" - assert isinstance(p1 - p2, (cirq.PauliSum, cirq.PauliString)), f"Subtraction failed for {p1} - {p2}" \ No newline at end of file + assert isinstance(p1 - p2, (cirq.PauliSum, cirq.PauliString)), f"Subtraction failed for {p1} - {p2}" From e5e39ba7ff2bfd14bc37d5a9ecfc9c4c56b0239d Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Thu, 17 Apr 2025 13:31:07 -0700 Subject: [PATCH 5/7] Updated lint + added issue number --- cirq-core/cirq/ops/pauli_string_test.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cirq-core/cirq/ops/pauli_string_test.py b/cirq-core/cirq/ops/pauli_string_test.py index c0bc8ecf43e..a63edea9ee7 100644 --- a/cirq-core/cirq/ops/pauli_string_test.py +++ b/cirq-core/cirq/ops/pauli_string_test.py @@ -2144,7 +2144,12 @@ def test_pauli_ops_identity_gate_operation(): paulis = (cirq.I(q), cirq.X(q), cirq.Y(q), cirq.Z(q)) for p1 in paulis: for p2 in paulis: - # TODO: Support addition and subtraction of identity gate operations - if p1 == cirq.I(q) and p2 == cirq.I(q): continue - assert isinstance(p1 + p2, (cirq.PauliSum, cirq.PauliString)), f"Addition failed for {p1} + {p2}" - assert isinstance(p1 - p2, (cirq.PauliSum, cirq.PauliString)), f"Subtraction failed for {p1} - {p2}" + # TODO: Issue #7280 - Support addition and subtraction of identity gate operations. + if p1 == cirq.I(q) and p2 == cirq.I(q): + continue + assert isinstance( + p1 + p2, (cirq.PauliSum, cirq.PauliString) + ), f"Addition failed for {p1} + {p2}" + assert isinstance( + p1 - p2, (cirq.PauliSum, cirq.PauliString) + ), f"Subtraction failed for {p1} - {p2}" \ No newline at end of file From e860d08447a8dfe23277a4663a646c9823918011 Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Thu, 17 Apr 2025 14:11:58 -0700 Subject: [PATCH 6/7] fixed lints --- cirq-core/cirq/ops/pauli_string_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cirq-core/cirq/ops/pauli_string_test.py b/cirq-core/cirq/ops/pauli_string_test.py index a63edea9ee7..5f6c5c525a9 100644 --- a/cirq-core/cirq/ops/pauli_string_test.py +++ b/cirq-core/cirq/ops/pauli_string_test.py @@ -2152,4 +2152,5 @@ def test_pauli_ops_identity_gate_operation(): ), f"Addition failed for {p1} + {p2}" assert isinstance( p1 - p2, (cirq.PauliSum, cirq.PauliString) - ), f"Subtraction failed for {p1} - {p2}" \ No newline at end of file + ), f"Subtraction failed for {p1} - {p2}" + \ No newline at end of file From f9dee5441c86a5782f63be58feade4d882b413f4 Mon Sep 17 00:00:00 2001 From: Revanth Gundala Date: Thu, 17 Apr 2025 18:35:14 -0700 Subject: [PATCH 7/7] Added new line --- cirq-core/cirq/ops/pauli_string_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cirq-core/cirq/ops/pauli_string_test.py b/cirq-core/cirq/ops/pauli_string_test.py index 5f6c5c525a9..952c9f333f0 100644 --- a/cirq-core/cirq/ops/pauli_string_test.py +++ b/cirq-core/cirq/ops/pauli_string_test.py @@ -2153,4 +2153,3 @@ def test_pauli_ops_identity_gate_operation(): assert isinstance( p1 - p2, (cirq.PauliSum, cirq.PauliString) ), f"Subtraction failed for {p1} - {p2}" - \ No newline at end of file