microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.23.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/qdk_package/tests/mocks.py

169lines · modecode

1# Copyright (c) Microsoft Corporation.
2# Licensed under the MIT License.
3
4"""Centralized mock helpers for tests.
5
6Provides lightweight stand-ins for optional (and required during tests) dependencies.
7
8Functions return a list of module names they created so callers can later clean them
9up using cleanup_modules(). This keeps test intent explicit.
10"""
11
12import sys
13import types
14from typing import List
15
16
17def _not_impl(*_a, **_k):
18 raise NotImplementedError("qsharp stub: real 'qsharp' package not installed")
19
20
21def mock_qsharp() -> List[str]:
22 """Ensure a minimal 'qsharp' module exists.
23
24 In real usage the qsharp package provides a compiled extension. Tests only
25 need the attribute surface that qdk re-exports (run/estimate presently used
26 for sanity checks). If the real package is installed this is a no-op.
27 """
28 created: List[str] = []
29 if "qsharp" not in sys.modules:
30 stub = types.ModuleType("qsharp")
31
32 stub.run = _not_impl
33 stub.estimate = _not_impl
34 # Provide utility symbols expected to re-export at root
35 stub.code = object()
36 stub.set_quantum_seed = _not_impl
37 stub.set_classical_seed = _not_impl
38 stub.dump_machine = _not_impl
39 stub.init = _not_impl
40
41 class _T: # placeholder types
42 pass
43
44 stub.Result = _T
45 stub.TargetProfile = _T
46 stub.StateDump = _T
47 stub.ShotResult = _T
48 stub.PauliNoise = _T
49 stub.DepolarizingNoise = _T
50 stub.BitFlipNoise = _T
51 stub.PhaseFlipNoise = _T
52 stub.__all__ = [
53 "run",
54 "estimate",
55 "code",
56 "set_quantum_seed",
57 "set_classical_seed",
58 "dump_machine",
59 "init",
60 "Result",
61 "TargetProfile",
62 "StateDump",
63 "ShotResult",
64 "PauliNoise",
65 "DepolarizingNoise",
66 "BitFlipNoise",
67 "PhaseFlipNoise",
68 "estimator",
69 "openqasm",
70 "utils",
71 ]
72 # Minimal submodules to back lifted shims
73 est = types.ModuleType("qsharp.estimator")
74 est.__doc__ = "mock estimator"
75 sys.modules["qsharp.estimator"] = est
76 stub.estimator = est
77 oq = types.ModuleType("qsharp.openqasm")
78 oq.__doc__ = "mock openqasm"
79 sys.modules["qsharp.openqasm"] = oq
80 stub.openqasm = oq
81 utils_mod = types.ModuleType("qsharp.utils")
82 utils_mod.dump_operation = _not_impl
83 sys.modules["qsharp.utils"] = utils_mod
84 stub.utils = utils_mod
85
86 sys.modules["qsharp"] = stub
87 # Telemetry events package with on_qdk_import function expected by qdk import
88 telemetry_pkg = types.ModuleType("qsharp.telemetry_events")
89
90 def on_qdk_import():
91 return None
92
93 telemetry_pkg.on_qdk_import = on_qdk_import
94 sys.modules["qsharp.telemetry_events"] = telemetry_pkg
95 # Interop namespace for qiskit shim expectations
96 interop = types.ModuleType("qsharp.interop")
97 sys.modules["qsharp.interop"] = interop
98 interop_qk = types.ModuleType("qsharp.interop.qiskit")
99 interop_qk.__doc__ = "mock qsharp interop qiskit"
100 sys.modules["qsharp.interop.qiskit"] = interop_qk
101
102 created.extend(
103 [
104 "qsharp",
105 "qsharp.estimator",
106 "qsharp.openqasm",
107 "qsharp.utils",
108 "qsharp.telemetry_events",
109 "qsharp.interop",
110 "qsharp.interop.qiskit",
111 ]
112 )
113 return created
114
115
116def mock_widgets() -> List[str]:
117 created: List[str] = []
118 if "qsharp_widgets" not in sys.modules:
119 mod = types.ModuleType("qsharp_widgets")
120 sys.modules["qsharp_widgets"] = mod
121 created.append("qsharp_widgets")
122 return created
123
124
125def mock_azure() -> List[str]:
126 created: List[str] = []
127 if "azure" not in sys.modules:
128 sys.modules["azure"] = types.ModuleType("azure")
129 created.append("azure")
130 if "azure.quantum" not in sys.modules:
131 aq = types.ModuleType("azure.quantum")
132 # Minimal submodules expected by qdk.azure shim
133 tgt = types.ModuleType("azure.quantum.target")
134 argt = types.ModuleType("azure.quantum.argument_types")
135 job = types.ModuleType("azure.quantum.job")
136 # Register in sys.modules first
137 sys.modules["azure.quantum.target"] = tgt
138 sys.modules["azure.quantum.argument_types"] = argt
139 sys.modules["azure.quantum.job"] = job
140 # Attach to parent for attribute access
141 aq.target = tgt
142 aq.argument_types = argt
143 aq.job = job
144 sys.modules["azure.quantum"] = aq
145 created.extend(
146 [
147 "azure.quantum",
148 "azure.quantum.target",
149 "azure.quantum.argument_types",
150 "azure.quantum.job",
151 ]
152 )
153 return created
154
155
156def mock_qiskit() -> List[str]:
157 created: List[str] = []
158 if "qiskit" not in sys.modules:
159 qk = types.ModuleType("qiskit")
160 qk.transpile = _not_impl
161 sys.modules["qiskit"] = qk
162 created.append("qiskit")
163 return created
164
165
166def cleanup_modules(created: List[str]) -> None:
167 """Remove synthetic modules created during a test if still present."""
168 for name in created:
169 sys.modules.pop(name, None)
170