Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[labs.dla 3] structure_constants_dense #6376

Open
wants to merge 40 commits into
base: dense_util
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9b08fd3
Add structure_constants_dense and tests
Qottmann Oct 10, 2024
ea19314
docstring
Qottmann Oct 10, 2024
3946804
pylint
Qottmann Oct 10, 2024
29a6619
tmp
dwierichs Oct 10, 2024
d92ae06
normalization in structure constants. add tests
dwierichs Oct 10, 2024
8d4450d
update
dwierichs Oct 10, 2024
da3bec4
is_orthogonal option
dwierichs Oct 11, 2024
f8a6ddf
docstrings, reduce implementation, extend tests
dwierichs Oct 12, 2024
bcbb987
Merge branch 'lie_closure_dense' of https://github.com/PennyLaneAI/pe…
Qottmann Oct 14, 2024
4986956
merge and rename
Qottmann Oct 14, 2024
58e0b45
remove unused d
Qottmann Oct 14, 2024
8d89d74
typo
Qottmann Oct 14, 2024
bcc07e4
Merge branch 'lie_closure_dense' of https://github.com/PennyLaneAI/pe…
Qottmann Oct 14, 2024
2877d55
Merge branch 'lie_closure_dense' of https://github.com/PennyLaneAI/pe…
Qottmann Oct 14, 2024
f67b63a
init
Qottmann Oct 14, 2024
92346c2
merge
Qottmann Oct 14, 2024
07b5c69
add pauli_decompose (to operators rather than coefficients). Add docs
dwierichs Oct 14, 2024
85ed314
tests
dwierichs Oct 14, 2024
c55f947
Merge branch 'lie_closure_dense' of https://github.com/PennyLaneAI/pe…
Qottmann Oct 15, 2024
1526c93
Merge branch 'structure_constants_dense' of https://github.com/PennyL…
Qottmann Oct 15, 2024
3d5fd72
Merge branch 'lie_closure_dense' of https://github.com/PennyLaneAI/pe…
Qottmann Oct 16, 2024
17cd42f
docs
Qottmann Oct 16, 2024
d5aeb01
allow list input
Qottmann Oct 28, 2024
ec11734
merge
Qottmann Oct 29, 2024
f5371d7
Merge branch 'dense_util' of https://github.com/PennyLaneAI/pennylane…
Qottmann Nov 11, 2024
b92163d
Merge branch 'dense_util' of https://github.com/PennyLaneAI/pennylane…
Qottmann Nov 14, 2024
a88db10
Apply suggestions from code review
Qottmann Nov 14, 2024
633589f
Merge branch 'structure_constants_dense' of https://github.com/PennyL…
Qottmann Nov 14, 2024
6132e51
update normalization
Qottmann Nov 14, 2024
50b7a93
orthonormalize function
Qottmann Nov 14, 2024
b5f4137
orthonormalize
Qottmann Nov 14, 2024
6903eff
add example from code review
Qottmann Nov 14, 2024
c03afe1
trace inner product
Qottmann Nov 14, 2024
b3bf572
consistency checks
Qottmann Nov 14, 2024
cf92b3c
trace inner product in test
Qottmann Nov 14, 2024
d5d9add
Merge branch 'dense_util' into structure_constants_dense
dwierichs Nov 14, 2024
61309e2
is_orthogonal in qml.structure_constants. tests. changelog
dwierichs Nov 14, 2024
2b60e0b
Merge branch 'dense_util' into structure_constants_dense
dwierichs Nov 14, 2024
04b6780
extend tests, slight polish of methods
dwierichs Nov 14, 2024
6d7662c
fix normalization in `structure_constants_dense` and its tests
dwierichs Nov 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pennylane/labs/lie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
"""Experimental Lie theory features"""

from .lie_closure import lie_closure_dense
from .structure_constants import structure_constants_dense

Check warning on line 17 in pennylane/labs/lie/__init__.py

View check run for this annotation

Codecov / codecov/patch

pennylane/labs/lie/__init__.py#L17

Added line #L17 was not covered by tests
70 changes: 70 additions & 0 deletions pennylane/labs/lie/structure_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2024 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A function to compute the structure constants that make up the adjoint representation of a Lie algebra"""

import numpy as np

Check warning on line 16 in pennylane/labs/lie/structure_constants.py

View check run for this annotation

Codecov / codecov/patch

pennylane/labs/lie/structure_constants.py#L16

Added line #L16 was not covered by tests

from pennylane.typing import TensorLike

Check warning on line 18 in pennylane/labs/lie/structure_constants.py

View check run for this annotation

Codecov / codecov/patch

pennylane/labs/lie/structure_constants.py#L18

Added line #L18 was not covered by tests


def structure_constants_dense(g: TensorLike) -> TensorLike:

Check warning on line 21 in pennylane/labs/lie/structure_constants.py

View check run for this annotation

Codecov / codecov/patch

pennylane/labs/lie/structure_constants.py#L21

Added line #L21 was not covered by tests
r"""
Compute the structure constants that make up the adjoint representation of a Lie algebra.

This function computes the structure constants of a Lie algebra provided by their dense matrix representation,
obtained from, e.g., :func:`~lie_closure_dense`.
This is sometimes more efficient than using the sparse Pauli representations of :class:`~PauliWord` and
`~PauliSentence` that are employed in :func:`~structure_constants`, e.g., when ther are few but dense sums of Paulis.

