Skip to content

Commit

Permalink
Add test: vqls_with_lcu
Browse files Browse the repository at this point in the history
  • Loading branch information
classiqdor committed Feb 13, 2025
1 parent 0a74ef8 commit 20949f2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 29 deletions.
50 changes: 21 additions & 29 deletions algorithms/vqls/lcu_vqls/vqls_with_lcu.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
"\n",
" def optimize(self):\n",
" random.seed(1000)\n",
" out = minimize(\n",
" self._out = out = minimize(\n",
" self.f,\n",
" x0=[\n",
" float(random.randint(0, 3000)) / 1000\n",
Expand All @@ -242,7 +242,7 @@
" options={\"maxiter\": 2000},\n",
" )\n",
" print(out)\n",
" out_f = [out[\"x\"][0 : self.ansatz_param_count]]\n",
" self._out_f = out_f = [out[\"x\"][0 : self.ansatz_param_count]]\n",
" print(out_f)\n",
" plt.plot(\n",
" [l for l in range(len(self.intermediate))], list(self.intermediate.values())\n",
Expand Down Expand Up @@ -615,9 +615,9 @@
" apply_fixed_3_qubit_system_ansatz(params, system_qubits)\n",
"\n",
"\n",
"model = create_model(main)\n",
"qprog = synthesize(model)\n",
"show(qprog)"
"qmod_1 = create_model(main)\n",
"qprog_1 = synthesize(qmod_1)\n",
"show(qprog_1)"
]
},
{
Expand Down Expand Up @@ -705,16 +705,16 @@
"from classiq.synthesis import set_execution_preferences\n",
"\n",
"backend_preferences = ClassiqBackendPreferences(backend_name=\"simulator_statevector\")\n",
"model = create_model(main)\n",
"qmod_2 = create_model(main)\n",
"\n",
"model = set_execution_preferences(\n",
" model,\n",
"qmod_2 = set_execution_preferences(\n",
" qmod_2,\n",
" execution_preferences=ExecutionPreferences(\n",
" num_shots=204800, backend_preferences=backend_preferences\n",
" ),\n",
")\n",
"qprog = synthesize(model)\n",
"show(qprog)"
"qprog_2 = synthesize(qmod_2)\n",
"show(qprog_2)"
]
},
{
Expand All @@ -737,7 +737,7 @@
"source": [
"from classiq import write_qmod\n",
"\n",
"write_qmod(model, name=\"vqls_with_lcu\", decimal_precision=15)"
"write_qmod(qmod_2, name=\"vqls_with_lcu\", decimal_precision=15)"
]
},
{
Expand Down Expand Up @@ -781,7 +781,7 @@
],
"source": [
"optimizer = VqlsOptimizer(\n",
" qprog, ansatz_param_count, \"system_qubits\", \"ancillary_qubits\"\n",
" qprog_2, ansatz_param_count, \"system_qubits\", \"ancillary_qubits\"\n",
")\n",
"optimal_params = optimizer.optimize()"
]
Expand Down Expand Up @@ -823,17 +823,17 @@
"from classiq.synthesis import set_execution_preferences\n",
"\n",
"backend_preferences = ClassiqBackendPreferences(backend_name=\"simulator_statevector\")\n",
"qmod = create_model(main)\n",
"qmod_3 = create_model(main)\n",
"\n",
"qmod = set_execution_preferences(\n",
" qmod,\n",
"qmod_3 = set_execution_preferences(\n",
" qmod_3,\n",
" execution_preferences=ExecutionPreferences(\n",
" num_shots=20480, backend_preferences=backend_preferences\n",
" ),\n",
")\n",
"\n",
"\n",
"qprog = synthesize(qmod)"
"qprog_3 = synthesize(qmod_3)"
]
},
{
Expand All @@ -852,7 +852,7 @@
"metadata": {},
"outputs": [],
"source": [
"state_vector = results.value.state_vector"
"state_vector = execute(qprog_3).result_value().state_vector"
]
},
{
Expand All @@ -879,12 +879,8 @@
}
],
"source": [
"probabilities = []\n",
"for key, val in state_vector.items():\n",
" probabilities.append(abs(complex(val)) ** 2)\n",
"\n",
"\n",
"probabilities"
"amplitudes = [abs(complex(val)) for val in state_vector.values()]\n",
"amplitudes"
]
},
{
Expand All @@ -911,12 +907,8 @@
}
],
"source": [
"amplitudes = []\n",
"for key, val in state_vector.items():\n",
" amplitudes.append(abs(complex(val)))\n",
"\n",
"\n",
"amplitudes"
"probabilities = [amp**2 for amp in amplitudes]\n",
"probabilities"
]
},
{
Expand Down
46 changes: 46 additions & 0 deletions tests/notebooks/test_vqls_with_lcu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from tests.utils_for_testbook import (
validate_quantum_program_size,
validate_quantum_model,
wrap_testbook,
)
from testbook.client import TestbookNotebookClient

import numpy as np


@wrap_testbook(
"vqls_with_lcu",
timeout_seconds=1200,
)
def test_notebook(tb: TestbookNotebookClient) -> None:
"""
A notebook for a hybrid classical quantum neural network.
The test verifies that the pre-trained model is indeed well trained.
"""
# test models
validate_quantum_model(tb.ref("qmod_1"))
validate_quantum_model(tb.ref("qmod_2"))
validate_quantum_model(tb.ref("qmod_3"))
# test quantum programs
validate_quantum_program_size(
tb.ref("qprog_1"),
expected_width=5, # actual width: 3
expected_depth=15, # actual depth: 9
)
validate_quantum_program_size(
tb.ref("qprog_2"),
expected_width=7, # actual width: 5
expected_depth=50, # actual depth: 35
)
validate_quantum_program_size(
tb.ref("qprog_1"),
expected_width=5, # actual width: 3
expected_depth=20, # actual depth: 10
)

# test notebook content
assert tb.ref(
"bool(optimizer._out.success)"
) # convert `np.bool_` to `bool` to help serialize

assert tb.ref("optimizer.my_cost(optimal_params)") < 0.5 # it even was 0.06

0 comments on commit 20949f2

Please sign in to comment.