Skip to content

Commit d255fc4

Browse files
committed
Add tests for several notebooks
1 parent 50eef07 commit d255fc4

12 files changed

+292
-2
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from tests.utils_for_testbook import (
2+
validate_quantum_program_size,
3+
validate_quantum_model,
4+
wrap_testbook,
5+
)
6+
from testbook.client import TestbookNotebookClient
7+
8+
9+
@wrap_testbook(
10+
"3_sat_grover",
11+
timeout_seconds=36,
12+
)
13+
def test_notebook(tb: TestbookNotebookClient) -> None:
14+
# test models
15+
validate_quantum_model(tb.ref("qmod"))
16+
validate_quantum_model(tb.ref("qmod_large"))
17+
# test quantum programs
18+
validate_quantum_program_size(
19+
tb.ref("qprog"),
20+
expected_width=10, # actual width: 10
21+
expected_depth=450, # actual depth: 429
22+
)
23+
validate_quantum_program_size(
24+
tb.ref("qprog_large"),
25+
expected_width=20, # actual width: 19
26+
expected_depth=2500, # actual depth: 2375
27+
)
28+
29+
# test notebook content
30+
pass # TODO

tests/notebooks/test_bernstein_vazirani.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
@wrap_testbook("bernstein_vazirani", timeout_seconds=20)
10-
def test_bernstein_vazirani(tb: TestbookNotebookClient) -> None:
10+
def test_notebook(tb: TestbookNotebookClient) -> None:
1111
# test models
1212
validate_quantum_model(tb.ref("qmod"))
1313
# test quantum programs
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from tests.utils_for_testbook import (
2+
validate_quantum_program_size,
3+
validate_quantum_model,
4+
wrap_testbook,
5+
)
6+
from testbook.client import TestbookNotebookClient
7+
8+
9+
@wrap_testbook("deutsch_jozsa", timeout_seconds=48)
10+
def test_notebook(tb: TestbookNotebookClient) -> None:
11+
# test models
12+
validate_quantum_model(tb.ref("qmod"))
13+
# test quantum programs
14+
validate_quantum_program_size(
15+
tb.ref("qprog"), expected_width=None, expected_depth=None
16+
)
17+
18+
# test notebook content
19+
pass
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from tests.utils_for_testbook import (
2+
validate_quantum_program_size,
3+
validate_quantum_model,
4+
wrap_testbook,
5+
)
6+
from testbook.client import TestbookNotebookClient
7+
import numpy as np
8+
import pandas as pd
9+
from scipy.special import rel_entr
10+
11+
12+
@wrap_testbook("discrete_log", timeout_seconds=600)
13+
def test_notebook(tb: TestbookNotebookClient) -> None:
14+
# test models
15+
validate_quantum_model(tb.ref("qmod_Z5"))
16+
validate_quantum_model(tb.ref("qmod_Z13"))
17+
# test quantum programs
18+
validate_quantum_program_size(
19+
tb.ref("qprog_Z5"),
20+
expected_width=12, # actual width: 12
21+
expected_depth=5000, # actual depth: 4658
22+
)
23+
validate_quantum_program_size(
24+
tb.ref("qprog_Z13"),
25+
expected_width=22, # actual width: 20
26+
expected_depth=21000, # actual depth: 19770
27+
)
28+
29+
# test notebook content
30+
df = _get_result_as_dataframe(tb)
31+
actual_values = _get_distribution(df)
32+
expected_values = _get_uniform_distribution(df)
33+
# check that the distribution we got is close to even distribution
34+
distance = rel_entr(actual_values, expected_values) # 1.8, 3.01, 1.7, 3.1, 0.6, 4.4
35+
assert distance.sum() < 10
36+
37+
38+
def _get_result_as_dataframe(tb: TestbookNotebookClient) -> pd.DataFrame:
39+
parsed_counts = tb.ref("result_Z5.parsed_counts")
40+
data_list = [
41+
(sample_state.state["func_res"], sample_state.shots)
42+
for sample_state in parsed_counts
43+
]
44+
df = pd.DataFrame(data_list).rename(columns={0: "func_res", 1: "shots"})
45+
return df
46+
47+
48+
def _get_distribution(df: DataFrame) -> np.ndarray:
49+
grouped = df.groupby("func_res").sum()
50+
values = grouped_data.values.flatten()
51+
return values
52+
53+
54+
def _get_uniform_distribution(df: DataFrame) -> np.ndarray:
55+
total_num_shots = data["shots"].sum()
56+
num_func_res_options = len(data["func_res"].unique())
57+
58+
expected_values = [total_num_shots / num_func_res_options] * num_func_res_options
59+
return expected_values
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from tests.utils_for_testbook import (
2+
validate_quantum_program_size,
3+
validate_quantum_model,
4+
wrap_testbook,
5+
)
6+
from testbook.client import TestbookNotebookClient
7+
8+
9+
@wrap_testbook("discrete_poisson_solver", timeout_seconds=300)
10+
def test_notebook(tb: TestbookNotebookClient) -> None:
11+
# test models
12+
validate_quantum_model(tb.ref("qmod"))
13+
# test quantum programs
14+
validate_quantum_program_size(
15+
tb.ref("qprog"),
16+
expected_width=16, # actual width: 16
17+
expected_depth=12000, # actual depth: 11538
18+
)
19+
20+
# test notebook content
21+
pass # TODO
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from tests.utils_for_testbook import (
2+
validate_quantum_program_size,
3+
validate_quantum_model,
4+
wrap_testbook,
5+
)
6+
from testbook.client import TestbookNotebookClient
7+
8+
9+
@wrap_testbook("glued_trees", timeout_seconds=500)
10+
def test_notebook(tb: TestbookNotebookClient) -> None:
11+
# everything in this test is inside a function, storing results in a file
12+
pass # TODO: need to rewrite the notebook in order to test it
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from tests.utils_for_testbook import (
2+
validate_quantum_program_size,
3+
validate_quantum_model,
4+
wrap_testbook,
5+
)
6+
from testbook.client import TestbookNotebookClient
7+
8+
9+
@wrap_testbook(
10+
"grover_max_cut",
11+
timeout_seconds=220,
12+
)
13+
def test_notebook(tb: TestbookNotebookClient) -> None:
14+
# test models
15+
validate_quantum_model(tb.ref("qmod"))
16+
# test quantum programs
17+
validate_quantum_program_size(
18+
tb.ref("qprog"),
19+
expected_width=None, # actual width: ???
20+
expected_depth=None, # actual depth: ???
21+
)
22+
23+
# test notebook content
24+
pass # TODO

