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

[WIP] Fix performance regression due to flattening Wires objects #7132

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

Conversation

jzaia18
Copy link

@jzaia18 jzaia18 commented Mar 21, 2025

Before submitting

Please complete the following checklist when submitting a PR:

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    test directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Add a new entry to the doc/releases/changelog-dev.md file, summarizing the
    change, and including a link back to the PR.

  • The PennyLane source code conforms to
    PEP8 standards.
    We check all of our code against Pylint.
    To lint modified files, simply pip install pylint, and then
    run pylint pennylane/path/to/file.py.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Context:
#6933 fixes a bug where Wires objects could be nested causing a 2-dimensional structure. However, for workloads that create large number of Wires objects, the extra work to flatten an already 1-dimensional iterable causes a performance regression (in some cases as much as 0.8x).

Description of the Change:
Cache tuples so workloads that commonly use the same sets of wires the flattening step can be skip redundant flattening operations. Prevent running the flattening logic when the input wires is of type range since these inputs will always be flat.

Benefits:
Increases performance back to be within +/- 0.02x of before the wire-flattening change for tested workloads.

Possible Drawbacks:
Increased code complexity

Related GitHub Issues:
[sc-86632] [sc-87017]

Changes co-authored with @mlxd

@jzaia18 jzaia18 self-assigned this Mar 21, 2025
@jzaia18 jzaia18 added WIP 🚧 Work-in-progress performance ⏲️ Benchmarking and performance improvements labels Mar 21, 2025
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.

@jzaia18
Copy link
Author

jzaia18 commented Mar 21, 2025

Scrapping this PR for now. May revisit later.

@jzaia18 jzaia18 added the do not merge ⚠️ Do not merge the pull request until this label is removed label Mar 21, 2025
@@ -71,6 +71,8 @@ def _process(wires):
# Note, this is not the same as `isinstance(wires, Iterable)` which would
# pass for 0-dim numpy arrays that cannot be iterated over.
tuple_of_wires = tuple(wires)
if type(wires) is range: # pylint: disable=unidiomatic-typecheck
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if type(wires) is range: # pylint: disable=unidiomatic-typecheck
if isinstance(wires, range):

Try this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge ⚠️ Do not merge the pull request until this label is removed performance ⏲️ Benchmarking and performance improvements WIP 🚧 Work-in-progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants