microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/pip/tests/test_re.py
398lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | import qsharp |
| 5 | from qsharp.estimator import EstimatorParams, QubitParams, QECScheme, LogicalCounts |
| 6 | |
| 7 | |
| 8 | def test_qsharp_estimation() -> None: |
| 9 | source = """{{ |
| 10 | use qs = Qubit[10]; |
| 11 | for q in qs {{ |
| 12 | T(q); |
| 13 | M(q); |
| 14 | }} |
| 15 | }}""" |
| 16 | |
| 17 | qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted) |
| 18 | res = qsharp.estimate(source) |
| 19 | assert res["status"] == "success" |
| 20 | assert res["physicalCounts"] is not None |
| 21 | assert res.logical_counts == LogicalCounts( |
| 22 | { |
| 23 | "numQubits": 10, |
| 24 | "tCount": 10, |
| 25 | "rotationCount": 0, |
| 26 | "rotationDepth": 0, |
| 27 | "cczCount": 0, |
| 28 | "measurementCount": 10, |
| 29 | } |
| 30 | ) |
| 31 | |
| 32 | res_logical = qsharp.logical_counts(source) |
| 33 | assert res_logical == res.logical_counts |
| 34 | |
| 35 | |
| 36 | def test_qsharp_estimation_from_precalculated_counts() -> None: |
| 37 | source = """{{ |
| 38 | import Std.ResourceEstimation.*; |
| 39 | use qubits = Qubit[12581]; |
| 40 | AccountForEstimates( |
| 41 | [TCount(12), RotationCount(12), RotationDepth(12), |
| 42 | CczCount(3731607428), MeasurementCount(1078154040)], |
| 43 | PSSPCLayout(), qubits); |
| 44 | }}""" |
| 45 | |
| 46 | qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted) |
| 47 | res = qsharp.estimate(source) |
| 48 | |
| 49 | assert res["status"] == "success" |
| 50 | assert res["physicalCounts"] is not None |
| 51 | assert res.logical_counts == LogicalCounts( |
| 52 | { |
| 53 | "numQubits": 12581, |
| 54 | "tCount": 12, |
| 55 | "rotationCount": 12, |
| 56 | "rotationDepth": 12, |
| 57 | "cczCount": 3731607428, |
| 58 | "measurementCount": 1078154040, |
| 59 | } |
| 60 | ) |
| 61 | |
| 62 | res_logical = qsharp.logical_counts(source) |
| 63 | |
| 64 | assert res_logical == res.logical_counts |
| 65 | |
| 66 | |
| 67 | def test_qsharp_estimation_with_single_params() -> None: |
| 68 | qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted) |
| 69 | |
| 70 | params = EstimatorParams() |
| 71 | params.error_budget = 0.333 |
| 72 | params.qubit_params.name = QubitParams.MAJ_NS_E4 |
| 73 | assert params.as_dict() == { |
| 74 | "qubitParams": {"name": "qubit_maj_ns_e4"}, |
| 75 | "errorBudget": 0.333, |
| 76 | } |
| 77 | |
| 78 | res = qsharp.estimate( |
| 79 | """{{ |
| 80 | use qs = Qubit[10]; |
| 81 | for q in qs {{ |
| 82 | T(q); |
| 83 | M(q); |
| 84 | }} |
| 85 | }}""", |
| 86 | params=params, |
| 87 | ) |
| 88 | |
| 89 | assert res["status"] == "success" |
| 90 | assert res["physicalCounts"] is not None |
| 91 | assert res["jobParams"]["qubitParams"]["name"] == "qubit_maj_ns_e4" |
| 92 | assert res.logical_counts == LogicalCounts( |
| 93 | { |
| 94 | "numQubits": 10, |
| 95 | "tCount": 10, |
| 96 | "rotationCount": 0, |
| 97 | "rotationDepth": 0, |
| 98 | "cczCount": 0, |
| 99 | "measurementCount": 10, |
| 100 | } |
| 101 | ) |
| 102 | |
| 103 | |
| 104 | def test_qsharp_estimation_with_multiple_params() -> None: |
| 105 | qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted) |
| 106 | |
| 107 | params = EstimatorParams(3) |
| 108 | params.items[0].qubit_params.name = QubitParams.GATE_US_E3 |
| 109 | params.items[0].error_budget = 0.333 |
| 110 | params.items[1].qubit_params.name = QubitParams.GATE_US_E4 |
| 111 | params.items[1].error_budget = 0.333 |
| 112 | params.items[2].qubit_params.name = QubitParams.MAJ_NS_E6 |
| 113 | params.items[2].qec_scheme.name = QECScheme.FLOQUET_CODE |
| 114 | params.items[2].error_budget = 0.333 |
| 115 | assert params.as_dict() == { |
| 116 | "items": [ |
| 117 | { |
| 118 | "qubitParams": {"name": "qubit_gate_us_e3"}, |
| 119 | "errorBudget": 0.333, |
| 120 | }, |
| 121 | { |
| 122 | "qubitParams": {"name": "qubit_gate_us_e4"}, |
| 123 | "errorBudget": 0.333, |
| 124 | }, |
| 125 | { |
| 126 | "qubitParams": {"name": "qubit_maj_ns_e6"}, |
| 127 | "qecScheme": {"name": "floquet_code"}, |
| 128 | "errorBudget": 0.333, |
| 129 | }, |
| 130 | ], |
| 131 | "resumeAfterFailedItem": True, |
| 132 | } |
| 133 | |
| 134 | res = qsharp.estimate( |
| 135 | """{{ |
| 136 | use qs = Qubit[10]; |
| 137 | for q in qs {{ |
| 138 | T(q); |
| 139 | M(q); |
| 140 | }} |
| 141 | }}""", |
| 142 | params=params, |
| 143 | ) |
| 144 | |
| 145 | for idx in res: |
| 146 | assert res[idx]["status"] == "success" |
| 147 | assert res[idx]["physicalCounts"] is not None |
| 148 | assert ( |
| 149 | res[idx]["jobParams"]["qubitParams"]["name"] |
| 150 | == params.items[idx].qubit_params.name |
| 151 | ) |
| 152 | assert res[idx]["logicalCounts"] == LogicalCounts( |
| 153 | { |
| 154 | "numQubits": 10, |
| 155 | "tCount": 10, |
| 156 | "rotationCount": 0, |
| 157 | "rotationDepth": 0, |
| 158 | "cczCount": 0, |
| 159 | "measurementCount": 10, |
| 160 | } |
| 161 | ) |
| 162 | assert res[2]["jobParams"]["qecScheme"]["name"] == QECScheme.FLOQUET_CODE |
| 163 | |
| 164 | |
| 165 | def test_qsharp_estimation_with_multiple_params_from_python_callable() -> None: |
| 166 | qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted) |
| 167 | |
| 168 | params = EstimatorParams(3) |
| 169 | params.items[0].qubit_params.name = QubitParams.GATE_US_E3 |
| 170 | params.items[0].error_budget = 0.333 |
| 171 | params.items[1].qubit_params.name = QubitParams.GATE_US_E4 |
| 172 | params.items[1].error_budget = 0.333 |
| 173 | params.items[2].qubit_params.name = QubitParams.MAJ_NS_E6 |
| 174 | params.items[2].qec_scheme.name = QECScheme.FLOQUET_CODE |
| 175 | params.items[2].error_budget = 0.333 |
| 176 | assert params.as_dict() == { |
| 177 | "items": [ |
| 178 | { |
| 179 | "qubitParams": {"name": "qubit_gate_us_e3"}, |
| 180 | "errorBudget": 0.333, |
| 181 | }, |
| 182 | { |
| 183 | "qubitParams": {"name": "qubit_gate_us_e4"}, |
| 184 | "errorBudget": 0.333, |
| 185 | }, |
| 186 | { |
| 187 | "qubitParams": {"name": "qubit_maj_ns_e6"}, |
| 188 | "qecScheme": {"name": "floquet_code"}, |
| 189 | "errorBudget": 0.333, |
| 190 | }, |
| 191 | ], |
| 192 | "resumeAfterFailedItem": True, |
| 193 | } |
| 194 | |
| 195 | qsharp.eval( |
| 196 | """ |
| 197 | operation Test() : Unit { |
| 198 | use qs = Qubit[10]; |
| 199 | for q in qs { |
| 200 | T(q); |
| 201 | M(q); |
| 202 | } |
| 203 | } |
| 204 | """ |
| 205 | ) |
| 206 | |
| 207 | res = qsharp.estimate(qsharp.code.Test, params=params) |
| 208 | |
| 209 | for idx in res: |
| 210 | assert res[idx]["status"] == "success" |
| 211 | assert res[idx]["physicalCounts"] is not None |
| 212 | assert ( |
| 213 | res[idx]["jobParams"]["qubitParams"]["name"] |
| 214 | == params.items[idx].qubit_params.name |
| 215 | ) |
| 216 | assert res[idx]["logicalCounts"] == LogicalCounts( |
| 217 | { |
| 218 | "numQubits": 10, |
| 219 | "tCount": 10, |
| 220 | "rotationCount": 0, |
| 221 | "rotationDepth": 0, |
| 222 | "cczCount": 0, |
| 223 | "measurementCount": 10, |
| 224 | } |
| 225 | ) |
| 226 | assert res[2]["jobParams"]["qecScheme"]["name"] == QECScheme.FLOQUET_CODE |
| 227 | |
| 228 | res_logical = qsharp.logical_counts(qsharp.code.Test) |
| 229 | assert res_logical == res[0]["logicalCounts"] |
| 230 | |
| 231 | |
| 232 | def test_qsharp_estimation_with_multiple_params_from_python_callable_with_arg() -> None: |
| 233 | qsharp.init(target_profile=qsharp.TargetProfile.Unrestricted) |
| 234 | |
| 235 | params = EstimatorParams(3) |
| 236 | params.items[0].qubit_params.name = QubitParams.GATE_US_E3 |
| 237 | params.items[0].error_budget = 0.333 |
| 238 | params.items[1].qubit_params.name = QubitParams.GATE_US_E4 |
| 239 | params.items[1].error_budget = 0.333 |
| 240 | params.items[2].qubit_params.name = QubitParams.MAJ_NS_E6 |
| 241 | params.items[2].qec_scheme.name = QECScheme.FLOQUET_CODE |
| 242 | params.items[2].error_budget = 0.333 |
| 243 | assert params.as_dict() == { |
| 244 | "items": [ |
| 245 | { |
| 246 | "qubitParams": {"name": "qubit_gate_us_e3"}, |
| 247 | "errorBudget": 0.333, |
| 248 | }, |
| 249 | { |
| 250 | "qubitParams": {"name": "qubit_gate_us_e4"}, |
| 251 | "errorBudget": 0.333, |
| 252 | }, |
| 253 | { |
| 254 | "qubitParams": {"name": "qubit_maj_ns_e6"}, |
| 255 | "qecScheme": {"name": "floquet_code"}, |
| 256 | "errorBudget": 0.333, |
| 257 | }, |
| 258 | ], |
| 259 | "resumeAfterFailedItem": True, |
| 260 | } |
| 261 | |
| 262 | qsharp.eval( |
| 263 | """ |
| 264 | operation Test(nQubits : Int) : Unit { |
| 265 | use qs = Qubit[nQubits]; |
| 266 | for q in qs { |
| 267 | T(q); |
| 268 | M(q); |
| 269 | } |
| 270 | } |
| 271 | """ |
| 272 | ) |
| 273 | |
| 274 | res = qsharp.estimate(qsharp.code.Test, params, 7) |
| 275 | |
| 276 | for idx in res: |
| 277 | assert res[idx]["status"] == "success" |
| 278 | assert res[idx]["physicalCounts"] is not None |
| 279 | assert ( |
| 280 | res[idx]["jobParams"]["qubitParams"]["name"] |
| 281 | == params.items[idx].qubit_params.name |
| 282 | ) |
| 283 | assert res[idx]["logicalCounts"] == LogicalCounts( |
| 284 | { |
| 285 | "numQubits": 7, |
| 286 | "tCount": 7, |
| 287 | "rotationCount": 0, |
| 288 | "rotationDepth": 0, |
| 289 | "cczCount": 0, |
| 290 | "measurementCount": 7, |
| 291 | } |
| 292 | ) |
| 293 | assert res[2]["jobParams"]["qecScheme"]["name"] == QECScheme.FLOQUET_CODE |
| 294 | |
| 295 | res_logical = qsharp.logical_counts(qsharp.code.Test, 7) |
| 296 | assert res_logical == res[0]["logicalCounts"] |
| 297 | |
| 298 | |
| 299 | def test_estimation_from_logical_counts() -> None: |
| 300 | logical_counts = LogicalCounts( |
| 301 | { |
| 302 | "numQubits": 12581, |
| 303 | "tCount": 12, |
| 304 | "rotationCount": 12, |
| 305 | "rotationDepth": 12, |
| 306 | "cczCount": 3731607428, |
| 307 | "measurementCount": 1078154040, |
| 308 | } |
| 309 | ) |
| 310 | res = logical_counts.estimate() |
| 311 | |
| 312 | assert res["status"] == "success" |
| 313 | assert res["physicalCounts"] is not None |
| 314 | assert res.logical_counts == logical_counts |
| 315 | |
| 316 | |
| 317 | def test_estimation_from_logical_counts_with_single_params() -> None: |
| 318 | logical_counts = LogicalCounts( |
| 319 | { |
| 320 | "numQubits": 12581, |
| 321 | "tCount": 12, |
| 322 | "rotationCount": 12, |
| 323 | "rotationDepth": 12, |
| 324 | "cczCount": 3731607428, |
| 325 | "measurementCount": 1078154040, |
| 326 | } |
| 327 | ) |
| 328 | params = EstimatorParams() |
| 329 | params.error_budget = 0.333 |
| 330 | params.qubit_params.name = QubitParams.MAJ_NS_E4 |
| 331 | res = logical_counts.estimate(params=params) |
| 332 | |
| 333 | assert res["status"] == "success" |
| 334 | assert res["physicalCounts"] is not None |
| 335 | assert res["jobParams"]["qubitParams"]["name"] == "qubit_maj_ns_e4" |
| 336 | assert res.logical_counts == logical_counts |
| 337 | assert "frontierEntries" not in res |
| 338 | |
| 339 | |
| 340 | def test_estimation_from_logical_counts_with_multiple_params() -> None: |
| 341 | logical_counts = LogicalCounts( |
| 342 | { |
| 343 | "numQubits": 12581, |
| 344 | "tCount": 12, |
| 345 | "rotationCount": 12, |
| 346 | "rotationDepth": 12, |
| 347 | "cczCount": 3731607428, |
| 348 | "measurementCount": 1078154040, |
| 349 | } |
| 350 | ) |
| 351 | params = EstimatorParams(3) |
| 352 | params.items[0].qubit_params.name = QubitParams.GATE_US_E3 |
| 353 | params.items[0].error_budget = 0.333 |
| 354 | params.items[1].qubit_params.name = QubitParams.GATE_US_E4 |
| 355 | params.items[1].error_budget = 0.333 |
| 356 | params.items[2].qubit_params.name = QubitParams.MAJ_NS_E6 |
| 357 | params.items[2].qec_scheme.name = QECScheme.FLOQUET_CODE |
| 358 | params.items[2].error_budget = 0.333 |
| 359 | res = logical_counts.estimate(params=params) |
| 360 | |
| 361 | for idx in res: |
| 362 | assert res[idx]["status"] == "success" |
| 363 | assert res[idx]["physicalCounts"] is not None |
| 364 | assert ( |
| 365 | res[idx]["jobParams"]["qubitParams"]["name"] |
| 366 | == params.items[idx].qubit_params.name |
| 367 | ) |
| 368 | assert res[idx]["logicalCounts"] == logical_counts |
| 369 | assert res[2]["jobParams"]["qecScheme"]["name"] == QECScheme.FLOQUET_CODE |
| 370 | |
| 371 | |
| 372 | def test_building_frontier_from_logical_counts_with_single_params() -> None: |
| 373 | logical_counts = LogicalCounts( |
| 374 | { |
| 375 | "numQubits": 12581, |
| 376 | "tCount": 12, |
| 377 | "rotationCount": 12, |
| 378 | "rotationDepth": 12, |
| 379 | "cczCount": 3731607428, |
| 380 | "measurementCount": 1078154040, |
| 381 | } |
| 382 | ) |
| 383 | params = EstimatorParams() |
| 384 | params.error_budget = 0.333 |
| 385 | params.qubit_params.name = QubitParams.MAJ_NS_E4 |
| 386 | params.estimate_type = "frontier" |
| 387 | res = logical_counts.estimate(params=params) |
| 388 | |
| 389 | assert res["status"] == "success" |
| 390 | assert "physicalCounts" not in res |
| 391 | assert "physicalCountsFormatted" not in res |
| 392 | assert res["jobParams"]["qubitParams"]["name"] == "qubit_maj_ns_e4" |
| 393 | assert res.logical_counts == logical_counts |
| 394 | assert res["frontierEntries"] is not None |
| 395 | assert len(res["frontierEntries"]) > 0 |
| 396 | first_entry = res["frontierEntries"][0] |
| 397 | assert first_entry["physicalCounts"] is not None |
| 398 | assert first_entry["physicalCountsFormatted"] is not None |
| 399 | |