microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fedimser/test-utils-1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/pip/tests/test_qsharp_shim.py

93lines · modecode

1# Copyright (c) Microsoft Corporation.
2# Licensed under the MIT License.
3
4"""Tests for the ``qsharp`` compatibility shim.
5
6The ``qsharp`` package is a thin deprecation wrapper that re-exports the
7public API from ``qdk``. These tests verify that the expected symbols and
8submodules are accessible via ``import qsharp``.
9"""
10
11import importlib
12import warnings
13
14import pytest
15
16# ---- Symbols that must be directly accessible on ``qsharp`` ----
17
18_EXPECTED_SYMBOLS = [
19 # functions
20 "init",
21 "eval",
22 "run",
23 "compile",
24 "circuit",
25 "estimate",
26 "estimate_custom",
27 "logical_counts",
28 "set_quantum_seed",
29 "set_classical_seed",
30 "dump_machine",
31 "dump_circuit",
32 # types / classes
33 "Result",
34 "Pauli",
35 "QSharpError",
36 "TargetProfile",
37 "StateDump",
38 "ShotResult",
39 "PauliNoise",
40 "DepolarizingNoise",
41 "BitFlipNoise",
42 "PhaseFlipNoise",
43 "CircuitGenerationMethod",
44]
45
46# Submodules that must be importable *and* accessible as attributes
47# on the ``qsharp`` module after a bare ``import qsharp``.
48_EXPECTED_SUBMODULES = [
49 "code",
50 "estimator",
51]
52
53
54@pytest.fixture()
55def qsharp_module():
56 """Import the ``qsharp`` shim while suppressing the deprecation warning."""
57 with warnings.catch_warnings():
58 warnings.simplefilter("ignore", DeprecationWarning)
59 mod = importlib.import_module("qsharp")
60 return mod
61
62
63def test_deprecation_warning_on_import():
64 """Importing ``qsharp`` must emit a DeprecationWarning."""
65 with warnings.catch_warnings(record=True) as caught:
66 warnings.simplefilter("always")
67 importlib.reload(importlib.import_module("qsharp"))
68
69 deprecations = [w for w in caught if issubclass(w.category, DeprecationWarning)]
70 assert any("qsharp" in str(w.message) for w in deprecations)
71
72
73@pytest.mark.parametrize("name", _EXPECTED_SYMBOLS)
74def test_symbol_accessible(qsharp_module, name):
75 """Every expected symbol must be an attribute of ``qsharp``."""
76 assert hasattr(qsharp_module, name), f"qsharp.{name} is missing"
77
78
79@pytest.mark.parametrize("name", _EXPECTED_SUBMODULES)
80def test_submodule_accessible_as_attribute(qsharp_module, name):
81 """Submodules like ``code`` must be reachable via ``qsharp.<name>``
82 without a separate ``from qsharp import <name>``."""
83 attr = getattr(qsharp_module, name, None)
84 assert attr is not None, f"qsharp.{name} is not accessible as an attribute"
85
86
87@pytest.mark.parametrize("name", _EXPECTED_SUBMODULES)
88def test_submodule_importable(name):
89 """``from qsharp import <submodule>`` must work."""
90 with warnings.catch_warnings():
91 warnings.simplefilter("ignore", DeprecationWarning)
92 mod = importlib.import_module(f"qsharp.{name}")
93 assert mod is not None
94