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

Update API to allow QubitMapper where QubitConverter is used. #999

Merged
merged 25 commits into from
Jan 12, 2023

Conversation

Anthony-Gandon
Copy link
Contributor

@Anthony-Gandon Anthony-Gandon commented Dec 8, 2022

Summary

Closes #972

This PR updates the API to allow QubitMapper object where QubitConverter were previously required. THis is possible because the QubitConverter is a wrapper around the QubitMapper with additional methods for qubit reduction (two qubit reduction and tapering). 
The utility method map() was modified in the QubitMapper to act on lists, as the convert_match() does for the QubitConverter.

The following gist goes through a the main classes where this functionality is accessible: gist

This PR is going to be affected by #971, but I wanted to initiate the reflexion and feedback process.

Copy link
Member

@mrossinek mrossinek left a comment

Choose a reason for hiding this comment

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

Just some initial observations 👍

@@ -34,7 +34,7 @@ class ExcitedStatesEigensolver(ExcitedStatesSolver):

def __init__(
self,
qubit_converter: QubitConverter,
qubit_converter: Union[QubitConverter, QubitMapper],
Copy link
Member

Choose a reason for hiding this comment

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

In the future I would like to rename this argument to qubit_mapper. This will not be an easy process so it will be done in a separate PR once all the API is updated to support QubitMapper and works. I'm mentioning it now just as an FYI.
This will not be an easy undertaking but I will start discussing some plans on how to do this properly offline.

@@ -58,7 +58,7 @@ def get_qubit_operators(
) -> Tuple[PauliSumOp, Optional[dict[str, PauliSumOp]]]:
"""Construct qubit operators by getting the second quantized operators from the problem
(potentially running a driver in doing so [can be computationally expensive])
and using a QubitConverter to map and reduce the operators to qubit operators.
and using a QubitConverter or QubitMapper to map and reduce the operators to qubit operators.
Copy link
Member

Choose a reason for hiding this comment

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

We will need to do a thorough pass of the docs. Just mentioning this so its on our radar (I know that this is still a draft PR)

qiskit_nature/second_q/mappers/qubit_converter.py Outdated Show resolved Hide resolved
qiskit_nature/second_q/mappers/qubit_mapper.py Outdated Show resolved Hide resolved
qiskit_nature/second_q/mappers/spin_mapper.py Outdated Show resolved Hide resolved
test/second_q/circuit/library/test_bogoliubov_transform.py Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Dec 21, 2022

Pull Request Test Coverage Report for Build 3856020053

  • 152 of 172 (88.37%) changed or added relevant lines in 35 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 85.891%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit_nature/second_q/circuit/library/initial_states/vscf.py 5 6 83.33%
qiskit_nature/second_q/mappers/qubit_mapper.py 34 36 94.44%
qiskit_nature/second_q/algorithms/excited_states_solvers/excited_states_eigensolver.py 6 9 66.67%
qiskit_nature/second_q/algorithms/excited_states_solvers/qeom.py 36 39 92.31%
qiskit_nature/second_q/algorithms/excited_states_solvers/qeom_vibrational_ops_builder.py 1 4 25.0%
qiskit_nature/second_q/algorithms/ground_state_solvers/ground_state_eigensolver.py 6 9 66.67%
qiskit_nature/second_q/algorithms/excited_states_solvers/qeom_electronic_ops_builder.py 2 7 28.57%
Totals Coverage Status
Change from base Build 3836845227: 0.02%
Covered Lines: 17600
Relevant Lines: 20491

💛 - Coveralls

Copy link
Member

@mrossinek mrossinek left a comment

Choose a reason for hiding this comment

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

I think this is now looking a lot better in terms of API 👍

Could you please add more unittests to cover the usage of the new API?

@Anthony-Gandon
Copy link
Contributor Author

New unit tests were added following the API changes, however I still need to review properly the docstrings.

@Anthony-Gandon Anthony-Gandon marked this pull request as ready for review January 4, 2023 10:31
@Anthony-Gandon Anthony-Gandon changed the title [WIP] Update API to allow QubitMapper where QubitConverter is used. Update API to allow QubitMapper where QubitConverter is used. Jan 4, 2023
Copy link
Member

@mrossinek mrossinek left a comment

Choose a reason for hiding this comment

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

This is already looking quite good. A few more details need to be ironed out.
Ideally, we would add even more tests but I think for now until the remaining issues of the
QubitMapper refactoring are done, we can keep things as they are in this PR.

@@ -63,20 +63,21 @@ def __init__(
)

@property
def qubit_converter(self) -> QubitConverter:
def qubit_converter(self) -> QubitConverter | QubitMapper | None:
Copy link
Member

Choose a reason for hiding this comment

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

This currently still holds QubitConverter(DirectMapper()) as the internally-generated default
value. Can we already change this or do we need to keep this as is?
If the latter, shall we log a warning that this default is going to change?
Also pinging @woodsp-ibm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having the default value set to DirectMapper() won't change any of the logic. However, because it is a public attribute, it might be possible that one defines a VSCF object with the default value and then affects symmetries to the qubit converter. In that case, the QubitMapper will fail.

qiskit_nature/second_q/mappers/qubit_converter.py Outdated Show resolved Hide resolved
qiskit_nature/second_q/mappers/qubit_mapper.py Outdated Show resolved Hide resolved
qiskit_nature/second_q/mappers/qubit_mapper.py Outdated Show resolved Hide resolved
Copy link
Member

@mrossinek mrossinek left a comment

Choose a reason for hiding this comment

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

Thanks, Anthony! 👍

@mrossinek mrossinek merged commit 6c8bc95 into qiskit-community:main Jan 12, 2023
@Anthony-Gandon Anthony-Gandon deleted the refactor_qubitmapper branch May 24, 2023 15:50
Anthony-Gandon added a commit to Anthony-Gandon/qiskit-nature that referenced this pull request May 25, 2023
…-community#999)

* Failing tests but stable  QubitConverter and QubitMapper can now be interverted

* Modifications of the mappers map()

* Update API for QubitMapper

* Adds release note

* Small corrections after first feedback.

* Fix indentation:

* Change for map_all to map and map to map_single.

* Updated release note

* Updated release note 2

* Small updates before pushing

* small fix spell

* First review of docstrings

* Other docstrings

* Make black

* Fix mypy

* Fix comments

* fix

* Rolled back the type expansion of the map and convert_match methods, added new tests to cover more cases

* Implement the unwrapper in the _ListOrDict class

* Fix spelling

* Fix spell 2
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.

Update API to allow QubitMapper where QubitConverter is used
4 participants