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

Fix ControlledQubitUnitary consistency with program capture #6719

Merged
merged 24 commits into from
Dec 18, 2024

Conversation

andrijapau
Copy link
Contributor

@andrijapau andrijapau commented Dec 13, 2024

Context:

The ControlledQubitUnitary operator previously exhibited inconsistent behavior when capture was enabled. This PR addresses the issue by updating and overwriting the _primitive_bind_call class method to handle the operator's unique call signature correctly.

Description of the Change:

Important Consequence: If the user does not specify wires but provides more than one control_wire, the wires will now be inferred from the input dimensions of the base operator. This is necessary to get program capture working. Once control_wires is deprecated, this can be improved.

The behaviour, with or without capture enabled, is the following,

>>> import pennylane as qml
>>> A = [[0, 1], [1, 0]]
>>> qml.ControlledQubitUnitary(A, [0, 1, 2])
Controlled(QubitUnitary(array([[0, 1],[1, 0]]), wires=[2]), control_wires=[0, 1])

>>> qml.ControlledQubitUnitary(A, 0, 1)
Controlled(QubitUnitary(array([[0, 1],[1, 0]]), wires=[1]), control_wires=[0])

>>> qml.ControlledQubitUnitary(A, control_wires=[0,1], wires=None)
Controlled(QubitUnitary(array([[0, 1],[1, 0]]), wires=[1]), control_wires=[0])

>>> qml.ControlledQubitUnitary(A, control_wires=0, wires=None)
"TypeError: Must specify a set of wires. None is not a valid `wires` label."

Benefits:

Code is now behaving consistently when qml.capture is enabled or disabled.

Possible Drawbacks: Example (3) above isn't necessarily expected. This should be sorted out when control_wires is deprecated.

Related GitHub Issues: Fixes #6675

[sc-79808]

This comment was marked as resolved.

Copy link

codecov bot commented Dec 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.62%. Comparing base (97f2cb5) to head (9c12a1a).
Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6719   +/-   ##
=======================================
  Coverage   99.62%   99.62%           
=======================================
  Files         478      478           
  Lines       45025    45045   +20     
=======================================
+ Hits        44856    44876   +20     
  Misses        169      169           

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

@andrijapau andrijapau requested a review from mudit2812 December 16, 2024 15:48
pennylane/ops/op_math/controlled_ops.py Outdated Show resolved Hide resolved
pennylane/ops/op_math/controlled_ops.py Outdated Show resolved Hide resolved
pennylane/ops/op_math/controlled_ops.py Outdated Show resolved Hide resolved
tests/ops/op_math/test_controlled_ops.py Show resolved Hide resolved
pennylane/ops/op_math/controlled_ops.py Show resolved Hide resolved
tests/ops/op_math/test_controlled_ops.py Show resolved Hide resolved
tests/ops/op_math/test_controlled_ops.py Outdated Show resolved Hide resolved
tests/ops/op_math/test_controlled_ops.py Show resolved Hide resolved
@astralcai
Copy link
Contributor

I would actually expect

qml.ControlledQubitUnitary(A, control_wires=[0,1], wires=None)

to raise the same error as

qml.ControlledQubitUnitary(A, control_wires=0, wires=None)

@JerryChen97
Copy link
Contributor

I would actually expect

qml.ControlledQubitUnitary(A, control_wires=[0,1], wires=None)

to raise the same error as

qml.ControlledQubitUnitary(A, control_wires=0, wires=None)

Why are you still working when on vacation (

Copy link
Contributor

@PietropaoloFrisoni PietropaoloFrisoni left a comment

Choose a reason for hiding this comment

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

🚀

@andrijapau andrijapau enabled auto-merge (squash) December 18, 2024 14:33
@andrijapau andrijapau disabled auto-merge December 18, 2024 14:36
@andrijapau andrijapau enabled auto-merge (squash) December 18, 2024 14:50
@andrijapau andrijapau merged commit 65b06ff into master Dec 18, 2024
46 checks passed
@andrijapau andrijapau deleted the fix-ctrl-qb-unit branch December 18, 2024 15:07
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.

[BUG] ControlledQubitUnitary as incorrect wires when using with qml.capture.enable()
5 participants