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

Added argsort and sort method to SparsePauliOp #8016

Merged
merged 14 commits into from
Jul 12, 2022

Conversation

kUmezawa
Copy link
Contributor

@kUmezawa kUmezawa commented May 3, 2022

Summary

This is an update as part of QAMP's activities.
Relating issue: qiskit-advocate/qamp-spring-22#20
PauliList has argsort() method and sort() method.
We need these features in SparsePauliOp.

Details and comments

argsort() method returns the composition of permutations in the order of sorting by coefficient and sorting by Pauli.

sort() method returns a new sorted SparsePauliOp class. After sorting the coefficients using numpy's argsort, sort by Pauli.

@kUmezawa kUmezawa requested review from a team and ikkoham as code owners May 3, 2022 08:41
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

@kUmezawa
Copy link
Contributor Author

kUmezawa commented May 3, 2022

@ikkoham
Please review. Thank you.

@ikkoham ikkoham added Changelog: New Feature Include in the "Added" section of the changelog mod: quantum info Related to the Quantum Info module (States & Operators) labels May 5, 2022
@coveralls
Copy link

coveralls commented May 5, 2022

Pull Request Test Coverage Report for Build 2654643569

  • 8 of 8 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 83.996%

Totals Coverage Status
Change from base Build 2651587373: 0.003%
Covered Lines: 55829
Relevant Lines: 66466

💛 - Coveralls

@HuangJunye
Copy link
Collaborator

@kUmezawa Is this part of QAMP? If so can you please mention the QAMP project in the description? I will also ad QAMP label to the PR. Thank you.

@ikkoham ikkoham added the QAMP issues and PRs relating to Qiskit Advocates Mentorship Program label May 9, 2022
@kUmezawa
Copy link
Contributor Author

kUmezawa commented May 9, 2022

@HuangJunye

Is this part of QAMP? If so can you please mention the QAMP project in the description?

Yes, this is part of QAMP. I have updated this pr description. Thank you.

Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

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

Thank you. Originally, the simplify method sorted as out of spec, but since 0.20.0 it no longer sorts, so this method is useful.

Basically, It looks good to me, but release note is required for new feature. Could you please add the release note?

@kUmezawa
Copy link
Contributor Author

kUmezawa commented May 9, 2022

@ikkoham
Is it okay to add a new yml file to releasenotes / notes / 0.20?

@t-imamichi
Copy link
Member

There is an attempt to parametrize coefficients of SparsePauliOp #7215.
Does weight option of this PR work fine with parameter expressions as well as complex values?

@t-imamichi
Copy link
Member

As for release note, please check out this documentation.
https://github.com/Qiskit/qiskit-terra/blob/main/CONTRIBUTING.md#release-notes

@kUmezawa
Copy link
Contributor Author

@t-imamichi

Does weight option of this PR work fine with parameter expressions as well as complex values?

I haven't considered it yet.
@ikkoham Let's discuss it in QAMP.

As for release note, please check out this documentation.

Thank you.
I follow the steps to create the release notes

@kUmezawa
Copy link
Contributor Author

@t-imamichi
I consulted with Hamamura-sann
Since the Parameter has nothing to do with this weight, we judge that it is not necessary to consider it this time.

@ikkoham
Copy link
Contributor

ikkoham commented May 13, 2022

Thanks. I'll have to think about whether we handle the error or simply just get a TypeError from NumPy when introducing Parameter for SparsePauliOp. I'll keep this in my mind.

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

Also from my side the code LGTM! But what is the motivation for adding these methods? That wasn't quite clear from the PR description 🙂

I'm quite confused by the weight argument -- I thought it was related to the coefficients and only when looking at PauliList I realized that you define the weight as the number of non-identity Paulis. In the context of the PauliList I see how that makes sense but since we have coefficients in the SparsePauliOp I think this naming might be a bit unclear. Could we maybe use a more precise argument?

@kUmezawa
Copy link
Contributor Author

@Cryoris

Thank you for your review.

Also from my side the code LGTM! But what is the motivation for adding these methods? That wasn't quite clear from the PR description

I'm sorry that the motive is difficult to understand. As Mr. Hamamura wrote, the simplify method cannot be sorted, so the motivation was to prepare an alternative method. I will specify it in the release notes.

Could we maybe use a more precise argument?

OK. I will consider it.

@HuangJunye HuangJunye added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jun 21, 2022
@kUmezawa
Copy link
Contributor Author

kUmezawa commented Jul 6, 2022

@Cryoris @ikkoham
Sorry for the late response.
The release notes have been fixed.
Added test code for empty list.
Added sample code to the documentation.

Thank you.


Args:
weight (bool): optionally sort by weight if True (Default: False).
This argument is the same as weight of argsort method of PauliList
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
This argument is the same as weight of argsort method of PauliList
This argument is the same as weight of argsort method of :class:`.PauliList`.


Args:
weight (bool): optionally sort by weight if True (Default: False).
This argument is the same as weight of sort method of PauliList
Copy link
Contributor

Choose a reason for hiding this comment

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

See comment on the documentation of argsort 🙂

@kUmezawa
Copy link
Contributor Author

kUmezawa commented Jul 8, 2022

@Cryoris @ikkoham
Thank you for your review.

Could you add a test for a corner case like an empty SparsePauliOp to make sure no errors are being raised?

Shouldn't sorting on an empty set work? I would argue since it's empty it's already "sorted" anyways

I understand that the meaning of your advice is to point out what happens when you sort SparcePauliOp with an empty list. Is that wrong?

See comment on the documentation of argsort

I will change the comment sort to argsort.

@Cryoris
Copy link
Contributor

Cryoris commented Jul 8, 2022

I understand that the meaning of your advice is to point out what happens when you sort SparcePauliOp with an empty list. Is that wrong?

I mean that right now it looks like sorting an empty SparsePauliOp raises an error. But I think it should not raise an error and just return the empty SparsePauliOp 🙂

@kUmezawa
Copy link
Contributor Author

kUmezawa commented Jul 8, 2022

@Cryoris
Thank you.

I mean that right now it looks like sorting an empty SparsePauliOp raises an error. But I think it should not raise an error and just return the empty SparsePauliOp

I get an error when generating a SparcePauliOp class with an empty list.
I don't think it's possible to sort an empty SparcePauliOp.

image

@Cryoris
Copy link
Contributor

Cryoris commented Jul 8, 2022

Oh fair enough! Then of course sorting doesn't work. Thanks for trying! 🙂

@kUmezawa
Copy link
Contributor Author

@Cryoris
Thank you.

>Oh fair enough! Then of course sorting doesn't work. Thanks for trying!

OK. Thank you.

>See comment on the documentation of argsort

Added description of arguments.

@ikkoham
Copy link
Contributor

ikkoham commented Jul 12, 2022

right. empty SparsePauliOp is not allowed.

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

LGTM thanks for the updates @kUmezawa!

Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you for your contribution!

@mergify mergify bot merged commit 925d8cb into Qiskit:main Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog Community PR PRs from contributors that are not 'members' of the Qiskit repo mod: quantum info Related to the Quantum Info module (States & Operators) QAMP issues and PRs relating to Qiskit Advocates Mentorship Program
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

7 participants