Skip to content

Commit

Permalink
Deprecate QuantumInstance and Opflow
Browse files Browse the repository at this point in the history
  • Loading branch information
manoelmarques committed Jan 7, 2023
1 parent 27da80d commit 1bcb65d
Show file tree
Hide file tree
Showing 79 changed files with 748 additions and 207 deletions.
9 changes: 7 additions & 2 deletions qiskit/algorithms/evolvers/trotterization/trotter_qrte.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,23 @@ class TrotterQRTE(RealEvolver):
Examples::
import warnings
from qiskit.opflow import X, Z, Zero
from qiskit.algorithms import EvolutionProblem, TrotterQRTE
from qiskit import BasicAer
from qiskit.utils import QuantumInstance
operator = X + Z
with warnings.catch_warnings():
warnings.simplefilter("ignore")
operator = X + Z
initial_state = Zero
time = 1
evolution_problem = EvolutionProblem(operator, 1, initial_state)
# LieTrotter with 1 rep
backend = BasicAer.get_backend("statevector_simulator")
quantum_instance = QuantumInstance(backend=backend)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
quantum_instance = QuantumInstance(backend=backend)
trotter_qrte = TrotterQRTE(quantum_instance=quantum_instance)
evolved_state = trotter_qrte.evolve(evolution_problem).evolved_state
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class AbsoluteAverage(LinearSystemObservable):
# Observable operator
observable_op = observable.observable(num_qubits)
state_vec = (~StateFn(observable_op) @ StateFn(qc)).eval()
with warnings.catch_warnings():
warnings.simplefilter('ignore')
state_vec = (~StateFn(observable_op) @ StateFn(qc)).eval()
# Obtain result
result = observable.post_processing(state_vec, num_qubits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ class MatrixFunctional(LinearSystemObservable):
observable_ops = observable.observable(num_qubits)
state_vecs = []
# First is the norm
state_vecs.append((~StateFn(observable_ops[0]) @ StateFn(qcs[0])).eval())
for i in range(1, len(observable_ops), 2):
state_vecs += [(~StateFn(observable_ops[i]) @ StateFn(qcs[i])).eval(),
(~StateFn(observable_ops[i + 1]) @ StateFn(qcs[i + 1])).eval()]
with warnings.catch_warnings():
warnings.simplefilter('ignore')
state_vecs.append((~StateFn(observable_ops[0]) @ StateFn(qcs[0])).eval())
for i in range(1, len(observable_ops), 2):
state_vecs += [(~StateFn(observable_ops[i]) @ StateFn(qcs[i])).eval(),
(~StateFn(observable_ops[i + 1]) @ StateFn(qcs[i + 1])).eval()]
# Obtain result
result = observable.post_processing(state_vecs, num_qubits)
Expand Down
29 changes: 15 additions & 14 deletions qiskit/opflow/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2019, 2020.
# (C) Copyright IBM 2019, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -17,7 +17,7 @@
.. currentmodule:: qiskit.opflow
Operators and State functions are the building blocks of Quantum Algorithms.
Deprecation: Operators and State functions are the building blocks of Quantum Algorithms.
A library for Quantum Algorithms & Applications is more than a collection of
algorithms wrapped in Python functions. It needs to provide tools to make writing
Expand Down Expand Up @@ -77,8 +77,9 @@
Operator Base Class
===================
The OperatorBase serves as the base class for all Operators, State functions and measurements, and
enforces the presence and consistency of methods to manipulate these objects conveniently.
Deprecation: The OperatorBase serves as the base class for all Operators, State functions
and measurements, and enforces the presence and consistency of methods to manipulate these
objects conveniently.
.. autosummary::
:toctree: ../stubs/
Expand All @@ -90,8 +91,8 @@
Operator Globals
================
The :mod:`operator_globals` is a set of immutable Operator instances that are convenient building
blocks to reach for while working with the Operator flow.
Deprecation: The :mod:`operator_globals` is a set of immutable Operator instances that are
convenient building blocks to reach for while working with the Operator flow.
One qubit Pauli operators:
:attr:`X`, :attr:`Y`, :attr:`Z`, :attr:`I`
Expand All @@ -108,8 +109,8 @@
Operators
---------
The Operators submodules include the PrimitiveOp, ListOp, and StateFn class groups which
represent the primary Operator modules.
Deprecation: The Operators submodules include the PrimitiveOp, ListOp, and StateFn class
groups which represent the primary Operator modules.
.. autosummary::
:toctree: ../stubs/
Expand All @@ -122,12 +123,12 @@
Converters
----------
The Converter submodules include objects which manipulate Operators, usually recursing over an
Operator structure and changing certain Operators' representation. For example, the
:class:`~.expectations.PauliExpectation` traverses an Operator structure, and replaces all of the
:class:`~.state_fns.OperatorStateFn` measurements containing non-diagonal Pauli terms into
diagonalizing circuits following by :class:`~.state_fns.OperatorStateFn` measurement containing
only diagonal Paulis.
Deprecation: The Converter submodules include objects which manipulate Operators,
usually recursing over an Operator structure and changing certain Operators' representation.
For example, the :class:`~.expectations.PauliExpectation` traverses an Operator structure, and
replaces all of the :class:`~.state_fns.OperatorStateFn` measurements containing non-diagonal
Pauli terms into diagonalizing circuits following by :class:`~.state_fns.OperatorStateFn`
measurement containing only diagonal Paulis.
.. autosummary::
:toctree: ../stubs/
Expand Down
4 changes: 2 additions & 2 deletions qiskit/opflow/converters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
# (C) Copyright IBM 2020, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -16,7 +16,7 @@
.. currentmodule:: qiskit.opflow.converters
Converters are objects which manipulate Operators, usually traversing an Operator to
Deprecation: Converters are objects which manipulate Operators, usually traversing an Operator to
change certain sub-Operators into a desired representation. Often the converted Operator is
isomorphic or approximate to the original Operator in some way, but not always. For example,
a converter may accept :class:`~qiskit.opflow.primitive_ops.CircuitOp` and return a
Expand Down
13 changes: 11 additions & 2 deletions qiskit/opflow/converters/abelian_grouper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
# (C) Copyright IBM 2020, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -13,6 +13,7 @@
"""AbelianGrouper Class"""

from collections import defaultdict
import warnings
from typing import List, Tuple, Union, cast

import numpy as np
Expand All @@ -27,10 +28,11 @@
from qiskit.opflow.primitive_ops.pauli_op import PauliOp
from qiskit.opflow.primitive_ops.pauli_sum_op import PauliSumOp
from qiskit.opflow.state_fns.operator_state_fn import OperatorStateFn
from qiskit.utils.deprecation import deprecate_function


class AbelianGrouper(ConverterBase):
"""The AbelianGrouper converts SummedOps into a sum of Abelian sums.
"""Deprecation: The AbelianGrouper converts SummedOps into a sum of Abelian sums.
Meaning, it will traverse the Operator, and when it finds a SummedOp, it will evaluate which of
the summed sub-Operators commute with one another. It will then convert each of the groups of
Expand All @@ -41,12 +43,19 @@ class AbelianGrouper(ConverterBase):
diagonalized together.
"""

@deprecate_function(
"The AbelianGrouper opflow class is deprecated as of Qiskit Terra 0.23.0 "
"and will be removed no sooner than 3 months after the release date. "
)
def __init__(self, traverse: bool = True) -> None:
"""
Args:
traverse: Whether to convert only the Operator passed to ``convert``, or traverse
down that Operator.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
super().__init__()
self._traverse = traverse

def convert(self, operator: OperatorBase) -> OperatorBase:
Expand Down
13 changes: 11 additions & 2 deletions qiskit/opflow/converters/circuit_sampler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
# (C) Copyright IBM 2020, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -14,6 +14,7 @@


import logging
import warnings
from functools import partial
from time import time
from typing import Any, Dict, List, Optional, Tuple, Union, cast
Expand All @@ -32,13 +33,14 @@
from qiskit.providers import Backend
from qiskit.utils.backend_utils import is_aer_provider, is_statevector_backend
from qiskit.utils.quantum_instance import QuantumInstance
from qiskit.utils.deprecation import deprecate_function

logger = logging.getLogger(__name__)


class CircuitSampler(ConverterBase):
"""
The CircuitSampler traverses an Operator and converts any CircuitStateFns into
Deprecation: The CircuitSampler traverses an Operator and converts any CircuitStateFns into
approximations of the state function by a DictStateFn or VectorStateFn using a quantum
backend. Note that in order to approximate the value of the CircuitStateFn, it must 1) send
state function through a depolarizing channel, which will destroy all phase information and
Expand All @@ -51,6 +53,10 @@ class CircuitSampler(ConverterBase):
you are better off using a different CircuitSampler for each Operator to avoid cache thrashing.
"""

@deprecate_function(
"The CircuitSampler opflow class is deprecated as of Qiskit Terra 0.23.0 "
"and will be removed no sooner than 3 months after the release date. "
)
def __init__(
self,
backend: Union[Backend, QuantumInstance],
Expand All @@ -76,6 +82,9 @@ def __init__(
Raises:
ValueError: Set statevector or param_qobj True when not supported by backend.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
super().__init__()
self._quantum_instance = (
backend if isinstance(backend, QuantumInstance) else QuantumInstance(backend=backend)
)
Expand Down
12 changes: 10 additions & 2 deletions qiskit/opflow/converters/converter_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
# (C) Copyright IBM 2020, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -15,11 +15,12 @@
from abc import ABC, abstractmethod

from qiskit.opflow.operator_base import OperatorBase
from qiskit.utils.deprecation import deprecate_function


class ConverterBase(ABC):
r"""
Converters take an Operator and return a new Operator, generally isomorphic
Deprecation: Converters take an Operator and return a new Operator, generally isomorphic
in some way with the first, but with certain desired properties. For example,
a converter may accept ``CircuitOp`` and return a ``SummedOp`` of
``PauliOps`` representing the circuit unitary. Converters may not
Expand All @@ -29,6 +30,13 @@ class ConverterBase(ABC):
in the number of qubits unless a clever trick is known (such as the use of sparse
matrices)."""

@deprecate_function(
"The ConverterBase opflow class is deprecated as of Qiskit Terra 0.23.0 "
"and will be removed no sooner than 3 months after the release date. "
)
def __init__(self) -> None:
pass

@abstractmethod
def convert(self, operator: OperatorBase) -> OperatorBase:
"""Accept the Operator and return the converted Operator
Expand Down
17 changes: 13 additions & 4 deletions qiskit/opflow/converters/dict_to_circuit_sum.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
# (C) Copyright IBM 2020, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -12,22 +12,28 @@

"""DictToCircuitSum Class """

import warnings
from qiskit.opflow.converters.converter_base import ConverterBase
from qiskit.opflow.list_ops.list_op import ListOp
from qiskit.opflow.operator_base import OperatorBase
from qiskit.opflow.state_fns.circuit_state_fn import CircuitStateFn
from qiskit.opflow.state_fns.dict_state_fn import DictStateFn
from qiskit.opflow.state_fns.vector_state_fn import VectorStateFn
from qiskit.utils.deprecation import deprecate_function


class DictToCircuitSum(ConverterBase):
r"""
Converts ``DictStateFns`` or ``VectorStateFns`` to equivalent ``CircuitStateFns`` or sums
thereof. The behavior of this class can be mostly replicated by calling ``to_circuit_op`` on
an Operator, but with the added control of choosing whether to convert only ``DictStateFns``
Deprecation: Converts ``DictStateFns`` or ``VectorStateFns`` to equivalent ``CircuitStateFns``
or sums thereof. The behavior of this class can be mostly replicated by calling ``to_circuit_op``
on an Operator, but with the added control of choosing whether to convert only ``DictStateFns``
or ``VectorStateFns``, rather than both.
"""

@deprecate_function(
"The DictToCircuitSum opflow class is deprecated as of Qiskit Terra 0.23.0 "
"and will be removed no sooner than 3 months after the release date. "
)
def __init__(
self, traverse: bool = True, convert_dicts: bool = True, convert_vectors: bool = True
) -> None:
Expand All @@ -38,6 +44,9 @@ def __init__(
convert_dicts: Whether to convert VectorStateFn.
convert_vectors: Whether to convert DictStateFns.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
super().__init__()
self._traverse = traverse
self._convert_dicts = convert_dicts
self._convert_vectors = convert_vectors
Expand Down
13 changes: 11 additions & 2 deletions qiskit/opflow/converters/pauli_basis_change.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
# (C) Copyright IBM 2020, 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand All @@ -12,6 +12,7 @@

""" PauliBasisChange Class """

import warnings
from functools import partial, reduce
from typing import Callable, List, Optional, Tuple, Union, cast

Expand All @@ -30,11 +31,12 @@
from qiskit.opflow.state_fns.operator_state_fn import OperatorStateFn
from qiskit.opflow.state_fns.state_fn import StateFn
from qiskit.quantum_info import Pauli
from qiskit.utils.deprecation import deprecate_function


class PauliBasisChange(ConverterBase):
r"""
Converter for changing Paulis into other bases. By default, the diagonal basis
Deprecation: Converter for changing Paulis into other bases. By default, the diagonal basis
composed only of Pauli {Z, I}^n is used as the destination basis to which to convert.
Meaning, if a Pauli containing X or Y terms is passed in, which cannot be
sampled or evolved natively on some Quantum hardware, the Pauli can be replaced by a
Expand All @@ -55,6 +57,10 @@ class PauliBasisChange(ConverterBase):
this method, such as the placement of the CNOT chains.
"""

@deprecate_function(
"The PauliBasisChange opflow class is deprecated as of Qiskit Terra 0.23.0 "
"and will be removed no sooner than 3 months after the release date. "
)
def __init__(
self,
destination_basis: Optional[Union[Pauli, PauliOp]] = None,
Expand Down Expand Up @@ -83,6 +89,9 @@ def __init__(
beginning and ending operators are equivalent.
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
super().__init__()
if destination_basis is not None:
self.destination = destination_basis # type: ignore
else:
Expand Down
Loading

0 comments on commit 1bcb65d

Please sign in to comment.