Skip to content

Commit

Permalink
Allow empty PauliVSpace init (#5675)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Qottmann authored May 17, 2024
1 parent 5b81a8a commit 59c3287
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -147,6 +150,7 @@ Gabriel Bottrill,
Isaac De Vlugt,
Pietropaolo Frisoni,
Soran Jahangiri,
Korbinian Kottmann,
Christina Lee,
Vincent Michaud-Rioux,
Kenya Sakka,
Expand Down
6 changes: 5 additions & 1 deletion pennylane/pauli/dla/lie_closure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
9 changes: 9 additions & 0 deletions tests/pauli/dla/test_lie_closure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 59c3287

Please sign in to comment.