Repository: PennyLaneAI/pennylane
Pull request #5675 (2024-05-10 10:48:00)

Allow empty `PauliVSpace` init
In situations where one wants to iteratively build up a `PaulIVSpace` it is handy to be able to initialize an empty `PauliVSpace`. This is currently not possible and fixed with this PR.

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://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5675?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 99.68%. Comparing base [(`9f7e8ba`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/9f7e8ba8f41580fc3efe88bf88f0cc05c04a66ab?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) to head [(`77e0b53`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5675?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #5675      +/-   ##
==========================================
- Coverage   99.68%   99.68%   -0.01%     
==========================================
  Files         412      412              
  Lines       38645    38354     -291     
==========================================
- Hits        38525    38232     -293     
- Misses        120      122       +2     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5675?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).

Alex-Preciado: sc-63068

Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 1571d9276..04650804a 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -82,6 +82,9 @@
   allowing error types to be more consistent with the context the `decompose` function is used in.
   [(#5669)](https://github.com/PennyLaneAI/pennylane/pull/5669)
 
+* Empty initialization of `PauliVSpace` is permitted.
+  [(#5675)](https://github.com/PennyLaneAI/pennylane/pull/5675)
+
 <h4>Community contributions 🥳</h4>
 
 * Implemented kwargs (`check_interface`, `check_trainability`, `rtol` and `atol`) support in `qml.equal` for the operators `Pow`, `Adjoint`, `Exp`, and `SProd`.
@@ -147,6 +150,7 @@ Gabriel Bottrill,
 Isaac De Vlugt,
 Pietropaolo Frisoni,
 Soran Jahangiri,
+Korbinian Kottmann,
 Christina Lee,
 Vincent Michaud-Rioux,
 Kenya Sakka,
diff --git a/pennylane/pauli/dla/lie_closure.py b/pennylane/pauli/dla/lie_closure.py
index 3113dd182..aadfad832 100644
--- a/pennylane/pauli/dla/lie_closure.py
+++ b/pennylane/pauli/dla/lie_closure.py
@@ -249,7 +249,11 @@ class PauliVSpace:
             ]
 
         # Get all Pauli words that are present in at least one Pauli sentence
-        all_pws = list(reduce(set.__or__, [set(ps.keys()) for ps in generators]))
+        if len(generators) != 0:
+            all_pws = list(reduce(set.__or__, [set(ps.keys()) for ps in generators]))
+        else:
+            all_pws = []
+
         num_pw = len(all_pws)
         # Create a dictionary mapping from PauliWord to row index
         self._pw_to_idx = {pw: i for i, pw in enumerate(all_pws)}


Test patch:
diff --git a/tests/pauli/dla/test_lie_closure.py b/tests/pauli/dla/test_lie_closure.py
index 9d18c7922..fc2f4361e 100644
--- a/tests/pauli/dla/test_lie_closure.py
+++ b/tests/pauli/dla/test_lie_closure.py
@@ -65,6 +65,15 @@ class TestPauliVSpace:
         assert len(vspace._pw_to_idx) == 2
         assert vspace.tol == np.finfo(vspace._M.dtype).eps * 100
 
+    def test_empty_init(self):
+        """Test that a PauliVSpace can be initialized as an empty vector space"""
+        vspace = PauliVSpace([])
+        assert vspace.basis == []
+        assert vspace._rank == 0
+        assert vspace._num_pw == 0
+        assert len(vspace._pw_to_idx) == 0
+        assert vspace.tol == np.finfo(vspace._M.dtype).eps * 100
+
     @pytest.mark.parametrize("dtype", [float, complex])
     def test_dtype(self, dtype):
         vspace = PauliVSpace(ops1, dtype=dtype)