forked from munich-quantum-toolkit/bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtket_helper.py
311 lines (266 loc) · 9.4 KB
/
tket_helper.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
"""Compilation functions to create benchmarks on different levels of abstraction using TKET."""
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Literal, overload
from pytket import OpType
from pytket.architecture import Architecture
from pytket.extensions.qiskit import qiskit_to_tk
from pytket.passes import (
AutoRebase,
CXMappingPass,
FullPeepholeOptimise,
PeepholeOptimise2Q,
PlacementPass,
RoutingPass,
SynthesiseTket,
)
from pytket.placement import LinePlacement
from pytket.qasm import circuit_to_qasm_str
from qiskit import QuantumCircuit, transpile
from .utils import get_openqasm_gates, save_as_qasm
if TYPE_CHECKING: # pragma: no cover
from pytket._tket.passes import BasePass
from pytket.circuit import Circuit
from .devices import Device, Provider
def get_rebase(gate_set: list[str]) -> BasePass:
"""Get the rebase pass for the given gate set."""
op_dict = {
"r": OpType.U3,
"rx": OpType.Rx,
"ry": OpType.Ry,
"rz": OpType.Rz,
"rxx": OpType.XXPhase,
"rzz": OpType.ZZPhase,
"sx": OpType.SX,
"x": OpType.X,
"cx": OpType.CX,
"cz": OpType.CZ,
"ecr": OpType.ECR,
"measure": OpType.Measure,
}
return AutoRebase({op_dict[key] for key in gate_set if key in op_dict})
@overload
def get_indep_level(
qc: QuantumCircuit,
num_qubits: int | None,
file_precheck: bool,
return_qc: Literal[True],
target_directory: str = "./",
target_filename: str = "",
) -> Circuit: ...
@overload
def get_indep_level(
qc: QuantumCircuit,
num_qubits: int | None,
file_precheck: bool,
return_qc: Literal[False],
target_directory: str = "./",
target_filename: str = "",
) -> bool: ...
def get_indep_level(
qc: QuantumCircuit,
num_qubits: int | None,
file_precheck: bool,
return_qc: bool = False,
target_directory: str = "./",
target_filename: str = "",
) -> bool | Circuit:
"""Handles the creation of the benchmark on the target-independent level.
Arguments:
qc: quantum circuit which the to be created benchmark circuit is based on
num_qubits: number of qubits
file_precheck: flag indicating whether to check whether the file already exists before creating it (again)
return_qc: flag if the actual circuit shall be returned
target_directory: alternative directory to the default one to store the created circuit
target_filename: alternative filename to the default one
Returns:
if return_qc == True: quantum circuit object
else: True/False indicating whether the function call was successful or not
"""
filename_indep = target_filename or qc.name + "_indep_tket_" + str(num_qubits)
path = Path(target_directory, filename_indep + ".qasm")
if file_precheck and path.is_file():
return True
try:
gates = list(set(get_openqasm_gates()) - {"rccx"})
qc = transpile(
qc,
basis_gates=gates,
seed_transpiler=10,
optimization_level=0,
)
qc_tket = qiskit_to_tk(qc)
except Exception as e:
print("TKET Exception Indep: ", e)
return False
if return_qc:
return qc_tket
return save_as_qasm(
circuit_to_qasm_str(qc_tket, maxwidth=qc.num_qubits),
filename_indep,
target_directory=target_directory,
)
@overload
def get_native_gates_level(
qc: QuantumCircuit,
provider: Provider,
num_qubits: int | None,
file_precheck: bool,
return_qc: Literal[True],
target_directory: str = "./",
target_filename: str = "",
) -> Circuit: ...
@overload
def get_native_gates_level(
qc: QuantumCircuit,
provider: Provider,
num_qubits: int | None,
file_precheck: bool,
return_qc: Literal[False],
target_directory: str = "./",
target_filename: str = "",
) -> bool: ...
def get_native_gates_level(
qc: QuantumCircuit,
provider: Provider,
num_qubits: int | None,
file_precheck: bool,
return_qc: bool = False,
target_directory: str = "./",
target_filename: str = "",
) -> bool | Circuit:
"""Handles the creation of the benchmark on the target-dependent native gates level.
Arguments:
qc: quantum circuit which the to be created benchmark circuit is based on
provider: determines the native gate set
num_qubits: number of qubits
file_precheck: flag indicating whether to check whether the file already exists before creating it (again)
return_qc: flag if the actual circuit shall be returned
target_directory: alternative directory to the default one to store the created circuit
target_filename: alternative filename to the default one
Returns:
if return_qc == True: quantum circuit object
else: True/False indicating whether the function call was successful or not
"""
if not target_filename:
filename_native = qc.name + "_nativegates_" + provider.provider_name + "_tket_" + str(num_qubits)
else:
filename_native = target_filename
path = Path(target_directory, filename_native + ".qasm")
if file_precheck and path.is_file():
return True
try:
gates = list(set(get_openqasm_gates()) - {"rccx"})
qc = transpile(
qc,
basis_gates=gates,
seed_transpiler=10,
optimization_level=0,
)
qc_tket = qiskit_to_tk(qc)
except Exception as e:
print("TKET Exception NativeGates: ", e)
return False
gate_set = provider.get_native_gates()
native_gate_set_rebase = get_rebase(gate_set)
native_gate_set_rebase.apply(qc_tket)
FullPeepholeOptimise(target_2qb_gate=OpType.TK2).apply(qc_tket)
native_gate_set_rebase.apply(qc_tket)
if return_qc:
return qc_tket
return save_as_qasm(
circuit_to_qasm_str(qc_tket, maxwidth=qc.num_qubits),
filename_native,
gate_set,
target_directory=target_directory,
)
@overload
def get_mapped_level(
qc: QuantumCircuit,
num_qubits: int | None,
device: Device,
file_precheck: bool,
return_qc: Literal[True],
target_directory: str = "./",
target_filename: str = "",
) -> Circuit: ...
@overload
def get_mapped_level(
qc: QuantumCircuit,
num_qubits: int | None,
device: Device,
file_precheck: bool,
return_qc: Literal[False],
target_directory: str = "./",
target_filename: str = "",
) -> bool: ...
def get_mapped_level(
qc: QuantumCircuit,
num_qubits: int | None,
device: Device,
file_precheck: bool,
return_qc: bool = False,
target_directory: str = "./",
target_filename: str = "",
) -> bool | Circuit:
"""Handles the creation of the benchmark on the target-dependent mapped level.
Arguments:
qc: quantum circuit which the to be created benchmark circuit is based on
num_qubits: number of qubits
device: target device
lineplacement: if true line placement is used, else graph placement
file_precheck: flag indicating whether to check whether the file already exists before creating it (again)
return_qc: flag if the actual circuit shall be returned
target_directory: alternative directory to the default one to store the created circuit
target_filename: alternative filename to the default one
Returns:
if return_qc == True: quantum circuit object
else: True/False indicating whether the function call was successful or not
"""
if not target_filename:
filename_mapped = qc.name + "_mapped_" + device.name + "_tket_" + str(num_qubits)
else:
filename_mapped = target_filename
path = Path(target_directory, filename_mapped + ".qasm")
if file_precheck and path.is_file():
return True
try:
gates = list(set(get_openqasm_gates()) - {"rccx"})
qc = transpile(
qc,
basis_gates=gates,
seed_transpiler=10,
optimization_level=0,
)
qc_tket = qiskit_to_tk(qc)
except Exception as e:
print("TKET Exception Mapped: ", e)
return False
cmap = device.coupling_map
cmap_converted = [(c[0], c[1]) for c in cmap]
arch = Architecture(cmap_converted)
# add blank wires to the circuit such that afterwards the number of qubits is equal to the number of qubits of the architecture
highest_used_qubit_index = max(max(sublist) for sublist in cmap)
diff = highest_used_qubit_index + 1 - qc_tket.n_qubits # offset of one is added because the indices start at 0
qc_tket.add_blank_wires(diff)
native_gate_set_rebase = get_rebase(device.basis_gates)
native_gate_set_rebase.apply(qc_tket)
FullPeepholeOptimise(target_2qb_gate=OpType.TK2).apply(qc_tket)
placer = LinePlacement(arch)
PlacementPass(placer).apply(qc_tket)
RoutingPass(arch).apply(qc_tket)
PeepholeOptimise2Q(allow_swaps=False).apply(qc_tket)
SynthesiseTket().apply(qc_tket)
if not qc_tket.valid_connectivity(arch, directed=True):
CXMappingPass(arc=arch, placer=placer, directed_cx=True, delay_measures=False).apply(qc_tket)
native_gate_set_rebase.apply(qc_tket)
if return_qc:
return qc_tket
return save_as_qasm(
circuit_to_qasm_str(qc_tket, maxwidth=qc.num_qubits),
filename_mapped,
device.basis_gates,
True,
cmap,
target_directory,
)