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

Deprecate QubitUnitary as a valid input to ControlledQubitUnitary #6840

Conversation

JerryChen97
Copy link
Contributor

@JerryChen97 JerryChen97 commented Jan 15, 2025

Context:
Providing a QubitUnitary as the base operator defeats the purpose entirely and is just a messier way of using qml.ctrl(qml.QubitUnitary(...), ...)

For richer context, check #6839 for better illustration; this PR should in principle be an add-on to it.

Note: this deprecation is related with a previous bug. Trace via attached story link to see full discussions.

Description of the Change:
__init__ and _unflatten: If get a QubitUnitary base, raise PLDW and then get matrix by base.matrix().

Added an overloading method _flatten. This is a fix to #6839.

Deleted two tests that restrictively replied on the support of QubitUnitary base.

Benefits:

Possible Drawbacks:

Related GitHub Issues:
[sc-80843]

Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@JerryChen97 JerryChen97 self-assigned this Jan 15, 2025
@JerryChen97 JerryChen97 added the deprecation 🗑️ Deprecating an API or user-facing feature/behaviour label Jan 15, 2025
@JerryChen97 JerryChen97 changed the base branch from master to Deprecate-`control_wires`-in-`ControlledQubitUnitary`-in-favour-of-`wires` January 21, 2025 18:37
…n-favour-of-`wires`' into Deprecate-`QubitUnitary`-as-a-valid-input-to-`ControlledQubitUnitary`
@JerryChen97 JerryChen97 marked this pull request as ready for review January 23, 2025 19:00

This comment was marked as outdated.

@JerryChen97
Copy link
Contributor Author

@albi3ro @astralcai Here's how I deprecate the QubitUnitary base input right now. I separate it from 6839 because it's much clearer if we review what changed for QubitUnitary input deprecation based on control_wires deprecation. And the plan is if we are happy with this then we merge to branch of 6839; that PR will be the final one to carry two deprecations into master branch.

@JerryChen97 JerryChen97 requested a review from astralcai January 27, 2025 18:30
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.

Don't forget to add entries to the changelog and the deprecations page.

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.

I just realized something: for every other controlled operator, its base property is an operator. For example:

>>> op = qml.CRX(0.5, [0, 1])
>>> op.base
RX(0.5, wires=[1])

Should we maintain the same behaviour for ControlledQubitUnitary? @albi3ro

EDIT:

To be more consistent with how every other custom controlled operation works, the ControlledQubitUnitary should probably create a QubitUnitary internally and set its base to that.

EDIT:

nvm I'm dumb.

JerryChen97 and others added 6 commits January 28, 2025 15:31
…tary`-as-a-valid-input-to-`ControlledQubitUnitary`' into Deprecate-`QubitUnitary`-as-a-valid-input-to-`ControlledQubitUnitary`
…n-favour-of-`wires`' into Deprecate-`QubitUnitary`-as-a-valid-input-to-`ControlledQubitUnitary`
@JerryChen97 JerryChen97 requested a review from albi3ro January 28, 2025 22:45
JerryChen97 and others added 7 commits January 28, 2025 18:00
…n-favour-of-`wires`' into Deprecate-`QubitUnitary`-as-a-valid-input-to-`ControlledQubitUnitary`
…n-favour-of-`wires`' into Deprecate-`QubitUnitary`-as-a-valid-input-to-`ControlledQubitUnitary`
@JerryChen97 JerryChen97 merged commit 51fb48c into Deprecate-`control_wires`-in-`ControlledQubitUnitary`-in-favour-of-`wires` Jan 29, 2025
42 of 47 checks passed
@JerryChen97 JerryChen97 deleted the Deprecate-`QubitUnitary`-as-a-valid-input-to-`ControlledQubitUnitary` branch January 29, 2025 20:25
JerryChen97 added a commit to PennyLaneAI/pennylane-lightning that referenced this pull request Feb 3, 2025
**Context:**
Due to the deprecations of `ControlledQubitUnitary` input args
PennyLaneAI/pennylane#6839
PennyLaneAI/pennylane#6840, we need to update
several tests of lightning that use deprecated interfaces.

**Description of the Change:**

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**
[sc-80842]

---------

Co-authored-by: ringo-but-quantum <[email protected]>
Co-authored-by: Alex Preciado <[email protected]>
JerryChen97 added a commit that referenced this pull request Feb 6, 2025
**Context:**
A recent deprecation #6840
deprecated the usage of `QubitUnitary` type input `base` for
instantiation of `ControlledQubitUnitary`.
However, a modification that was equivalent in PennyLane caused
unexpected behavior in Catalyst,
PennyLaneAI/catalyst#1494.
After investigation, it appears to be the issue with the private method
`_try_wrap_in_custom_ctrl_op` of `pennylane/ops/op_math/controlled.py`,
since it used to call the deprecated `init` to directly use base as
`QubitUnitary`, and to safely deprecate it we decided to locally replace
with equivalent `ControlledQubitUnitary(base=op.matrix(), ...)`, this
caused Catalyst to received double `QubitUnitary`.

It is also noteable that the call `ControlledQubitUnitary` should not
appear in this script, since logically all the classes living in
`controlled_ops` depend on `controlled.py`, which means that from the
very beginning the affected private method served as a tricky hack with
cyclic dependence.

Therefore, we would like to replace the `ControlledQubitUnitary` call
with a direct `ControlledOp`.

**Description of the Change:**
source files: as mentioned above.

tests: necessary improvements were made to correctly reflect the
assertion results.

**Benefits:**
 - Remove bugs
 - Less cyclic dependency

**Possible Drawbacks:**
- Probably there're more that depend on this method, which might be
affected by this fix

**Related GitHub Issues:**
PennyLaneAI/catalyst#1494

---------

Co-authored-by: Christina Lee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:run-full-test-suite Run the full test-suite on the pull request deprecation 🗑️ Deprecating an API or user-facing feature/behaviour
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants