Skip to content

Commit

Permalink
[labs.dla] Allow dense inputs for lie_closure_dense (#6695)
Browse files Browse the repository at this point in the history
Ironically, the `lie_closure_dense` function in `pennylane.labs.dla`
currently doesnt allow dense inputs, fixing that
  • Loading branch information
Qottmann authored and mudit2812 committed Dec 13, 2024
1 parent 94d65ae commit a232fda
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
* Added a dense implementation of computing the Lie closure in a new function
`lie_closure_dense` in `pennylane.labs.dla`.
[(#6371)](https://github.com/PennyLaneAI/pennylane/pull/6371)
[(#6695)](https://github.com/PennyLaneAI/pennylane/pull/6695)

* New functionality to calculate angles for QSP and QSVT has been added. This includes the function `qml.poly_to_angles`
to obtain angles directly and the function `qml.transform_angles` to convert angles from one subroutine to another.
Expand Down
29 changes: 20 additions & 9 deletions pennylane/labs/dla/lie_closure_dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _hermitian_basis(matrices: Iterable[np.ndarray], tol: float = None, subbasis


def lie_closure_dense(
generators: Iterable[Union[PauliWord, PauliSentence, Operator]],
generators: Iterable[Union[PauliWord, PauliSentence, Operator, np.ndarray]],
n: int = None,
max_iterations: int = 10000,
verbose: bool = False,
Expand All @@ -81,9 +81,10 @@ def lie_closure_dense(
`Introduction to Dynamical Lie Algebras for quantum practitioners <https://pennylane.ai/qml/demos/tutorial_liealgebra/>`__.
Args:
generators (Iterable[Union[PauliWord, PauliSentence, Operator]]): generating set for which to compute the
generators (Iterable[Union[PauliWord, PauliSentence, Operator, np.ndarray]]): generating set for which to compute the
Lie closure.
n (int): The number of qubits involved. If ``None`` is provided, it is automatically deduced from the generators.
Ignored when the inputs are ``np.ndarray`` instances.
max_iterations (int): maximum depth of nested commutators to consider. Default is ``10000``.
verbose (bool): whether to print out progress updates during Lie closure
calculation. Default is ``False``.
Expand Down Expand Up @@ -112,14 +113,24 @@ def lie_closure_dense(
so Hermitian operators alone can not form an algebra with the standard commutator).
"""

if n is None:
all_wires = qml.wires.Wires.all_wires([_.wires for _ in generators])
n = len(all_wires)
assert all_wires.toset() == set(range(n))
dense_in = isinstance(generators, np.ndarray) or all(
isinstance(op, np.ndarray) for op in generators
)

gens = np.array([qml.matrix(op, wire_order=range(n)) for op in generators], dtype=complex)
chi = 2**n
assert gens.shape == (len(generators), chi, chi)
if not dense_in:
if n is None:
all_wires = qml.wires.Wires.all_wires([_.wires for _ in generators])
n = len(all_wires)
assert all_wires.toset() == set(range(n))

gens = np.array([qml.matrix(op, wire_order=range(n)) for op in generators], dtype=complex)
chi = 2**n
assert gens.shape == (len(generators), chi, chi)

else:
gens = np.array(generators)
chi = generators[0].shape[0]
assert gens.shape == (len(generators), chi, chi)

epoch = 0
old_length = 0
Expand Down
15 changes: 15 additions & 0 deletions pennylane/labs/tests/dla/test_lie_closure_dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ def test_lie_closure_dense_with_pl_ops(self):
res11 = [qml.pauli_decompose(op) for op in res11] # back to pauli_rep for easier comparison
assert PauliVSpace(res11) == PauliVSpace(dla11)

def test_lie_closure_dense_with_ndarrays(self):
"""Test that lie_closure_dense works properly with ndarray inputs"""
dla = [
qml.sum(qml.prod(X(0), X(1)), qml.prod(Y(0), Y(1))),
Z(0),
Z(1),
qml.sum(qml.prod(Y(0), X(1)), qml.s_prod(-1.0, qml.prod(X(0), Y(1)))),
]
dla = [qml.matrix(op, wire_order=range(2)) for op in dla]
gen11 = dla[:-1]
res11 = lie_closure_dense(gen11)

res11 = [qml.pauli_decompose(op) for op in res11] # back to pauli_rep for easier comparison
assert PauliVSpace(res11) == PauliVSpace(dla11)

def test_lie_closure_dense_with_PauliWords(self):
"""Test that lie_closure_dense works properly with PauliWords"""
gen = [
Expand Down

0 comments on commit a232fda

Please sign in to comment.