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
30 changes: 30 additions & 0 deletions tests/notebooks/test_bernstein_vazirani_tutorial.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(
"bernstein_vazirani_tutorial", timeout_seconds=100
) # 2025.03.06 bump from 30 seconds
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=6, # actual width: 6
expected_depth=5, # actual depth: 5
)

# test notebook content
assert np.floor(np.log2(tb.ref("SECRET_INT")) + 1) <= tb.ref(
"STRING_LENGTH"
), "The STRING_LENGTH cannot be smaller than the secret string length"

assert tb.ref("secret_integer_q") == tb.ref("SECRET_INT")
assert tb.ref("result.parsed_counts[0].shots / NUM_SHOTS") == 1
27 changes: 27 additions & 0 deletions tests/notebooks/test_example_exponentiation.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("example_exponentiation", timeout_seconds=216)
def test_notebook(tb: TestbookNotebookClient) -> None:
# test models
validate_quantum_model(tb.ref("qmod"))
validate_quantum_model(tb.ref("qmod_minimize_error"))
# test quantum programs
validate_quantum_program_size(
tb.ref("qprog"),
expected_width=15, # actual width: 12
expected_depth=1750, # actual depth: 1451
)
validate_quantum_program_size(
tb.ref("qprog_minimize_error"),
expected_width=10, # actual width: 8
expected_depth=350, # actual depth: 301
)

# test notebook content
pass # Todo
70 changes: 70 additions & 0 deletions tests/notebooks/test_hamiltonian_simulation_guide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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(
"hamiltonian_simulation_guide", timeout_seconds=2000
) # 2025.03.06 bump from 1000 seconds
def test_notebook(tb: TestbookNotebookClient) -> None:
# test models
validate_quantum_model(tb.ref("qmod_trotter"))
validate_quantum_model(tb.ref("qmod_exponentiation"))
validate_quantum_model(tb.ref("qmod_qdrift"))
validate_quantum_model(tb.ref("qmod_magnetization_trotter"))
validate_quantum_model(tb.ref("qmod_magnetization_qdrift"))
for qmod in tb.ref("qmods_magnetization_exponentiation"):
validate_quantum_model(qmod)

# test quantum programs
validate_quantum_program_size(
tb.ref("qprog_trotter"),
expected_width=2, # actual width: 2
expected_depth=50, # actual depth: 40
)
validate_quantum_program_size(
tb.ref("qprog_exponentiation"),
expected_width=2, # actual width: 2
expected_depth=30, # actual depth: 20
)
validate_quantum_program_size(
tb.ref("qprog_qdrift"),
expected_width=2, # actual width: 2
expected_depth=150, # actual depth: 114
)
validate_quantum_program_size(
tb.ref("qprog_magnetization_trotter"),
expected_width=2, # actual width: 2
expected_depth=250, # actual depth: 180
)
validate_quantum_program_size(
tb.ref("qprog_magnetization_qdrift"),
expected_width=2, # actual width: 2
expected_depth=600, # actual depth: 480
)
# widths:
# [2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
# depths:
# [0,11,11,11,11,11,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20]
for qprog in tb.ref("qprogs_magnetization_exponentiation")[1:]:
validate_quantum_program_size(
qprog,
expected_width=2, # actual width: 2
expected_depth=20, # actual depth: 20
)

# test notebook content
time_list = tb.ref("time_list")
results_ST = tb.ref_numpy("np.real(magnetization_ST)")
results_ewdc = tb.ref_numpy("np.real(magnetization_ewdc)")
results_qdrift = tb.ref_numpy("np.real(magnetization_qdrift)")

tolerance = 0.1 # that's a large tolerance. we should lower it.
np.allclose(results_ST, results_ewdc, atol=tolerance)
np.allclose(results_ST, results_qdrift, atol=tolerance)
np.allclose(results_ewdc, results_qdrift, atol=tolerance)
62 changes: 62 additions & 0 deletions tests/notebooks/test_hamiltonian_simulation_with_block_encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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("hamiltonian_simulation_with_block_encoding", timeout_seconds=600)
def test_notebook(tb: TestbookNotebookClient) -> None:
# 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: 4
expected_depth=100, # actual depth: 70
)
validate_quantum_program_size(
tb.ref("qprog_2"),
expected_width=12, # actual width: 11
expected_depth=14000, # actual depth: 12929
)
validate_quantum_program_size(
tb.ref("qprog_3"),
expected_width=10, # actual width: 8
expected_depth=6000, # actual depth: 5500
)

# test notebook content
assert np.allclose(
np.real(tb.ref_numpy("state_result_1")), tb.ref_numpy("expected_state_1")
)

