Skip to content

Commit da9d5bc

Browse files
committed
Add test: rainbow_options_bruteforce_method
1 parent c34fdf2 commit da9d5bc

File tree

4 files changed

+95
-17
lines changed

4 files changed

+95
-17
lines changed

research/rainbow_options/rainbow_options_bruteforce_method.ipynb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,11 @@
457457
" rainbow_brute_force(qvars)\n",
458458
"\n",
459459
"\n",
460-
"constraints = Constraints(max_width=23)\n",
461-
"qmod = create_model(main, constraints=constraints)\n",
462-
"qprog = synthesize(qmod)\n",
463-
"show(qprog)"
460+
"MAX_WIDTH_1 = 23\n",
461+
"qmod_1 = create_model(main)\n",
462+
"qmod_1 = update_constraints(qmod_1, max_width=MAX_WIDTH_1)\n",
463+
"qprog_1 = synthesize(qmod_1)\n",
464+
"show(qprog_1)"
464465
]
465466
},
466467
{
@@ -569,17 +570,18 @@
569570
},
570571
"outputs": [],
571572
"source": [
572-
"qmod = create_model(\n",
573+
"MAX_WIDTH_2 = 25\n",
574+
"qmod_2 = create_model(\n",
573575
" main,\n",
574-
" constraints=Constraints(max_width=25),\n",
576+
" constraints=Constraints(max_width=MAX_WIDTH_2),\n",
575577
" classical_execution_function=cmain,\n",
576578
" out_file=\"rainbow_options_bruteforce_method\",\n",
577579
")\n",
578580
"print(\"Starting synthesis\")\n",
579-
"qprog = synthesize(qmod)\n",
580-
"show(qprog)\n",
581+
"qprog_2 = synthesize(qmod_2)\n",
582+
"show(qprog_2)\n",
581583
"print(\"Starting execution\")\n",
582-
"result = execute(qprog).result_value()"
584+
"result = execute(qprog_2).result_value()"
583585
]
584586
},
585587
{

research/rainbow_options/rainbow_options_bruteforce_method.qmod

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

11+
qfunc grover_algorithm(k: int, oracle_operand: qfunc (qbit[]), sp_operand: qfunc (qbit[]), x: qbit[]) {
12+
sp_operand(x);
13+
power (k) {
14+
grover_operator(oracle_operand, sp_operand, x);
15+
}
16+
}
17+
1118
qfunc qmci_oracle(qvars: EstimationVars) {
1219
Z(qvars.ind);
1320
}
@@ -44,13 +51,6 @@ qfunc rainbow_brute_force(qvars: EstimationVars) {
4451
}
4552
}
4653

47-
qfunc grover_algorithm(k: int, oracle_operand: qfunc (qbit[]), sp_operand: qfunc (qbit[]), x: qbit[]) {
48-
sp_operand(x);
49-
power (k) {
50-
grover_operator(oracle_operand, sp_operand, x);
51-
}
52-
}
53-
5454
qfunc main(k: int, output ind: qbit) {
5555
qvars: EstimationVars;
5656
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+
"id",
12+
"tdg",
13+
"sdg",
14+
"t",
15+
"u1",
16+
"cx",
17+
"u",
18+
"cz",
19+
"rz",
20+
"s",
21+
"r",
22+
"p",
23+
"sxdg",
24+
"sx",
25+
"rx",
26+
"ry",
27+
"y",
28+
"cy",
29+
"u2",
30+
"z",
31+
"x",
32+
"h"
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": 3581329524
444
}
545
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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_bruteforce_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=1000, # actual depth: 794
28+
)
29+
validate_quantum_program_size(
30+
tb.ref("qprog_2"),
31+
expected_width=tb.ref("MAX_WIDTH_2"),
32+
expected_depth=2750, # actual depth: 2441
33+
)
34+
35+
# test notebook content
36+
pass # TODO

0 commit comments

Comments
 (0)