Skip to content

Commit 88b0d15

Browse files
author
Zichang He
committed
update gpumpi tests
1 parent c952264 commit 88b0d15

2 files changed

Lines changed: 18 additions & 24 deletions

File tree

tests/gpumpi_tests.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from qokit.sk import sk_obj, get_random_J
99
from qokit.qaoa_objective_sk import get_qaoa_sk_objective
1010
from qokit.utils import precompute_energies
11+
from qokit.parameter_utils import get_sk_gamma_beta, get_fixed_gamma_beta
1112
from pathlib import Path
1213

1314
SCRIPT_DIR = Path(__file__).resolve().parent
@@ -29,10 +30,10 @@ def gpumpi_labs_test(N=10, p=4):
2930
elif objective == "expectation":
3031
assert np.isclose(f_gpumpi_labs(gamma, beta), f_c_labs(gamma, beta))
3132

32-
def gpumpi_maxcut_test(N=16, p=4, seed=1):
33+
def gpumpi_maxcut_test(N=12, p=3, seed=1):
3334
d = 3
3435
G = nx.random_regular_graph(d, N, seed=seed)
35-
gamma, beta = np.random.rand(p), np.random.rand(p)
36+
gamma, beta = get_fixed_gamma_beta(d, p)
3637
obj_maxcut = partial(maxcut_obj, w=get_adjacency_matrix(G))
3738
precomputed_energies = precompute_energies(obj_maxcut, N)
3839
for objective in ["overlap", "expectation"]:
@@ -45,13 +46,14 @@ def gpumpi_maxcut_test(N=16, p=4, seed=1):
4546
o2_gpumpi_maxcut = get_qaoa_maxcut_objective(
4647
N, p, G=G, simulator="gpumpi", parameterization="gamma beta", objective=objective
4748
)(gamma, beta)
48-
assert np.isclose(o1_c_maxcut, o1_gpumpi_maxcut)
49-
assert np.isclose(o1_c_maxcut, o2_gpumpi_maxcut)
49+
50+
assert np.isclose(o1_c_maxcut, o1_gpumpi_maxcut.real)
51+
assert np.isclose(o1_c_maxcut, o2_gpumpi_maxcut.real)
5052

5153
def gpumpi_sk_test(N=10, p=3, seed=42):
5254

5355
J = get_random_J(N=N, seed=seed)
54-
gamma, beta = np.random.rand(p), np.random.rand(p)
56+
gamma, beta = get_sk_gamma_beta(p)
5557
obj_sk = partial(sk_obj, J=J)
5658
precomputed_energies = precompute_energies(obj_sk, N)
5759
for objective in ["overlap", "expectation"]:
@@ -64,8 +66,9 @@ def gpumpi_sk_test(N=10, p=3, seed=42):
6466
o2_gpumpi_sk = get_qaoa_sk_objective(
6567
N, p, J=J, simulator="gpumpi", parameterization="gamma beta", objective="overlap"
6668
)(gamma, beta)
67-
assert np.isclose(o1_c_sk, o1_gpumpi_sk)
68-
assert np.isclose(o1_c_sk, o2_gpumpi_sk)
69+
70+
assert np.isclose(o1_c_sk, o1_gpumpi_sk.real)
71+
assert np.isclose(o1_c_sk, o2_gpumpi_sk.real)
6972

7073
if __name__ == "__main__":
7174

tests/test_gpumpi.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import networkx as nx
55
from functools import partial
66
import subprocess
7-
87
from qokit.qaoa_objective_labs import get_qaoa_labs_objective
98
from qokit.fur.mpi_nbcuda.qaoa_simulator import mpi_available
109
from qokit.fur import get_available_simulator_names
@@ -14,7 +13,7 @@
1413
from qokit.qaoa_objective_sk import get_qaoa_sk_objective
1514
from qokit.utils import precompute_energies
1615
from qokit.parameter_utils import get_sk_gamma_beta, get_fixed_gamma_beta
17-
16+
from pathlib import Path
1817

1918
simulators = get_available_simulator_names("x")
2019

@@ -32,27 +31,19 @@
3231
is_mpi_available = mpi_available()
3332
is_gpumpi = "gpumpi" in simulators
3433
is_gpu = "gpu" in simulators
35-
34+
SCRIPT_DIR = Path(__file__).resolve().parent
3635

3736
@pytest.mark.skipif(not is_nvlink, reason="NVLINK not available.")
3837
def test_gpumpi(n_procs=n_gpumpi):
39-
result = subprocess.run(["mpirun", "-np", str(n_procs), "python", "./tests/gpumpi_tests.py"], capture_output=True, text=True, check=True)
40-
output_lines = result.stdout.strip().split("\n")
41-
output = [item == "True" for item in output_lines]
42-
assert all(output)
43-
38+
result = subprocess.run(["mpirun", "-np", str(n_procs), "python", f"{SCRIPT_DIR}/gpumpi_tests.py"], capture_output=True, text=True, check=True)
4439

4540
@pytest.mark.skipif(not is_gpu, reason="GPU simulator not available.")
4641
def test_gpumpi_singleprocs():
47-
result = subprocess.run(["python", "./tests/gpumpi_tests.py"], capture_output=True, text=True, check=True)
48-
output_lines = result.stdout.strip().split("\n")
49-
output = [item == "True" for item in output_lines]
50-
assert all(output)
51-
42+
result = subprocess.run(["python", f"{SCRIPT_DIR}/gpumpi_tests.py"], capture_output=True, text=True, check=True)
5243

5344
@pytest.mark.skipif(not is_gpu, reason="GPU simulator not available.")
5445
def test_gpu_labs(N=16, p=4, seed=1):
55-
df = pd.read_json("./qokit/assets/QAOA_with_fixed_parameters_p_opt.json", orient="index")
46+
df = pd.read_json(f"{SCRIPT_DIR.parent}/qokit/assets/QAOA_with_fixed_parameters_p_opt.json", orient="index")
5647
row = df[(df["N"] == N) & (df["p"] == p)]
5748
beta = row["beta"].values[0]
5849
gamma = row["gamma"].values[0]
@@ -64,7 +55,7 @@ def test_gpu_labs(N=16, p=4, seed=1):
6455

6556

6657
@pytest.mark.skipif(not is_gpu, reason="GPU simulator not available.")
67-
def test_gpu_maxcut(N=16, p=4, seed=1):
58+
def test_gpu_maxcut(N=12, p=3, seed=1):
6859
d = 3
6960
G = nx.random_regular_graph(d, N, seed=seed)
7061
obj_maxcut = partial(maxcut_obj, w=get_adjacency_matrix(G))
@@ -82,8 +73,8 @@ def test_gpu_maxcut(N=16, p=4, seed=1):
8273

8374

8475
@pytest.mark.skipif(not is_gpu, reason="GPU simulator not available.")
85-
def test_gpu_sk(N=16, p=4, seed=1):
86-
J = get_random_J(N=N)
76+
def test_gpu_sk(N=10, p=3, seed=42):
77+
J = get_random_J(N=N, seed=seed)
8778
obj_sk = partial(sk_obj, J=J)
8879
precomputed_energies = precompute_energies(obj_sk, N)
8980
gamma, beta = get_sk_gamma_beta(p)

0 commit comments

Comments
 (0)