Commit c000fae
Fix: Allow subtraction of IdentityGate in PauliSum.__sub__ (#7276)
### Summary
Fixes #7120
This PR fixes a bug where subtracting a GateOperation with an
IdentityGate (e.g., cirq.I(q) - cirq.Z(q)) would raise a TypeError
instead of returning a valid PauliSum.
### Details
Updated PauliSum.__sub__ in cirq-core/cirq/ops/linear_combinations.py to
promote GateOperation(IdentityGate) to a PauliString() when performing
subtraction.
This ensures arithmetic like cirq.I(q) - cirq.Z(q) now works as expected
and matches the behavior for addition.
### Reproduction
Before this fix, the following would raise an error:
```python
import cirq
q = cirq.LineQubit(0)
result = cirq.I(q) - cirq.Z(q) # TypeError
---------
Co-authored-by: Michael Hucka <mhucka@caltech.edu>
Co-authored-by: Pavol Juhas <juhas@google.com>1 parent b0a1751 commit c000fae
File tree
3 files changed
+44
-7
lines changed- cirq-core/cirq/ops
3 files changed
+44
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
746 | 746 | | |
747 | 747 | | |
748 | 748 | | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
749 | 753 | | |
750 | 754 | | |
751 | 755 | | |
| |||
754 | 758 | | |
755 | 759 | | |
756 | 760 | | |
757 | | - | |
758 | | - | |
759 | | - | |
760 | | - | |
761 | | - | |
| 761 | + | |
| 762 | + | |
762 | 763 | | |
763 | 764 | | |
764 | 765 | | |
| |||
772 | 773 | | |
773 | 774 | | |
774 | 775 | | |
775 | | - | |
| 776 | + | |
776 | 777 | | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
777 | 782 | | |
778 | 783 | | |
779 | 784 | | |
| |||
782 | 787 | | |
783 | 788 | | |
784 | 789 | | |
785 | | - | |
| 790 | + | |
786 | 791 | | |
787 | 792 | | |
788 | 793 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
971 | 971 | | |
972 | 972 | | |
973 | 973 | | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
974 | 978 | | |
975 | 979 | | |
976 | 980 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1992 | 1992 | | |
1993 | 1993 | | |
1994 | 1994 | | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| 2001 | + | |
| 2002 | + | |
| 2003 | + | |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
| 2007 | + | |
| 2008 | + | |
| 2009 | + | |
| 2010 | + | |
| 2011 | + | |
| 2012 | + | |
| 2013 | + | |
| 2014 | + | |
| 2015 | + | |
| 2016 | + | |
| 2017 | + | |
| 2018 | + | |
| 2019 | + | |
| 2020 | + | |
| 2021 | + | |
| 2022 | + | |
0 commit comments