Skip to content

Commit 5a335a4

Browse files
committed
Add test: rainbow_options_integration_method
1 parent da9d5bc commit 5a335a4

File tree

5 files changed

+106
-19
lines changed

5 files changed

+106
-19
lines changed

research/rainbow_options/rainbow_options_integration_method.ipynb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,12 @@
520520
" rainbow_integration(qvars)\n",
521521
"\n",
522522
"\n",
523-
"constraints = Constraints(max_width=23)\n",
524-
"qmod = create_model(main, constraints=constraints)\n",
523+
"MAX_WIDTH_1 = 23\n",
524+
"qmod_1 = create_model(main)\n",
525+
"qmod_1 = update_constraints(qmod_1, max_width=MAX_WIDTH_1)\n",
525526
"print(\"Starting synthesis\")\n",
526-
"qprog = synthesize(qmod)\n",
527-
"show(qprog)"
527+
"qprog_1 = synthesize(qmod_1)\n",
528+
"show(qprog_1)"
528529
]
529530
},
530531
{
@@ -633,17 +634,18 @@
633634
},
634635
"outputs": [],
635636
"source": [
636-
"qmod = create_model(\n",
637+
"MAX_WIDTH_2 = 25\n",
638+
"qmod_2 = create_model(\n",
637639
" main,\n",
638-
" constraints=Constraints(max_width=25),\n",
640+
" constraints=Constraints(max_width=MAX_WIDTH_2),\n",
639641
" classical_execution_function=cmain,\n",
640642
" out_file=\"rainbow_options_integration_method\",\n",
641643
")\n",
642644
"print(\"Starting synthesis\")\n",
643-
"qprog = synthesize(qmod)\n",
644-
"show(qprog)\n",
645+
"qprog_2 = synthesize(qmod_2)\n",
646+
"show(qprog_2)\n",
645647
"print(\"Starting execution\")\n",
646-
"result = execute(qprog).result_value()\n",
648+
"result = execute(qprog_2).result_value()\n",
647649
"print(\"raw iqae results:\", result.estimation, result.confidence_interval)"
648650
]
649651
},

research/rainbow_options/rainbow_options_integration_method.qmod

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ qstruct EstimationVars {
99
ind: qbit;
1010
}
1111

12+
qfunc grover_algorithm(k: int, oracle_operand: qfunc (qbit[]), sp_operand: qfunc (qbit[]), x: qbit[]) {
13+
sp_operand(x);
14+
power (k) {
15+
grover_operator(oracle_operand, sp_operand, x);
16+
}
17+
}
18+
1219
qfunc qmci_oracle(qvars: EstimationVars) {
1320
Z(qvars.ind);
1421
}
@@ -65,13 +72,6 @@ qfunc rainbow_integration(qvars: EstimationVars) {
6572
}
6673
}
6774

68-
qfunc grover_algorithm(k: int, oracle_operand: qfunc (qbit[]), sp_operand: qfunc (qbit[]), x: qbit[]) {
69-
sp_operand(x);
70-
power (k) {
71-
grover_operator(oracle_operand, sp_operand, x);
72-
}
73-
}
74-
7575
qfunc main(k: int, output ind: qbit) {
7676
qvars: EstimationVars;
7777
allocate(qvars.size, qvars);
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
{
22
"constraints": {
3-
"max_width": 25
3+
"max_width": 25,
4+
"max_gate_count": {},
5+
"optimization_parameter": "no_opt"
6+
},
7+
"preferences": {
8+
"machine_precision": 8,
9+
"custom_hardware_settings": {
10+
"basis_gates": [
11+
"sx",
12+
"u1",
13+
"id",
14+
"x",
15+
"p",
16+
"z",
17+
"y",
18+
"h",
19+
"rz",
20+
"cz",
21+
"u",
22+
"rx",
23+
"r",
24+
"sxdg",
25+
"ry",
26+
"sdg",
27+
"t",
28+
"cy",
29+
"u2",
30+
"s",
31+
"cx",
32+
"tdg"
33+
],
34+
"is_symmetric_connectivity": true
35+
},
36+
"debug_mode": true,
37+
"synthesize_all_separately": false,
38+
"optimization_level": 3,
39+
"output_format": ["qasm"],
40+
"pretty_qasm": true,
41+
"transpilation_option": "auto optimize",
42+
"timeout_seconds": 300,
43+
"random_seed": 1749957507
444
}
545
}

tests/notebooks/test_rainbow_options_bruteforce_method.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
)
66
from testbook.client import TestbookNotebookClient
77

8-
import numpy as np
9-
108

119
@wrap_testbook(
1210
"rainbow_options_bruteforce_method",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
import numpy as np
9+
10+
11+
@wrap_testbook(
12+
"rainbow_options_integration_method",
13+
timeout_seconds=1000,
14+
)
15+
def test_notebook(tb: TestbookNotebookClient) -> None:
16+
"""
17+
A notebook for a hybrid classical quantum neural network.
18+
The test verifies that the pre-trained model is indeed well trained.
19+
"""
20+
# test models
21+
validate_quantum_model(tb.ref("qmod_1"))
22+
validate_quantum_model(tb.ref("qmod_2"))
23+
# test quantum programs
24+
validate_quantum_program_size(
25+
tb.ref("qprog_1"),
26+
expected_width=tb.ref("MAX_WIDTH_1"),
27+
expected_depth=1700, # actual depth: 1456
28+
)
29+
validate_quantum_program_size(
30+
tb.ref("qprog_2"),
31+
expected_width=tb.ref("MAX_WIDTH_2"),
32+
expected_depth=5500, # actual depth: 4930
33+
)
34+
35+
# test notebook content
36+
expected_payoff = 23.0238
37+
alpha_assertion = 1e-5
38+
39+
measured_confidence = tb.ref("conf_interval[1] - conf_interval[0]")
40+
# based on e^2=(1/2N)*log(2T/alpha) from "Iterative Quantum Amplitude Estimation" since our alpha is low, we want to check within a bigger confidence interval
41+
confidence_scale_by_alpha = np.sqrt(np.log(tb.ref("ALPHA_VALUE") / alpha_assertion))
42+
43+
parsed_result = tb.ref("parsed_result")
44+
assert (
45+
np.abs(parsed_result - expected_payoff)
46+
<= 0.5 * measured_confidence * confidence_scale_by_alpha
47+
), f"Payoff result is out of the {alpha_assertion*100}% confidence interval: |{parsed_result} - {expected_payoff}| > {0.5*measured_confidence * confidence_scale_by_alpha}"

0 commit comments

Comments
 (0)