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

Implements a reverse_map() method for JordanWignerMapper(). #1344

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

MarcoBarroca
Copy link
Contributor

Summary

This implements a reverse_map() method in the JordanWignerMapper(). It allows users to go back from qubit operators into fermionic operators. Completes issue #255.

Also included a unit test.

Details and comments

Instead of using the implementation linked in issue #255 I modified it to use the FermionicOp object for the mapping. Seemed like it would be easier for anyone to understand the code. Both implementations are equivalent.

@CLAassistant
Copy link

CLAassistant commented Feb 25, 2024

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@MarcoBarroca MarcoBarroca changed the title Implemets a reverse_map() method for JordanWignerMapper(). Implements a reverse_map() method for JordanWignerMapper(). Feb 25, 2024
@woodsp-ibm
Copy link
Member

I see there are failures which are not of this change but due to a newly released pylint version - I created #1346 as this needs fixing outside of this PR to get CI running again and passing on the code that is already in main here.

@MarcoBarroca
Copy link
Contributor Author

Sure, I'll fix the mypy error by pushing the complex inside FermionicOp.

@MarcoBarroca
Copy link
Contributor Author

MarcoBarroca commented Mar 1, 2024

Included the pylint fixes in this PR. Hope that's'okay. Should fix #1346

@woodsp-ibm
Copy link
Member

Included the pylint fixes in this PR. Hope that's'okay.

Would it be too much to ask if you could do them in a separate PR. I think we will need to get the same fixes onto the stable branch so would want to backport the PR. Given this PR introduces a new feature its not something we would backport - that is more for critical fixes where we can do a bug fix release to address things.

Of course once such a separate PR was merged, and the branch here updated, it would all pass.

@woodsp-ibm
Copy link
Member

I'll let this run so we can check things anyway if you like. (Once you have a PR that has been merged here it will not longer need a maintainers approval to run - its just on the first PR)

@coveralls
Copy link

coveralls commented Mar 1, 2024

Pull Request Test Coverage Report for Build 8665226229

Details

  • 38 of 40 (95.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 86.795%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit_nature/second_q/mappers/jordan_wigner_mapper.py 38 40 95.0%
Totals Coverage Status
Change from base Build 8662422324: 0.04%
Covered Lines: 8821
Relevant Lines: 10163

💛 - Coveralls

return total_fermionic_op.normal_order()

@staticmethod
def invert_pauli_terms(sparse_pauli_op: SparsePauliOp) -> SparsePauliOp:
Copy link
Member

Choose a reason for hiding this comment

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

I am thinking this should be private method - ie something we do not want as part of supported public API. (If it was it would havte to have full docstring stating args, return etc. I think this is more intended as some private utility right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right now I think I’m the only one asking for this. I don’t see why we shouldn’t have this on the public API though. If Open Fermion can do this and thinks it’s relevant why shouldn’t Qiskit Nature?

Copy link
Member

Choose a reason for hiding this comment

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

We are talking just about the invert_pauli_terms method right - that was all I was meaning as being private not the whole reverse map method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh! my bad. Sure, I can turn that into a private method. It's just a utility for the reverse_map().


@classmethod
def reverse_map(cls, qubit_op: SparsePauliOp) -> FermionicOp:
"""Maps a qubit operator ``SparsePauliOp`` back into the second
Copy link
Member

Choose a reason for hiding this comment

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

I have a question around this. This seems to take qubit operator and maps it back regardless of what it really is. I get if I this is given a JW mapped Fermionic Op as a qubit operator it will reverse that. What happens to some other operator - does it always work and produce something even if its meaningless? Is there any check possible? Should we add any note here about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will work for any operator that is a SparsePauliOp and write that in terms of fermionic operators. But if a user tries to reverse an operator that was mapped with a different mapper(I.e. ParityMapper()) they will just get different ones.

It can also take any qubit operator and turn into a fermionic Op even if it doesn’t make sense, so it will always give a result.

Maybe a note making it clear that this works for JW? I wouldn’t know how to check if an qubit operator was transformed via Jordan Wigner or something else.

Copy link
Member

Choose a reason for hiding this comment

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

A note sounds like the best way then - that says it users responsibility to provide an operator that was mapped with JW to get a meaningful outcome as this does the reverse mapping for any operator on the given assumption it was done with a JW mapping in the first place - or however you want to phrase it,.

MarcoBarroca and others added 2 commits March 2, 2024 14:58
…f61.yaml

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
…f61.yaml

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
@woodsp-ibm
Copy link
Member

Sorry my bad in the suggestion, if you did not correct it was

:class:``JordanWignerMapper`

should have been

:class:`.JordanWignerMapper` where it was a . not a second `

@MarcoBarroca
Copy link
Contributor Author

@woodsp-ibm Let me know if you think the note is okay when you can.

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
@woodsp-ibm
Copy link
Member

@mrossinek Can I get you to take a look at this too.

@MarcoBarroca
Copy link
Contributor Author

Hey, just wanted an update here. Is there anything I should do about this still?

@woodsp-ibm
Copy link
Member

The build errors with mypy are fixed by #1362, which is still open. As far as progressing this hopefully @mrossinek can take a look.

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.

None yet

4 participants