Repository: PennyLaneAI/pennylane
Pull request #3294 (2022-11-08 17:10:46)

Make the Pauli display nice
Update the display of an empty PauliWord from `()` to `I`, as it functionally behaves like Identity.

github-actions[bot]: Hello. You may have forgotten to update the changelog!
Please edit [`doc/releases/changelog-dev.md`](/PennyLaneAI/pennylane/blob/master/doc/releases/changelog-dev.md) with:
* A one-to-two sentence description of the change. You may include a small working example for new features.
* A link back to this PR.
* Your name (or GitHub username) in the contributors section.
codecov[bot]: # [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3294?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
> :exclamation: No coverage uploaded for pull request base (`v0.27.0-rc0@1e9d15f`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#section-missing-base-commit).
> The diff coverage is `n/a`.

```diff
@@              Coverage Diff               @@
##             v0.27.0-rc0    #3294   +/-   ##
==============================================
  Coverage               ?   99.70%           
==============================================
  Files                  ?      299           
  Lines                  ?    26119           
  Branches               ?        0           
==============================================
  Hits                   ?    26042           
  Misses                 ?       77           
  Partials               ?        0           
```



Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)


Patch:
diff --git a/pennylane/pauli/pauli_arithmetic.py b/pennylane/pauli/pauli_arithmetic.py
index 1927df00f..b2001661d 100644
--- a/pennylane/pauli/pauli_arithmetic.py
+++ b/pennylane/pauli/pauli_arithmetic.py
@@ -143,7 +143,7 @@ class PauliWord(dict):
     def __str__(self):
         """String representation of a PauliWord."""
         if len(self) == 0:
-            return "()"
+            return "I"
         return " @ ".join(f"{op}({w})" for w, op in self.items())
 
     def __repr__(self):
@@ -224,6 +224,8 @@ class PauliSentence(dict):
 
     def __str__(self):
         """String representation of the PauliSentence."""
+        if len(self) == 0:
+            return "I"
         return "\n+ ".join(f"{coeff} * {str(pw)}" for pw, coeff in self.items())
 
     def __repr__(self):


Test patch:
diff --git a/tests/pauli/test_pauli_arithmetic.py b/tests/pauli/test_pauli_arithmetic.py
index 34a43209d..ec8ca8795 100644
--- a/tests/pauli/test_pauli_arithmetic.py
+++ b/tests/pauli/test_pauli_arithmetic.py
@@ -96,7 +96,7 @@ class TestPauliWord:
         (pw1, "X(1) @ Y(2)"),
         (pw2, "X(a) @ X(b) @ Z(c)"),
         (pw3, "Z(0) @ Z(b) @ Z(c)"),
-        (pw4, "()"),
+        (pw4, "I"),
     )
 
     @pytest.mark.parametrize("pw, str_rep", tup_pw_str)
@@ -185,8 +185,9 @@ class TestPauliSentence:
             ps2,
             "-1.23 * X(1) @ Y(2)\n" "+ (-0-4j) * X(a) @ X(b) @ Z(c)\n" "+ 0.5 * Z(0) @ Z(b) @ Z(c)",
         ),
-        (ps3, "-0.5 * Z(0) @ Z(b) @ Z(c)\n" "+ 1 * ()"),
-        (ps4, "1 * ()"),
+        (ps3, "-0.5 * Z(0) @ Z(b) @ Z(c)\n" "+ 1 * I"),
+        (ps4, "1 * I"),
+        (ps5, "I"),
     )
 
     @pytest.mark.parametrize("ps, str_rep", tup_ps_str)