Skip to content

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

Open
MarcoBarroca wants to merge 21 commits into
qiskit-community:mainfrom
MarcoBarroca:main
Open

Implements a reverse_map() method for JordanWignerMapper().#1344
MarcoBarroca wants to merge 21 commits into
qiskit-community:mainfrom
MarcoBarroca:main

Conversation

@MarcoBarroca

Copy link
Copy Markdown
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

CLAassistant commented Feb 25, 2024

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@CLAassistant

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
Contributor Author

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

@MarcoBarroca

MarcoBarroca commented Mar 1, 2024

Copy link
Copy Markdown
Contributor Author

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

@woodsp-ibm

Copy link
Copy Markdown
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
Copy Markdown
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)

@MarcoBarroca MarcoBarroca force-pushed the main branch 2 times, most recently from be27527 to 4c15ca7 Compare March 1, 2024 16:57
@coveralls

coveralls commented Mar 1, 2024

Copy link
Copy Markdown

Coverage Report for CI Build 26298873383

Coverage increased (+0.03%) to 86.548%

Details

  • Coverage increased (+0.03%) from the base build.
  • Patch coverage: 2 uncovered changes across 1 file (38 of 40 lines covered, 95.0%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
qiskit_nature/second_q/mappers/jordan_wigner_mapper.py 40 38 95.0%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 10601
Covered Lines: 9175
Line Coverage: 86.55%
Coverage Strength: 0.87 hits per line

💛 - Coveralls

Comment thread releasenotes/notes/reverse-jordan-wigner-mapping-be0e0ab217967f61.yaml Outdated
Comment thread releasenotes/notes/reverse-jordan-wigner-mapping-be0e0ab217967f61.yaml Outdated
Comment thread releasenotes/notes/reverse-jordan-wigner-mapping-be0e0ab217967f61.yaml Outdated
return total_fermionic_op.normal_order()

@staticmethod
def invert_pauli_terms(sparse_pauli_op: SparsePauliOp) -> SparsePauliOp:

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

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

Comment thread qiskit_nature/second_q/mappers/jordan_wigner_mapper.py Outdated
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
@woodsp-ibm

Copy link
Copy Markdown
Member

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

@MarcoBarroca

Copy link
Copy Markdown
Contributor Author

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

@woodsp-ibm

Copy link
Copy Markdown
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.

@MarcoBarroca

Copy link
Copy Markdown
Contributor Author

Saw that this repo is seeing some activity recently. Let me know if you need anything from me to get this merged.

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.

5 participants