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

Legacy operator arithmetic deprecation #6287

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

mudit2812
Copy link
Contributor

@mudit2812 mudit2812 commented Sep 19, 2024

Context:
Legacy op math is being deprecated.

Description of the Change:

  • qml.ops.Hamiltonian is deprecated.

  • qml.operation.Tensor is deprecated.

  • qml.operation.disable_new_opmath and qml.operation.enable_new_opmath (as well as the context managers) are deprecated.

  • qml.operation.convert_to_legacy_H is deprecated.

  • PauliWord.hamiltonian and PauliSentence.hamiltonian is deprecated.

  • qml.pauli.simplify is deprecated.

  • Removed use_legacy_opmath and use_new_opmath fixtures. Instead, we use either legacy_opmath_only, new_opmath_only, or no fixture, as appropriate for the tests.

  • Added warning suppression for all the above deprecation warnings to pytest.ini. Additionally, I also added the same warning suppressions to conftest.py for the case when --disable-opmath=True, since the warning filters in pytest.ini only suppress warnings that come from inside tests, not ones that come from outside the tests, such as when deprecated code is used for creating parameters for a test.

Benefits:

Possible Drawbacks:
In some instances, there will be a LOT of deprecation warnings at the same time. The most impactful example of this if if using qml.qchem.molecular_hamiltonian, qml.qchem.qubit_observable. These using simplify and PauliSentence.hamiltonian with legacy op math, so there will be up to 4 warnings at once when using these.

Additionally, qml.qchem.tapering.symmetry_generators and qml.qchem.tapering.clifford use PauliSentence.hamiltonian when legacy op math is enabled, so there will be up to 3 warnings at once when using these.

Related GitHub Issues:

I will run the test suite with disable_new_opmath set to True once before merging to confirm that legacy op math tests do not fail.

[sc-71940] [sc-66727] [sc-66724]

@mudit2812 mudit2812 marked this pull request as ready for review September 19, 2024 21:11
Copy link

codecov bot commented Sep 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.39%. Comparing base (1a143d2) to head (1553356).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6287      +/-   ##
==========================================
- Coverage   99.70%   99.39%   -0.32%     
==========================================
  Files         444      444              
  Lines       42236    42244       +8     
==========================================
- Hits        42113    41989     -124     
- Misses        123      255     +132     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 123 to 126
* Legacy operator arithmetic has been deprecated. This includes `qml.ops.Hamiltonian`, `qml.operation.enable_new_opmath`,
`qml.operation.disable_new_opmath`, and `qml.operation.convert_to_legacy_H`.
[(#6287)](https://github.com/PennyLaneAI/pennylane/pull/6287)

Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not suggesting doing that in this PR, but are we going to remove the Tensor class without deprecating it first? If so, then I guess it can cause quite a few issues

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the logic behind not deprecating Tensor is that it is a private class. We've always exposed qml.Hamiltonian in a very public way, but Tensor has always been hidden inside the operation module, and the recommended way to create a Tensor has been using the @ operator. That being said, I see your point, and I'd feel wrong merging this PR before getting more input from product about it. cc @DSGuala

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After more discussions, Tensor has been deprecated.

@lillian542 lillian542 self-requested a review September 20, 2024 19:50
@PietropaoloFrisoni
Copy link
Contributor

Should we also replace use_legacy_opmath with legacy_opmath_only in this test?

Copy link
Contributor

@astralcai astralcai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot more than can be simplified around our testing with legacy opmath. Although I'm ok to approve if we're under pressure of time to merge this by today, since we're removing legacy opmath completely in a few months anyway?

doc/development/deprecations.rst Show resolved Hide resolved
tests/fermi/test_bravyi_kitaev.py Outdated Show resolved Hide resolved
tests/devices/test_preprocess.py Outdated Show resolved Hide resolved
tests/fermi/test_bravyi_kitaev.py Outdated Show resolved Hide resolved


@pytest.mark.usefixtures("legacy_opmath_only")
@pytest.mark.usefixtures("use_legacy_and_new_opmath")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the use_legacy_and_new_opmath fixture can be removed because everything is going to be tested with both legacy and new opmath unless marked new_opmath_only or legacy_opmath_only. The use_new_opmath fixture can also be removed because it doesn't really do anything.

Copy link
Contributor Author

@mudit2812 mudit2812 Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still see merit in keeping the use_legacy_and_new_opmath fixture for things that we want to keep testing in regular CI with legacy op math. I agree we can remove the use_new_opmath fixture and should replace tests that use it with new_opmath_only where appropriate, or not use any fixture. That way, their behaviour in regular CI won't change, but we won't run unnecessary tests in legacy op math CI.

Copy link
Contributor

@lillian542 lillian542 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get through that much of it but here are a few comment for now :)

doc/development/deprecations.rst Outdated Show resolved Hide resolved
Comment on lines +42 to +43
:func:`~pennylane.operation.convert_to_opmath` and :func:`~pennylane.operation.convert_to_H` to convert legacy operators
into those using the new operator arithmetic.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really suggest this? Since I guess these are also deprecated, so we just want them to enable new_opmath and stop explicitly making Tensors and Hamiltonians.

Copy link
Contributor Author

@mudit2812 mudit2812 Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not deprecate these functions because they're the only way users can easily convert legacy ops into new ops without having to do something like

new_op = qml.Hamiltonian(old_op.coeffs, old_op.ops)

Args:
op (Operator): The operator instance to convert.

Returns:
Operator: The operator as a :class:`~pennylane.Hamiltonian` instance
"""
with disable_new_opmath_cm():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want a deprecation warning in this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because a deprecation warning will be raised when a legacy Hamiltonian is created.

@mudit2812
Copy link
Contributor Author

Looks like running some tests only when legacy op math is active has caused some humps with codecov. Not sure if it should be addressed or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants