Skip to content

Commit

Permalink
QMOA experiments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Edric-Matwiejew committed Nov 6, 2022
1 parent e81961f commit dc5afe5
Show file tree
Hide file tree
Showing 21 changed files with 2,233 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/multivariable/experiments/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import test_function

__all__ = ["test_function"]
102 changes: 102 additions & 0 deletions examples/multivariable/experiments/circulant_mixers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import sys

sys.path.append("../../")
import os
from time import time
from pathlib import Path
from mpi4py import MPI
import numpy as np
import test_function
from quop_mpi.algorithm import qmoa
from quop_mpi.__lib import fCQAOA

nn = int(os.getenv("N"))
time_limit = int(os.getenv("TIMELIMITSECONDS"))
repeats = int(os.getenv("REPEATS"))
pmax = int(os.getenv("PMAX"))
maxiter = int(os.getenv("MAXITER"))
d = int(os.getenv("D"))
funcint = int(os.getenv("FUNCINT"))

if d == 2:
function = test_function.functions[funcint]
else:
function = test_function.functions_d3[funcint]

for c in [1, 2, 4, 8, 16]:

output = f"results/circulant_mixers/d={d} n={nn}/{c}"
basename = f"{output}/{function.name} d={d} n={nn} c={c}"
suspend_path = "suspend_data/circulant_mixers"

if MPI.COMM_WORLD.Get_rank() == 0:
Path(output).mkdir(parents=True, exist_ok=True)
Path(suspend_path).mkdir(parents = True, exist_ok = True)

n = 2 ** nn # number of grid point per dimension

L = np.diff(function.search_domain(2)[0])[0] / 2

dq = 2 * L / n # position space grid spacing
Ns = d * [n] # shape of d-dimensional grid
deltas = np.array(d * [dq], dtype=np.float64)
mins = np.array(d * [-(n / 2) * dq], dtype=np.float64)

strides = np.empty(len(Ns), dtype=int)
strides[-1] = 1
for i in range(len(Ns) - 2, -1, -1):
strides[i] = strides[i + 1] * Ns[i]

def cost_function(local_i, local_i_offset, MPI_COMM, function = None):

x = fCQAOA.continuous.gen_local_grid( n**d,
Ns,
strides,
deltas,
mins,
local_i_offset,
local_i)
f = []
for point in x:
f.append(function(point))

f = np.array(f, dtype = np.float64)

f_max = MPI_COMM.allreduce(np.max(f), op = MPI.MAX)
f_min = MPI_COMM.allreduce(np.min(f), op = MPI.MIN)


return 100*(f - f_min)/(f_max - f_min)

alg = qmoa(Ns, deltas, mins)
alg.set_mixer(d * [c])
alg.UQ.operator_function = cost_function
alg.UQ.operator_kwargs = {"function": function}
alg.set_observables(0)

alg.set_optimiser(
"scipy",
{"method": "Nelder-Mead", "options": {"maxiter": maxiter, "adaptive": True}},
["fun", "nfev", "success"],
)

alg.set_log(f"{basename}", f"d={d}, n={n}, c={c}", action="w")


start_time = time()

alg.benchmark(
range(1, pmax + 1),
repeats,
param_persist=True,
filename=f"{basename}",
label=f"d={d}, n={n}, c={c}",
save_action="w",
verbose=True,
time_limit = time_limit,
suspend_path = f"{suspend_path}/{function.name} d={d} n={nn} c={c}",
)

end_time = time()

time_limit -= end_time - start_time
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import sys

sys.path.append("../../")
import os
from time import time
from pathlib import Path
from mpi4py import MPI
import numpy as np
import test_function
from quop_mpi.algorithm import qmoa
from quop_mpi.__lib import fCQAOA

nn = int(os.getenv("N"))
time_limit = int(os.getenv("TIMELIMITSECONDS"))
repeats = int(os.getenv("REPEATS"))
pmax = int(os.getenv("PMAX"))
maxiter = int(os.getenv("MAXITER"))
d = int(os.getenv("D"))
funcint = int(os.getenv("FUNCINT"))

if d == 2:
function = test_function.functions[funcint]
else:
function = test_function.functions_d3[funcint]

for c in [1, 2, 4, 8, 16]:

output = f"results/circulant_mixers_non_independent_t/d={d} n={nn}/{c}"
basename = f"{output}/{function.name} d={d} n={nn} c={c}"
suspend_path = "suspend_data/circulant_mixers"

if MPI.COMM_WORLD.Get_rank() == 0:
Path(output).mkdir(parents=True, exist_ok=True)
Path(suspend_path).mkdir(parents = True, exist_ok = True)

n = 2 ** nn # number of grid point per dimension

L = np.diff(function.search_domain(2)[0])[0] / 2

dq = 2 * L / n # position space grid spacing
Ns = d * [n] # shape of d-dimensional grid
deltas = np.array(d * [dq], dtype=np.float64)
mins = np.array(d * [-(n / 2) * dq], dtype=np.float64)

strides = np.empty(len(Ns), dtype=int)
strides[-1] = 1
for i in range(len(Ns) - 2, -1, -1):
strides[i] = strides[i + 1] * Ns[i]

def cost_function(local_i, local_i_offset, MPI_COMM, function = None):

x = fCQAOA.continuous.gen_local_grid( n**d,
Ns,
strides,
deltas,
mins,
local_i_offset,
local_i)
f = []
for point in x:
f.append(function(point))

