-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetamorph.py
1336 lines (1095 loc) · 49.4 KB
/
metamorph.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Metamorphic relationships and circuit manipulations for Qiskit.
"""
import random
import numpy as np
import pandas as pd
import scipy
import qiskit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import Aer, transpile
from qiskit.tools.visualization import circuit_drawer
from qiskit.quantum_info import state_fidelity
from typing import List, Dict, Any, Tuple
import re
import ast
import astunparse
import uuid
from importlib import import_module
from sys import version_info
from timeit import default_timer as timer
from copy import deepcopy
from lib.generation_strategy_python import *
from lib.generation_strategy_python import Fuzzer
import re
import networkx as nx
from itertools import combinations
from deprecated import deprecated
def get_sections(circ_source_code: str) -> Dict[str, str]:
"""Extract the main sections in the generated code file.
Note that the sections are separated by comments like this:
# SECTION
# NAME: PROLOGUE
This method return the sections as a dictionary, where the key is the name
of the section, and the value is the source code of the section.
"""
section_contents = circ_source_code.split("# SECTION\n")
regex_name_extr = r"^# NAME:\s([a-zA-Z_]+)\s"
named_sections = {
re.match(regex_name_extr, content).group(1): remove_comments(content)
for content in section_contents
if re.match(regex_name_extr, content) is not None
}
return named_sections
def reconstruct_sections(sections: Dict[str, str]) -> str:
"""Reconstruct the source code from the sections.
Args:
sections: The sections of the source code program.
Returns:
The source code of the circuit with the sections reconstructed.
"""
reconstructed_source_code = ""
for section_name, section_content in sections.items():
reconstructed_source_code += f"# SECTION\n# NAME: {section_name}\n"
reconstructed_source_code += section_content
return reconstructed_source_code
def add_section(sections: Dict[str, str],
new_section_name: str,
after_section: str = None,
before_section: str = None) -> Dict[str, str]:
"""Add a new section after the given one.
Args:
sections: The sections of the source code program.
Returns:
Old sections plus the new one.
"""
new_sections = {}
items = sections.items()
assert (after_section is not None and before_section is None) or \
(after_section is None and before_section is not None)
target_section = after_section
if before_section is not None:
items = reversed(items)
target_section = before_section
for c_section_name, section_content in items:
new_sections[c_section_name] = section_content
if c_section_name == target_section:
new_empty_section = f"# SECTION\n# NAME: {new_section_name}\n"
new_sections[new_section_name] = new_empty_section
if before_section is not None:
# reorder the keys
new_sections = {k: v for k, v in reversed(new_sections.items())}
return new_sections
def remove_comments(source_code: str) -> str:
"""Remove comments from the source code via ast.
Note that the comments are separated by lines like this:
# COMMENT
"""
return astunparse.unparse(ast.parse(source_code))
def create_random_mapping(qubit_indices: List[int]):
"""Create a mapping between two sets of the same qubit indices."""
qubit_indices = list(set(qubit_indices))
target_qubits = deepcopy(qubit_indices)
np.random.shuffle(target_qubits)
return {int(k): int(v) for k, v in zip(qubit_indices, target_qubits)}
def create_random_coupling_map(n_nodes: int,
edge_density: float) -> List[List[int]]:
"""Create a random coupling map."""
adjacency_matrix = scipy.sparse.random(
n_nodes, n_nodes, density=edge_density, format='coo')
return [[int(r), int(c)]
for (r, c) in zip(adjacency_matrix.row, adjacency_matrix.col)]
def create_empty_circuit(id_quantum_reg: str = None,
id_classical_reg: str = None,
id_circuit: str = None,
n_qubits: int = 1):
"""Create empty circuit.
Return its identifier name and the source code of the circuit.
"""
source_code = "\n"
source_code += f"{id_quantum_reg} = QuantumRegister({n_qubits}, name='{id_quantum_reg}')\n"
source_code += f"{id_classical_reg} = ClassicalRegister({n_qubits}, name='{id_classical_reg}')\n"
source_code += f"{id_circuit} = QuantumCircuit({id_quantum_reg}, {id_classical_reg}, name='{id_circuit}')\n"
return source_code + "\n"
def create_circuit(id_quantum_reg: str = None,
id_classical_reg: str = None,
id_circuit: str = None,
n_qubits: int = 1,
n_ops: int = 1,
gate_set: Dict[str, Any] = None,
generator_name: str = None,
only_circuit: bool = False):
"""Create circuit.
Return its identifier name and the source code of the circuit.
"""
generator: Fuzzer = eval(generator_name)()
source_code, metadata = generator.generate_circuit_via_atomic_ops(
gate_set=gate_set,
n_qubits=n_qubits,
n_ops=n_ops,
force_circuit_identifier=id_circuit,
force_classical_reg_identifier=id_classical_reg,
force_quantum_reg_identifier=id_quantum_reg,
only_circuit=only_circuit)
id_circuit = metadata["circuit_id"]
return id_circuit, source_code
def replace_identifier(source_code: str, identifier: str, replacement: str):
"""Replace an identifier in the source code.
Args:
source_code: The source code of the circuit.
identifier: The identifier to be replaced.
replacement: The replacement for the identifier.
Returns:
The source code with the replacement.
"""
tree = ast.parse(source_code)
class IdentifierReplacer(ast.NodeTransformer):
def __init__(self, identifier: str, replacement: str):
self.identifier = identifier
self.replacement = replacement
def visit_Name(self, node):
if (isinstance(node, ast.Name) and node.id == self.identifier):
node.id = self.replacement
return node
changer = IdentifierReplacer(identifier, replacement)
modified_tree = changer.visit(tree)
return to_code(modified_tree)
def remap_qubits(source_code: str,
id_quantum_reg: str,
id_classical_reg: str,
mapping: Dict[int, int]):
"""Map the qubits indexes to another set of qubit indexes."""
tree = ast.parse(source_code)
class QubitOrderChanger(ast.NodeTransformer):
def __init__(self,
id_quantum_reg: str,
id_classical_reg: str, mapping: Dict[int, int]):
self.id_quantum_reg = id_quantum_reg
self.id_classical_reg = id_classical_reg
self.mapping = mapping
def visit_Subscript(self, node):
if (isinstance(node, ast.Subscript) and
isinstance(node.value, ast.Name) and
(node.value.id == self.id_quantum_reg or
node.value.id == self.id_classical_reg) and
isinstance(node.slice, ast.Index) and
isinstance(node.slice.value, ast.Constant) and
node.slice.value.value in self.mapping.keys()):
node.slice.value.value = self.mapping[node.slice.value.value]
return node
changer = QubitOrderChanger(
id_quantum_reg=id_quantum_reg,
id_classical_reg=id_classical_reg,
mapping=mapping)
modified_tree = changer.visit(tree)
return to_code(modified_tree)
def create_random_connected_coupling_map(
n_nodes: int, edge_density: float,
force_symmetric: bool = True) -> List[List[int]]:
"""Create a random coupling map which is connected.
Inspired by: https://stackoverflow.com/a/2041539/13585425
"""
m = np.zeros((n_nodes, n_nodes), dtype=int)
# we remove n_nodes because we forbid the diagonal (self loops)
possible_edges = (n_nodes * n_nodes) - n_nodes
nodes_in_network = [0, 1]
m[0, 1] = 1
if force_symmetric:
m[1, 0] = 1
c_density = 1 / possible_edges
nodes_out_of_network = list(
set(list(range(n_nodes))).difference(set(nodes_in_network)))
while c_density < edge_density or len(nodes_out_of_network) > 0:
outgoing_edge = np.random.choice([True, False])
# master node
master_node = np.random.choice(nodes_in_network)
# print(master_node)
if outgoing_edge:
direction_to_look_for = m[:, master_node]
else:
direction_to_look_for = m[master_node, :]
if len(nodes_out_of_network) > 0:
# reason: we want to include all the nodes in the network first,
# then we care about density target
available_targets = nodes_out_of_network
else:
available_targets = list(np.argwhere(~np.array(
direction_to_look_for, dtype=bool)).flatten())
if master_node in available_targets:
# reason: to forbid self loops to be chosen
available_targets.remove(master_node)
if len(available_targets) > 0:
# print(f"available_targets: {available_targets}")
target_node = int(np.random.choice(available_targets))
# print(f"target_node: {target_node}")
if force_symmetric:
m[master_node][target_node] = 1
m[target_node][master_node] = 1
else:
if outgoing_edge:
m[master_node][target_node] = 1
else:
m[target_node][master_node] = 1
nodes_in_network.append(target_node)
nodes_out_of_network = list(
set(list(range(n_nodes))).difference(set(nodes_in_network)))
# print(m)
c_edges = np.sum(m)
c_density = float(c_edges) / possible_edges
adjacency_matrix = scipy.sparse.coo_matrix(m)
return [[int(r), int(c)] for (r, c) in zip(
adjacency_matrix.row, adjacency_matrix.col)]
def get_registers_used(circ_definition: str) -> List[Dict[str, Any]]:
"""Extract the available quantum and classical registers.
For each register in the main program report:
- the number of qubit used
- the identifier name
- the type or register
"""
tree = ast.parse(circ_definition)
class RegisterHunter(ast.NodeVisitor):
def __init__(self, register_type: str):
self.register_type = register_type
self.registers = []
def visit(self, node: ast.AST):
self.check_if_register(node)
for child in ast.iter_child_nodes(node):
self.visit(child)
def check_if_register(self, node: ast.AST):
if (isinstance(node, ast.Assign) and
isinstance(node.value, ast.Call) and
isinstance(node.value.func, ast.Name) and
node.value.func.id in [
"QuantumRegister", "ClassicalRegister"] and
isinstance(node.value.args[0], ast.Constant)):
register_type = node.value.func.id
identifier = node.targets[0].id
self.registers.append({
"name": identifier,
"type": register_type,
"size": node.value.args[0].value
})
def get_registers(self):
return self.registers
register_hunter = RegisterHunter("quantum")
register_hunter.generic_visit(tree)
return register_hunter.get_registers()
def get_circuits_used(circ_definition: str) -> List[Dict[str, Any]]:
"""Extract the available quantum circuits and their registers.
For each quantum circuit in the main program report:
- the number of qubit used
- the identifier name
- the name of its quantum register
- the name of its classical register
"""
tree = ast.parse(circ_definition)
registers = get_registers_used(circ_definition=circ_definition)
class CircuitHunter(ast.NodeVisitor):
def __init__(self):
self.circuits = []
def visit(self, node: ast.AST):
self.check_if_circuit(node)
for child in ast.iter_child_nodes(node):
self.visit(child)
def check_if_circuit(self, node: ast.AST):
if (isinstance(node, ast.Assign) and
isinstance(node.value, ast.Call) and
isinstance(node.value.func, ast.Name) and
node.value.func.id == "QuantumCircuit" and
not isinstance(node.value.args[0], ast.Constant) and
not isinstance(node.value.args[1], ast.Constant)):
register_type = node.value.func.id
identifier = node.targets[0].id
quantum_register_identifier = node.value.args[0].id
classical_register_identifier = node.value.args[1].id
size_quantum_reg = [
r for r in registers
if r["name"] == quantum_register_identifier][0]["size"]
size_classical_reg = [
r for r in registers
if r["name"] == classical_register_identifier][0]["size"]
assert size_quantum_reg == size_classical_reg
self.circuits.append({
"name": identifier,
"quantum_register": quantum_register_identifier,
"classical_register": classical_register_identifier,
"size": size_quantum_reg
})
def get_circuits(self):
return self.circuits
circuit_hunter = CircuitHunter()
circuit_hunter.generic_visit(tree)
return circuit_hunter.get_circuits()
def get_circuit_via_regex(circ_definition: str) -> str:
"""Extract the identifier names of all the declared circuits.
"""
m = re.search(r"([a-zA-Z_]*)\s=\sQuantumCircuit", circ_definition)
if m:
return m.group(1)
return None
def to_code(node):
"""
Convert the AST input to Python source string
:param node: AST node
:type node: ```AST```
:return: Python source
:rtype: ```str```
"""
return (
getattr(import_module("astor"), "to_source")
if version_info[:2] < (3, 9)
else getattr(import_module("ast"), "unparse")
)(node)
def get_instructions(circ_with_instructions: str) -> List[Dict[str, str]]:
"""Return extract all the instructions with the name of the circuit.
Example Input:
qc_2.append(RZXGate(2.5674333, p_29392d), qargs=[qr_2[2], qr_2[1]], cargs=[])
qc_1.append(CUGate(p_a7c416, p_53f0d4, 1.1382126210061985, p_b2bdc1), qargs
=[qr_1[4], qr_1[3]], cargs=[])
Result Output:
[
{"circuit_id": "qc_2",
"gate": "RZXGate",
"params": [2.5674333, "p_29392d"],
"qregs": ["qr_2", "qr_2"], "cregs": [],
"qargs": [2, 1], "cargs": []},
{"circuit_id": "qc_1",
"gate": "CUGate",
"params": ["p_a7c416", "p_53f0d4", 1.1382126210061985, "p_b2bdc1"],
"qregs": ["qr_1", "qr_1"], "cregs": [],
"qargs": [4, 3], "cargs": []}
]
"""
class InstructionsCollector(ast.NodeVisitor):
def __init__(self):
self.instructions = []
def recursive(func):
""" decorator to make visitor work recursive """
def wrapper(self, node):
func(self, node)
for child in ast.iter_child_nodes(node):
self.visit(child)
return wrapper
@recursive
def visit_Call(self, node):
#import pdb
#pdb.set_trace()
condition = (
# append call
isinstance(node, ast.Call) and
isinstance(node.func, ast.Attribute) and
(node.func.attr == "append") and
# there is a gate call as first argument
(len(node.args) == 1) and
isinstance(node.args[0], ast.Call) and
isinstance(node.args[0].func, ast.Name) and
("Gate" in node.args[0].func.id)
)
if condition:
new_instr = dict()
new_instr["circuit_id"] = node.func.value.id
new_instr["gate"] = node.args[0].func.id
new_instr["params"] = [
e.id if isinstance(e, ast.Name) else float(e.value)
for e in node.args[0].args
]
new_instr["qregs"], new_instr["qbits"] = [], []
new_instr["cregs"], new_instr["cbits"] = [], []
if len(node.keywords) == 2:
to_upack_qubits = [
(e.value.id, e.slice.value.value)
for e in node.keywords[0].value.elts
]
if len(to_upack_qubits) > 0:
new_instr["qregs"], new_instr["qbits"] = \
zip(*list(to_upack_qubits))
to_upack_bits = [
(e.value.id, e.slice.value.value)
for e in node.keywords[1].value.elts
]
if len(to_upack_bits) > 0:
new_instr["cregs"], new_instr["cbits"] = \
zip(*list(to_upack_bits))
new_instr["qregs"] = list(new_instr["qregs"])
new_instr["qbits"] = list(new_instr["qbits"])
new_instr["cregs"] = list(new_instr["cregs"])
new_instr["cbits"] = list(new_instr["cbits"])
new_instr["lineno"] = int(node.lineno)
new_instr["end_lineno"] = int(node.end_lineno)
new_instr["code"] = to_code(node).replace("\n", "").replace("\t", "").replace(" ", " ")
self.instructions.append(new_instr)
tree = ast.parse(circ_with_instructions)
instrCollector = InstructionsCollector()
instrCollector.visit(tree)
return instrCollector.instructions
def get_consecutive_gates(
source_code: str, gate_name: str) -> List[Dict[str, str]]:
"""Return a list of pairs of consecutive gates acting on the same bit(s).
Limitation: note that some gates, such as SwapGate do not care about the
order used in qubits but this function doesn't handle that case.
Example Input:
gate_name: str
"HGate"
source_code: str
qc.append(CSwapGate(), qargs=[qr[1], qr[0], qr[6]], cargs=[])
qc.append(HGate(), qargs=[qr[2]], cargs=[])
qc.append(HGate(), qargs=[qr[2]], cargs=[])
qc.append(YGate(), qargs=[qr[3]], cargs=[])
qc_2.append(CZGate(), qargs=[qr_2[4], qr_2[3]], cargs=[])
qc_2.append(YGate(), qargs=[qr_2[1]], cargs=[])
qc_2.append(CZGate(), qargs=[qr_2[4], qr_2[3]], cargs=[])
Output:
[
{'lineno': 2,
'end_lineno': 2,
'next_lineno': 3,
'next_end_lineno': 3,
'qregs': ['qr'],
'qbits': [2],
'cregs': [],
'cbits': [],
'circuit_id': 'qc',
'gate': 'HGate'}
]
"""
suitable_line_pairs = []
instructions = get_instructions(source_code)
if len(instructions) == 0:
return suitable_line_pairs
df_instr = pd.DataFrame.from_records(instructions)
# get all the registers
nested_list = list(df_instr["qregs"])
flat_list = [x for xs in nested_list for x in xs]
unique_registers = list(set(flat_list))
# print("unique_registers: ", unique_registers)
# keep only operations referring to this register
for register in unique_registers:
df_instr_curr_register = df_instr[
(df_instr["qregs"].apply(
lambda used_regs: all([r == register for r in used_regs])))]
df_instr_curr_register = \
df_instr_curr_register.sort_values(by="lineno")
# get all the used qbits
possible_qubits_combinations = list(
df_instr_curr_register["qbits"])
# remove duplicates
possible_qubits_combinations = list(set([
tuple(e) for e in possible_qubits_combinations
]))
# print("possible_qubits_combinations: ", possible_qubits_combinations)
# for each used qubits (more than once), check if there are
# two consecutive operations of the given gate
for qubits_combination in possible_qubits_combinations:
df_instr_c_bit = df_instr_curr_register[
df_instr_curr_register.apply(
lambda row:
any([qbit in qubits_combination
for qbit in row["qbits"]]),
axis=1
)
]
# find directly contiguous HGates
for i in range(len(df_instr_c_bit) - 1):
i_next = i + 1
i_instr = df_instr_c_bit.iloc[i]
next_instr = df_instr_c_bit.iloc[i_next]
target_bits = list(qubits_combination)
if ( # same gate name
i_instr["gate"] == gate_name and
next_instr["gate"] == gate_name and
# same bits
i_instr["qbits"] == target_bits and
next_instr["qbits"] == target_bits):
suitable_line_pairs += [
{
"lineno": i_instr["lineno"],
"end_lineno": i_instr["end_lineno"],
"next_lineno": next_instr["lineno"],
"next_end_lineno": next_instr["end_lineno"],
"qregs": i_instr["qregs"],
"qbits": i_instr["qbits"],
"cregs": i_instr["cregs"],
"cbits": i_instr["cbits"],
"circuit_id": i_instr["circuit_id"],
"gate": i_instr["gate"]
}
]
df_suitable_line_pairs = pd.DataFrame.from_records(suitable_line_pairs)
return df_suitable_line_pairs.to_dict("records")
@deprecated(version='qmt_v08', reason="You should use ChangeBackend class")
def mr_change_backend(source_code: str, available_backends: str) -> str:
"""Change the backend used in the source code.
Args:
source_code: The source code of the circuit.
available_backends: available backends to use.
Returns:
The source code of the circuit with the backend changed.
"""
sections = get_sections(source_code)
execution_section = sections["EXECUTION"]
mr_metadata = {}
tree = ast.parse(execution_section)
class BackendChanger(ast.NodeTransformer):
def __init__(self, available_backends: List[str]):
self.available_backends = deepcopy(available_backends)
def visit_Call(self, node):
if (isinstance(node, ast.Call) and
isinstance(node.func, ast.Attribute) and
node.func.attr == "get_backend" and
isinstance(node.args[0], ast.Constant)):
if node.args[0].value in self.available_backends:
self.available_backends.remove(node.args[0].value)
target_backend = np.random.choice(self.available_backends)
print(f"Follow: replace backend {node.args[0].value} -> " +
f"{target_backend}")
mr_metadata["initial_backend"] = str(node.args[0].value)
mr_metadata["new_backend"] = str(target_backend)
node.args[0].value = target_backend
return node
backend_changer = BackendChanger(available_backends)
modified_tree = backend_changer.visit(tree)
changed_section = to_code(modified_tree)
sections["EXECUTION"] = changed_section
return reconstruct_sections(sections), mr_metadata
@deprecated(version='qmt_v08', reason="You should use ChangeTargetBasis class")
def mr_change_basis(source_code: str,
universal_gate_sets: List[Dict[str, Any]]) -> str:
"""Change the basic gates used in the source code (via transpile).
"""
target_gates = np.random.choice(universal_gate_sets)["gates"]
sections = get_sections(source_code)
opt_level_section = sections["OPTIMIZATION_LEVEL"]
mr_metadata = {}
tree = ast.parse(opt_level_section)
class BasisChanger(ast.NodeTransformer):
def __init__(self, target_gates: List[str]):
self.target_gates = target_gates
def visit_Call(self, node):
if (isinstance(node, ast.Call) and
isinstance(node.func, ast.Name) and
node.func.id == "transpile"):
args = [k.arg for k in node.keywords]
if "basis_gates" in args:
idx = args.index("basis_gates")
node.keywords[idx].value = ast.List(elts=[
ast.Constant(g_name) for g_name in self.target_gates
])
mr_metadata["new_basis_gates"] = self.target_gates
print("Follow: gateset replaced with: ", self.target_gates)
return node
changer = BasisChanger(target_gates)
modified_tree = changer.visit(tree)
changed_section = to_code(modified_tree)
sections["OPTIMIZATION_LEVEL"] = changed_section
return reconstruct_sections(sections), mr_metadata
@deprecated(version='qmt_v08', reason="You should use ChangeOptLevel class")
def mr_change_opt_level(source_code: str, levels: List[int]) -> str:
"""Change the optimization level (via transpile).
"""
sections = get_sections(source_code)
opt_level_section = sections["OPTIMIZATION_LEVEL"]
mr_metadata = {}
tree = ast.parse(opt_level_section)
class OptLevelChanger(ast.NodeTransformer):
def __init__(self, levels: int):
self.levels = deepcopy(levels)
def visit_Call(self, node):
if (isinstance(node, ast.Call) and
isinstance(node.func, ast.Name) and
node.func.id == "transpile"):
args = [k.arg for k in node.keywords]
if "optimization_level" in args:
initial_level = node.keywords[
args.index("optimization_level")].value.value
self.levels.remove(initial_level)
target_opt_level = int(np.random.choice(self.levels))
node.keywords[args.index("optimization_level")].value = \
ast.Constant(target_opt_level)
mr_metadata["initial_level"] = int(initial_level)
mr_metadata["new_level"] = int(target_opt_level)
print(f"Follow: optimization level changed:" +
f" {initial_level} -> {target_opt_level}")
return node
changer = OptLevelChanger(levels)
modified_tree = changer.visit(tree)
changed_section = to_code(modified_tree)
sections["OPTIMIZATION_LEVEL"] = changed_section
return reconstruct_sections(sections), mr_metadata
@deprecated(version='qmt_v08', reason="You should use ChangeCouplingMap class")
def mr_change_coupling_map(source_code: str,
min_perc_nodes: float,
max_perc_nodes: float,
min_connection_density: float,
max_connection_density: float,
force_connected: bool = True,
force_symmetric: bool = True) -> str:
"""Change the coupling map.
Note that the coupling map could be smaller or larger than the number of
qubits in the original circuit.
min_perc_nodes: defines the percentage reduction of the coupling map size
with respect to the number of qubits in the circuit.
max_perc_nodes: defines the percentage expansion of the coupling map size
with respect to the number of qubits in the circuit.
"""
sections = get_sections(source_code)
opt_level_section = sections["OPTIMIZATION_LEVEL"]
mr_metadata = {}
tree = ast.parse(opt_level_section)
source_code_circuit = sections["CIRCUIT"]
registers = get_registers_used(source_code_circuit)
# we assume to have exactly one quantum and one classical register
# and they have the same number of qubits
quantum_reg = [r for r in registers
if r["type"] == "QuantumRegister"][0]
classical_reg = [r for r in registers
if r["type"] == "ClassicalRegister"][0]
assert(quantum_reg["size"] == classical_reg["size"])
n_bits_declared = quantum_reg["size"]
n_bits = random.randint(
int(n_bits_declared * min_perc_nodes),
int(n_bits_declared * max_perc_nodes)
)
edge_density = random.uniform(
min_connection_density, max_connection_density)
if n_bits > 1:
if force_connected:
rnd_coupling_map = create_random_connected_coupling_map(
n_nodes=n_bits, edge_density=edge_density,
force_symmetric=force_symmetric)
else:
rnd_coupling_map = create_random_coupling_map(
n_nodes=n_bits, edge_density=edge_density)
else:
rnd_coupling_map = [[e] for e in range(n_bits)]
mr_metadata["edge_density"] = edge_density
mr_metadata["new_coupling_map"] = rnd_coupling_map
class CouplingChanger(ast.NodeTransformer):
def __init__(self, new_coupling: List[List[int]]):
self.new_coupling = deepcopy(new_coupling)
def visit_Call(self, node):
if (isinstance(node, ast.Call) and
isinstance(node.func, ast.Name) and
node.func.id == "transpile"):
args = [k.arg for k in node.keywords]
if "coupling_map" in args:
initial_map = \
node.keywords[args.index("coupling_map")].value.value
ast_list = ast.parse(str(self.new_coupling)).body[0].value
node.keywords[args.index("coupling_map")].value = ast_list
print(f"Follow: coupling map changed:" +
f"{initial_map} -> {self.new_coupling}")
return node
changer = CouplingChanger(rnd_coupling_map)
modified_tree = changer.visit(tree)
changed_section = to_code(modified_tree)
sections["OPTIMIZATION_LEVEL"] = changed_section
return reconstruct_sections(sections), mr_metadata
@deprecated(version='qmt_v08', reason="You should use ChangeQubitOrder class")
def mr_change_qubit_order(source_code: str, scramble_percentage: float) -> str:
"""Change the qubit order.
"""
sections = get_sections(source_code)
source_code_circuit = sections["CIRCUIT"]
tree = ast.parse(source_code_circuit)
mr_metadata = {}
registers = get_registers_used(source_code_circuit)
# we assume to have exactly one quantum and one classical register
# and they have the same number of qubits
quantum_reg = [r for r in registers
if r["type"] == "QuantumRegister"][0]
classical_reg = [r for r in registers
if r["type"] == "ClassicalRegister"][0]
assert(quantum_reg["size"] == classical_reg["size"])
n_idx = quantum_reg["size"]
idx_to_scramble = np.random.choice(
list(range(n_idx)),
size=int(n_idx*scramble_percentage), replace=False)
mapping = create_random_mapping(idx_to_scramble)
mr_metadata["mapping"] = str(mapping)
class QubitOrderChanger(ast.NodeTransformer):
def __init__(self,
id_quantum_reg: str,
id_classical_reg: str, mapping: Dict[int, int]):
self.id_quantum_reg = id_quantum_reg
self.id_classical_reg = id_classical_reg
self.mapping = mapping
def visit_Subscript(self, node):
if (isinstance(node, ast.Subscript) and
isinstance(node.value, ast.Name) and
(node.value.id == self.id_quantum_reg or
node.value.id == self.id_classical_reg) and
isinstance(node.slice, ast.Index) and
isinstance(node.slice.value, ast.Constant) and
node.slice.value.value in self.mapping.keys()):
node.slice.value.value = self.mapping[node.slice.value.value]
return node
changer = QubitOrderChanger(
id_quantum_reg=quantum_reg["name"],
id_classical_reg=classical_reg["name"],
mapping=mapping)
modified_tree = changer.visit(tree)
print("Follow: indices mapping: ", mapping)
changed_section = to_code(modified_tree)
sections["CIRCUIT"] = changed_section
helper_function = '''
def read_str_with_mapping(bitstring, direct_mapping):
"""Given a bitstring convert it to the original mapping."""
n_bits = len(bitstring)
bitstring = bitstring[::-1]
return "".join([bitstring[direct_mapping[i]] for i in range(n_bits)])[::-1]
'''
full_mapping = {**mapping, **{
i: i for i in range(n_idx) if i not in mapping.keys()}}
conversion = '''
counts = {
read_str_with_mapping(bitstring, ''' + f"{full_mapping}" + '''): freq
for bitstring, freq in counts.items()
}
RESULT = counts
'''
sections["EXECUTION"] = sections["EXECUTION"].replace(
"RESULT = counts",
helper_function + conversion)
return reconstruct_sections(sections), mr_metadata
@deprecated(version='qmt_v08', reason="You should use InjectNullEffect class")
def mr_inject_circuits_and_inverse(
source_code: str, min_n_ops: int, max_n_ops: int,
gate_set: Dict[str, Any], fuzzer_object: str) -> str:
"""Inject a subcircuit and its inverse with a null effect overall."""
sections = get_sections(source_code)
source_code_circuit = sections["CIRCUIT"]
mr_metadata = {}
registers = get_registers_used(source_code_circuit)
# we assume to have exactly one quantum and one classical register
# and they have the same number of qubits
quantum_reg = [r for r in registers
if r["type"] == "QuantumRegister"][0]
classical_reg = [r for r in registers
if r["type"] == "ClassicalRegister"][0]
assert(quantum_reg["size"] == classical_reg["size"])
n_bits_declared = quantum_reg["size"]
n_ops = np.random.randint(min_n_ops, max_n_ops)
mr_metadata["n_ops"] = n_ops
mr_metadata["fuzzer_object"] = fuzzer_object
id_sub_circuit, subcirc_source_code = create_circuit(
id_quantum_reg=quantum_reg['name'],
id_classical_reg=classical_reg['name'],
id_circuit="subcircuit",
n_qubits=n_bits_declared,
n_ops=n_ops,
gate_set=gate_set,
generator_name=fuzzer_object,
only_circuit=True)
main_circuit_id = get_circuit_via_regex(source_code_circuit)
if main_circuit_id is None:
raise Exception("Could not find main circuit id.")
all_lines = source_code_circuit.split("\n")
lines_to_inject = subcirc_source_code.split("\n")
# the generated circuit might contain the header of a new circuit section
# remove it by removing all comment lines
lines_to_inject = [line for line in lines_to_inject
if not line.startswith("#")]
lines_to_inject.append(
f"{main_circuit_id}.append({id_sub_circuit}, qargs={quantum_reg['name']}, cargs={classical_reg['name']})")
lines_to_inject.append(
f"{main_circuit_id}.append({id_sub_circuit}.inverse(), qargs={quantum_reg['name']}, cargs={classical_reg['name']})")
mask_suitable_lines = [
line.startswith(f"{main_circuit_id}.append(")
for line in all_lines]
possible_insertion_points = np.where(np.array(mask_suitable_lines))[0]
if len(possible_insertion_points) > 0:
# sometime the main generated circuit is empty
insertion_point = np.random.choice(possible_insertion_points)
else:
insertion_point = len(all_lines) - 1
for injected_line in lines_to_inject[::-1]:
all_lines.insert(insertion_point, injected_line)
changed_section = "\n".join(all_lines)
sections["CIRCUIT"] = changed_section
return reconstruct_sections(sections), mr_metadata
@deprecated(version='qmt_v08',
reason="You should use RunIndependentPartitions class")
def mr_run_partitions_and_aggregate(source_code: str, n_partitions: int):
"""Run the n_partitions separately and aggregate.
"""
sections = get_sections(source_code)
mr_metadata = {}
circuits = get_circuits_used(circ_definition=sections["CIRCUIT"])
# replace all the occurrences of qc_main
# with the same operation by on the partitions
main_circuit = [
c for c in circuits if "main" in c["name"]][0]