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

[new opmath] New opmath starting page #5483

Merged
merged 66 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
41d5e84
New opmath intro mage
Qottmann Apr 9, 2024
55fa963
[ci skip]
Qottmann Apr 9, 2024
e15ec1b
add file
Qottmann Apr 9, 2024
8519cbf
[ci skip]
Qottmann Apr 9, 2024
0f7a39b
format table
Qottmann Apr 9, 2024
3073171
[ci skip]
Qottmann Apr 9, 2024
8e894e9
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 10, 2024
8ab2efd
encourage to post straight away
Qottmann Apr 10, 2024
4649feb
make comment about not using explicit disable/enable in tests a note
Qottmann Apr 10, 2024
777363a
remove unrelated remnant from copypaste
Qottmann Apr 10, 2024
6cacec9
[ci skip]
Qottmann Apr 10, 2024
4bb80e7
expand examples for new users
Qottmann Apr 10, 2024
160df13
expand on developer questions and testing
Qottmann Apr 10, 2024
dac9a9b
[ci skip]
Qottmann Apr 10, 2024
017e893
typos
Qottmann Apr 10, 2024
025643b
[ci skip]
Qottmann Apr 10, 2024
439a30a
Update doc/introduction/new_opmath.rst
Qottmann Apr 11, 2024
d9c515f
rename page
Qottmann Apr 11, 2024
0fcd587
Merge branch 'newopmathintro' of https://github.com/PennyLaneAI/penny…
Qottmann Apr 11, 2024
c028ac6
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 11, 2024
8361b12
[ci skip]
Qottmann Apr 11, 2024
d535c97
Quick summary update more user facing
Qottmann Apr 11, 2024
56834ec
[ci skip]
Qottmann Apr 11, 2024
29a8c27
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 12, 2024
596b64f
code review
Qottmann Apr 12, 2024
b4d4abb
[ci skip]
Qottmann Apr 12, 2024
13b84ed
table formatting
Qottmann Apr 12, 2024
449796d
[ci skip]
Qottmann Apr 12, 2024
ae23a1f
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 12, 2024
067162f
[ci skip]
Qottmann Apr 12, 2024
ac02399
remove unrelated change
Qottmann Apr 12, 2024
121459f
[ci skip]
Qottmann Apr 12, 2024
ae8f42c
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 15, 2024
157d44b
typo
Qottmann Apr 15, 2024
c4d8405
typo
Qottmann Apr 15, 2024
0469b9c
[ci skip]
Qottmann Apr 15, 2024
1203508
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 18, 2024
12ea204
fix hyperlink
Qottmann Apr 18, 2024
01634ac
make bullet
Qottmann Apr 18, 2024
74057e2
remove last point, add new first one
Qottmann Apr 18, 2024
cb9c8c8
Apply suggestions from code review
Qottmann Apr 18, 2024
1b5c161
black formatting
Qottmann Apr 18, 2024
286e07f
[ci skip]
Qottmann Apr 18, 2024
3566e27
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 18, 2024
0e01fad
fix bullet points
Qottmann Apr 18, 2024
b7a6d11
[ci skip]
Qottmann Apr 18, 2024
0da2fce
admonition tip
Qottmann Apr 18, 2024
f0c5954
[ci skip]
Qottmann Apr 18, 2024
42e4f7d
links
Qottmann Apr 18, 2024
0b45a94
typo
Qottmann Apr 18, 2024
46f059f
[ci skip]
Qottmann Apr 18, 2024
1722183
Apply suggestions from code review
Qottmann Apr 22, 2024
e5b9546
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 22, 2024
51e01e3
[ci skip]
Qottmann Apr 22, 2024
a34262d
expand on pauli_rep
Qottmann Apr 22, 2024
b52b3a8
[ci skip]
Qottmann Apr 22, 2024
bbebd54
update cross-references and sub-headlines in technical details
Qottmann Apr 22, 2024
d179b54
[ci skip]
Qottmann Apr 22, 2024
3005cca
sub-headlines
Qottmann Apr 22, 2024
a29b26c
links
Qottmann Apr 22, 2024
9c6d334
[ci skip]
Qottmann Apr 22, 2024
6438a35
[ci skip]
Qottmann Apr 22, 2024
bd7f89f
links
Qottmann Apr 22, 2024
5108e32
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
Qottmann Apr 22, 2024
d32e837
Merge branch 'master' into newopmathintro
Qottmann Apr 23, 2024
2a7858d
Merge branch 'master' into newopmathintro
trbromley Apr 23, 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 doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ PennyLane is **free** and **open source**, released under the Apache License, Ve
introduction/chemistry
introduction/data
introduction/returns
introduction/new_opmath
introduction/logging