state_result_2 = tb.ref_numpy("state_result_2")
expected_state_2 = tb.ref_numpy("expected_state_2")
assert np.linalg.norm(state_result_2 - expected_state_2) < tb.ref("EPS")
overlap_2 = (
np.abs(np.vdot(state_result_2, expected_state_2))
* tb.ref("normalization_exp")
/ np.linalg.norm(state_result_2)
)
assert np.isclose(
overlap_2, 0.999, atol=0.01
) # 0.9996884609316635 # should be atol=0.001

state_result_3 = tb.ref_numpy("state_result_3")
expected_state_3 = tb.ref_numpy("expected_state_3")
assert np.linalg.norm(state_result_3 - expected_state_3) < tb.ref("EPS")
overlap_3 = (
np.abs(np.vdot(state_result_3, expected_state_3))
* tb.ref("normalization_exp")
/ np.linalg.norm(state_result_3)
)
assert np.isclose(
overlap_3, 0.999999, atol=0.01
) # 0.9999998243682983 # should be atol=0.000001 # in another run it was 1.0018339139233956
30 changes: 30 additions & 0 deletions tests/notebooks/test_learning_optimization.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


@wrap_testbook(
"learning_optimization", timeout_seconds=200
) # 2025.03.06 bump from 80 seconds
def test_notebook(tb: TestbookNotebookClient) -> None:
# test models
validate_quantum_model(tb.ref("qmod_1"))
validate_quantum_model(tb.ref("qmod_2"))

# test quantum programs
validate_quantum_program_size(
tb.ref("qprog_1"),
expected_width=10, # actual width: 8
expected_depth=60, # actual depth: 44
)
validate_quantum_program_size(
tb.ref("qprog_2"),
expected_width=10, # actual width: 8
expected_depth=60, # actual depth: 44
)

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


@wrap_testbook("mcx", timeout_seconds=236)
def test_notebook(tb: TestbookNotebookClient) -> None:
# test models
validate_quantum_model(tb.ref("qmod_1"))
validate_quantum_model(tb.ref("qmod_2"))
validate_quantum_model(tb.ref("qmod_3"))
validate_quantum_model(tb.ref("qmod_4"))

# test quantum programs
validate_quantum_program_size(
tb.ref("qprog_1"),
expected_width=tb.ref("MAX_WIDTH_1"),
expected_depth=125, # actual depth: 94
)
validate_quantum_program_size(
tb.ref("qprog_2"),
expected_width=tb.ref("MAX_WIDTH_2"),
expected_depth=500, # actual depth: 391
)
validate_quantum_program_size(
tb.ref("qprog_3"),
expected_width=tb.ref("MAX_WIDTH_3"),
expected_depth=250, # actual depth: 221
)
validate_quantum_program_size(
tb.ref("qprog_4"),
expected_width=80, # actual width: 69
expected_depth=1500, # actual depth: 494 ; also was 920
)

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


@wrap_testbook("prepare_state", timeout_seconds=20)
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=7, # actual width: 6
expected_depth=350, # actual depth: 278
)
validate_quantum_program_size(
tb.ref("qprog_simulator"),
compare_to=tb.ref("qprog"),
)

# test notebook content
pass # Todo
30 changes: 30 additions & 0 deletions tests/notebooks/test_qml_with_classiq_guide.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


@wrap_testbook("qml_with_classiq_guide", timeout_seconds=300)
def test_notebook(tb: TestbookNotebookClient) -> None:
# test models
validate_quantum_model(tb.ref("qmod_1"))
validate_quantum_model(tb.ref("qmod_2"))

# test quantum programs
validate_quantum_program_size(
tb.ref("qprog_1"),
expected_width=1, # actual width: 1
expected_depth=1, # actual depth: 1
)
validate_quantum_program_size(
tb.ref("qprog_2"),
expected_width=1, # actual width: 1
expected_depth=2, # actual depth: 2
)

# test notebook content
assert (
tb.ref("check_accuracy(model, data_loader)") > 0
) # sometimes 1, sometimes 0.5
11 changes: 11 additions & 0 deletions tests/utils_for_testbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,18 @@ def validate_quantum_program_size(
quantum_program: str,
expected_width: int | None = None,
expected_depth: int | None = None,
compare_to: str | None = None,
) -> None:
if compare_to is not None:
other_qp = QuantumProgram.model_validate_json(quantum_program)

other_width = other_qp.data.width

assert other_qp.transpiled_circuit is not None # for mypy
other_depth = other_qp.transpiled_circuit.depth

return validate_quantum_program_size(quantum_program, other_width, other_depth)

qp = QuantumProgram.model_validate_json(quantum_program)

actual_width = qp.data.width
Expand Down
Loading