|
| 1 | +from typing import cast |
| 2 | + |
| 3 | +import os |
| 4 | +import pulser as pl |
| 5 | +import pytest |
| 6 | +import conftest |
| 7 | +import torch_geometric.data as pyg_data |
| 8 | +import torch_geometric.datasets as pyg_dataset |
| 9 | +from qek.backends import CompilationError, QutipBackend, BaseBackend |
| 10 | +import qek.data.graphs as qek_graphs |
| 11 | + |
| 12 | +if os.name == "posix": |
| 13 | + # As of this writing, emu-mps only works under Unix. |
| 14 | + from qek.backends import EmuMPSBackend |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.asyncio |
| 18 | +async def test_async_emulators() -> None: |
| 19 | + """ |
| 20 | + Test that backends based on emulators can execute without exploding (async). |
| 21 | + """ |
| 22 | + conftest.preload_dataset() |
| 23 | + |
| 24 | + # Load dataset |
| 25 | + original_ptcfm_data = [ |
| 26 | + cast(pyg_data.Data, d) for d in pyg_dataset.TUDataset(root="dataset", name="PTC_FM") |
| 27 | + ] |
| 28 | + |
| 29 | + compiled: list[tuple[qek_graphs.BaseGraph, pl.Register, pl.Pulse]] = [] |
| 30 | + for i, data in enumerate(original_ptcfm_data): |
| 31 | + graph = qek_graphs.PTCFMGraph(data=data, device=pl.AnalogDevice, id=i) |
| 32 | + try: |
| 33 | + register = graph.compile_register() |
| 34 | + pulse = graph.compile_pulse() |
| 35 | + if len(register.qubits) >= 5: |
| 36 | + # This will be too slow to execute, skip. |
| 37 | + continue |
| 38 | + except CompilationError: |
| 39 | + # Let's just skip graphs that cannot be computed. |
| 40 | + continue |
| 41 | + compiled.append((graph, register, pulse)) |
| 42 | + if len(compiled) >= 5: |
| 43 | + # We only need a few samples. |
| 44 | + break |
| 45 | + |
| 46 | + assert len(compiled) >= 5 |
| 47 | + |
| 48 | + backends: list[BaseBackend] = [QutipBackend(pl.AnalogDevice)] |
| 49 | + if os.name == "posix": |
| 50 | + backends.append(EmuMPSBackend(pl.AnalogDevice)) |
| 51 | + |
| 52 | + for backend in backends: |
| 53 | + for g, register, pulse in compiled: |
| 54 | + result = await backend.run(register, pulse) |
| 55 | + # The only thing we can test from the result is that the values make _some_ kind of sense. |
| 56 | + assert isinstance(result, dict) |
| 57 | + for k, v in result.items(): |
| 58 | + assert isinstance(k, str) |
| 59 | + assert v >= 0 |
| 60 | + for c in k: |
| 61 | + assert c in {"0", "1"} |
0 commit comments