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

Suppress warnings from Qiskit Nature #230

Merged
merged 17 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
![Platform](https://img.shields.io/badge/Platform-Linux%20%7C%20macOS%20%7C%20Windows-informational)
[![Python](https://img.shields.io/badge/Python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11-informational)](https://www.python.org/)
[![Qiskit](https://img.shields.io/badge/Qiskit-%E2%89%A5%200.43.0-6133BD)](https://github.com/Qiskit/qiskit)
[![Qiskit Nature](https://img.shields.io/badge/Qiskit%20Nature-%E2%89%A5%200.5.2-6133BD)](https://github.com/Qiskit/qiskit-nature)
[![Qiskit Nature](https://img.shields.io/badge/Qiskit%20Nature-%E2%89%A5%200.6.0-6133BD)](https://github.com/Qiskit/qiskit-nature)
<br />
[![DOI](https://zenodo.org/badge/543181258.svg)](https://zenodo.org/badge/latestdoi/543181258)
[![License](https://img.shields.io/github/license/Qiskit-Extensions/circuit-knitting-toolbox?label=License)](LICENSE.txt)
Expand Down
10 changes: 10 additions & 0 deletions circuit_knitting/forging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
EntanglementForgingGroundStateSolver,
)
from .cholesky_decomposition import cholesky_decomposition, convert_cholesky_operator
from qiskit_nature import settings

# From now forward, the entanglement forging module requires that SparsePauliOp
# be used rather than PauliSumOp, as the latter is deprecated in Qiskit Terra 0.24.
# However, Qiskit Nature still is not expected to change this default until Qiskit
# Nature 0.7. Here, we modify the global state to opt into the new behavior early.
# Unfortunately, this means that any code that calls Qiskit Nature in the same
# process as entanglement forging will need to be updated to use SparsePauliOp as well.

settings.use_pauli_sum_op = False

__all__ = [
"EntanglementForgingAnsatz",
Expand Down
31 changes: 13 additions & 18 deletions circuit_knitting/forging/cholesky_decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import numpy as np

from qiskit.opflow import PauliSumOp
from qiskit.quantum_info import Pauli
from qiskit.quantum_info import SparsePauliOp
from qiskit_nature.second_q.problems import ElectronicStructureProblem
from qiskit_nature.second_q.mappers import QubitConverter, JordanWignerMapper
from qiskit_nature.second_q.mappers import JordanWignerMapper
from qiskit_nature.second_q.operators import (
FermionicOp,
PolynomialTensor,
Expand All @@ -36,8 +36,8 @@


def get_cholesky_op(
l_op: np.ndarray, g: int, converter: QubitConverter, opname: str
) -> PauliSumOp:
l_op: np.ndarray, g: int, converter: JordanWignerMapper, opname: str
) -> SparsePauliOp:
"""
Convert a two-body term into a cholesky operator.

Expand All @@ -52,7 +52,7 @@ def get_cholesky_op(
"""
pt = PolynomialTensor({"+-": l_op[:, :, g]})
fer_op = FermionicOp.from_polynomial_tensor(pt)
cholesky_op = converter.convert(fer_op)
cholesky_op = converter.map(fer_op)
cholesky_op._name = opname + "_chol" + str(g)

return cholesky_op
Expand All @@ -62,7 +62,7 @@ def cholesky_decomposition(
problem: ElectronicStructureProblem,
mo_coeff: np.ndarray | None = None,
orbitals_to_reduce: Sequence[int] | None = None,
) -> tuple[list[PauliSumOp], float]:
) -> tuple[list[SparsePauliOp], float]:
"""
Construct the decomposed Hamiltonian from an input ``ElectronicStructureProblem``.

Expand Down Expand Up @@ -124,14 +124,14 @@ def cholesky_decomposition(


def convert_cholesky_operator(
operator: list[PauliSumOp],
operator: list[SparsePauliOp],
ansatz: EntanglementForgingAnsatz,
) -> EntanglementForgingOperator:
"""
Convert the Cholesky operator (List[PauliSumOp]) into the entanglement forging format.
Convert the Cholesky operator (List[SparsePauliOp]) into the entanglement forging format.

Args:
operator: A `List[PauliSumOp]` containing the single-body Hamiltonian followed
operator: A `List[SparsePauliOp]` containing the single-body Hamiltonian followed
by the Cholesky operators
shape: [single-body hamiltonian, cholesky_0, ..., cholesky_N]
ansatz: The ansatz for which to compute expectation values of operator. The
Expand All @@ -153,11 +153,7 @@ def convert_cholesky_operator(
tensor_paulis = set()
superpos_paulis = set()
paulis_each_op = [
{
label: weight
for label, weight in op.primitive.to_list()
if np.abs(weight) > 0
}
{label: weight for label, weight in op.to_list() if np.abs(weight) > 0}
for op in [op1] + list(cholesky_ops)
]

Expand Down Expand Up @@ -242,7 +238,7 @@ def _get_fermionic_ops_with_cholesky(
occupied_orbitals_to_reduce: np.ndarray | None = None,
virtual_orbitals_to_reduce: np.ndarray | None = None,
epsilon_cholesky: float = 1e-10,
) -> tuple[PauliSumOp, list[PauliSumOp], float, np.ndarray, np.ndarray,]:
) -> tuple[SparsePauliOp, list[SparsePauliOp], float, np.ndarray, np.ndarray,]:
r"""
Decompose the Hamiltonian operators into a form appropriate for entanglement forging.

Expand Down Expand Up @@ -335,11 +331,10 @@ def _get_fermionic_ops_with_cholesky(
if halve_transformed_h2:
h2 /= 2 # type: ignore

converter = QubitConverter(JordanWignerMapper())
converter = JordanWignerMapper()
pt = PolynomialTensor({"+-": h1, "++--": to_physicist_ordering(h2)})
fer_op = FermionicOp.from_polynomial_tensor(pt)
qubit_op = converter.convert(fer_op)

qubit_op = converter.map(fer_op)
qubit_op._name = opname + "_onebodyop"

cholesky_ops = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
ElectronicBasis,
)
from qiskit_ibm_runtime import QiskitRuntimeService, Options
from qiskit.opflow import PauliSumOp
from qiskit.quantum_info import SparsePauliOp

from .entanglement_forging_ansatz import EntanglementForgingAnsatz
from .entanglement_forging_knitter import EntanglementForgingKnitter
Expand Down Expand Up @@ -363,7 +363,7 @@ def evaluate_eigenvalue(parameters: Sequence[float]) -> float:
def get_qubit_operators(
self,
problem: ElectronicStructureProblem,
) -> list[PauliSumOp]:
) -> list[SparsePauliOp]:
"""Construct decomposed qubit operators from an ``ElectronicStructureProblem``.

Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"\n",
"See [Tutorial 1](tutorial_1_getting_started.ipynb) for a high-level breakdown of the entanglement forging algorithm, or check out the [explanatory material](../explanation/index.rst) for a more detailed explanation.\n",
"\n",
"<span style=\"color:red\"><i>Quantum Serverless does not support Qiskit Nature 0.6.0 yet. In order to use entanglement forging with quantum serverless, users should use 0.5.2 < Qiskit Nature < 0.6.0. There is an [associated issue in the quantum-serverless repo](https://github.com/Qiskit-Extensions/quantum-serverless/issues/413) to address this.</i></span>"
"<span style=\"color:red\"><i>Quantum Serverless does not support Qiskit Nature 0.6.0 yet. In order to use entanglement forging with quantum serverless, users should downgrade to version 0.2 of the Circuit Knitting Toolbox, as it is compatible with Qiskit Nature 0.5.2. There is an [associated issue in the quantum-serverless repo](https://github.com/Qiskit-Extensions/quantum-serverless/issues/413) to address this.</i></span>"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice

]
},
{
Expand Down Expand Up @@ -497,7 +497,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
"version": "3.11.3"
}
},
"nbformat": 4,
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ requires-python = ">=3.8"
dependencies = [
"qiskit-aer>=0.12.0",
"qiskit-terra>=0.24.0",
"qiskit-nature>=0.5.2",
"qiskit-nature>=0.6.0",
garrison marked this conversation as resolved.
Show resolved Hide resolved
"qiskit-ibm-runtime>=0.9.2",
]

Expand Down Expand Up @@ -86,7 +86,6 @@ notebook-dependencies = [
"matplotlib",
"ipywidgets",
"pylatexenc",
"qiskit-nature<0.6.0",
]

[project.urls]
Expand Down
8 changes: 8 additions & 0 deletions releasenotes/notes/remove-opflow-177bb07ba0e68002.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice

upgrade:
- |
The dependency on the :mod:`qiskit.opflow` module has been removed from entanglement forging.
With this change, Qiskit Nature 0.6.0 is now required.
However, Qiskit Nature 0.6.0 is incompatible with Quantum Serverless, so users that wish to
use entanglement forging with Quantum Serverless must remain on version 0.2 of the Circuit
Knitting Toolbox until `issue #108 <https://github.com/Qiskit-Extensions/circuit-knitting-toolbox/issues/108>`__ is resolved.
2 changes: 1 addition & 1 deletion tools/extremal-python-dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def mapfunc_dev(dep):
"""Load the development version(s) of certain Qiskit-related packages"""
# https://peps.python.org/pep-0440/#direct-references
return re.sub(
r"^(qiskit-(?:terra|ibm-runtime)).*$",
r"^(qiskit-(?:terra|nature|ibm-runtime)).*$",
r"\1 @ git+https://github.com/Qiskit/\1.git",
dep,
)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extras =
nbtest
notebook-dependencies
commands =
pytest --nbmake --nbmake-timeout=3000 {posargs} docs/
pytest --nbmake --nbmake-timeout=3000 {posargs} docs/ --ignore=docs/entanglement_forging/tutorials/tutorial_2_forging_with_quantum_serverless.ipynb

[testenv:coverage]
basepython = python3.10
Expand Down