Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@
],
"source": [
"# create the molecule, insert the distance, prepare H, create UCC anzats and solve in energy\n",
"\n",
"qmods = []\n",
"qprogs = []\n",
"results = []\n",
"durations = []\n",
"\n",
"for x in distance:\n",
" time1 = time.time()\n",
"\n",
Expand All @@ -263,10 +269,13 @@
" initial_point=None,\n",
" ),\n",
" )\n",
" qmods.append(qmod)\n",
"\n",
" qprog = synthesize(qmod)\n",
" qprogs.append(qprog)\n",
"\n",
" result = execute(qprog).result()\n",
" results.append(result)\n",
" chemistry_result_dict = result[1].value\n",
"\n",
" operator = chemistry_problem.generate_hamiltonian()\n",
Expand All @@ -281,7 +290,9 @@
" exact_energy.append(result_exact)\n",
"\n",
" time2 = time.time()\n",
" print(time2 - time1)"
" duration = time2 - time1\n",
" durations.append(duration)\n",
" print(duration)"
]
},
{
Expand All @@ -299,7 +310,7 @@
"outputs": [],
"source": [
"# save the last model to a qmod file\n",
"write_qmod(qmod, \"molecular_energy_curve\")"
"write_qmod(qmods[-1], \"molecular_energy_curve\")"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
{}
{
"constraints": {
"max_gate_count": {},
"optimization_parameter": "no_opt"
},
"preferences": {
"machine_precision": 8,
"custom_hardware_settings": {
"basis_gates": [
"sx",
"p",
"x",
"cx",
"tdg",
"sdg",
"cy",
"s",
"rx",
"sxdg",
"y",
"ry",
"u2",
"id",
"rz",
"t",
"z",
"u",
"u1",
"cz",
"h",
"r"
],
"is_symmetric_connectivity": true
},
"debug_mode": true,
"synthesize_all_separately": false,
"optimization_level": 3,
"output_format": ["qasm"],
"pretty_qasm": true,
"transpilation_option": "auto optimize",
"timeout_seconds": 300,
"random_seed": 3365615472
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
" two_qubit_gates=[\"cx\"],\n",
")\n",
"\n",
"qmod = construct_chemistry_model(\n",
"qmod_hwea = construct_chemistry_model(\n",
" chemistry_problem=chemistry_problem,\n",
" use_hartree_fock=True,\n",
" ansatz_parameters=hwea_params,\n",
Expand All @@ -292,8 +292,8 @@
" backend_name=ClassiqSimulatorBackendNames.SIMULATOR\n",
")\n",
"\n",
"qmod = set_execution_preferences(\n",
" qmod,\n",
"qmod_hwea = set_execution_preferences(\n",
" qmod_hwea,\n",
" execution_preferences=ExecutionPreferences(\n",
" num_shots=1000, backend_preferences=backend_preferences\n",
" ),\n",
Expand Down Expand Up @@ -323,8 +323,8 @@
}
],
"source": [
"qprog = synthesize(qmod)\n",
"show(qprog)"
"qprog_hwea = synthesize(qmod_hwea)\n",
"show(qprog_hwea)"
]
},
{
Expand Down Expand Up @@ -402,10 +402,10 @@
" ),\n",
")\n",
"\n",
"qprog = synthesize(serialized_chemistry_model)\n",
"show(qprog)\n",
"qprog_ucc = synthesize(serialized_chemistry_model)\n",
"show(qprog_ucc)\n",
"\n",
"circuit = QuantumProgram.from_qprog(qprog)\n",
"circuit = QuantumProgram.from_qprog(qprog_ucc)\n",
"print(f\"circuit depth: {circuit.transpiled_circuit.depth}\")"
]
},
Expand Down Expand Up @@ -449,7 +449,7 @@
},
"outputs": [],
"source": [
"result = execute(qprog).result()\n",
"result = execute(qprog_ucc).result()\n",
"chemistry_result_dict = result[1].value"
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
{}
{
"constraints": {
"max_gate_count": {},
"optimization_parameter": "no_opt"
},
"preferences": {
"machine_precision": 8,
"custom_hardware_settings": {
"basis_gates": [
"cx",
"u1",
"sdg",
"id",
"u2",
"u",
"ry",
"p",
"r",
"t",
"sxdg",
"cy",
"cz",
"y",
"rx",
"sx",
"z",
"rz",
"x",
"tdg",
"h",
"s"
],
"is_symmetric_connectivity": true
},
"debug_mode": true,
"synthesize_all_separately": false,
"optimization_level": 3,
"output_format": ["qasm"],
"pretty_qasm": true,
"transpilation_option": "auto optimize",
"timeout_seconds": 300,
"random_seed": 3833092011
}
}
30 changes: 30 additions & 0 deletions tests/notebooks/test_molecular_energy_curve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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("molecular_energy_curve", timeout_seconds=1200)
def test_notebook(tb: TestbookNotebookClient) -> None:
# test models
for qmod in tb.ref("qmods"):
validate_quantum_model(qmod)
# test quantum programs
for qprog in tb.ref("qprogs"):
validate_quantum_program_size(
qprog,
expected_width=16, # actual width: 16
expected_depth=12000, # actual depth: 11538
)

# test notebook content
for duration in tb.ref("durations"):
assert duration < 40 # locally it is no longer than 15 seconds

assert all(
np.isclose(tb.ref("exact_energy"), tb.ref("VQE_energy"), atol=0.02)
) # should be atol=0.01
39 changes: 39 additions & 0 deletions tests/notebooks/test_molecule_eigensolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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("molecule_eigensolver", timeout_seconds=84)
def test_notebook(tb: TestbookNotebookClient) -> None:
# test models
validate_quantum_model(tb.ref("qmod_hwea"))
validate_quantum_model(tb.ref("serialized_chemistry_model"))
# test quantum programs
validate_quantum_program_size(
tb.ref("qprog_hwea"),
expected_width=4, # actual width: 4
expected_depth=15, # actual depth: 13
)
validate_quantum_program_size(
tb.ref("qprog_ucc"),
expected_width=1, # actual width: 1
expected_depth=3, # actual depth: 3
)

# test notebook content
# operator = tb.ref("operator.to_matrix()")
# w, v = np.linalg.eig(operator)
# exact_result = np.real(min(w))
exact_result = tb.ref("np.real(min( np.linalg.eig(operator.to_matrix())[0] ))")

chemistry_result_dict = tb.ref("chemistry_result_dict")
vqe_result = chemistry_result_dict["energy"]

assert np.isclose(
vqe_result, exact_result, atol=0.02
) # should be 0.01, but sometimes gets larger
21 changes: 21 additions & 0 deletions tests/notebooks/test_protein_folding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from tests.utils_for_testbook import (
validate_quantum_program_size,
validate_quantum_model,
wrap_testbook,
)
from testbook.client import TestbookNotebookClient


@wrap_testbook("protein_folding", timeout_seconds=240)
def test_notebook(tb: TestbookNotebookClient) -> None:
# test models
validate_quantum_model(tb.ref("qmod"))
# test quantum programs
validate_quantum_program_size(
tb.ref("qprog"),
expected_width=10, # actual width: 7
expected_depth=1600, # actual depth: 1417
)

# test notebook content
pass # Todo
27 changes: 27 additions & 0 deletions tests/notebooks/test_qpe_for_molecules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from tests.utils_for_testbook import (
validate_quantum_program_size,
validate_quantum_model,
wrap_testbook,
)
from testbook.client import TestbookNotebookClient


@wrap_testbook("qpe_for_molecules", timeout_seconds=1332)
def test_notebook(tb: TestbookNotebookClient) -> None:
# test models
validate_quantum_model(tb.ref("qmod"))
# test quantum programs
validate_quantum_program_size(
tb.ref("qprog"),
expected_width=10, # actual width: 7
expected_depth=85000, # actual depth: 74356
)

# test notebook content
solution = tb.ref("solution")
energy_resolution = tb.ref("energy_resolution")

exact_result = tb.ref("np.real(min( np.linalg.eig(operator.to_matrix())[0] ))")

for sol in solution:
assert sol - energy_resolution <= exact_result <= sol + energy_resolution
21 changes: 21 additions & 0 deletions tests/notebooks/test_second_quantized_hamiltonian.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from tests.utils_for_testbook import (
validate_quantum_program_size,
validate_quantum_model,
wrap_testbook,
)
from testbook.client import TestbookNotebookClient


@wrap_testbook("second_quantized_hamiltonian", timeout_seconds=44)
def test_notebook(tb: TestbookNotebookClient) -> None:
# test models
validate_quantum_model(tb.ref("qmod"))
# test quantum programs
validate_quantum_program_size(
tb.ref("qprog"),
expected_width=4, # actual width: 4
expected_depth=15, # actual depth: 13
)

# test notebook content
pass # Todo
8 changes: 4 additions & 4 deletions tests/test_no_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import nbformat

from utils_for_tests import iterate_notebooks
from utils_for_tests import iterate_notebook_names, resolve_notebook_path


def test_no_errors_in_notebooks() -> None:
for notebook in iterate_notebooks():
with open(notebook) as f:
for notebook in iterate_notebook_names():
with open(resolve_notebook_path(notebook)) as f:
notebook_data = nbformat.read(f, nbformat.NO_CONVERT)

for index, cell in enumerate(notebook_data["cells"]):
Expand All @@ -17,4 +17,4 @@ def test_no_errors_in_notebooks() -> None:

assert not any(
output.get("output_type") == "error" for output in cell["outputs"]
), f"Cell #{index} in {os.path.basename(notebook)} has an output error"
), f"Cell #{index} in {notebook} has an output error"