File size: 12,284 Bytes
b7d9967
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020, 2023.
#
# 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
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""PrimitiveOp Class"""

from typing import Dict, List, Optional, Set, Union, cast

import numpy as np
import scipy.linalg
from scipy.sparse import spmatrix

from qiskit import QuantumCircuit
from qiskit.circuit import Instruction, ParameterExpression
from qiskit.opflow.operator_base import OperatorBase
from qiskit.quantum_info import Operator, Pauli, SparsePauliOp, Statevector
from qiskit.utils.deprecation import deprecate_func


class PrimitiveOp(OperatorBase):
    r"""

    Deprecated: A class for representing basic Operators, backed by Operator primitives from

    Terra. This class (and inheritors) primarily serves to allow the underlying

    primitives to "flow" - i.e. interoperability and adherence to the Operator formalism

    - while the core computational logic mostly remains in the underlying primitives.

    For example, we would not produce an interface in Terra in which

    ``QuantumCircuit1 + QuantumCircuit2`` equaled the Operator sum of the circuit

    unitaries, rather than simply appending the circuits. However, within the Operator

    flow summing the unitaries is the expected behavior.



    Note that all mathematical methods are not in-place, meaning that they return a

    new object, but the underlying primitives are not copied.



    """

    def __init_subclass__(cls):
        cls.__new__ = lambda cls, *args, **kwargs: super().__new__(cls)

    @staticmethod
    # pylint: disable=unused-argument
    def __new__(

        cls,

        primitive: Union[

            Instruction, QuantumCircuit, List, np.ndarray, spmatrix, Operator, Pauli, SparsePauliOp

        ],

        coeff: Union[complex, ParameterExpression] = 1.0,

    ) -> "PrimitiveOp":
        """A factory method to produce the correct type of PrimitiveOp subclass

        based on the primitive passed in. Primitive and coeff arguments are passed into

        subclass's init() as-is automatically by new().



        Args:

            primitive: The operator primitive being wrapped.

            coeff: A coefficient multiplying the primitive.



        Returns:

            The appropriate PrimitiveOp subclass for ``primitive``.



        Raises:

            TypeError: Unsupported primitive type passed.

        """
        # pylint: disable=cyclic-import
        if isinstance(primitive, (Instruction, QuantumCircuit)):
            from .circuit_op import CircuitOp

            return super().__new__(CircuitOp)

        if isinstance(primitive, (list, np.ndarray, spmatrix, Operator)):
            from .matrix_op import MatrixOp

            return super().__new__(MatrixOp)

        if isinstance(primitive, Pauli):
            from .pauli_op import PauliOp

            return super().__new__(PauliOp)

        if isinstance(primitive, SparsePauliOp):
            from .pauli_sum_op import PauliSumOp

            return super().__new__(PauliSumOp)

        raise TypeError(
            "Unsupported primitive type {} passed into PrimitiveOp "
            "factory constructor".format(type(primitive))
        )

    @deprecate_func(

        since="0.24.0",

        additional_msg="For code migration guidelines, visit https://qisk.it/opflow_migration.",

    )
    def __init__(

        self,

        primitive: Union[QuantumCircuit, Operator, Pauli, SparsePauliOp, OperatorBase],

        coeff: Union[complex, ParameterExpression] = 1.0,

    ) -> None:
        """

        Args:

            primitive: The operator primitive being wrapped.

            coeff: A coefficient multiplying the primitive.

        """
        super().__init__()
        self._primitive = primitive
        self._coeff = coeff

    @property
    def primitive(self) -> Union[QuantumCircuit, Operator, Pauli, SparsePauliOp, OperatorBase]:
        """The primitive defining the underlying function of the Operator.



        Returns:

             The primitive object.

        """
        return self._primitive

    @property
    def coeff(self) -> Union[complex, ParameterExpression]:
        """

        The scalar coefficient multiplying the Operator.



        Returns:

              The coefficient.

        """
        return self._coeff

    @property
    def num_qubits(self) -> int:
        raise NotImplementedError

    @property
    def settings(self) -> Dict:
        """Return operator settings."""
        return {"primitive": self._primitive, "coeff": self._coeff}

    def primitive_strings(self) -> Set[str]:
        raise NotImplementedError

    def add(self, other: OperatorBase) -> OperatorBase:
        raise NotImplementedError

    def adjoint(self) -> OperatorBase:
        raise NotImplementedError

    def equals(self, other: OperatorBase) -> bool:
        raise NotImplementedError

    def mul(self, scalar: Union[complex, ParameterExpression]) -> OperatorBase:
        if not isinstance(scalar, (int, float, complex, ParameterExpression)):
            raise ValueError(
                "Operators can only be scalar multiplied by float or complex, not "
                "{} of type {}.".format(scalar, type(scalar))
            )
        # Need to return self.__class__ in case the object is one of the inherited OpPrimitives
        return self.__class__(self.primitive, coeff=self.coeff * scalar)

    def tensor(self, other: OperatorBase) -> OperatorBase:
        raise NotImplementedError

    def tensorpower(self, other: int) -> Union[OperatorBase, int]:
        # Hack to make Z^(I^0) work as intended.
        if other == 0:
            return 1
        if not isinstance(other, int) or other < 0:
            raise TypeError("Tensorpower can only take positive int arguments")
        temp = PrimitiveOp(self.primitive, coeff=self.coeff)  # type: OperatorBase
        for _ in range(other - 1):
            temp = temp.tensor(self)
        return temp

    def compose(

        self, other: OperatorBase, permutation: Optional[List[int]] = None, front: bool = False

    ) -> OperatorBase:
        # pylint: disable=cyclic-import
        from ..list_ops.composed_op import ComposedOp

        new_self, other = self._expand_shorter_operator_and_permute(other, permutation)
        if isinstance(other, ComposedOp):
            comp_with_first = new_self.compose(other.oplist[0])
            if not isinstance(comp_with_first, ComposedOp):
                new_oplist = [comp_with_first] + other.oplist[1:]
                return ComposedOp(new_oplist, coeff=other.coeff)
            return ComposedOp([new_self] + other.oplist, coeff=other.coeff)

        return ComposedOp([new_self, other])

    def _expand_dim(self, num_qubits: int) -> OperatorBase:
        raise NotImplementedError

    def permute(self, permutation: List[int]) -> OperatorBase:
        raise NotImplementedError

    def exp_i(self) -> OperatorBase:
        """Return Operator exponentiation, equaling e^(-i * op)"""
        # pylint: disable=cyclic-import
        from ..evolutions.evolved_op import EvolvedOp

        return EvolvedOp(self)

    def log_i(self, massive: bool = False) -> OperatorBase:
        """Return a ``MatrixOp`` equivalent to log(H)/-i for this operator H. This

        function is the effective inverse of exp_i, equivalent to finding the Hermitian

        Operator which produces self when exponentiated."""
        # pylint: disable=cyclic-import
        from ..operator_globals import EVAL_SIG_DIGITS
        from .matrix_op import MatrixOp

        return MatrixOp(
            np.around(
                scipy.linalg.logm(self.to_matrix(massive=massive)) / -1j, decimals=EVAL_SIG_DIGITS
            )
        )

    def __str__(self) -> str:
        raise NotImplementedError

    def __repr__(self) -> str:
        return f"{type(self).__name__}({repr(self.primitive)}, coeff={self.coeff})"

    def eval(

        self,

        front: Optional[

            Union[str, Dict[str, complex], np.ndarray, OperatorBase, Statevector]

        ] = None,

    ) -> Union[OperatorBase, complex]:
        raise NotImplementedError

    @property
    def parameters(self):
        params = set()
        if isinstance(self.primitive, (OperatorBase, QuantumCircuit)):
            params.update(self.primitive.parameters)
        if isinstance(self.coeff, ParameterExpression):
            params.update(self.coeff.parameters)
        return params

    def assign_parameters(self, param_dict: dict) -> OperatorBase:
        param_value = self.coeff
        if isinstance(self.coeff, ParameterExpression):
            unrolled_dict = self._unroll_param_dict(param_dict)
            if isinstance(unrolled_dict, list):
                # pylint: disable=cyclic-import
                from ..list_ops.list_op import ListOp

                return ListOp([self.assign_parameters(param_dict) for param_dict in unrolled_dict])
            if self.coeff.parameters <= set(unrolled_dict.keys()):
                binds = {param: unrolled_dict[param] for param in self.coeff.parameters}
                param_value = complex(self.coeff.bind(binds))
                if abs(param_value.imag) == 0:
                    param_value = param_value.real
        return self.__class__(self.primitive, coeff=param_value)

    # Nothing to collapse here.
    def reduce(self) -> OperatorBase:
        return self

    def to_matrix(self, massive: bool = False) -> np.ndarray:
        raise NotImplementedError

    def to_matrix_op(self, massive: bool = False) -> OperatorBase:
        """Returns a ``MatrixOp`` equivalent to this Operator."""
        coeff = self.coeff
        op = self.copy()
        op._coeff = 1
        prim_mat = op.to_matrix(massive=massive)
        from .matrix_op import MatrixOp

        return MatrixOp(prim_mat, coeff=coeff)

    def to_instruction(self) -> Instruction:
        """Returns an ``Instruction`` equivalent to this Operator."""
        raise NotImplementedError

    def to_circuit(self) -> QuantumCircuit:
        """Returns a ``QuantumCircuit`` equivalent to this Operator."""
        qc = QuantumCircuit(self.num_qubits)
        qc.append(self.to_instruction(), qargs=range(self.primitive.num_qubits))
        return qc.decompose()

    def to_circuit_op(self) -> OperatorBase:
        """Returns a ``CircuitOp`` equivalent to this Operator."""
        from .circuit_op import CircuitOp

        if self.coeff == 0:
            return CircuitOp(QuantumCircuit(self.num_qubits), coeff=0)
        return CircuitOp(self.to_circuit(), coeff=self.coeff)

    def to_pauli_op(self, massive: bool = False) -> OperatorBase:
        """Returns a sum of ``PauliOp`` s equivalent to this Operator."""
        # pylint: disable=cyclic-import
        from .matrix_op import MatrixOp

        mat_op = cast(MatrixOp, self.to_matrix_op(massive=massive))
        sparse_pauli = SparsePauliOp.from_operator(mat_op.primitive)
        if not sparse_pauli.to_list():
            from ..operator_globals import I

            return (I ^ self.num_qubits) * 0.0
        from .pauli_op import PauliOp

        if len(sparse_pauli) == 1:
            label, coeff = sparse_pauli.to_list()[0]
            coeff = coeff.real if np.isreal(coeff) else coeff
            return PauliOp(Pauli(label), coeff * self.coeff)

        from ..list_ops.summed_op import SummedOp

        return SummedOp(
            [
                PrimitiveOp(
                    Pauli(label),
                    coeff.real if coeff == coeff.real else coeff,
                )
                for (label, coeff) in sparse_pauli.to_list()
            ],
            self.coeff,
        )