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

[Strings1] Improve string repr of PauliX, PauliY, PauliZ #5116

Merged
merged 37 commits into from
Feb 6, 2024
Merged

Conversation

Qottmann
Copy link
Contributor

@Qottmann Qottmann commented Jan 29, 2024

Improving string representation of PauliX, PauliY and PauliZ to be less verbose.
Together with adding the alias for X, Y, Z and I, this makes the string outputs copy-and-paste-able.

>>> qml.PauliX(0)
X(0)
>>> qml.PauliX('a')
X('a')

Also tackling scalar products s.t.

>>> 0.5 * X(0) # no unnecessary bracket
0.5 * X(0)
>>> 0.5 * (X(0) + Y(1)) # keeps necessary bracket
0.5 * (X(0) + Y(1))

(all examples with new opmath qml.operation.enable_new_opmath())

  • Update PauliX, PauliY, PauliZ, Identity string repr
  • Update tests accordingly
  • update SProd to not unnecessarily use brackets
  • add tests accordingly
  • Add alias for X, Y, Z and I
  • add tests accordingly
  • fix potential issues for classical shadows
  • changelog
  • handle Identity()

[sc-52831]
[sc-54957]
[sc-43897]

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.

@Qottmann Qottmann changed the title Improve string repr of PauliX, PauliY, PauliZ [WIP] Improve string repr of PauliX, PauliY, PauliZ Jan 29, 2024
@Qottmann Qottmann changed the title [WIP] Improve string repr of PauliX, PauliY, PauliZ Improve string repr of PauliX, PauliY, PauliZ Jan 30, 2024
Copy link

codecov bot commented Jan 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d5e3cca) 99.69% compared to head (829ca1d) 99.68%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5116      +/-   ##
==========================================
- Coverage   99.69%   99.68%   -0.01%     
==========================================
  Files         394      394              
  Lines       36118    35875     -243     
==========================================
- Hits        36007    35763     -244     
- Misses        111      112       +1     

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

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 @Qottmann!

@Qottmann Qottmann changed the title Improve string repr of PauliX, PauliY, PauliZ [Strings1] Improve string repr of PauliX, PauliY, PauliZ Feb 5, 2024
@Qottmann Qottmann requested a review from albi3ro February 5, 2024 16:29
@Qottmann Qottmann enabled auto-merge (squash) February 6, 2024 08:37
@codecov-commenter
Copy link

codecov-commenter commented Feb 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (85dd14f) 99.69% compared to head (a5d8966) 99.68%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5116      +/-   ##
==========================================
- Coverage   99.69%   99.68%   -0.01%     
==========================================
  Files         394      394              
  Lines       36118    35879     -239     
==========================================
- Hits        36007    35767     -240     
- Misses        111      112       +1     

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

@Qottmann Qottmann disabled auto-merge February 6, 2024 09:27
@Qottmann Qottmann enabled auto-merge (squash) February 6, 2024 09:46
@Qottmann Qottmann disabled auto-merge February 6, 2024 12:45
@Qottmann Qottmann enabled auto-merge (squash) February 6, 2024 14:48
@albi3ro albi3ro disabled auto-merge February 6, 2024 15:41
@albi3ro albi3ro merged commit e73213e into master Feb 6, 2024
@albi3ro albi3ro deleted the paulistrings branch February 6, 2024 15:41
albi3ro pushed a commit that referenced this pull request Feb 8, 2024
`Sum` instances are now displayed multiline and without unnecessary
brackets. Further, all outputs should be copy-and-paste-able. Some
examples how this now looks:

```python3
>>> qml.operation.enable_new_opmath()
>>> X(0) + X(1) + X(2) # short outputs <60 characters in single line
X(0) + X(1) + X(2)
>>> 0.5 * (X(0) @ X(1)) + 0.7 * (X(1) @ X(2)) + 0.8 * (X(2) @ X(3)) # larger outputs break into multiple lines. copy-paste-able with surrounding brackets
(
    0.5 * (X(0) @ X(1))
  + 0.7 * (X(1) @ X(2))
  + 0.8 * (X(2) @ X(3))
)
```

There are also some peculiarities that cannot be improved at the moment
because they are due to the nesting and lazy execution of products and
sums:
```python3
>>> 0.5 * (X(0) @ (0.5 * X(1))) + 0.7 * X(1)
0.5 * (X(0) @ (0.5 * X(1))) + 0.7 * X(1)
```


ToDo

- [x] Basic functionality
- [x] Tests
- [x] multiline threshold solution? (Problem from nesting in e.g. X(0) +
X(1) + X(2))
- [x] changelog

Documentation will be updated together with the changes from
#5116 in a follow-up PR

Builds on top of #5116
[sc-54959]
[sc-54955]
Qottmann added a commit that referenced this pull request Feb 22, 2024
…tations of X, Y, Z and Identity (#5153)

Updating docs and codebase with `qml.X/Y/Z/I(*)` instead of
`qml.PauliX/Y/Z(*)` and `qml.Identity(*)` following
#5116 and updating
composite operation outputs in docs according to
#5138

ToDo

- [x] Improve
https://docs.pennylane.ai/en/stable/introduction/operations.html#qubit-operators
- [x] Update outputs in docs with PauliX(wires=*)
- [x] Update outputs in docs with SProds and other composites (i.e. with
extra spaces and removing unnecessary brackets)
- [x] manually check & update all intro pages
- [x] manually check & update pulse docs
- [x] All tests pass
- [x] changelog

---------

Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
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