.. seealso:: For details on the mathematical definitions, see :func:`~structure_constants` and the section "Lie algebra basics" in our `g-sim demo <https://pennylane.ai/qml/demos/tutorial_liesim/#lie-algebra-basics>`__.

Args:
g (np.array): The (dynamical) Lie algebra provided as dense matrices, as generated from :func:`pennylane.labs.lie.lie_closure_dense`.

Returns:
TensorLike: The adjoint representation of shape ``(d, d, d)``, corresponding to indices ``(gamma, alpha, beta)``.

**Example**

Let us generate the DLA of the transverse field Ising model using :func:`~lie_closure_dense`.

>>> n = 4
>>> gens = [X(i) @ X(i+1) + Y(i) @ Y(i+1) + Z(i) @ Z(i+1) for i in range(n-1)]
>>> g = lie_closure_dense(gens)
>>> g.shape
(12, 16, 16)

The DLA is represented by a collection of twelve :math:`2^4 \times 2^4` matrices.
Hence, the dimension of the DLA is :math:`d = 12`. Hence, the structure constants have shape ``(12, 12, 12)``.

>>> adj = structure_constants_dense(g)
>>> adj.shape
(12, 12, 12)

"""
dimg, chi, _ = g.shape

Check warning on line 56 in pennylane/labs/lie/structure_constants.py

View check run for this annotation

Codecov / codecov/patch

pennylane/labs/lie/structure_constants.py#L56

Added line #L56 was not covered by tests

# compute all commutators
prod = np.einsum("aij,bjk->abik", g, g)
m0m1 = np.reshape(prod, (-1, chi, chi))
m1m0 = np.reshape(prod.transpose((1, 0, 2, 3)), (-1, chi, chi))
all_coms = m0m1 - m1m0

Check warning on line 62 in pennylane/labs/lie/structure_constants.py

View check run for this annotation

Codecov / codecov/patch

pennylane/labs/lie/structure_constants.py#L59-L62

Added lines #L59 - L62 were not covered by tests

# project commutators on basis of g
# vectorized computation to obtain coefficients in decomposition:
# v = ∑ (tr(v @ e_j) / ||e_j||^2) * e_j
gram_inv = np.linalg.pinv(np.tensordot(g, g, axes=[[1, 2], [2, 1]])).real
Qottmann marked this conversation as resolved.
Show resolved Hide resolved
adj = gram_inv @ np.tensordot(g, all_coms, axes=[[1, 2], [-1, -2]]).imag
adj = -np.reshape(adj, (dimg, dimg, dimg))
return adj

Check warning on line 70 in pennylane/labs/lie/structure_constants.py

View check run for this annotation

Codecov / codecov/patch

pennylane/labs/lie/structure_constants.py#L67-L70

Added lines #L67 - L70 were not covered by tests
80 changes: 80 additions & 0 deletions pennylane/labs/tests/lie/test_structure_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2024 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for pennylane/labs/lie/structure_constants.py functionality"""
# pylint: disable=no-self-use

import numpy as np
import pytest

import pennylane as qml
from pennylane.labs.lie import structure_constants_dense
from pennylane.pauli import PauliSentence, PauliWord

## Construct some example DLAs
# TFIM
gens = [PauliSentence({PauliWord({i: "X", i + 1: "X"}): 1.0}) for i in range(2)]
gens += [PauliSentence({PauliWord({i: "Z"}): 1.0}) for i in range(3)]
Ising3 = qml.pauli.lie_closure(gens, pauli=True)

# XXZ-type DLA, i.e. with true PauliSentences
gens2 = [
PauliSentence(
{
PauliWord({i: "X", i + 1: "X"}): 1.0,
PauliWord({i: "Y", i + 1: "Y"}): 1.0,
}
)
for i in range(2)
]
gens2 += [PauliSentence({PauliWord({i: "Z"}): 1.0}) for i in range(3)]
XXZ3 = qml.pauli.lie_closure(gens2, pauli=True)


class TestAdjointRepr:
"""Tests for structure_constants"""

def test_structure_constants_dim(self):
"""Test the dimension of the adjoint repr"""
d = len(Ising3)
Ising3_dense = np.array([qml.matrix(op, wire_order=range(3)) for op in Ising3])
adjoint = structure_constants_dense(Ising3_dense)
assert adjoint.shape == (d, d, d)
assert adjoint.dtype == float

@pytest.mark.parametrize("dla", [Ising3, XXZ3])
def test_structure_constants_elements(self, dla):
r"""Test relation :math:`[i G_\alpha, i G_\beta] = \sum_{\gamma = 0}^{\mathfrak{d}-1} f^\gamma_{\alpha, \beta} iG_\gamma`."""

d = len(dla)
dla_dense = np.array([qml.matrix(op, wire_order=range(3)) for op in dla])
ad_rep = structure_constants_dense(dla_dense)
for i in range(d):
for j in range(d):

comm_res = 1j * dla[i].commutator(dla[j])
res = sum(
np.array(c, dtype=complex) * dla[gamma]
for gamma, c in enumerate(ad_rep[:, i, j])
)
res.simplify()
assert comm_res == res

@pytest.mark.parametrize("dla", [Ising3, XXZ3])
def test_use_operators(self, dla):
"""Test that operators can be passed and lead to the same result"""
ad_rep_true = qml.pauli.dla.structure_constants(dla)

ops = np.array([qml.matrix(op.operation(), wire_order=range(3)) for op in dla])
ad_rep = structure_constants_dense(ops)
assert qml.math.allclose(ad_rep, ad_rep_true)
Loading