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

Optimize PhaseShift, T, S gates #5876

Merged
merged 25 commits into from
Jul 9, 2024
Merged

Optimize PhaseShift, T, S gates #5876

merged 25 commits into from
Jul 9, 2024

Conversation

vincentmr
Copy link
Contributor

@vincentmr vincentmr commented Jun 18, 2024

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:
PauliZ has a fast implementation which relies on the sparsity of the operator in DefaultQubit. Several operations have the same non-zero matrix elements and could be similarly accelerated. One candidate is PhaseShift which is abundantly used in iterative_qpe.

Description of the Change:
Port the fast-PauliZ to PhaseShift and use the implementation for PauliZ, S and T.

Benefits:
Faster execution. For example the simple system

nwires = 24
dev = qml.device("default.qubit", shots=1)
@qml.qnode(dev)
def circuit(iters):
    for i in range(iters):
        qml.PhaseShift(0.1234, i % nwires)
    return qml.sample(wires=[0])
circuit(100)

takes 0m13.178s on master and 0m9.146s on optim_apply_operations. We observe the same speed-up for S and T.

Possible Drawbacks:

Related GitHub Issues:
[sc-67827]

@vincentmr vincentmr closed this Jun 19, 2024
@trbromley trbromley deleted the optim_apply_operations branch June 19, 2024 14:22
@vincentmr vincentmr reopened this Jun 19, 2024
@vincentmr vincentmr changed the title Optim apply operations Optimize PhaseShift, T, S gates Jun 19, 2024
Copy link

codecov bot commented Jun 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.67%. Comparing base (2947317) to head (75af4b6).
Report is 274 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5876      +/-   ##
==========================================
- Coverage   99.67%   99.67%   -0.01%     
==========================================
  Files         426      426              
  Lines       40875    40625     -250     
==========================================
- Hits        40742    40491     -251     
- Misses        133      134       +1     

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

@vincentmr vincentmr marked this pull request as ready for review June 25, 2024 12:40
@vincentmr vincentmr requested a review from a team July 5, 2024 12:44
Copy link
Contributor

@trbromley trbromley left a comment

Choose a reason for hiding this comment

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

Thanks @vincentmr! Please could you attach the corresponding Shortcut story?

Copy link
Contributor

@mudit2812 mudit2812 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. Just one change requested to the phase shift implementation. If the suggestion doesn't work, this looks good. Happy to approve either way.

pennylane/devices/qubit/apply_operation.py Outdated Show resolved Hide resolved
pennylane/devices/qubit/apply_operation.py Outdated Show resolved Hide resolved
@vincentmr
Copy link
Contributor Author

Tests are failing because of Catalyst, I'll just wait for the release and ping reviewers again.

1 similar comment
@vincentmr
Copy link
Contributor Author

Tests are failing because of Catalyst, I'll just wait for the release and ping reviewers again.

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Looks great :) thanks for adding this 🚀

pennylane/devices/qubit/apply_operation.py Outdated Show resolved Hide resolved
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
@vincentmr vincentmr enabled auto-merge (squash) July 9, 2024 17:43
@vincentmr vincentmr merged commit e63362d into master Jul 9, 2024
40 checks passed
RenkeHuang pushed a commit to RenkeHuang/pennylane that referenced this pull request Jul 11, 2024
### Before submitting

Please complete the following checklist when submitting a PR:

- [x] 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!

- [x] 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`.

- [x] Ensure that the test suite passes, by running `make test`.

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

- [x] The PennyLane source code conforms to
      [PEP8 standards](https://www.python.org/dev/peps/pep-0008/).
We check all of our code against [Pylint](https://www.pylint.org/).
      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:**
`PauliZ` has a fast implementation which relies on the sparsity of the
operator in `DefaultQubit`. Several operations have the same non-zero
matrix elements and could be similarly accelerated. One candidate is
`PhaseShift` which is abundantly used in `iterative_qpe`.

**Description of the Change:**
Port the fast-`PauliZ` to `PhaseShift` and use the implementation for
`PauliZ`, `S` and `T`.

**Benefits:**
Faster execution. For example the simple system
```
nwires = 24
dev = qml.device("default.qubit", shots=1)
@qml.qnode(dev)
def circuit(iters):
    for i in range(iters):
        qml.PhaseShift(0.1234, i % nwires)
    return qml.sample(wires=[0])
circuit(100)
```
takes 0m13.178s on `master` and 0m9.146s on `optim_apply_operations`. We
observe the same speed-up for `S` and `T`.

**Possible Drawbacks:**

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

---------

Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
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