|
14 | 14 | import datetime |
15 | 15 | import functools |
16 | 16 | import io |
| 17 | +import math |
17 | 18 | import re |
18 | 19 | from enum import Enum |
19 | 20 | from importlib import metadata |
@@ -252,7 +253,55 @@ def test_graphstate_seed() -> None: |
252 | 253 | assert qc_no_seed.name == "graphstate" |
253 | 254 |
|
254 | 255 |
|
255 | | -# Test the dynamic GHZ circuit |
| 256 | +# Test the dynamic circuits |
| 257 | +def _conditional_phase_angles(qc: QuantumCircuit) -> list[float]: |
| 258 | + """Return phase angles applied inside conditional blocks.""" |
| 259 | + angles = [] |
| 260 | + for instruction in qc.data: |
| 261 | + operation = instruction.operation |
| 262 | + if isinstance(operation, IfElseOp): |
| 263 | + angles.extend( |
| 264 | + float(conditional_instruction.operation.params[0]) |
| 265 | + for conditional_instruction in operation.blocks[0].data |
| 266 | + if conditional_instruction.operation.name == "p" |
| 267 | + ) |
| 268 | + return angles |
| 269 | + |
| 270 | + |
| 271 | +def test_dynamic_qft_description() -> None: |
| 272 | + """Verify that the Dynamic QFT benchmark is registered with its canonical name.""" |
| 273 | + description = get_benchmark_description("dynamic_qft") |
| 274 | + assert "Dynamic QFT" in description |
| 275 | + assert "Dynamical" not in description |
| 276 | + |
| 277 | + |
| 278 | +def test_dynamic_qft_circuit_structure() -> None: |
| 279 | + """Verify the structure of the Dynamic QFT benchmark.""" |
| 280 | + qc = create_circuit("dynamic_qft", 5) |
| 281 | + |
| 282 | + assert qc.name == "dynamic_qft" |
| 283 | + assert qc.num_qubits == 5 |
| 284 | + assert len(qc.cregs) == 1 |
| 285 | + assert qc.cregs[0].name == "c" |
| 286 | + assert qc.cregs[0].size == 5 |
| 287 | + |
| 288 | + ops = qc.count_ops() |
| 289 | + assert ops.get("h", 0) == 5 |
| 290 | + assert ops.get("measure", 0) == 5 |
| 291 | + assert ops.get("if_else", 0) == 10 |
| 292 | + assert ops.get("cp", 0) == 0 |
| 293 | + |
| 294 | + expected_angles = sorted(round(math.pi / (2**shift), 12) for shift in range(1, 5) for _ in range(5 - shift)) |
| 295 | + actual_angles = sorted(round(angle, 12) for angle in _conditional_phase_angles(qc)) |
| 296 | + assert actual_angles == expected_angles |
| 297 | + |
| 298 | + |
| 299 | +def test_dynamic_qft_rejects_too_many_qubits() -> None: |
| 300 | + """Verify that Dynamic QFT rejects instances with impractically small phase shifts.""" |
| 301 | + with pytest.raises(ValueError, match="at most 64 qubits"): |
| 302 | + create_circuit("dynamic_qft", 65) |
| 303 | + |
| 304 | + |
256 | 305 | @pytest.mark.parametrize("num_qubits", [1, 2, 3, 7, 10]) |
257 | 306 | def test_dynamic_ghz_circuit_structure(num_qubits: int) -> None: |
258 | 307 | """Verify the structure of the dynamic GHZ circuit for various qubit counts.""" |
|
0 commit comments