|
| 1 | +from pyqubo import Array, Constraint, Placeholder |
| 2 | +import logging |
| 3 | +import time |
| 4 | +import argparse |
| 5 | +import numpy as np |
| 6 | +from timeout_decorator import timeout, TimeoutError |
| 7 | +from memory_profiler import memory_usage |
| 8 | + |
| 9 | + |
| 10 | +parser = argparse.ArgumentParser() |
| 11 | + |
| 12 | +logging.basicConfig(level=logging.INFO) |
| 13 | +logger = logging.getLogger("benchmark_tsp") |
| 14 | + |
| 15 | + |
| 16 | +def number_partition_with_timeout(n, timeout_sec): |
| 17 | + |
| 18 | + @timeout(timeout_sec) |
| 19 | + def number_partition(n): |
| 20 | + t0 = time.time() |
| 21 | + s = Array.create('s', n, 'SPIN') |
| 22 | + numbers = np.random.randint(0, 10, n) |
| 23 | + H = sum([si * ni for si, ni in zip(s, numbers)])**2 |
| 24 | + |
| 25 | + # Compile model |
| 26 | + t1 = time.time() |
| 27 | + model = H.compile() |
| 28 | + t2 = time.time() |
| 29 | + qubo, offset = model.to_qubo(index_label=False) |
| 30 | + t3 = time.time() |
| 31 | + print("len(qubo)", len(qubo)) |
| 32 | + |
| 33 | + return t1-t0, t2-t1, t3-t2 |
| 34 | + |
| 35 | + return number_partition(n) |
| 36 | + |
| 37 | +def tsp_with_timeout(n_city, timeout_sec): |
| 38 | + |
| 39 | + @timeout(timeout_sec) |
| 40 | + def tsp(n_city): |
| 41 | + t0 = time.time() |
| 42 | + x = Array.create('c', (n_city, n_city), 'BINARY') |
| 43 | + use_for_loop=False |
| 44 | + |
| 45 | + # Constraint not to visit more than two cities at the same time. |
| 46 | + time_const = 0.0 |
| 47 | + for i in range(n_city): |
| 48 | + # If you wrap the hamiltonian by Const(...), this part is recognized as constraint |
| 49 | + time_const += Constraint((sum(x[i, j] for j in range(n_city)) - 1)**2, label="time{}".format(i)) |
| 50 | + |
| 51 | + # Constraint not to visit the same city more than twice. |
| 52 | + city_const = 0.0 |
| 53 | + for j in range(n_city): |
| 54 | + city_const += Constraint((sum(x[i, j] for i in range(n_city)) - 1)**2, label="city{}".format(j)) |
| 55 | + |
| 56 | + # distance of route |
| 57 | + feed_dict = {} |
| 58 | + |
| 59 | + if use_for_loop: |
| 60 | + distance = 0.0 |
| 61 | + for i in range(n_city): |
| 62 | + for j in range(n_city): |
| 63 | + for k in range(n_city): |
| 64 | + # we set the constant distance |
| 65 | + d_ij = 10 |
| 66 | + distance += d_ij * x[k, i] * x[(k + 1) % n_city, j] |
| 67 | + |
| 68 | + else: |
| 69 | + distance = [] |
| 70 | + for i in range(n_city): |
| 71 | + for j in range(n_city): |
| 72 | + for k in range(n_city): |
| 73 | + # we set the constant distance |
| 74 | + d_ij = 10 |
| 75 | + distance.append(d_ij * x[k, i] * x[(k + 1) % n_city, j]) |
| 76 | + distance = sum(distance) |
| 77 | + |
| 78 | + print("express done") |
| 79 | + |
| 80 | + # Construct hamiltonian |
| 81 | + A = Placeholder("A") |
| 82 | + H = distance |
| 83 | + |
| 84 | + feed_dict["A"] = 1.0 |
| 85 | + |
| 86 | + # Compile model |
| 87 | + t1 = time.time() |
| 88 | + model = H.compile() |
| 89 | + t2 = time.time() |
| 90 | + qubo, offset = model.to_qubo(index_label=False, feed_dict=feed_dict) |
| 91 | + t3 = time.time() |
| 92 | + |
| 93 | + print("len(qubo)", len(qubo)) |
| 94 | + |
| 95 | + return t1-t0, t2-t1, t3-t2 |
| 96 | + |
| 97 | + return tsp(n_city) |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +def measure(problem, step, init_size, max_size, timeout): |
| 102 | + if problem == "tsp": |
| 103 | + f = tsp_with_timeout |
| 104 | + elif problem == "number_partition": |
| 105 | + f = number_partition_with_timeout |
| 106 | + |
| 107 | + for n in range(init_size, max_size+step, step): |
| 108 | + try: |
| 109 | + max_memory, (express_time, compile_time, to_qubo_time) = memory_usage((f, (n, timeout)), max_usage=True, retval=True) |
| 110 | + logger.info("Memory usage is {} MB for n={}".format(max_memory, n)) |
| 111 | + logger.info("Elapsed time is {} sec (expression: {} sec, compile: {} sec, to_qubo {} sec), for n={}".format( |
| 112 | + express_time+compile_time+to_qubo_time, express_time, compile_time, to_qubo_time, n)) |
| 113 | + |
| 114 | + except TimeoutError as e: |
| 115 | + logger.error("TimeoutError: Elapsed time exceeded {} sec for n_city={}".format(timeout, n)) |
| 116 | + break |
| 117 | + |
| 118 | + |
| 119 | +if __name__=="__main__": |
| 120 | + parser.add_argument('-p', '--problem', type=str) |
| 121 | + parser.add_argument('-m', '--max_size', type=int) |
| 122 | + parser.add_argument('-i', '--init_size', type=int) |
| 123 | + parser.add_argument('-s', '--step', type=int) |
| 124 | + parser.add_argument('-t', '--timeout', type=int) |
| 125 | + args = parser.parse_args() |
| 126 | + #number_partition_with_timeout(2000, timeout_sec=10) |
| 127 | + measure(args.problem, args.step, args.init_size, args.max_size, args.timeout) |
0 commit comments