Repository: PennyLaneAI/pennylane
Pull request #1465 (2021-07-21 09:03:46)

Use comma when printing the wires of `qml.Hermitian` in `qml.Hamiltonian`
**Context**

When printing the observables building up `qml.Hamiltonian`, coefficients and terms are printed following the OpenFermion convention. OpenFermion doesn't represent aribtrary Hermitian observables, therefore we're using a custom notation for `qml.Hermitian` (https://github.com/PennyLaneAI/pennylane/pull/987):
```
  (0.8) [Z0]
+ (0.8) [Z0 Hermitian1'3 Z2]
```
**Changes**
Changes the separator between the wires of `qml.Hermitian` to be a comma:
```
  (0.8) [Z0]
+ (0.8) [Z0 Hermitian1,3 Z2]
```
**Related issues**
Closes https://github.com/PennyLaneAI/pennylane/issues/1068

github-actions[bot]: Hello. You may have forgotten to update the changelog!
Please edit [.github/CHANGELOG.md](/PennyLaneAI/pennylane/blob/master/.github/CHANGELOG.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/1465?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
> Merging [#1465](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1465?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (89bec34) into [master](https://codecov.io/gh/PennyLaneAI/pennylane/commit/2b451debbb7dcfbef861267763b36926545e7eeb?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (2b451de) will **not change** coverage.
> The diff coverage is `0.00%`.

[![Impacted file tree graph](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1465/graphs/tree.svg?width=650&height=150&src=pr&token=9S3YzlUgGk&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1465?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)

```diff
@@           Coverage Diff           @@
##           master    #1465   +/-   ##
=======================================
  Coverage   98.28%   98.28%           
=======================================
  Files         168      168           
  Lines       12335    12335           
=======================================
  Hits        12124    12124           
  Misses        211      211           
```


| [Impacted Files](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1465?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) | Coverage Δ | |
|---|---|---|
| [pennylane/vqe/vqe.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1465/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL3ZxZS92cWUucHk=) | `92.19% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1465?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1465?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Last update [2b451de...89bec34](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1465?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).


Patch:
diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md
index 100b411cc..e848655ed 100644
--- a/.github/CHANGELOG.md
+++ b/.github/CHANGELOG.md
@@ -258,6 +258,10 @@
 
 <h3>Improvements</h3>
 
+* Changed to using commas as the separator of wires in the string
+  representation of `qml.Hamiltonian` objects for multi-qubit terms.
+  [(#1465)](https://github.com/PennyLaneAI/pennylane/pull/1465)
+
 * Changed to using `np.object_` instead of `np.object` as per the NumPy
   deprecations starting version 1.20.
   [(#1466)](https://github.com/PennyLaneAI/pennylane/pull/1466)
diff --git a/pennylane/vqe/vqe.py b/pennylane/vqe/vqe.py
index 2b0369ca9..365ea0aae 100644
--- a/pennylane/vqe/vqe.py
+++ b/pennylane/vqe/vqe.py
@@ -67,7 +67,7 @@ class Hamiltonian:
     >>> obs = qml.Hermitian(obs_matrix, wires=[0, 1])
     >>> H = qml.Hamiltonian((0.8, ), (obs, ))
     >>> print(H)
-    (0.8) [Hermitian0'1]
+    (0.8) [Hermitian0,1]
 
     Alternatively, the :func:`~.molecular_hamiltonian` function from the
     :doc:`/introduction/chemistry` module can be used to generate a molecular
@@ -198,7 +198,7 @@ class Hamiltonian:
 
     def __str__(self):
         # Lambda function that formats the wires
-        wires_print = lambda ob: "'".join(map(str, ob.wires.tolist()))
+        wires_print = lambda ob: ",".join(map(str, ob.wires.tolist()))
 
         paired_coeff_obs = list(zip(self.coeffs, self.ops))
         paired_coeff_obs.sort(key=lambda pair: (len(pair[1].wires), pair[0]))


Test patch:
diff --git a/tests/test_vqe.py b/tests/test_vqe.py
index 64b3467ea..2243fb08c 100644
--- a/tests/test_vqe.py
+++ b/tests/test_vqe.py
@@ -79,16 +79,16 @@ valid_hamiltonians = [
 ]
 
 valid_hamiltonians_str = [
-    "  (1.0) [Hermitian0'1]",
+    "  (1.0) [Hermitian0,1]",
     "  (-0.8) [Z0]",
     "  (0.6) [X0 X1]",
     "  (-1.6) [Y1]\n+ (0.5) [X0]",
     "  (-1.6) [Y1]\n+ (0.5) [X1]",
     "  (-1.6) [Yb]\n+ (0.5) [Xa]",
     "  (-0.4) [Hermitian2]\n+ (0.333) [Z2]\n+ (1.1) [X0]",
-    "  (0.15) [Z1]\n+ (-0.4) [Hermitian0'2]",
+    "  (0.15) [Z1]\n+ (-0.4) [Hermitian0,2]",
     "  (1.5) [Z0]\n+ (2.0) [Y2]",
-    "  (0.5) [Y0]\n+ (-0.1) [Hermitian0'1]",
+    "  (0.5) [Y0]\n+ (-0.1) [Hermitian0,1]",
     "  (0.5) [X0]\n+ (1.2) [X0 X1]",
 ]