-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessFiles.py
94 lines (70 loc) · 3.37 KB
/
processFiles.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
import typing
import glob, os, csv
from parsing import CQASMParser
from metrics.longestRepeatingSubcircuit import longestRepeatingSubcircuit
from metrics.paths import getPathStats
from multiprocessing import Pool, Value
OUTPUT_FILE = os.path.dirname(os.path.realpath(__file__)) + "/result.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__)) + "/metrics/data/*.qasm")
# files = ["/shares/bulk/plehenaff/cQASM-tools/metrics/data/q=11_s=89_2qbf=022_1.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)