|
| 1 | +import json |
| 2 | +import os |
| 3 | +from typing import Any, Callable |
| 4 | +import pytest |
| 5 | + |
| 6 | +from testbook import testbook |
| 7 | +from tests.utils_for_tests import resolve_notebook_path, should_skip_notebook |
| 8 | + |
| 9 | +from classiq.interface.generator.quantum_program import QuantumProgram |
| 10 | + |
| 11 | + |
| 12 | +def wrap_testbook(notebook_name: str, timeout_seconds: float = 10) -> Callable: |
| 13 | + def inner_decorator(func: Callable) -> Any: |
| 14 | + notebook_path = resolve_notebook_path(notebook_name) |
| 15 | + |
| 16 | + for decorator in [ |
| 17 | + testbook(notebook_path, execute=True, timeout=timeout_seconds), |
| 18 | + _build_cd_decorator(notebook_path), |
| 19 | + _build_skip_decorator(notebook_path), |
| 20 | + ]: |
| 21 | + func = decorator(func) |
| 22 | + return func |
| 23 | + |
| 24 | + return inner_decorator |
| 25 | + |
| 26 | + |
| 27 | +# The purpose of the `cd_decorator` is to execute the test in the same folder as the `ipynb` file |
| 28 | +# so that relative files (images, csv, etc.) will be available |
| 29 | +def _build_cd_decorator(file_path: str) -> Callable: |
| 30 | + def cd_decorator(func: Callable) -> Any: |
| 31 | + def inner(*args: Any, **kwargs: Any) -> Any: |
| 32 | + previous_dir = os.getcwd() |
| 33 | + os.chdir(os.path.dirname(file_path)) |
| 34 | + |
| 35 | + func(*args, **kwargs) |
| 36 | + |
| 37 | + os.chdir(previous_dir) |
| 38 | + |
| 39 | + return inner |
| 40 | + |
| 41 | + return cd_decorator |
| 42 | + |
| 43 | + |
| 44 | +def _build_skip_decorator(notebook_path: str) -> Callable: |
| 45 | + notebook_name = os.path.basename(notebook_path) |
| 46 | + return pytest.mark.skipif( |
| 47 | + should_skip_notebook(notebook_name), |
| 48 | + reason="Didn't change", |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def validate_quantum_model(model: str) -> None: |
| 53 | + # currently that's some dummy test - only checking that it's a valid dict |
| 54 | + assert isinstance(json.loads(model), dict) |
| 55 | + |
| 56 | + |
| 57 | +def validate_quantum_program_size( |
| 58 | + quantum_program: str, |
| 59 | + expected_width: int | None = None, |
| 60 | + expected_depth: int | None = None, |
| 61 | +) -> None: |
| 62 | + qp = QuantumProgram.model_validate_json(quantum_program) |
| 63 | + |
| 64 | + actual_width = qp.data.width |
| 65 | + if expected_width is not None: |
| 66 | + assert ( |
| 67 | + actual_width <= expected_width |
| 68 | + ), f"The width of the circuit changed! (for the worse!). From {expected_width} to {actual_width}" |
| 69 | + |
| 70 | + assert qp.transpiled_circuit is not None # for mypy |
| 71 | + actual_depth = qp.transpiled_circuit.depth |
| 72 | + if expected_depth is not None: |
| 73 | + assert ( |
| 74 | + actual_depth <= expected_depth |
| 75 | + ), f"The depth of the circuit changed! (for the worse!). From {expected_depth} to {actual_depth}" |
0 commit comments