microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/pip/tests-integration/interop_qiskit/test_re.py
193lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | from concurrent.futures import ThreadPoolExecutor |
| 5 | import pytest |
| 6 | |
| 7 | from qsharp import QSharpError |
| 8 | from qsharp.estimator import ( |
| 9 | EstimatorParams, |
| 10 | QubitParams, |
| 11 | LogicalCounts, |
| 12 | ) |
| 13 | |
| 14 | |
| 15 | from interop_qiskit import QISKIT_AVAILABLE, SKIP_REASON |
| 16 | |
| 17 | if QISKIT_AVAILABLE: |
| 18 | from .test_circuits import ( |
| 19 | generate_repro_information, |
| 20 | ) |
| 21 | from qiskit.circuit import QuantumCircuit, Parameter |
| 22 | from qiskit.circuit.library import RGQFTMultiplier |
| 23 | from qsharp.interop.qiskit import ResourceEstimatorBackend |
| 24 | from qiskit.version import __version__ as QISKIT_VERSION |
| 25 | |
| 26 | |
| 27 | @pytest.mark.skipif(not QISKIT_AVAILABLE, reason=SKIP_REASON) |
| 28 | def test_qsharp_estimation_with_single_params() -> None: |
| 29 | params = EstimatorParams() |
| 30 | params.error_budget = 0.333 |
| 31 | params.qubit_params.name = QubitParams.MAJ_NS_E4 |
| 32 | assert params.as_dict() == { |
| 33 | "qubitParams": {"name": "qubit_maj_ns_e4"}, |
| 34 | "errorBudget": 0.333, |
| 35 | } |
| 36 | circuit = QuantumCircuit(10, 10) |
| 37 | for index in range(10): |
| 38 | circuit.t(index) |
| 39 | circuit.measure(index, index) |
| 40 | sim = ResourceEstimatorBackend(skip_transpilation=True) |
| 41 | res = sim.run(circuit, params=params).result() |
| 42 | |
| 43 | assert res["status"] == "success" |
| 44 | assert res["physicalCounts"] is not None |
| 45 | assert res["jobParams"]["qubitParams"]["name"] == "qubit_maj_ns_e4" |
| 46 | assert res.logical_counts == LogicalCounts( |
| 47 | { |
| 48 | "numQubits": 10, |
| 49 | "tCount": 10, |
| 50 | "rotationCount": 0, |
| 51 | "rotationDepth": 0, |
| 52 | "cczCount": 0, |
| 53 | "measurementCount": 10, |
| 54 | } |
| 55 | ) |
| 56 | |
| 57 | |
| 58 | @pytest.mark.skipif(not QISKIT_AVAILABLE, reason=SKIP_REASON) |
| 59 | def test_estimate_qiskit_rgqft_multiplier() -> None: |
| 60 | bitwidth = 4 |
| 61 | circuit = RGQFTMultiplier(num_state_qubits=bitwidth) |
| 62 | params = EstimatorParams() |
| 63 | sim = ResourceEstimatorBackend() |
| 64 | job = sim.run(circuit, params=params, optimization_level=0) |
| 65 | res = job.result() |
| 66 | assert res["status"] == "success" |
| 67 | |
| 68 | if QISKIT_VERSION.startswith("1."): |
| 69 | assert res.logical_counts == LogicalCounts( |
| 70 | { |
| 71 | "numQubits": 16, |
| 72 | "tCount": 90, |
| 73 | "rotationCount": 972, |
| 74 | "rotationDepth": 666, |
| 75 | "cczCount": 0, |
| 76 | "ccixCount": 0, |
| 77 | "measurementCount": 0, |
| 78 | } |
| 79 | ) |
| 80 | elif QISKIT_VERSION.startswith("2."): |
| 81 | assert res.logical_counts == LogicalCounts( |
| 82 | { |
| 83 | "numQubits": 16, |
| 84 | "tCount": 154, |
| 85 | "rotationCount": 574, |
| 86 | "rotationDepth": 374, |
| 87 | "cczCount": 0, |
| 88 | "ccixCount": 0, |
| 89 | "measurementCount": 0, |
| 90 | } |
| 91 | ) |
| 92 | else: |
| 93 | assert False, f"Unsupported Qiskit version {QISKIT_VERSION}." |
| 94 | |
| 95 | |
| 96 | @pytest.mark.skipif(not QISKIT_AVAILABLE, reason=SKIP_REASON) |
| 97 | def test_estimate_qiskit_rgqft_multiplier_without_tranpspile() -> None: |
| 98 | bitwidth = 4 |
| 99 | circuit = RGQFTMultiplier(num_state_qubits=bitwidth) |
| 100 | params = EstimatorParams() |
| 101 | sim = ResourceEstimatorBackend(skip_transpilation=True) |
| 102 | job = sim.run(circuit, params=params) |
| 103 | res = job.result() |
| 104 | assert res["status"] == "success" |
| 105 | |
| 106 | if QISKIT_VERSION.startswith("1."): |
| 107 | assert res.logical_counts == LogicalCounts( |
| 108 | { |
| 109 | "numQubits": 16, |
| 110 | "tCount": 76, |
| 111 | "rotationCount": 936, |
| 112 | "rotationDepth": 665, |
| 113 | "cczCount": 0, |
| 114 | "ccixCount": 0, |
| 115 | "measurementCount": 0, |
| 116 | } |
| 117 | ) |
| 118 | elif QISKIT_VERSION.startswith("2."): |
| 119 | assert res.logical_counts == LogicalCounts( |
| 120 | { |
| 121 | "numQubits": 16, |
| 122 | "tCount": 140, |
| 123 | "rotationCount": 532, |
| 124 | "rotationDepth": 369, |
| 125 | "cczCount": 0, |
| 126 | "ccixCount": 0, |
| 127 | "measurementCount": 0, |
| 128 | } |
| 129 | ) |
| 130 | else: |
| 131 | assert False, f"Unsupported Qiskit version {QISKIT_VERSION}." |
| 132 | |
| 133 | |
| 134 | @pytest.mark.skipif(not QISKIT_AVAILABLE, reason=SKIP_REASON) |
| 135 | def test_estimate_qiskit_rgqft_multiplier_in_threadpool() -> None: |
| 136 | bitwidth = 4 |
| 137 | circuit = RGQFTMultiplier(num_state_qubits=bitwidth) |
| 138 | params = EstimatorParams() |
| 139 | executor = ThreadPoolExecutor(max_workers=1) |
| 140 | sim = ResourceEstimatorBackend(executor=executor, skip_transpilation=True) |
| 141 | job = sim.run(circuit, params=params) |
| 142 | res = job.result() |
| 143 | assert res["status"] == "success" |
| 144 | |
| 145 | if QISKIT_VERSION.startswith("1."): |
| 146 | assert res.logical_counts == LogicalCounts( |
| 147 | { |
| 148 | "numQubits": 16, |
| 149 | "tCount": 76, |
| 150 | "rotationCount": 936, |
| 151 | "rotationDepth": 665, |
| 152 | "cczCount": 0, |
| 153 | "ccixCount": 0, |
| 154 | "measurementCount": 0, |
| 155 | } |
| 156 | ) |
| 157 | elif QISKIT_VERSION.startswith("2."): |
| 158 | assert res.logical_counts == LogicalCounts( |
| 159 | { |
| 160 | "numQubits": 16, |
| 161 | "tCount": 140, |
| 162 | "rotationCount": 532, |
| 163 | "rotationDepth": 369, |
| 164 | "cczCount": 0, |
| 165 | "ccixCount": 0, |
| 166 | "measurementCount": 0, |
| 167 | } |
| 168 | ) |
| 169 | else: |
| 170 | assert False, f"Unsupported Qiskit version {QISKIT_VERSION}." |
| 171 | |
| 172 | |
| 173 | @pytest.mark.skipif(not QISKIT_AVAILABLE, reason=SKIP_REASON) |
| 174 | def test_estimating_with_unbound_param_raises(): |
| 175 | theta = Parameter("theta") |
| 176 | |
| 177 | circuit = QuantumCircuit(1) |
| 178 | circuit.name = "test" |
| 179 | circuit.rx(theta, 0) |
| 180 | circuit.measure_all() |
| 181 | |
| 182 | backend = ResourceEstimatorBackend() |
| 183 | try: |
| 184 | with pytest.raises(QSharpError) as ex: |
| 185 | _ = backend.run(circuit).result() |
| 186 | message = str(ex.value) |
| 187 | assert "Circuit has unbound input parameters" in message |
| 188 | assert "help: Parameters: theta: Double" in message |
| 189 | except AssertionError: |
| 190 | raise |
| 191 | except Exception as ex: |
| 192 | additional_info = generate_repro_information(circuit, backend) |
| 193 | raise RuntimeError(additional_info) from ex |
| 194 | |