diff --git a/training.py b/training.py index 1c91015..17066c6 100644 --- a/training.py +++ b/training.py @@ -13,6 +13,8 @@ from qiskit.converters import circuit_to_dag import networkx as nx +import sys + gdv_name = "TFL" depth_range = { "TFL" : [5 * x for x in range(1, 10)], @@ -211,6 +213,10 @@ def benchmark(depth, trail, parameters): map_test_circuit, init_time, init_map = k7mcomp.run(test_circuit) execution_time = time.time() - execution_time + if (map_test_circuit is None) and (init_map is None): + # this happens when the execution was interrupted + return optimal_depth, -1, execution_time, init_time, -1, -1 + # print(map_test_circuit.draw(output="text", fold=-1)) # tmp_circuit = map_test_circuit.decompose() tmp_circuit = map_test_circuit @@ -242,61 +248,66 @@ def benchmark(depth, trail, parameters): "total_time", "init_time", "nr_t1", "nr_t2"] -with open("circuits_analysis.csv", "w", buffering=1) as csvFile: - writer = csv.writer(csvFile) - writer.writerow(header) - print(*header) +def main(argumentList): + + with open("circuits_analysis.csv", "w", buffering=1) as csvFile: + writer = csv.writer(csvFile) + writer.writerow(header) + print(*header) + + for trail in range(10): + for depth in depth_range[gdv_name]: + analysis = (depth, trail) + circuit_analysis(depth, trail) + print(analysis) + writer.writerow(analysis) + csvFile.flush() - for trail in range(10): - for depth in depth_range[gdv_name]: - analysis = (depth, trail) + circuit_analysis(depth, trail) - print(analysis) - writer.writerow(analysis) - csvFile.flush() + with open("_private_data/training_no_analysis.csv", "w", buffering=1) as csvFile: + writer = csv.writer(csvFile) -with open("_private_data/training_no_analysis.csv", "w", buffering=1) as csvFile: - writer = csv.writer(csvFile) + writer.writerow(header) + print(*header) - writer.writerow(header) - print(*header) + for m_depth_p in range(1, nr_qubits + 2, 4): + for m_c_p in range(1, nr_qubits + 2, 4): + for b_p in range(0, 21, 2): + for c_p in range(0, 21, 5): + for div_p in range(2, 11, 4): + for cx_p in range(2, 11, 4): - for m_depth_p in range(1, nr_qubits + 2, 4): - for m_c_p in range(1, nr_qubits + 2, 4): - for b_p in range(0, 21, 2): - for c_p in range(0, 21, 5): - for div_p in range(2, 11, 4): - for cx_p in range(2, 11, 4): + for trail in range(10): + for depth in depth_range[gdv_name]: - for trail in range(10): - for depth in depth_range[gdv_name]: + parameters = { + "max_depth": m_depth_p, + "max_children": m_c_p, - parameters = { - "max_depth": m_depth_p, - "max_children": m_c_p, + "att_b": b_p, + "att_c": c_p / 20, - "att_b": b_p, - "att_c": c_p / 20, + "div_dist": div_p / 10, + "cx": cx_p, - "div_dist": div_p / 10, - "cx": cx_p, + # UNUSED + "opt_att": True, + "opt_max_t_min": False, + "qubit_increase_factor": 3, + "option_skip_cx": False, + "penalty_skip_cx": 20, + "opt_div_by_act": False, + } - # UNUSED - "opt_att": True, - "opt_max_t_min": False, - "qubit_increase_factor": 3, - "option_skip_cx": False, - "penalty_skip_cx": 20, - "opt_div_by_act": False, - } + analysis = ("n/a", "|") + res = benchmark(depth, trail, parameters) + params = ("|", m_depth_p, m_c_p, b_p, c_p, div_p, cx_p, depth, trail) + line = analysis + res + params - analysis = ("n/a", "|") - res = benchmark(depth, trail, parameters) - params = ("|", m_depth_p, m_c_p, b_p, c_p, div_p, cx_p, depth, trail) - line = analysis + res + params + print(line) + writer.writerow(line) - print(line) - writer.writerow(line) + csvFile.flush() - csvFile.flush() +if __name__ == "__main__": + main(sys.argv[1:]) \ No newline at end of file diff --git a/wrs/run.sh b/wrs/run.sh new file mode 100755 index 0000000..21d412a --- /dev/null +++ b/wrs/run.sh @@ -0,0 +1,5 @@ +#!/bin/bash +cd $1 +export PYTHONPATH=$1:PYTHONPATH +source ./venv/bin/activate +python3 "${@:2}" diff --git a/wrs/wrs.py b/wrs/wrs.py new file mode 100644 index 0000000..a82107b --- /dev/null +++ b/wrs/wrs.py @@ -0,0 +1,135 @@ +from training import depth_range, gdv_name, benchmark + +import getopt, sys +from statistics import mean +import random + + +# this is meant to be called from WRS +def main_wrs(argumentList): + unixOptions = "w:d:b:c:e:m:r:s:" + gnuOptions = ["max_breadth=", "max_depth=", "attr_b=", "attr_c=", "edge_cost=", "movement_factor=", + "use_random_circuit=", "seed="] + + try: + arguments, values = getopt.getopt(argumentList, unixOptions, gnuOptions) + except getopt.error as err: + # output error, and return with an error code + print(str(err)) + sys.exit(2) + + argumentsDict = dict(arguments) + + # if this is true, a single circuit randomly chosen will be used for evaluation + use_random_circuit = bool(getValue(argumentsDict, '-r', '--use_random_circuit', False)) + + # the random generator seed + seed = int(getValue(argumentsDict, '-s', '--seed', 1234567890)) + + # 1 to 55 increment of 1 + max_breadth = int(getValue(argumentsDict, '-w', '--max_breadth', 1)) + + # 2 to 55 increment of 1 + max_depth = int(getValue(argumentsDict, '-d', '--max_depth', 2)) + + # -50 to 50 increment of 0.1 + attr_b = float(getValue(argumentsDict, '-b', '--attr_b', -50)) + + # -1 to 1 increment of 0.1 + attr_c = float(getValue(argumentsDict, '-c', '--attr_c', -1)) + + # 0.1 to 1 increment of 0.1 + edge_cost = float(getValue(argumentsDict, '-e', '--edge_cost', 0.1)) + + # 1 to 55 increment of 1 + movement_factor = int(getValue(argumentsDict, '-m', '--movement_factor', 1)) + + print("-----") + print("Evaluating for max_breadth={}, max_depth={}, attr_b={}, attr_c={}, edge_cost={}, movement_factor={}".format( + max_breadth, + max_depth, + attr_b, + attr_c, + edge_cost, + movement_factor + )) + + # compute target value (score) here !!! + parameters = { + "max_depth": max_depth, + "max_children": max_breadth, + + "att_b": attr_b, + "att_c": attr_c, + + "cx": edge_cost, + "div_dist": movement_factor, + + # UNUSED + "opt_att": True, + "opt_max_t_min": False, + "qubit_increase_factor": 3, + "option_skip_cx": False, + "penalty_skip_cx": 20, + "opt_div_by_act": False, + "TIME_LIMIT": 10 # seconds + } + + scores = [] + + test = False + target_value = 0.0 + + if not test: + + if use_random_circuit: + # maintain reproducibility + random.seed(seed) + trail = random.randint(0, 10) + depthindex = random.randint(0, len(depth_range[gdv_name])) + depth = depth_range[gdv_name][depthindex] + # return optimal_depth, depth_result, execution_time, init_time, nr_t1, nr_t2 + res = benchmark(depth, trail, parameters) + # since we know optimal_depth and depth_result I guess it would make sense to optimize their square diff. + score = (res[0] - res[1]) ** 2 + scores.append(score) + print(res) + + for trail in range(10): + for depth in depth_range[gdv_name]: + # return optimal_depth, depth_result, execution_time, init_time, nr_t1, nr_t2 + res = benchmark(depth, trail, parameters) + # since we know optimal_depth and depth_result I guess it would make sense to optimize their square diff. + if res[1] == -1: + # could not optimiza the circuit + target_value = sys.maxsize + print("Target value is {}".format(target_value)) + print("______") + + # This is to return the value to the caller + sys.stdout.write(str(target_value)) + sys.stdout.flush() + sys.exit(0) + + score = (res[0] - res[1]) ** 2 + scores.append(score) + print(res) + + # optimizing avg((optimal_depth-depth_result)**2) + target_value = mean(scores) + + print("Target value is {}".format(target_value)) + print("______") + + # This is to return the value to the caller + sys.stdout.write(str(target_value)) + sys.stdout.flush() + sys.exit(0) + + +def getValue(dictionary, shortKey, longKey, default): + return dictionary.get(shortKey, dictionary.get(longKey, default)) + + +if __name__ == "__main__": + main_wrs(sys.argv[1:])