From 59c32874dc825af65a86026ea51f68b9338446a4 Mon Sep 17 00:00:00 2001 From: Korbinian Kottmann <43949391+Qottmann@users.noreply.github.com> Date: Fri, 17 May 2024 09:37:55 +0200 Subject: [PATCH] Allow empty `PauliVSpace` init (#5675) 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. --- doc/releases/changelog-dev.md | 4 ++++ pennylane/pauli/dla/lie_closure.py | 6 +++++- tests/pauli/dla/test_lie_closure.py | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 1571d9276ad..04650804ade 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) +

Community contributions 🥳

* 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 3113dd18276..aadfad83210 100644 --- a/pennylane/pauli/dla/lie_closure.py +++ b/pennylane/pauli/dla/lie_closure.py @@ -249,7 +249,11 @@ def __init__(self, generators, dtype=float, tol=None): ] # 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)} diff --git a/tests/pauli/dla/test_lie_closure.py b/tests/pauli/dla/test_lie_closure.py index 9d18c792292..fc2f4361e90 100644 --- a/tests/pauli/dla/test_lie_closure.py +++ b/tests/pauli/dla/test_lie_closure.py @@ -65,6 +65,15 @@ def test_init(self): 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)