-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathtest_fold_rzz_angle.py
168 lines (144 loc) · 6.06 KB
/
test_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
# 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.
"""Test folding Rzz angle into calibrated range."""
from math import pi
from ddt import ddt, named_data
import numpy as np
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.parameter import Parameter
from qiskit.circuit.library import RZZGate
from qiskit.transpiler.target import InstructionProperties
from qiskit.transpiler.passmanager import PassManager
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.quantum_info import Operator
from qiskit_ibm_runtime.transpiler.passes.basis import FoldRzzAngle
from qiskit_ibm_runtime.fake_provider import FakeFractionalBackend, FakeSherbrooke
from .....ibm_test_case import IBMTestCase
@ddt
class TestFoldRzzAngle(IBMTestCase):
"""Test FoldRzzAngle pass"""
@named_data(
("pi/2_pos", pi / 2),
("pi/2_neg", -pi / 2),
("pi_pos", pi),
("pi_neg", -pi),
("quad1_no_wrap", 0.1),
("quad2_no_wrap", pi / 2 + 0.1),
("quad3_no_wrap", -pi + 0.1),
("quad4_no_wrap", -0.1),
("quad1_2pi_wrap", 2 * pi + 0.1),
("quad2_2pi_wrap", -3 * pi / 2 + 0.1),
("quad3_2pi_wrap", pi + 0.1),
("quad4_2pi_wrap", 2 * pi - 0.1),
("quad1_12pi_wrap", -12 * pi + 0.1),
("quad2_12pi_wrap", 23 * pi / 2 + 0.1),
("quad3_12pi_wrap", 11 * pi + 0.1),
("quad4_12pi_wrap", -12 * pi - 0.1),
)
def test_folding_rzz_angles(self, angle):
"""Test folding gate angle into calibrated range."""
qc = QuantumCircuit(2)
qc.rzz(angle, 0, 1)
pm = PassManager([FoldRzzAngle()])
isa = pm.run(qc)
self.assertEqual(Operator.from_circuit(qc), Operator.from_circuit(isa))
for inst_data in isa.data:
if inst_data.operation.name == "rzz":
fold_angle = inst_data.operation.params[0]
self.assertGreaterEqual(fold_angle, 0.0)
self.assertLessEqual(fold_angle, pi / 2)
@named_data(
("pi/2_pos", pi / 2),
("pi/2_neg", -pi / 2),
("pi_pos", pi),
("pi_neg", -pi),
("quad1_no_wrap", 0.1),
("quad2_no_wrap", pi / 2 + 0.1),
("quad3_no_wrap", -pi + 0.1),
("quad4_no_wrap", -0.1),
("quad1_2pi_wrap", 2 * pi + 0.1),
("quad2_2pi_wrap", -3 * pi / 2 + 0.1),
("quad3_2pi_wrap", pi + 0.1),
("quad4_2pi_wrap", 2 * pi - 0.1),
("quad1_12pi_wrap", -12 * pi + 0.1),
("quad2_12pi_wrap", 23 * pi / 2 + 0.1),
("quad3_12pi_wrap", 11 * pi + 0.1),
("quad4_12pi_wrap", -12 * pi - 0.1),
)
def test_folding_rzz_angle_unbound(self, angle):
"""Test transformation in the case of an unbounded parameter"""
param = Parameter("angle")
qc = QuantumCircuit(2)
qc.rzz(param, 0, 1)
pm = PassManager([FoldRzzAngle()])
isa = pm.run(qc)
qc.assign_parameters({param: angle}, inplace=True)
isa.assign_parameters({param: angle}, inplace=True)
self.assertEqual(Operator.from_circuit(qc), Operator.from_circuit(isa))
for inst_data in isa.data:
if inst_data.operation.name == "rzz":
fold_angle = inst_data.operation.params[0]
self.assertGreaterEqual(fold_angle, 0.0)
self.assertLessEqual(fold_angle, pi / 2)
def test_controlflow(self):
"""Test non-ISA Rzz gates inside/outside a control flow branch."""
qc = QuantumCircuit(2, 1)
qc.rzz(-0.2, 0, 1)
with qc.if_test((0, 1)): # pylint: disable=not-context-manager
qc.rzz(-0.1, 0, 1)
with qc.if_test((0, 1)): # pylint: disable=not-context-manager
qc.rzz(-0.3, 0, 1)
pm = PassManager([FoldRzzAngle()])
isa = pm.run(qc)
expected = QuantumCircuit(2, 1)
expected.x(0)
expected.rzz(0.2, 0, 1)
expected.x(0)
with expected.if_test((0, 1)): # pylint: disable=not-context-manager
expected.x(0)
expected.rzz(0.1, 0, 1)
expected.x(0)
with expected.if_test((0, 1)): # pylint: disable=not-context-manager
expected.x(0)
expected.rzz(0.3, 0, 1)
expected.x(0)
self.assertEqual(isa, expected)
def test_fractional_plugin(self):
"""Verify that a pass manager created for a fractional backend applies the rzz folding
pass"""
circ = QuantumCircuit(2)
circ.rzz(7, 0, 1)
pm = generate_preset_pass_manager(
optimization_level=0,
backend=FakeFractionalBackend(),
translation_method="ibm_fractional",
)
isa_circ = pm.run(circ)
self.assertEqual(isa_circ.data[0].operation.name, "global_phase")
self.assertEqual(isa_circ.data[1].operation.name, "rzz")
self.assertTrue(np.isclose(isa_circ.data[1].operation.params[0], 7 - 2 * pi))
def test_unsupported_instructions_skip(self):
"""Verify that the pass does not output gates that are not in the basis gates"""
backend = FakeSherbrooke()
backend.target.add_instruction(RZZGate(Parameter("θ")), {(0, 1): InstructionProperties()})
p = Parameter("p")
circ = QuantumCircuit(2)
circ.rzz(p, 0, 1)
pm = generate_preset_pass_manager(
optimization_level=0,
backend=FakeSherbrooke(),
translation_method="ibm_fractional",
basis_gates=backend.target.operation_names,
)
isa_circ = pm.run(circ)
self.assertEqual(isa_circ.data[0].operation.name, "rzz")
self.assertTrue(isinstance(isa_circ.data[0].operation.params[0], Parameter))