Skip to content

Commit

Permalink
add OqtopusToOuquTpDeviceConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Dec 24, 2024
1 parent 12443fc commit 54020f3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/tranqu/device_converter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,6 +16,7 @@
"DeviceConverterError",
"DeviceConverterManager",
"DeviceConverterNotFoundError",
"OqtopusToOuquTpDeviceConverter",
"OqtoqusToQiskitDeviceConverter",
"PassThroughDeviceConverter",
"QiskitDevice",
Expand Down
21 changes: 21 additions & 0 deletions src/tranqu/device_converter/oqtopus_to_ouqu_tp_device_converter.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions src/tranqu/tranqu.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
from .device_converter import (
DeviceConverter,
DeviceConverterManager,
OqtopusToOuquTpDeviceConverter,
OqtoqusToQiskitDeviceConverter,
)
from .device_type_manager import DeviceTypeManager
Expand Down Expand Up @@ -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())
Expand Down
58 changes: 52 additions & 6 deletions tests/tranqu/transpiler/test_ouqu_tp_transpiler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

import pytest
from pytket import Circuit as TketCircuit
from qiskit import QuantumCircuit # type: ignore[import-untyped]
Expand All @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit 54020f3

Please sign in to comment.