.. toctree::
Expand Down
161 changes: 161 additions & 0 deletions doc/introduction/new_opmath.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
.. _new_opmath:

New opmath
==========
Qottmann marked this conversation as resolved.
Show resolved Hide resolved

Version ``0.36`` of PennyLane updated the default ``Operator`` subclasses, changing the operator arithmetic.
An end-user should not notice any (breaking) changes.
This pages should help developers with troubleshooting and guide in the process of legacy support while both systems are supported.

.. note::

If you are looking for a quick fix, jump to the :ref:`Troubleshooting` section!
Qottmann marked this conversation as resolved.
Show resolved Hide resolved

After visiting the :ref:`Troubleshooting` section, if you are still stuck then you can:

- Post on the PennyLane `discussion forum <https://discuss.pennylane.ai>`_.

- If you suspect that your issue is due to a bug in PennyLane itself, please open a
`bug report <https://github.com/PennyLaneAI/pennylane/issues/new?labels=bug+%3Abug%3A&template=bug_report.yml&title=[BUG]>`_
on the PennyLane GitHub page.

.. note::

The old return system has been completely removed from PennyLane as of version 0.33.0, along with the
``enable_return()`` and ``disable_return()`` toggle functions.
Qottmann marked this conversation as resolved.
Show resolved Hide resolved

Summary of the update
---------------------
Qottmann marked this conversation as resolved.
Show resolved Hide resolved

The opt-in feature ``qml.operation.enable_new_opmath()`` is now the default. You can still opt-out via
``qml.operation.disable_new_opmath()``.

The changes between the old and new system mainly concern Python operators ``+ - * / @``,
that now create the following ``Operator`` subclass instances.


+----------------------------------+----------------------+---------------------------+
Qottmann marked this conversation as resolved.
Show resolved Hide resolved
| | Legacy | New opmath |
+==================================+======================+===========================+
| tensor products | ``operation.Tensor`` | ``ops.Prod`` |
| ``X(0) @ X(1)`` | | |
+----------------------------------+----------------------+---------------------------+
| sums | ``ops.Hamiltonian`` | ``ops.Sum`` |
| ``X(0) + X(1)`` | | |
+----------------------------------+----------------------+---------------------------+
| scalar products | ``ops.Hamiltonian`` | ``ops.SProd`` |
| ``1.5 * X(1)`` | | |
+----------------------------------+----------------------+---------------------------+
| ``qml.dot(coeffs,ops)`` | ``ops.Sum`` | ``ops.Sum`` |
+----------------------------------+----------------------+---------------------------+
| ``qml.Hamiltonian(coeffs, ops)`` | ``ops.Hamiltonian`` | ``ops.LinearCombination`` |
+----------------------------------+----------------------+---------------------------+


The three main new opmath classes ``SProd``, ``Prod``, and ``Sum`` have already been around for a while.
E.g. ``qml.dot()`` has always returned a ``Sum`` instance.
The legacy classes :class:`~Tensor` and :class:`~Hamiltonian` will soon be deprecated.
:class:`~LinearCombination` offers the same API as :class:`~Hamiltonian` but works well with new opmath classes.

Depending on whether or not new opmath is active, ``qml.Hamiltonian`` will return either of the two classes.

>>> import pennylane as qml
>>> from pennylane import X
>>> qml.operation.active_new_opmath()
True
>>> H = qml.Hamiltonian([0.5, 0.5], [X(0), X(1)])
>>> type(H)
pennylane.ops.op_math.linear_combination.LinearCombination

>>> qml.operation.disable_new_opmath_()
>>> qml.operation.active_new_opmath()
False
>>> H = qml.Hamiltonian([0.5, 0.5], [X(0), X(1)])
>>> type(H)
pennylane.ops.qubit.hamiltonian.Hamiltonian


.. _Troubleshooting:

Troubleshooting
---------------

If you are a developer or power-user that explicitly uses ``qml.operation.Tensor`` or ``qml.ops.Hamiltonian``, you
may run into one of the following common issues.
Qottmann marked this conversation as resolved.
Show resolved Hide resolved

