-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocessFiles.py
More file actions
93 lines (71 loc) · 3.32 KB
/
processFiles.py
File metadata and controls
93 lines (71 loc) · 3.32 KB
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
import typing
import glob, os, csv
import CQASMParser
from longestRepeatingSubcircuit import longestRepeatingSubcircuit
from paths import getPathStats
from multiprocessing import Pool, Value
from os import listdir
from os.path import isfile, join
OUTPUT_FILE = os.path.dirname(os.path.realpath(__file__)) + "/GDG+_ketgpt.csv"
# MAX_NUM_LINES = 50000
# files = []
# for f in glob.glob(os.path.dirname(os.path.realpath(__file__)) + "/metrics/data/*.qasm"):
# num_lines = sum(1 for _ in open(f))
# if num_lines > MAX_NUM_LINES:
# files.append(f)
files = glob.glob(os.path.dirname(os.path.realpath(__file__)) + "/generated_qasm_files_cqasm_new/*.qasm")
counter = Value('i', 0)
statistics = sorted([
"FileName",
"SubcircuitIndex",
"LengthOfLongestRepeatingSubcircuit",
"NumberOfRepetitionsOfLongestRepeatingSubcircuit",
"NumberOfGatesInCriticalPath",
"MaxNumberOfTwoQubitGatesInCriticalPath",
"NumberOfCriticalPaths",
"NumberOfCriticalPathsWithMaxTwoQubitsGates",
"PathLengthMean",
"PathLengthStandardDeviation",
])
def writeToFile(data: dict):
with open(OUTPUT_FILE, "a") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=statistics)
assert(sorted(data.keys()) == statistics)
writer.writerow(data)
global counter
counter.value += 1
print(f"Done {counter.value} / {len(files)} files")
def processFile(fileName):
try:
ast = CQASMParser.parseCQASMFile(fileName)
for index, subcircuit in enumerate(ast.subcircuits):
repeatingSubcircuitStats = longestRepeatingSubcircuit(subcircuit.instructions)
lengthOfLongestRepeatingSubcircuit = len(repeatingSubcircuitStats["LongestRepeatingSubcircuit"])
numberOfRepetitionsOfLongestRepeatingSubcircuit = repeatingSubcircuitStats["NumberOfRepetitionsOfLongestRepeatingSubcircuit"]
pathStats = getPathStats(subcircuit.instructions)
thisSubcircuitData = {
"FileName": os.path.basename(fileName),
"SubcircuitIndex": index,
"LengthOfLongestRepeatingSubcircuit": lengthOfLongestRepeatingSubcircuit,
"NumberOfRepetitionsOfLongestRepeatingSubcircuit": numberOfRepetitionsOfLongestRepeatingSubcircuit,
"NumberOfGatesInCriticalPath": pathStats["NumberOfGatesInCriticalPath"],
"MaxNumberOfTwoQubitGatesInCriticalPath": pathStats["MaxNumberOfTwoQubitGatesInCriticalPath"],
"NumberOfCriticalPaths": pathStats["NumberOfCriticalPaths"],
"NumberOfCriticalPathsWithMaxTwoQubitsGates": pathStats["NumberOfCriticalPathsWithMaxTwoQubitsGates"],
"PathLengthMean": pathStats["PathLengthMean"],
"PathLengthStandardDeviation": pathStats["PathLengthStandardDeviation"],
}
writeToFile(thisSubcircuitData)
except Exception as e:
print(f"File {fileName} gave error: {e} and was not processed")
return
if __name__ == "__main__":
# print(f"Will process files: {files}")
with open(OUTPUT_FILE, "w") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=statistics)
writer.writeheader()
# for f in files:
# print(f"processing {f}")
# processFile(f)
with Pool(8, initargs = (counter, )) as p:
p.map(processFile, files)