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