f = np.array(f, dtype = np.float64)

f_max = MPI_COMM.allreduce(np.max(f), op = MPI.MAX)
f_min = MPI_COMM.allreduce(np.min(f), op = MPI.MIN)


return 100*(f - f_min)/(f_max - f_min)

alg = qmoa(Ns, deltas, mins)
alg.set_mixer(d * [c])
alg.UQ.operator_function = cost_function
alg.UQ.operator_kwargs = {"function": function}
alg.set_observables(0)

alg.set_independent_t(False)

alg.set_optimiser(
"scipy",
{"method": "Nelder-Mead", "options": {"maxiter": maxiter, "adaptive": True}},
["fun", "nfev", "success"],
)

alg.set_log(f"{basename}", f"d={d}, n={n}, c={c}", action="w")

start_time = time()

alg.benchmark(
range(1, pmax + 1),
repeats,
param_persist=True,
filename=f"{basename}",
label=f"d={d}, n={n}, c={c}",
save_action="w",
verbose=True,
time_limit = time_limit,
suspend_path = f"{suspend_path}/{function.name} d={d} n={n} c={c} non independent t"
)

end_time = time()

time_limit -= max([0, end_time - start_time])
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import sys
sys.path.append("../../")
import os
from pathlib import Path
import numpy as np
from quop_mpi.algorithm import qmoa
from quop_mpi.algorithm import qwoa
from quop_mpi.__lib import fCQAOA
import test_function
from mpi4py import MPI

repeats = 1
nn = 4

pmax = int(os.getenv("PMAX"))
d = int(os.getenv("DMIN"))

function = test_function.rastrigin

output_dir = f"results/commuting_and_non_commuting_permutations/{function.name} d={d} n={nn}"

if MPI.COMM_WORLD.Get_rank() == 0:

Path(output_dir).mkdir(parents=True, exist_ok=True)

n = 2 ** nn # number of grid point per dimension

L = np.diff(function.search_domain(2)[0])[0] / 2

dq = 2 * L / n # position space grid spacing
Ns = d * [n] # shape of d-dimensional grid
deltasq = np.array(d * [dq], dtype=np.float64)
minsq = np.array(d * [-(n / 2) * dq], dtype=np.float64)

strides = np.empty(len(Ns), dtype=int)
strides[-1] = 1

for i in range(len(Ns) - 2, -1, -1):
strides[i] = strides[i + 1] * Ns[i]

permute = False
mix = False

def cost_function(local_i, local_i_offset, MPI_COMM, function = None):

if MPI_COMM.Get_rank() == 0:
position_grid = fCQAOA.continuous.gen_local_grid(
n ** d, Ns, strides, deltasq, minsq, 0, n ** d
)

f = []
for point in position_grid:
f.append(function(point))

f = np.array(f, dtype=np.float64)

if permute:
f = np.reshape(f, Ns)
f = f[np.random.permutation(f.shape[0]), :]
f = f[:, np.random.permutation(f.shape[1])]
f = f.flatten()

if mix:
np.random.shuffle(f)
else:

f = None

return MPI_COMM.bcast(f, root=0)[local_i_offset : local_i_offset + local_i]


alg = qmoa(Ns, deltasq, minsq)
alg.UQ.operator_function = cost_function
alg.UQ.operator_kwargs = {"function": function}
alg.set_observables(0)

alg.set_optimiser(
"scipy",
{"method": "Nelder-Mead", "options": {"maxiter": 100000, "adaptive": True}},
["fun", "nfev", "success"],
)

alg.set_log(f"{output_dir}/baseline", f"{function.name}", action="w")

alg.benchmark(
range(1, pmax + 1),
repeats,
param_persist=True,
filename=f"{output_dir}/baseline",
label=f"{function.name}",
save_action="w",
verbose=True,
)


permute = True

alg = qmoa(Ns, deltasq, minsq)
alg.UQ.operator_function = cost_function
alg.UQ.operator_kwargs = {"function": function}
alg.set_observables(0)

alg.set_log(f"{output_dir}/commuting", f"{function.name}", action="w")

alg.benchmark(
range(1, pmax + 1),
repeats,
param_persist=True,
filename=f"{output_dir}/commuting",
label=f"{function.name}",
save_action="w",
verbose=True,
)

permuted = False
mix = True

alg = qmoa(Ns, deltasq, minsq)
alg.UQ.operator_function = cost_function
alg.UQ.operator_kwargs = {"function": function}
alg.set_observables(0)

alg.set_log(f"{output_dir}/non_commuting", f"{function.name}", action="w")

alg.benchmark(
range(1, pmax + 1),
repeats,
param_persist=True,
filename=f"{output_dir}/non_commuting",
label=f"{function.name}",
save_action="w",
verbose=True,
)

permuted = False
mix = False

alg = qwoa(n ** d)

alg.set_qualities(cost_function, kwargs = {"function":function})

alg.set_optimiser(
"scipy",
{"method": "Nelder-Mead", "options": {"maxiter": 100000, "adaptive": True}},
["fun", "nfev", "success"],
)

alg.set_log(f"{output_dir}/qaoa", f"{function.name}", action="w")

alg.benchmark(
range(1, pmax + 1),
repeats,
param_persist=True,
filename=f"{output_dir}/qaoa",
label="qaoa",
save_action="w",
verbose=True,
)
Loading

0 comments on commit dc5afe5

Please sign in to comment.