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 12 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
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.operators.symplectic import SparsePauliOp
SaashaJoshi marked this conversation as resolved.
Show resolved Hide resolved
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.operators.symplectic import SparsePauliOp
SaashaJoshi marked this conversation as resolved.
Show resolved Hide resolved

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
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
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