-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathfold_rzz_angle.py
330 lines (286 loc) · 11.8 KB
/
fold_rzz_angle.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
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
325
326
327
328
329
330
# This code is part of Qiskit.
#
# (C) Copyright IBM 2024.
#
# 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.
"""Pass to wrap Rzz gate angle in calibrated range of 0-pi/2."""
from typing import Tuple, Optional, Union, List
from math import pi
from operator import mod
from qiskit.converters import dag_to_circuit, circuit_to_dag
from qiskit.circuit.library.standard_gates import RZZGate, RZGate, XGate, GlobalPhaseGate, RXGate
from qiskit.circuit.parameterexpression import ParameterExpression
from qiskit.circuit import Qubit, ControlFlowOp
from qiskit.dagcircuit import DAGCircuit
from qiskit.transpiler import Target
from qiskit.transpiler.basepasses import TransformationPass
import numpy as np
class FoldRzzAngle(TransformationPass):
"""Fold Rzz gate angle into calibrated range of 0-pi/2 with
local gate tweaks.
In the IBM Quantum ISA, the instruction Rzz(theta) has
valid "theta" value of [0, pi/2] and any instruction outside
this range becomes a non-ISA operation for the quantum backend.
The transpiler pass discovers such non-ISA Rzz gates
and folds the gate angle into the calibrated range
with addition of single qubit gates while preserving
logical equivalency of the input quantum circuit.
Added local gates might be efficiently merged into
neighboring single qubit gates by the following single qubit
optimization passes.
This pass allows the Qiskit users to naively use the Rzz gates
with angle of arbitrary real numbers.
"""
def __init__(self, target: Optional[Union[Target, List[str]]] = None):
"""
Args:
target - either a target or only a list of basis gates, either way it can be checked
if an instruction is supported using the `in` operator, for example `"rx" in target`.
If None then we assume that there is no limit on the gates in the transpiled circuit.
"""
super().__init__()
if target is None:
self._target = ["rz", "x", "rx", "rzz"]
else:
self._target = target
def run(self, dag: DAGCircuit) -> DAGCircuit:
self._run_inner(dag)
return dag
def _run_inner(self, dag: DAGCircuit) -> bool:
"""Mutate the input dag to fix non-ISA Rzz angles.
Return true if the dag was modified."""
modified = False
for node in dag.op_nodes():
if isinstance(node.op, ControlFlowOp):
modified_blocks = False
new_blocks = []
for block in node.op.blocks:
block_dag = circuit_to_dag(block)
if self._run_inner(block_dag):
# Store circuit with Rzz modification
new_blocks.append(dag_to_circuit(block_dag))
modified_blocks = True
else:
# Keep original circuit to save memory
new_blocks.append(block)
if modified_blocks:
dag.substitute_node(
node,
node.op.replace_blocks(new_blocks),
inplace=True,
)
modified = True
continue
if not isinstance(node.op, RZZGate):
continue
angle = node.op.params[0]
if not isinstance(angle, ParameterExpression) and 0 <= angle <= pi / 2:
# Angle is an unbound parameter or a calibrated value.
continue
# Modify circuit around Rzz gate to address non-ISA angles.
if isinstance(angle, ParameterExpression):
replace = self._unbounded_parameter(angle, node.qargs)
else:
replace = self._numeric_parameter(angle, node.qargs)
if replace is not None:
dag.substitute_node_with_dag(node, replace)
modified = True
return modified
# The next function is required because sympy doesn't convert Boolean values to integers.
# symengine maybe does but I failed to find it in its documentation.
def gteq_op(self, exp1: ParameterExpression, exp2: ParameterExpression) -> ParameterExpression:
"""Return an expression which, after substitution, will be equal to 1 if `exp1` is
greater or equal than `exp2`, and 0 otherwise"""
tmp = (exp1 - exp2).sign()
# We want to return 1 if tmp is 1 or 0, and 0 otherwise
return ((tmp + 0.1).sign() + 1) / 2
def _unbounded_parameter(
self, angle: ParameterExpression, qubits: Tuple[Qubit, ...]
) -> DAGCircuit:
if "rz" not in self._target or "rx" not in self._target or "rzz" not in self._target:
return None
global_phase = (
(-pi / 2) * self.gteq_op((angle + pi / 2)._apply_operation(mod, 2 * pi), pi)
+ pi * self.gteq_op(angle._apply_operation(mod, 2 * pi), 3 * pi / 2)
+ pi * self.gteq_op((angle + pi)._apply_operation(mod, 4 * pi), 2 * pi)
)
rz_angle = pi * self.gteq_op((angle + pi / 2)._apply_operation(mod, 2 * pi), pi)
rx_angle = pi * self.gteq_op(angle._apply_operation(mod, pi), pi / 2)
rzz_angle = pi / 2 - (angle._apply_operation(mod, pi) - pi / 2).abs()
new_dag = DAGCircuit()
new_dag.add_qubits(qubits=qubits)
new_dag.apply_operation_back(GlobalPhaseGate(global_phase))
new_dag.apply_operation_back(
RZGate(rz_angle),
qargs=(qubits[0],),
check=False,
)
new_dag.apply_operation_back(
RZGate(rz_angle),
qargs=(qubits[1],),
check=False,
)
new_dag.apply_operation_back(
RXGate(rx_angle),
qargs=(qubits[0],),
check=False,
)
new_dag.apply_operation_back(
RZZGate(rzz_angle),
qargs=qubits,
check=False,
)
new_dag.apply_operation_back(
RXGate(rx_angle),
qargs=(qubits[0],),
check=False,
)
return new_dag
def _numeric_parameter(self, angle: float, qubits: Tuple[Qubit, ...]) -> DAGCircuit:
wrap_angle = np.angle(np.exp(1j * angle))
if 0 <= wrap_angle <= pi / 2:
# In the first quadrant.
replace = self._quad1(wrap_angle, qubits)
elif pi / 2 < wrap_angle <= pi:
# In the second quadrant.
replace = self._quad2(wrap_angle, qubits)
elif -pi <= wrap_angle <= -pi / 2:
# In the third quadrant.
replace = self._quad3(wrap_angle, qubits)
elif -pi / 2 < wrap_angle < 0:
# In the forth quadrant.
replace = self._quad4(wrap_angle, qubits)
else:
raise RuntimeError("Unreacheable.")
if pi < angle % (4 * pi) < 3 * pi:
replace.apply_operation_back(GlobalPhaseGate(pi))
return replace
def _quad1(self, angle: float, qubits: Tuple[Qubit, ...]) -> DAGCircuit:
"""Handle angle between [0, pi/2].
Circuit is not transformed - the Rzz gate is calibrated for the angle.
Returns:
A new dag with the same Rzz gate.
"""
if "rzz" not in self._target:
return None
new_dag = DAGCircuit()
new_dag.add_qubits(qubits=qubits)
new_dag.apply_operation_back(
RZZGate(angle),
qargs=qubits,
check=False,
)
return new_dag
def _quad2(self, angle: float, qubits: Tuple[Qubit, ...]) -> DAGCircuit:
"""Handle angle between (pi/2, pi].
Circuit is transformed into the following form:
┌───────┐┌───┐ ┌───┐
q_0: ┤ Rz(π) ├┤ X ├─■──────────┤ X ├
├───────┤└───┘ │ZZ(π - θ) └───┘
q_1: ┤ Rz(π) ├──────■───────────────
└───────┘
Returns:
New dag to replace Rzz gate.
"""
if "rz" not in self._target or "x" not in self._target or "rzz" not in self._target:
return None
new_dag = DAGCircuit()
new_dag.add_qubits(qubits=qubits)
new_dag.apply_operation_back(GlobalPhaseGate(pi / 2))
new_dag.apply_operation_back(
RZGate(pi),
qargs=(qubits[0],),
cargs=(),
check=False,
)
new_dag.apply_operation_back(
RZGate(pi),
qargs=(qubits[1],),
check=False,
)
if not np.isclose(new_angle := (pi - angle), 0.0):
new_dag.apply_operation_back(
XGate(),
qargs=(qubits[0],),
check=False,
)
new_dag.apply_operation_back(
RZZGate(new_angle),
qargs=qubits,
check=False,
)
new_dag.apply_operation_back(
XGate(),
qargs=(qubits[0],),
check=False,
)
return new_dag
def _quad3(self, angle: float, qubits: Tuple[Qubit, ...]) -> DAGCircuit:
"""Handle angle between [-pi, -pi/2].
Circuit is transformed into following form:
┌───────┐
q_0: ┤ Rz(π) ├─■───────────────
├───────┤ │ZZ(π - Abs(θ))
q_1: ┤ Rz(π) ├─■───────────────
└───────┘
Returns:
New dag to replace Rzz gate.
"""
if "rz" not in self._target or "rzz" not in self._target:
return None
new_dag = DAGCircuit()
new_dag.add_qubits(qubits=qubits)
new_dag.apply_operation_back(GlobalPhaseGate(-pi / 2))
new_dag.apply_operation_back(
RZGate(pi),
qargs=(qubits[0],),
check=False,
)
new_dag.apply_operation_back(
RZGate(pi),
qargs=(qubits[1],),
check=False,
)
if not np.isclose(new_angle := (pi - np.abs(angle)), 0.0):
new_dag.apply_operation_back(
RZZGate(new_angle),
qargs=qubits,
check=False,
)
return new_dag
def _quad4(self, angle: float, qubits: Tuple[Qubit, ...]) -> DAGCircuit:
"""Handle angle between (-pi/2, 0).
Circuit is transformed into following form:
┌───┐ ┌───┐
q_0: ┤ X ├─■───────────┤ X ├
└───┘ │ZZ(Abs(θ)) └───┘
q_1: ──────■────────────────
Returns:
New dag to replace Rzz gate.
"""
if "x" not in self._target or "rzz" not in self._target:
return None
new_dag = DAGCircuit()
new_dag.add_qubits(qubits=qubits)
new_dag.apply_operation_back(
XGate(),
qargs=(qubits[0],),
check=False,
)
new_dag.apply_operation_back(
RZZGate(abs(angle)),
qargs=qubits,
check=False,
)
new_dag.apply_operation_back(
XGate(),
qargs=(qubits[0],),
check=False,
)
return new_dag