Skip to content

Commit

Permalink
Remove old opmath (#6548)
Browse files Browse the repository at this point in the history
**Context:**
It's time to fully remove the legacy operator arithmetic (`Tensor` and
`qml.ops.Hamiltonian`, and all of the structure surrounding them).

**Description of the Change:**

- [x] remove `qml.ops.Hamiltonian`
- [x]  remove `Tensor`
- [x]  remove `disable_` and `enable_new_opmath`
- [x]  remove `operation.convert_to_legacy_H`
- [x]  remove `qml.pauli.simplify`
- [x] remove `PauliWord.hamiltonian` and `PauliSentence.hamiltonian`,
and any other functionality in the pauli module that supported legacy
opmath
- [x] remove any if/else handling in place to deal with legacy opmath
through the code base
- [x] remove `legacy_opmath_only`, `new_opmath_only` and
`use_legacy_and_new_opmath` test fixtures, and all irrelevant tests
- [x] remove the warning suppressions added in
[PR6287](#6287)
- [x]  remove the legacy test fixture (--disable-opmath)


[sc-77523]

---------

Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: Astral Cai <astral.cai@xanadu.ai>
  • Loading branch information
3 people authored Nov 17, 2024
1 parent 080e5ed commit 97c852b
Show file tree
Hide file tree
Showing 166 changed files with 736 additions and 9,669 deletions.
29 changes: 12 additions & 17 deletions .github/workflows/interface-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ on:
required: false
type: string
default: ''
disable_new_opmath:
description: Whether to disable the new op_math or not when running the tests
required: false
type: string
default: "False"
additional_python_packages:
description: Additional Python packages to install separated by a space
required: false
Expand Down Expand Up @@ -246,7 +241,7 @@ jobs:
${{ needs.default-dependency-versions.outputs.pytorch-version }}
${{ inputs.additional_python_packages }}
additional_pip_packages_post: ${{ needs.default-dependency-versions.outputs.pennylane-lightning-latest }}
pytest_additional_args: --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_markers: torch and not qcut and not finite-diff and not param-shift
requirements_file: ${{ github.event_name == 'schedule' && strategy.job-index == 0 && 'torch.txt' || '' }}
Expand Down Expand Up @@ -282,7 +277,7 @@ jobs:
additional_pip_packages: ${{ inputs.additional_python_packages }}
additional_pip_packages_post: ${{ needs.default-dependency-versions.outputs.pennylane-lightning-latest }}
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_additional_args: --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_markers: autograd and not qcut and not finite-diff and not param-shift


Expand Down Expand Up @@ -320,7 +315,7 @@ jobs:
additional_pip_packages_post: ${{ needs.default-dependency-versions.outputs.pennylane-lightning-latest }}
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_markers: tf and not qcut and not finite-diff and not param-shift
pytest_additional_args: --splits 3 --group ${{ matrix.group }} --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: --splits 3 --group ${{ matrix.group }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_durations_file_path: '.github/durations/tf_tests_durations.json'
requirements_file: ${{ github.event_name == 'schedule' && strategy.job-index == 0 && 'tf.txt' || '' }}

Expand Down Expand Up @@ -358,7 +353,7 @@ jobs:
additional_pip_packages_post: ${{ needs.default-dependency-versions.outputs.pennylane-lightning-latest }}
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_markers: jax and not qcut and not finite-diff and not param-shift
pytest_additional_args: --dist=loadscope --splits 4 --group ${{ matrix.group }} --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: --dist=loadscope --splits 4 --group ${{ matrix.group }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_durations_file_path: '.github/durations/jax_tests_durations.json'
requirements_file: ${{ github.event_name == 'schedule' && strategy.job-index == 0 && 'jax.txt' || '' }}

Expand Down Expand Up @@ -395,7 +390,7 @@ jobs:
additional_pip_packages_post: ${{ needs.default-dependency-versions.outputs.pennylane-lightning-latest }}
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_markers: core and not qcut and not finite-diff and not param-shift
pytest_additional_args: --splits 6 --group ${{ matrix.group }} --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: --splits 6 --group ${{ matrix.group }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_durations_file_path: '.github/durations/core_tests_durations.json'
requirements_file: ${{ github.event_name == 'schedule' && strategy.job-index == 0 && 'core.txt' || '' }}

Expand Down Expand Up @@ -435,7 +430,7 @@ jobs:
additional_pip_packages_post: ${{ needs.default-dependency-versions.outputs.pennylane-lightning-latest }}
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_markers: all_interfaces
pytest_additional_args: --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
requirements_file: ${{ github.event_name == 'schedule' && strategy.job-index == 0 && 'all_interfaces.txt' || '' }}


Expand Down Expand Up @@ -468,7 +463,7 @@ jobs:
python_version: ${{ matrix.python-version }}
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_markers: external
pytest_additional_args: --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
additional_pip_packages: |
pyzx matplotlib stim quimb mitiq ply
git+https://github.com/PennyLaneAI/pennylane-qiskit.git@master
Expand Down Expand Up @@ -515,7 +510,7 @@ jobs:
python_version: ${{ matrix.python-version }}
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_markers: qcut
pytest_additional_args: --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
additional_pip_packages: |
kahypar==1.1.7
opt_einsum
Expand Down Expand Up @@ -557,7 +552,7 @@ jobs:
additional_pip_packages_post: ${{ needs.default-dependency-versions.outputs.pennylane-lightning-latest }}
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_markers: qchem
pytest_additional_args: --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
additional_pip_packages: |
openfermionpyscf basis-set-exchange
${{ inputs.additional_python_packages }}
Expand Down Expand Up @@ -592,7 +587,7 @@ jobs:
branch: ${{ inputs.branch }}
coverage_artifact_name: gradients-${{ matrix.config.suite }}-coverage
python_version: ${{ matrix.python-version }}
pytest_additional_args: --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
additional_pip_packages: |
${{ needs.default-dependency-versions.outputs.jax-version }}
${{ needs.default-dependency-versions.outputs.tensorflow-version }}
Expand Down Expand Up @@ -632,7 +627,7 @@ jobs:
python_version: ${{ matrix.python-version }}
additional_pip_packages_post: ${{ needs.default-dependency-versions.outputs.pennylane-lightning-latest }}
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_additional_args: --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_markers: data
additional_pip_packages: |
h5py
Expand Down Expand Up @@ -681,7 +676,7 @@ jobs:
additional_pip_packages_post: ${{ needs.default-dependency-versions.outputs.pennylane-lightning-latest }}
pytest_test_directory: pennylane/devices/tests
pytest_coverage_flags: ${{ inputs.pytest_coverage_flags }}
pytest_additional_args: --device=${{ matrix.config.device }} --shots=${{ matrix.config.shots }} --disable-opmath=${{ inputs.disable_new_opmath }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}
pytest_additional_args: --device=${{ matrix.config.device }} --shots=${{ matrix.config.shots }} -W ${{ inputs.python_warning_level }} ${{ inputs.python_warning_level != 'default' && '--continue-on-collection-errors' || '' }}


upload-to-codecov:
Expand Down
16 changes: 0 additions & 16 deletions .github/workflows/legacy_op_math.yml

This file was deleted.

2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@
# built documents.

import pennylane
pennylane.Hamiltonian = pennylane.ops.op_math.linear_combination.Hamiltonian

pennylane.Hamiltonian = pennylane.ops.Hamiltonian

# The full version, including alpha/beta/rc tags.
release = pennylane.__version__
Expand Down
70 changes: 32 additions & 38 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,58 +44,47 @@ Pending deprecations
- Deprecated in v0.39
- Will be removed in v0.40

New operator arithmetic deprecations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In PennyLane v0.39, the legacy operator arithmetic system has been deprecated. Check out the :ref:`Updated operators <new_opmath>` page
for details on how to port your legacy code to the new system. The old system is still accessible via :func:`~.disable_new_opmath`, though
it is not recommended, as the old system is deprecated and will be removed in the v0.40 release. The following functionality will explicitly
raise a deprecation warning when used:

* In PennyLane v0.39, legacy operator arithmetic has been deprecated. This includes :func:`~pennylane.operation.enable_new_opmath`,
:func:`~pennylane.operation.disable_new_opmath`, :class:`~pennylane.ops.Hamiltonian`, and :class:`~pennylane.operation.Tensor`. Note
that when new operator arithmetic is enabled, ``qml.Hamiltonian`` will continue to dispatch to :class:`~pennylane.ops.LinearCombination`;
this behaviour is not deprecated.

- Deprecated in v0.39
- Will be removed in v0.40

* :meth:`~pennylane.pauli.PauliSentence.hamiltonian` and :meth:`~pennylane.pauli.PauliWord.hamiltonian` are deprecated. Instead, please use
:meth:`~pennylane.pauli.PauliSentence.operation` and :meth:`~pennylane.pauli.PauliWord.operation` respectively.

- Deprecated in v0.39
- Will be removed in v0.40

* :func:`pennylane.pauli.simplify` is deprecated. Instead, please use :func:`pennylane.simplify` or :meth:`~pennylane.operation.Operator.simplify`.

- Deprecated in v0.39
- Will be removed in v0.40

* ``op.ops`` and ``op.coeffs`` will be deprecated in the future. Use
* ``op.ops`` and ``op.coeffs`` for ``Sum`` and ``Prod`` will be removed in the future. Use
:meth:`~.Operator.terms` instead.

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

* Accessing terms of a tensor product (e.g., ``op = X(0) @ X(1)``) via ``op.obs`` is deprecated with new operator arithmetic.
A user should use :class:`op.operands <~.CompositeOp>` instead.

- Deprecated in v0.36


Other deprecations
~~~~~~~~~~~~~~~~~~

* PennyLane Lightning and Catalyst will no longer support ``manylinux2014`` (GLIBC 2.17) compatibile Linux operating systems, and will be migrated to ``manylinux_2_28`` (GLIBC 2.28). See `pypa/manylinux <https://github.com/pypa/manylinux>`_ for additional details.

- Last supported version of ``manylinux2014`` with v0.36
- Fully migrated to ``manylinux_2_28`` with v0.37

* ``MultiControlledX`` is the only controlled operation that still supports specifying control
values with a bit string. In the future, it will no longer accepts strings as control values.

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

Completed removal of legacy operator arithmetic
-----------------------------------------------

In PennyLane v0.40, the legacy operator arithmetic system has been removed, and is fully replaced by the new
operator arithmetic functionality that was introduced in v0.36. Check out the :ref:`Updated operators <new_opmath>` page
for details on how to port your legacy code to the new system. The following functionality has been removed:

* In PennyLane v0.40, legacy operator arithmetic has been removed. This includes :func:`~pennylane.operation.enable_new_opmath`,
:func:`~pennylane.operation.disable_new_opmath`, :class:`~pennylane.ops.Hamiltonian`, and :class:`~pennylane.operation.Tensor`. Note
that ``qml.Hamiltonian`` will continue to dispatch to :class:`~pennylane.ops.LinearCombination`.

- Deprecated in v0.39
- Removed in v0.40

* :meth:`~pennylane.pauli.PauliSentence.hamiltonian` and :meth:`~pennylane.pauli.PauliWord.hamiltonian` has been removed. Instead, please use
:meth:`~pennylane.pauli.PauliSentence.operation` and :meth:`~pennylane.pauli.PauliWord.operation` respectively.

- Deprecated in v0.39
- Removed in v0.40

* :func:`pennylane.pauli.simplify` has been removed. Instead, please use :func:`pennylane.simplify` or :meth:`~pennylane.operation.Operator.simplify`.

- Deprecated in v0.39
- Removed in v0.40

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

Expand Down Expand Up @@ -164,6 +153,11 @@ Completed deprecation cycles
- Deprecated in v0.39
- Removed in v0.40

* PennyLane Lightning and Catalyst will no longer support ``manylinux2014`` (GLIBC 2.17) compatibile Linux operating systems, and will be migrated to ``manylinux_2_28`` (GLIBC 2.28). See `pypa/manylinux <https://github.com/pypa/manylinux>`_ for additional details.

- Last supported version of ``manylinux2014`` with v0.36
- Fully migrated to ``manylinux_2_28`` with v0.37

* The ``simplify`` argument in ``qml.Hamiltonian`` and ``qml.ops.LinearCombination`` has been removed.
Instead, ``qml.simplify()`` can be called on the constructed operator.

Expand Down
6 changes: 6 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@

<h3>Breaking changes 💔</h3>

* Legacy operator arithmetic has been removed. This includes `qml.ops.Hamiltonian`, `qml.operation.Tensor`,
`qml.operation.enable_new_opmath`, `qml.operation.disable_new_opmath`, and `qml.operation.convert_to_legacy_H`.
Note that `qml.Hamiltonian` will continue to dispatch to `qml.ops.LinearCombination`. For more information,
check out the [updated operator troubleshooting page](https://docs.pennylane.ai/en/stable/news/new_opmath.html).
[(#6548)](https://github.com/PennyLaneAI/pennylane/pull/6548)

* The developer-facing `qml.utils` module has been removed. Specifically, the
following 4 sets of functions have been either moved or removed[(#6588)](https://github.com/PennyLaneAI/pennylane/pull/6588):

Expand Down
8 changes: 1 addition & 7 deletions pennylane/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
)
from pennylane.ops import *
from pennylane.ops import adjoint, ctrl, cond, exp, sum, pow, prod, s_prod
from pennylane.ops import LinearCombination as Hamiltonian
from pennylane.templates import layer
from pennylane.templates.embeddings import *
from pennylane.templates.layers import *
Expand Down Expand Up @@ -165,14 +166,7 @@ class PennyLaneDeprecationWarning(UserWarning):
"""Warning raised when a PennyLane feature is being deprecated."""


del globals()["Hamiltonian"]


def __getattr__(name):
if name == "Hamiltonian":
if pennylane.operation.active_new_opmath():
return pennylane.ops.LinearCombination
return pennylane.ops.Hamiltonian

if name == "plugin_devices":
return pennylane.devices.device_constructor.plugin_devices
Expand Down
18 changes: 3 additions & 15 deletions pennylane/data/attributes/operator/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import pennylane as qml
from pennylane.data.base.attribute import DatasetAttribute
from pennylane.data.base.hdf5 import HDF5Group, h5py
from pennylane.operation import Operator, Tensor
from pennylane.operation import Operator

from ._wires import wires_to_json

Expand All @@ -44,8 +44,6 @@ class DatasetOperator(Generic[Op], DatasetAttribute[HDF5Group, Op, Op]):
arguments.
- Hyperparameters are not used or are automatically derived by ``__init__()``.
Almost all operators meet these conditions. This type also supports serializing the
``Hamiltonian`` and ``Tensor`` operators.
"""

type_id = "operator"
Expand All @@ -56,13 +54,9 @@ def supported_ops(cls) -> frozenset[Type[Operator]]:
"""Set of supported operators."""
return frozenset(
(
# pennylane/operation/Tensor
Tensor,
# pennylane/ops/qubit/arithmetic_qml.py
qml.QubitCarry,
qml.QubitSum,
# pennylane/ops/qubit/hamiltonian.py
qml.ops.Hamiltonian,
# pennylane/ops/op_math/linear_combination.py
qml.ops.LinearCombination,
# pennylane/ops/op_math - prod.py, s_prod.py, sum.py
Expand Down Expand Up @@ -219,10 +213,7 @@ def _ops_to_hdf5(
f"Serialization of operator type '{type(op).__name__}' is not supported."
)

if isinstance(op, Tensor):
self._ops_to_hdf5(bind, op_key, op.obs)
op_wire_labels.append("null")
elif isinstance(op, (qml.ops.Hamiltonian, qml.ops.LinearCombination)):
if isinstance(op, qml.ops.LinearCombination):
coeffs, ops = op.terms()
ham_grp = self._ops_to_hdf5(bind, op_key, ops)
ham_grp["hamiltonian_coeffs"] = coeffs
Expand Down Expand Up @@ -260,10 +251,7 @@ def _hdf5_to_ops(self, bind: HDF5Group) -> list[Operator]:
op_key = f"op_{i}"

op_cls = self._supported_ops_dict()[op_class_name]
if op_cls is Tensor:
prod_op = qml.ops.Prod if qml.operation.active_new_opmath() else Tensor
ops.append(prod_op(*self._hdf5_to_ops(bind[op_key])))
elif op_cls in (qml.ops.Hamiltonian, qml.ops.LinearCombination):
if op_cls is qml.ops.LinearCombination:
ops.append(
qml.Hamiltonian(
coeffs=list(bind[op_key]["hamiltonian_coeffs"]),
Expand Down
Loading

0 comments on commit 97c852b

Please sign in to comment.