This repository has been archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 376
/
pauli_op.py
291 lines (232 loc) · 11.8 KB
/
pauli_op.py
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
# -*- coding: utf-8 -*-
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# 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.
""" PauliOp Class """
from typing import Union, Set, Dict, Optional, cast
import logging
import numpy as np
from scipy.sparse import spmatrix
from qiskit import QuantumCircuit
from qiskit.circuit import ParameterExpression, Instruction
from qiskit.quantum_info import Pauli
from qiskit.circuit.library import RZGate, RYGate, RXGate, XGate, YGate, ZGate, IGate
from ..operator_base import OperatorBase
from .primitive_op import PrimitiveOp
from ..list_ops.summed_op import SummedOp
from ..list_ops.tensored_op import TensoredOp
from ..legacy.weighted_pauli_operator import WeightedPauliOperator
logger = logging.getLogger(__name__)
PAULI_GATE_MAPPING = {'X': XGate(), 'Y': YGate(), 'Z': ZGate(), 'I': IGate()}
class PauliOp(PrimitiveOp):
""" Class for Operators backed by Terra's ``Pauli`` module.
"""
def __init__(self,
primitive: Union[Pauli] = None,
coeff: Union[int, float, complex, ParameterExpression] = 1.0) -> None:
"""
Args:
primitive: The Pauli which defines the behavior of the underlying function.
coeff: A coefficient multiplying the primitive.
Raises:
TypeError: invalid parameters.
"""
if not isinstance(primitive, Pauli):
raise TypeError(
'PauliOp can only be instantiated with Paulis, not {}'.format(type(primitive)))
super().__init__(primitive, coeff=coeff)
def primitive_strings(self) -> Set[str]:
return {'Pauli'}
@property
def num_qubits(self) -> int:
return len(self.primitive)
def add(self, other: OperatorBase) -> OperatorBase:
if not self.num_qubits == other.num_qubits:
raise ValueError(
'Sum over operators with different numbers of qubits, {} and {}, is not well '
'defined'.format(self.num_qubits, other.num_qubits))
if isinstance(other, PauliOp) and self.primitive == other.primitive:
return PauliOp(self.primitive, coeff=self.coeff + other.coeff)
return SummedOp([self, other])
def adjoint(self) -> OperatorBase:
return PauliOp(self.primitive, coeff=np.conj(self.coeff))
def equals(self, other: OperatorBase) -> bool:
if not isinstance(other, PauliOp) or not self.coeff == other.coeff:
return False
return self.primitive == other.primitive
def tensor(self, other: OperatorBase) -> OperatorBase:
# Both Paulis
if isinstance(other, PauliOp):
# Copying here because Terra's Pauli kron is in-place.
op_copy = Pauli(x=other.primitive.x, z=other.primitive.z) # type: ignore
# NOTE!!! REVERSING QISKIT ENDIANNESS HERE
return PauliOp(op_copy.kron(self.primitive), coeff=self.coeff * other.coeff)
# pylint: disable=cyclic-import,import-outside-toplevel
from .circuit_op import CircuitOp
if isinstance(other, CircuitOp):
return self.to_circuit_op().tensor(other)
return TensoredOp([self, other])
def compose(self, other: OperatorBase) -> OperatorBase:
other = self._check_zero_for_composition_and_expand(other)
# If self is identity, just return other.
if not any(self.primitive.x + self.primitive.z): # type: ignore
return other * self.coeff # type: ignore
# Both Paulis
if isinstance(other, PauliOp):
product, phase = Pauli.sgn_prod(self.primitive, other.primitive)
return PrimitiveOp(product, coeff=self.coeff * other.coeff * phase)
# pylint: disable=cyclic-import,import-outside-toplevel
from .circuit_op import CircuitOp
from ..state_fns.circuit_state_fn import CircuitStateFn
if isinstance(other, (CircuitOp, CircuitStateFn)):
return self.to_circuit_op().compose(other)
return super().compose(other)
def to_matrix(self, massive: bool = False) -> np.ndarray:
if self.num_qubits > 16 and not massive:
raise ValueError(
'to_matrix will return an exponentially large matrix, '
'in this case {0}x{0} elements.'
' Set massive=True if you want to proceed.'.format(2 ** self.num_qubits))
return self.primitive.to_matrix() * self.coeff # type: ignore
def to_spmatrix(self) -> spmatrix:
""" Returns SciPy sparse matrix representation of the Operator.
Returns:
CSR sparse matrix representation of the Operator.
Raises:
ValueError: invalid parameters.
"""
return self.primitive.to_spmatrix() * self.coeff # type: ignore
def __str__(self) -> str:
prim_str = str(self.primitive)
if self.coeff == 1.0:
return prim_str
else:
return "{} * {}".format(self.coeff, prim_str)
def eval(self,
front: Optional[Union[str, Dict[str, complex], np.ndarray, OperatorBase]] = None
) -> Union[OperatorBase, float, complex]:
if front is None:
return self.to_matrix_op()
# pylint: disable=import-outside-toplevel,cyclic-import
from ..state_fns.state_fn import StateFn
from ..state_fns.dict_state_fn import DictStateFn
from ..state_fns.circuit_state_fn import CircuitStateFn
from ..list_ops.list_op import ListOp
from .circuit_op import CircuitOp
new_front = None
# For now, always do this. If it's not performant, we can be more granular.
if not isinstance(front, OperatorBase):
front = StateFn(front, is_measurement=False)
if isinstance(front, ListOp) and front.distributive:
new_front = front.combo_fn([self.eval(front.coeff * front_elem) # type: ignore
for front_elem in front.oplist])
else:
if self.num_qubits != front.num_qubits:
raise ValueError(
'eval does not support operands with differing numbers of qubits, '
'{} and {}, respectively.'.format(
self.num_qubits, front.num_qubits))
if isinstance(front, DictStateFn):
new_dict = {} # type: Dict
corrected_x_bits = self.primitive.x[::-1] # type: ignore
corrected_z_bits = self.primitive.z[::-1] # type: ignore
for bstr, v in front.primitive.items():
bitstr = np.asarray(list(bstr)).astype(np.int).astype(np.bool)
new_b_str = np.logical_xor(bitstr, corrected_x_bits)
new_str = ''.join(map(str, 1 * new_b_str))
z_factor = np.product(1 - 2 * np.logical_and(bitstr, corrected_z_bits))
y_factor = np.product(np.sqrt(1 - 2 * np.logical_and(corrected_x_bits,
corrected_z_bits) + 0j))
new_dict[new_str] = (v * z_factor * y_factor) + new_dict.get(new_str, 0)
new_front = StateFn(new_dict, coeff=self.coeff * front.coeff)
elif isinstance(front, StateFn) and front.is_measurement:
raise ValueError('Operator composed with a measurement is undefined.')
# Composable types with PauliOp
elif isinstance(front, (PauliOp, CircuitOp, CircuitStateFn)):
new_front = self.compose(front)
# Covers VectorStateFn and OperatorStateFn
elif isinstance(front, OperatorBase):
new_front = self.to_matrix_op().eval(front.to_matrix_op()) # type: ignore
return new_front
def exp_i(self) -> OperatorBase:
""" Return a ``CircuitOp`` equivalent to e^-iH for this operator H. """
# if only one qubit is significant, we can perform the evolution
corrected_x = self.primitive.x[::-1] # type: ignore
corrected_z = self.primitive.z[::-1] # type: ignore
# pylint: disable=import-outside-toplevel,no-member
sig_qubits = np.logical_or(corrected_x, corrected_z)
if np.sum(sig_qubits) == 0:
# e^I is just a global phase, but we can keep track of it! Should we?
# For now, just return identity
return PauliOp(self.primitive)
if np.sum(sig_qubits) == 1:
sig_qubit_index = sig_qubits.tolist().index(True)
coeff = np.real(self.coeff) \
if not isinstance(self.coeff, ParameterExpression) \
else self.coeff
# Y rotation
if corrected_x[sig_qubit_index] and corrected_z[sig_qubit_index]:
rot_op = PrimitiveOp(RYGate(coeff))
# Z rotation
elif corrected_z[sig_qubit_index]:
rot_op = PrimitiveOp(RZGate(coeff))
# X rotation
elif corrected_x[sig_qubit_index]:
rot_op = PrimitiveOp(RXGate(coeff))
from ..operator_globals import I
left_pad = I.tensorpower(sig_qubit_index)
right_pad = I.tensorpower(self.num_qubits - sig_qubit_index - 1)
# Need to use overloaded operators here in case left_pad == I^0
return left_pad ^ rot_op ^ right_pad
else:
from ..evolutions.evolved_op import EvolvedOp
return EvolvedOp(self)
def commutes(self, other_op: OperatorBase) -> bool:
""" Returns whether self commutes with other_op.
Args:
other_op: An ``OperatorBase`` with which to evaluate whether self commutes.
Returns:
A bool equaling whether self commutes with other_op
"""
if not isinstance(other_op, PauliOp):
return False
# Don't use compose because parameters will break this
self_bits = self.primitive.z + 2 * self.primitive.x # type: ignore
other_bits = other_op.primitive.z + 2 * other_op.primitive.x # type: ignore
return all((self_bits * other_bits) * (self_bits - other_bits) == 0)
def to_circuit(self) -> QuantumCircuit:
# If Pauli equals identity, don't skip the IGates
is_identity = sum(self.primitive.x + self.primitive.z) == 0 # type: ignore
# Note: Reversing endianness!!
qc = QuantumCircuit(len(self.primitive))
for q, pauli_str in enumerate(reversed(self.primitive.to_label())): # type: ignore
gate = PAULI_GATE_MAPPING[pauli_str]
if not pauli_str == 'I' or is_identity:
qc.append(gate, qargs=[q])
return qc
def to_instruction(self) -> Instruction:
# TODO should we just do the following because performance of adding and deleting IGates
# doesn't matter?
# (Reduce removes extra IGates).
# return PrimitiveOp(self.primitive.to_instruction(), coeff=self.coeff).reduce()
return self.to_circuit().to_instruction()
def to_pauli_op(self, massive: bool = False) -> OperatorBase:
return self
def to_legacy_op(self, massive: bool = False) -> WeightedPauliOperator:
if isinstance(self.coeff, ParameterExpression):
try:
coeff = float(self.coeff)
except TypeError as ex:
raise TypeError('Cannot convert Operator with unbound parameter {} to Legacy '
'Operator'.format(self.coeff)) from ex
else:
coeff = cast(float, self.coeff)
return WeightedPauliOperator(paulis=[(coeff, self.primitive)]) # type: ignore