.. details::
:title: My old PennyLane script does not run anymore
:href: old-script-broken
Qottmann marked this conversation as resolved.
Show resolved Hide resolved

A quick-and-dirty fix for this issue is to deactivate new opmath at the beginning of the script via ``qml.operation.disable_new_opmath()``.
We recommend to do the following checks instead

* Check explicit use of the legacy :class:`~Tensor` class. If you find it in your script it can just be changed from ``Tensor(*terms)`` to ``qml.prod(*terms)`` with the same signature.

* Check explicit use of ``op.obs`` attribute, where ``op`` is some operator. This is how the terms of a tensor product is accessed in :class:`~Tensor` instances. Use ``op.operands`` instead.
Qottmann marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: python

# new opmath enabled (default)
op = X(0) @ X(1)
assert op.operands == (X(0), X(1))

with qml.operation.disable_new_opmath_cm():
# context manager that disables new opmath temporarilly
op = X(0) @ X(1)
assert op.obs == [X(0), X(1)]

* Check explicit use of ``qml.ops.Hamiltonian``. In that case, simply change to ``qml.Hamiltonian``.

>>> op = qml.ops.Hamiltonian([0.5, 0.5], [X(0) @ X(1), X(1) @ X(2)])
ValueError: Could not create circuits. Some or all observables are not valid.
>>> op = qml.Hamiltonian([0.5, 0.5], [X(0) @ X(1), X(1) @ X(2)])
>>> isinstance(op, qml.ops.LinearCombination)
True

* Check if you are explicitly enabling and disabling new opmath somewhere in your script. Mixing both systems is not supported.

If for some unexpected reason your script still breaks, please see the :ref:`I am unsure what to do <unsure>` section below.
Qottmann marked this conversation as resolved.
Show resolved Hide resolved

.. details::
:title: I want to contribute to PennyLane
:href: PL-developer

If you want to contribute a new feature to PennyLane or update an existing one, you likely also need to update the tests.
Please refrain from explicitly using ``qml.operation.disable_new_opmath()`` and ``qml.operation.enable_new_opmath()`` anywhere in tests and code as that globally
changes the status of new opmath and thereby can affect other parts of your code or other tests.
Qottmann marked this conversation as resolved.
Show resolved Hide resolved

Instead, please use the context managers ``qml.operation.disable_new_opmath_cm()`` and `qml.operation.enable_new_opmath_cm()``.

>>> with qml.operation.disable_new_opmath_cm():
... op = qml.Hamiltonian([0.5], [X(0) @ X(1)])
>>> assert isinstance(op, qml.ops.Hamiltonian)

Our continuous integration (CI) test suite is running all tests with the default of new opmath being enabled.
Qottmann marked this conversation as resolved.
Show resolved Hide resolved
We also periodically run the CI test suite with new opmath disabled, as we support both new and legacy systems for some limited time.
Qottmann marked this conversation as resolved.
Show resolved Hide resolved
In case a test needs to be adopted for either case, you can use the following fixtures.

* Use ``@pytest.mark.usefixtures("use_legacy_opmath")`` to test functionality that is explicitly only supported by legacy opmath, e.g. for backward compatibility.
Qottmann marked this conversation as resolved.
Show resolved Hide resolved

* Use ``@pytest.mark.usefixtures("use_new_opmath")`` to test functionality that `only` works with new opmath. That is because for the intermittent period
of supporting both systems, we periodically run the test suite with new opmath disabled.

* Use ``@pytest.mark.usefixtures("use_legacy_and_new_opmath")`` if you want to test support for both systems in one single test. You can use ``qml.operation.active_new_opmath``
inside the test to account for minor differences between both systems.

One sharp bit about testing is that ``pytest`` runs collection and test execution separately. That means that instances generated outside the test, e.g. for parametrization, have been created
Qottmann marked this conversation as resolved.
Show resolved Hide resolved
using the respective system. So you may need to also put that creation in the appropriate context manager.


.. details::
:title: I am unsure what to do
:href: unsure

Please carefully read through the options above. If you are still stuck, you can:

- Post on the PennyLane `discussion forum <https://discuss.pennylane.ai>`_. Please include
a complete block of code demonstrating your issue so that we can quickly troubleshoot.

- If you suspect that your issue is due to a bug in PennyLane itself, please open a
`bug report <https://github.com/PennyLaneAI/pennylane/issues/new?labels=bug+%3Abug%3A&template=bug_report.yml&title=[BUG]>`_
on the PennyLane GitHub page.