diff --git a/src/tranqu/device_converter/__init__.py b/src/tranqu/device_converter/__init__.py index c04b4e6..e8497ad 100644 --- a/src/tranqu/device_converter/__init__.py +++ b/src/tranqu/device_converter/__init__.py @@ -5,6 +5,7 @@ DeviceConverterManager, DeviceConverterNotFoundError, ) +from .oqtopus_to_ouqu_tp_device_converter import OqtopusToOuquTpDeviceConverter from .oqtopus_to_qiskit_device_converter import OqtoqusToQiskitDeviceConverter from .pass_through_device_converter import PassThroughDeviceConverter from .qiskit_device import QiskitDevice @@ -15,6 +16,7 @@ "DeviceConverterError", "DeviceConverterManager", "DeviceConverterNotFoundError", + "OqtopusToOuquTpDeviceConverter", "OqtoqusToQiskitDeviceConverter", "PassThroughDeviceConverter", "QiskitDevice", diff --git a/src/tranqu/device_converter/oqtopus_to_ouqu_tp_device_converter.py b/src/tranqu/device_converter/oqtopus_to_ouqu_tp_device_converter.py new file mode 100644 index 0000000..1097aef --- /dev/null +++ b/src/tranqu/device_converter/oqtopus_to_ouqu_tp_device_converter.py @@ -0,0 +1,21 @@ +import json +from typing import Any + +from .device_converter import DeviceConverter + + +class OqtopusToOuquTpDeviceConverter(DeviceConverter): + """Device converter for converting from Oqtopus to ouqu-tp format.""" + + @staticmethod + def convert(device: dict[str, Any]) -> str: + """Convert a Oqtopus device to ouqu-tp format. + + Args: + device (dict[str, Any]): The Oqtopus device to be converted. + + Returns: + str: The converted ouqu-tp format device. + + """ + return json.dumps(device) diff --git a/src/tranqu/tranqu.py b/src/tranqu/tranqu.py index 37a8641..f69f3ab 100644 --- a/src/tranqu/tranqu.py +++ b/src/tranqu/tranqu.py @@ -85,6 +85,7 @@ from .device_converter import ( DeviceConverter, DeviceConverterManager, + OqtopusToOuquTpDeviceConverter, OqtoqusToQiskitDeviceConverter, ) from .device_type_manager import DeviceTypeManager @@ -417,6 +418,11 @@ def _register_builtin_device_converters(self) -> None: "qiskit", OqtoqusToQiskitDeviceConverter(), ) + self.register_device_converter( + "oqtopus", + "ouqu-tp", + OqtopusToOuquTpDeviceConverter(), + ) def _register_builtin_transpilers(self) -> None: self.register_transpiler("qiskit", QiskitTranspiler()) diff --git a/tests/tranqu/transpiler/test_ouqu_tp_transpiler.py b/tests/tranqu/transpiler/test_ouqu_tp_transpiler.py index e9ad70c..899110e 100644 --- a/tests/tranqu/transpiler/test_ouqu_tp_transpiler.py +++ b/tests/tranqu/transpiler/test_ouqu_tp_transpiler.py @@ -1,3 +1,5 @@ +from typing import Any + import pytest from pytket import Circuit as TketCircuit from qiskit import QuantumCircuit # type: ignore[import-untyped] @@ -10,7 +12,35 @@ class TestOuquTpTranspiler: def tranqu(self) -> Tranqu: return Tranqu() - def test_transpile_simple_qasm3_program(self, tranqu: Tranqu): + @pytest.fixture + def simple_device(self) -> dict[str, Any]: + return { + "name": "simple_device", + "qubits": [ + { + "id": 0, + "fidelity": 0.99, + "gate_duration": {"x": 60.0, "sx": 30.0, "rz": 0}, + }, + { + "id": 1, + "fidelity": 0.98, + "gate_duration": {"x": 60.0, "sx": 30.0, "rz": 0}, + }, + ], + "couplings": [ + { + "control": 0, + "target": 1, + "fidelity": 0.95, + "gate_duration": {"cx": 100.0}, + } + ], + } + + def test_transpile_simple_qasm3_program( + self, tranqu: Tranqu, simple_device: dict[str, Any] + ): program = """OPENQASM 3.0; include "stdgates.inc"; qubit[2] q; @@ -19,31 +49,47 @@ def test_transpile_simple_qasm3_program(self, tranqu: Tranqu): cx q[0],q[1]; """ result = tranqu.transpile( - program=program, program_lib="openqasm3", transpiler_lib="ouqu-tp" + program=program, + program_lib="openqasm3", + transpiler_lib="ouqu-tp", + device=simple_device, + device_lib="oqtopus", ) assert isinstance(result.transpiled_program, str) assert result.stats != {} - def test_transpile_qiskit_program(self, tranqu: Tranqu): + def test_transpile_qiskit_program( + self, tranqu: Tranqu, simple_device: dict[str, Any] + ): circuit = QuantumCircuit(2) circuit.h(0) circuit.cx(0, 1) result = tranqu.transpile( - program=circuit, program_lib="qiskit", transpiler_lib="ouqu-tp" + program=circuit, + program_lib="qiskit", + transpiler_lib="ouqu-tp", + device=simple_device, + device_lib="oqtopus", ) assert isinstance(result.transpiled_program, QuantumCircuit) assert result.stats != {} - def test_transpile_tket_program(self, tranqu: Tranqu): + def test_transpile_tket_program( + self, tranqu: Tranqu, simple_device: dict[str, Any] + ): circuit = TketCircuit(2) circuit.H(0) circuit.CX(0, 1) result = tranqu.transpile( - program=circuit, program_lib="tket", transpiler_lib="ouqu-tp" + program=circuit, + program_lib="tket", + transpiler_lib="ouqu-tp", + device=simple_device, + device_lib="oqtopus", ) assert isinstance(result.transpiled_program, TketCircuit)