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 8 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
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
4 changes: 4 additions & 0 deletions circuit_knitting_toolbox/entanglement_forging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
)
import circuit_knitting.forging.cholesky_decomposition as cholesky_module

from qiskit_nature import settings

settings.use_pauli_sum_op = False

Copy link
Member

Choose a reason for hiding this comment

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

These lines actually need to be moved to circuit_knitting/forging/__init__.py now that #244 is merged.

Copy link
Member

Choose a reason for hiding this comment

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

And it'd be great if you could add a comment above it:

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.

Copy link
Member

Choose a reason for hiding this comment

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

Also, once we finally figure out the magic to get everything working, we should double check that this block is actually required to get tests to pass. The migration guide suggests that it is not always necessary to change this setting. If you provide a SparsePauliOp to a function that returns an operator, it should result in a SparsePauliOp regardless of this setting, from what I understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this was in fact added after a discussion between @caleb-johnson and Max (Rossmannek). He suggested that we can suppress the warnings with these settings until opflow is dropped, which will be in the 0.8.0 release.

__all__ = [
"EntanglementForgingAnsatz",
"EntanglementForgingKnitter",
Expand Down