Skip to content

Commit

Permalink
Remove using * for matrix and tensor products of PauliWord and PauliS…
Browse files Browse the repository at this point in the history
…entence (#5341)

**Context:**
Complete the deprecation cycle for using * for matrix and tensor
products of `PauliWord` and `PauliSentence`

**Description of the Change:**
Using * for matrix and tensor products of `PauliWord` and
`PauliSentence` will now raise an error.

**Related Shortcut Issues:**
[sc-58304]
  • Loading branch information
astralcai authored Mar 11, 2024
1 parent 097e4ff commit 844fcf8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 95 deletions.
12 changes: 6 additions & 6 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ Pending deprecations
- Deprecated in v0.34
- Will be removed in v0.36

* ``PauliWord`` and ``PauliSentence`` no longer use ``*`` for matrix and tensor products,
but instead use ``@`` to conform with the PennyLane convention.

- Deprecated in v0.35
- Will be removed in v0.36

* ``op.ops`` and ``op.coeffs`` will be deprecated in the future. Use ``op.terms()`` instead.

- Added and deprecated for ``Sum`` and ``Prod`` instances in v0.35

Completed deprecation cycles
----------------------------

* ``PauliWord`` and ``PauliSentence`` no longer use ``*`` for matrix and tensor products,
but instead use ``@`` to conform with the PennyLane convention.

- Deprecated in v0.35
- Removed in v0.36

* The private functions ``_pauli_mult``, ``_binary_matrix`` and ``_get_pauli_map`` from the
``pauli`` module have been removed, as they are no longer used anywhere and the same
functionality can be achieved using newer features in the ``pauli`` module.
Expand Down
2 changes: 2 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@
* The contents of ``qml.interfaces`` is moved inside ``qml.workflow``. The old import path no longer exists.
[(#5329)](https://github.com/PennyLaneAI/pennylane/pull/5329)

* Attempting to multiply ``PauliWord`` and ``PauliSentence`` with ``*`` will raise an error. Instead, use ``@`` to conform with the PennyLane convention.

<h3>Deprecations 👋</h3>

* ``qml.load`` is deprecated. Instead, please use the functions outlined in the *Importing workflows* quickstart guide, such as ``qml.from_qiskit``.
Expand Down
18 changes: 0 additions & 18 deletions pennylane/pauli/pauli_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
"""The Pauli arithmetic abstract reduced representation classes"""
# pylint:disable=protected-access
import warnings
from copy import copy
from functools import reduce, lru_cache
from typing import Iterable
Expand Down Expand Up @@ -272,16 +271,6 @@ def __mul__(self, other):
Returns:
PauliSentence
"""
if isinstance(other, PauliWord):
# this is legacy support and will be removed after a deprecation cycle
warnings.warn(
"Matrix/Tensor multiplication using the * operator on PauliWords and PauliSentences"
"is deprecated, use @ instead. Note also that moving forward the product between two"
"PauliWords will return a PauliSentence({new_word: ceoff}) instead of a tuple (coeff, new_word)."
"The latter can still be achieved via pw1._matmul(pw2) for lightweight processing",
qml.PennyLaneDeprecationWarning,
)
return self._matmul(other)

if isinstance(other, TensorLike):
if not qml.math.ndim(other) == 0:
Expand Down Expand Up @@ -719,13 +708,6 @@ def __mul__(self, other):
Returns:
PauliSentence
"""
if isinstance(other, PauliSentence):
# this is legacy support and will be removed after a deprecation cycle
warnings.warn(
"Matrix/Tensor multiplication using the * operator on PauliWords and PauliSentences is deprecated, use @ instead.",
qml.PennyLaneDeprecationWarning,
)
return self @ other

if isinstance(other, TensorLike):
if not qml.math.ndim(other) == 0:
Expand Down
93 changes: 22 additions & 71 deletions tests/pauli/test_pauli_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,28 @@
Z0 = PauliWord({0: "Z"})


def test_pw_pw_multiplication_non_commutativity():
"""Test that pauli word matrix multiplication is non-commutative and returns correct result"""

res1 = X0 @ Y0
res2 = Y0 @ X0
assert res1 == 1j * Z0
assert res2 == -1j * Z0


def test_ps_ps_multiplication_non_commutativity():
"""Test that pauli sentence matrix multiplication is non-commutative and returns correct result"""

pauliX = PauliSentence({PauliWord({0: "X"}): 1.0})
pauliY = PauliSentence({PauliWord({0: "Y"}): 1.0})
pauliZ = PauliSentence({PauliWord({0: "Z"}): 1j})

res1 = pauliX @ pauliY
res2 = pauliY @ pauliX
assert res1 == pauliZ
assert res2 == -1 * pauliZ


def _pauli_to_op(p):
"""convert PauliWord or PauliSentence to Operator"""
return p.operation()
Expand All @@ -73,77 +95,6 @@ def _id(p):
return p


class TestDeprecations:
def test_deprecation_warning_PauliWord(
self,
):
"""Test that a PennyLaneDeprecationWarning is raised when using * for matrix multiplication of two PauliWords"""
pauli1 = PauliWord({0: "X"})
pauli2 = PauliWord({0: "Y"})

with pytest.warns(
qml.PennyLaneDeprecationWarning, match="Matrix/Tensor multiplication using"
):
_ = pauli1 * pauli2

def test_deprecation_warning_PauliSentence(
self,
):
"""Test that a PennyLaneDeprecationWarning is raised when using * for matrix multiplication of two PauliSentences"""
pauli1 = PauliSentence({PauliWord({0: "X"}): 1})
pauli2 = PauliSentence({PauliWord({0: "Y"}): 1})

with pytest.warns(
qml.PennyLaneDeprecationWarning, match="Matrix/Tensor multiplication using"
):
_ = pauli1 * pauli2


@pytest.mark.parametrize("pauli1", words)
@pytest.mark.parametrize("pauli2", words)
def test_legacy_multiplication_pwords(pauli1, pauli2):
"""Test the legacy behavior for using the star operator for matrix multiplication of pauli words"""
with pytest.warns(qml.PennyLaneDeprecationWarning, match="Matrix/Tensor multiplication using"):
res1, coeff1 = pauli1 * pauli2
res2, coeff2 = pauli1._matmul(pauli2)
assert res1 == res2
assert coeff1 == coeff2


@pytest.mark.parametrize("pauli1", sentences)
@pytest.mark.parametrize("pauli2", sentences)
def test_legacy_multiplication_psentences(pauli1, pauli2):
"""Test the legacy behavior for using the star operator for matrix multiplication of pauli sentences"""
with pytest.warns(qml.PennyLaneDeprecationWarning, match="Matrix/Tensor multiplication using"):
assert pauli1 * pauli2 == pauli1 @ pauli2


def test_legacy_pw_pw_multiplication_non_commutativity():
"""Test that legacy pauli word matrix multiplication is non-commutative and returns correct result"""
pauliX = PauliWord({0: "X"})
pauliY = PauliWord({0: "Y"})
pauliZ = PauliWord({0: "Z"})

with pytest.warns(qml.PennyLaneDeprecationWarning, match="Matrix/Tensor multiplication using"):
res1 = pauliX * pauliY
res2 = pauliY * pauliX
assert res1 == (pauliZ, 1j)
assert res2 == (pauliZ, -1j)


def test_legacy_ps_ps_multiplication_non_commutativity():
"""Test that legacy pauli sentence matrix multiplication is non-commutative and returns correct result"""
pauliX = PauliSentence({PauliWord({0: "X"}): 1.0})
pauliY = PauliSentence({PauliWord({0: "Y"}): 1.0})
pauliZ = PauliSentence({PauliWord({0: "Z"}): 1j})

with pytest.warns(qml.PennyLaneDeprecationWarning, match="Matrix/Tensor multiplication using"):
res1 = pauliX * pauliY
res2 = pauliY * pauliX
assert res1 == pauliZ
assert res2 == -1 * pauliZ


class TestPauliWord:
def test_identity_removed_on_init(self):
"""Test that identities are removed on init."""
Expand Down

0 comments on commit 844fcf8

Please sign in to comment.