|
| 1 | +import math |
| 2 | +import pickle |
| 3 | +import sys |
| 4 | +import os |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import networkx as nx |
| 8 | +import matplotlib.pyplot as plt |
| 9 | + |
| 10 | +from qaoa import QAOA, mixers, initialstates # type: ignore |
| 11 | +from qaoa.initialstates import MaxKCutFeasible |
| 12 | +from qaoa.mixers import MaxKCutGrover, MaxKCutLX, XYTensor |
| 13 | +from qaoa.problems import MaxKCutBinaryPowerOfTwo, MaxKCutBinaryFullH |
| 14 | + |
| 15 | +from qiskit_algorithms.optimizers import SPSA, COBYLA, ADAM, NFT, NELDER_MEAD |
| 16 | + |
| 17 | +from qiskit_aer import AerSimulator |
| 18 | + |
| 19 | + |
| 20 | +def main( |
| 21 | + method, |
| 22 | + k, |
| 23 | + clf, |
| 24 | + mixerstr, |
| 25 | + casename, |
| 26 | + maxdepth, |
| 27 | + shots, |
| 28 | + ): |
| 29 | + |
| 30 | + angles = {"gamma": [0, 2 * np.pi, 20], "beta": [0, 2 * np.pi, 20]} |
| 31 | + optimizer = [COBYLA, {"maxiter": 100, "tol": 1e-3, "rhobeg": 0.05}] |
| 32 | + problem_encoding = "binary" |
| 33 | + |
| 34 | + if casename == "Barbell": |
| 35 | + V = np.arange(0, 2, 1) |
| 36 | + E = [(0, 1, 1.0)] |
| 37 | + G = nx.Graph() |
| 38 | + G.add_nodes_from(V) |
| 39 | + G.add_weighted_edges_from(E) |
| 40 | + elif casename == "BarabasiAlbert": |
| 41 | + G = nx.read_gml("data/w_ba_n10_k4_0.gml") |
| 42 | + # max_val = np.array([8.657714089848158, 10.87975400338161, 11.059417685176726, 11.059417685176726, 11.059417685176726, 11.059417685176726, 11.059417685176726]) |
| 43 | + elif casename == "ErdosRenyi": |
| 44 | + G = nx.read_gml("data/er_n10_k4_0.gml") |
| 45 | + # max_val = np.array([12, 16, 16, 16, 16, 16, 16]) |
| 46 | + |
| 47 | + string_identifier = ( |
| 48 | + "method" |
| 49 | + + str(method) |
| 50 | + + "_" |
| 51 | + + "k" |
| 52 | + + str(k) |
| 53 | + + "_" |
| 54 | + + "clf" |
| 55 | + + str(clf) |
| 56 | + + "_" |
| 57 | + + "mixer" |
| 58 | + + str(mixerstr) |
| 59 | + + "_" |
| 60 | + "casename" |
| 61 | + + str(casename) |
| 62 | + + "_" |
| 63 | + + "shots" |
| 64 | + + str(shots) |
| 65 | + ) |
| 66 | + print("Now running", string_identifier) |
| 67 | + |
| 68 | + if k == 3: |
| 69 | + kf = 4 |
| 70 | + elif k in [5,6,7]: |
| 71 | + kf = 8 |
| 72 | + |
| 73 | + if method == "fullH": |
| 74 | + problem = MaxKCutBinaryFullH( |
| 75 | + G, |
| 76 | + k, |
| 77 | + color_encoding=clf, |
| 78 | + ) |
| 79 | + |
| 80 | + if mixerstr == "X": |
| 81 | + mixer = mixers.X() |
| 82 | + else: |
| 83 | + mixer = MaxKCutGrover( |
| 84 | + kf, |
| 85 | + problem_encoding=problem_encoding, |
| 86 | + color_encoding="all", |
| 87 | + tensorized=True, |
| 88 | + ) |
| 89 | + |
| 90 | + initialstate = initialstates.Plus() |
| 91 | + |
| 92 | + else: |
| 93 | + problem = MaxKCutBinaryPowerOfTwo( |
| 94 | + G, |
| 95 | + kf, |
| 96 | + ) |
| 97 | + |
| 98 | + if mixerstr == "LX": |
| 99 | + mixer = MaxKCutLX(k, color_encoding="LessThanK") |
| 100 | + elif mixerstr == "Grover": |
| 101 | + mixer = MaxKCutGrover( |
| 102 | + k, |
| 103 | + problem_encoding=problem_encoding, |
| 104 | + color_encoding="LessThanK", |
| 105 | + tensorized=False, |
| 106 | + ) |
| 107 | + else: |
| 108 | + mixer = MaxKCutGrover( |
| 109 | + k, |
| 110 | + problem_encoding=problem_encoding, |
| 111 | + color_encoding="LessThanK", |
| 112 | + tensorized=True, |
| 113 | + ) |
| 114 | + initialstate = MaxKCutFeasible( |
| 115 | + k, problem_encoding=problem_encoding, color_encoding="LessThanK" |
| 116 | + ) |
| 117 | + |
| 118 | + fn = string_identifier + ".pickle" |
| 119 | + if os.path.exists(fn): |
| 120 | + try: |
| 121 | + with open(fn, "rb") as f: |
| 122 | + qaoa = pickle.load(f) |
| 123 | + except ValueError: |
| 124 | + print("file exists, but can not open it", fn) |
| 125 | + else: |
| 126 | + qaoa = QAOA( |
| 127 | + problem=problem, |
| 128 | + initialstate=initialstate, |
| 129 | + mixer=mixer, |
| 130 | + backend=AerSimulator(method="automatic", device="GPU"), |
| 131 | + shots=shots, |
| 132 | + optimizer=optimizer, |
| 133 | + sequential=True, |
| 134 | + ) |
| 135 | + |
| 136 | + qaoa.optimize(maxdepth, angles=angles) |
| 137 | + |
| 138 | + with open(fn, "wb") as f: |
| 139 | + pickle.dump(qaoa, f) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + |
| 144 | + method = str(sys.argv[1]) |
| 145 | + k = int(sys.argv[2]) |
| 146 | + clf = str(sys.argv[3]) |
| 147 | + mixerstr = str(sys.argv[4]) |
| 148 | + casename = str(sys.argv[5]) |
| 149 | + maxdepth = int(sys.argv[6]) |
| 150 | + shots = int(sys.argv[7]) |
| 151 | + |
| 152 | + main( |
| 153 | + method, |
| 154 | + k, |
| 155 | + clf, |
| 156 | + mixerstr, |
| 157 | + casename, |
| 158 | + maxdepth, |
| 159 | + shots, |
| 160 | + ) |
0 commit comments