Skip to content

Commit 8244361

Browse files
committed
scripts
1 parent 01fcc43 commit 8244361

File tree

4 files changed

+222
-1
lines changed

4 files changed

+222
-1
lines changed

qaoa/mixers/maxkcut_grover_mixer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(
1717
"k_cuts must be 2 or more, and is not implemented for k_cuts > 8"
1818
)
1919
if not problem_encoding in ["onehot", "binary"]:
20-
raise ValueError('case must be in ["onehot", "binary"]')
20+
raise ValueError('problem_encoding must be in ["onehot", "binary"]')
2121
self.k_cuts = k_cuts
2222
self.problem_encoding = problem_encoding
2323
self.color_encoding = color_encoding

run_all.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
problem_encoding="binary"
4+
5+
maxdepth=1
6+
shots=100000
7+
8+
9+
for casename in {"ErdosRenyi","BarabasiAlbert"}
10+
#for casename in {"Barbell",}
11+
do
12+
13+
for k in {3,5,6,7}
14+
do
15+
16+
#case full H
17+
18+
if [ "$k" -eq 5 ] || [ "$k" -eq 6 ]; then
19+
clf_options=("LessThanK" "max_balanced")
20+
else
21+
clf_options=("LessThanK")
22+
fi
23+
24+
for clf in "${clf_options[@]}"
25+
do
26+
27+
for mixer in {"X","Grovertensorized"}
28+
do
29+
30+
echo "fullH" $k $clf $mixer $casename $maxdepth $shots
31+
bash run_graphs.sh "fullH" $k $clf $mixer $casename $maxdepth $shots
32+
done
33+
done
34+
35+
#case subspace
36+
37+
for mixer in {"LX","Grover","Grovertensorized"}
38+
do
39+
40+
echo "subH" $k "None" $mixer $casename $maxdepth $shots
41+
bash run_graphs.sh "subH" $k "None" $mixer $casename $maxdepth $shots
42+
done
43+
44+
echo "------"
45+
46+
done
47+
48+
49+
done
50+

run_graphs.py

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
)

run_graphs.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
#SBATCH --job-name=run_graphs
3+
# d-hh:mm:ss
4+
#SBATCH --time=30-00:00:00
5+
#SBATCH --output=/home/franzf/MaxKCut/%j.out
6+
#SBATCH --nodes=1
7+
#SBATCH --tasks-per-node=1
8+
#SBATCH --cpus-per-task=1
9+
10+
python run_graphs.py "$1" "$2" "$3" "$4" "$5" "$6" "$7"
11+

0 commit comments

Comments
 (0)