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 qml.matrix with wire_order=None #5039

Merged
merged 9 commits into from
Jan 15, 2024

Conversation

timmysilv
Copy link
Contributor

@timmysilv timmysilv commented Jan 10, 2024

Context:
Wire-ordering can surprise people because PennyLane gives no promises. For example:

>>> qs = qml.tape.QuantumScript([qml.Identity(1), qml.CNOT([0, 1])])
>>> qml.matrix(qs)
array([[1., 0., 0., 0.],
       [0., 0., 0., 1.],
       [0., 0., 1., 0.],
       [0., 1., 0., 0.]])

This is in wire-order [1, 0] because that's the order in which they appeared in the tape. Most users would expect the other way.

Description of the Change:
Deprecate the default wire_order=None value in qml.matrix in certain cases. The warning is raised if:

  • the object is a tape with more than one wire
  • the object is a QNode and the device does not provide wires
  • the object is a qfunc

Benefits:
Users won't be surprised when they get a matrix in an unexpected wire order

Possible Drawbacks:
More deprecations, the user has to enter wire_order even though it's often safe to assume they meant ascending integers (but not often enough, so here we are)

Related GitHub Issues:
It was discussed in #3131

[sc-51277]

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.

Copy link

codecov bot commented Jan 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c01cb4b) 99.67% compared to head (b6b616d) 99.66%.
Report is 9 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5039      +/-   ##
==========================================
- Coverage   99.67%   99.66%   -0.02%     
==========================================
  Files         394      394              
  Lines       35670    35389     -281     
==========================================
- Hits        35554    35270     -284     
- Misses        116      119       +3     

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

@timmysilv timmysilv requested review from trbromley and a team January 11, 2024 13:49
@timmysilv timmysilv marked this pull request as ready for review January 11, 2024 13:49
@timmysilv timmysilv requested a review from a team January 12, 2024 17:01
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.

looks good to me!

pennylane/ops/functions/matrix.py Outdated Show resolved Hide resolved
@timmysilv timmysilv enabled auto-merge (squash) January 15, 2024 16:34
@timmysilv timmysilv merged commit e42edb3 into master Jan 15, 2024
35 checks passed
@timmysilv timmysilv deleted the deprecate-matrix-wire-order-none branch January 15, 2024 17:21
mudit2812 pushed a commit that referenced this pull request Jan 19, 2024
**Context:**
Wire-ordering can surprise people because PennyLane gives no promises.
For example:
```pycon
>>> qs = qml.tape.QuantumScript([qml.Identity(1), qml.CNOT([0, 1])])
>>> qml.matrix(qs)
array([[1., 0., 0., 0.],
       [0., 0., 0., 1.],
       [0., 0., 1., 0.],
       [0., 1., 0., 0.]])
```
This is in wire-order `[1, 0]` because that's the order in which they
appeared in the tape. Most users would expect the other way.

**Description of the Change:**
Deprecate the default `wire_order=None` value in `qml.matrix` in certain
cases. The warning is raised if:
- the object is a tape with more than one wire
- the object is a QNode and the device does not provide wires
- the object is a qfunc

**Benefits:**
Users won't be surprised when they get a matrix in an unexpected wire
order

**Possible Drawbacks:**
More deprecations, the user has to enter wire_order even though it's
often safe to assume they meant ascending integers (but not often
enough, so here we are)

**Related GitHub Issues:**
It was discussed in #3131

[sc-51277]
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