tests/notebooks/test_hidden_shift.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"hidden_shift",
1111
timeout_seconds=272, # we may lower this value
1212
)
13-
def test_hidden_shift(tb: TestbookNotebookClient) -> None:
13+
def test_notebook(tb: TestbookNotebookClient) -> None:
1414
# test models
1515
validate_quantum_model(tb.ref("qmod_simple"))
1616
validate_quantum_model(tb.ref("qmod_complex"))
@@ -31,5 +31,6 @@ def test_hidden_shift(tb: TestbookNotebookClient) -> None:
3131
expected_width=20, # actual width: 20
3232
expected_depth=1700, # actual depth: 1685
3333
)
34+
3435
# test notebook content
3536
pass # TODO
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from tests.utils_for_testbook import (
2+
validate_quantum_program_size,
3+
validate_quantum_model,
4+
wrap_testbook,
5+
)
6+
from testbook.client import TestbookNotebookClient
7+
8+
9+
@wrap_testbook(
10+
"hybrid_qnn_for_subset_majority",
11+
timeout_seconds=120,
12+
)
13+
def test_notebook(tb: TestbookNotebookClient) -> None:
14+
"""
15+
A notebook for a hybrid classical quantum neural network.
16+
The test verifies that the pre-trained model is indeed well trained.
17+
"""
18+
19+
assert (
20+
tb.ref("accuracy") > 0.85
21+
), "The network is not trained, although we load a pre-trained model."
22+
pass
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from tests.utils_for_testbook import (
2+
validate_quantum_program_size,
3+
validate_quantum_model,
4+
wrap_testbook,
5+
)
6+
from testbook.client import TestbookNotebookClient
7+
8+
9+
@wrap_testbook("qmc_user_defined", timeout_seconds=176)
10+
def test_notebook(tb: TestbookNotebookClient) -> None:
11+
# test models
12+
validate_quantum_model(tb.ref("model"))
13+
validate_quantum_model(tb.ref("model_2"))
14+
validate_quantum_model(tb.ref("model_3"))
15+
# test quantum programs
16+
validate_quantum_program_size(
17+
tb.ref("qprog"),
18+
expected_width=4, # actual width: 4
19+
expected_depth=150, # actual depth: 104
20+
)
21+
validate_quantum_program_size(
22+
tb.ref("qprog_2"),
23+
expected_width=5, # actual width: 4
24+
expected_depth=350, # actual depth: 236
25+
)
26+
validate_quantum_program_size(
27+
tb.ref("qprog_3"),
28+
expected_width=7, # actual width: 7
29+
expected_depth=2500, # actual depth: 2008
30+
)
31+
32+
# test notebook content
33+
pass # TODO

0 commit comments

Comments
 (0)