Skip to content

Commit

Permalink
set virtual-physical mapping in TranspileResult
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Dec 24, 2024
1 parent 54020f3 commit 0bc89b4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 63 deletions.
56 changes: 0 additions & 56 deletions src/tranqu/transpiler/base_qiskit_transpiler.py

This file was deleted.

6 changes: 5 additions & 1 deletion src/tranqu/transpiler/ouqu_tp_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from tranqu.transpile_result import TranspileResult

from .qiskit_layout_mapper import QiskitLayoutMapper
from .qiskit_stats_extractor import QiskitStatsExtractor
from .transpiler import Transpiler

Expand All @@ -18,6 +19,7 @@ class OuquTpTranspiler(Transpiler):
def __init__(self) -> None:
self._ouqu_tp = OuquTp()
self._qiskit_stats_extractor = QiskitStatsExtractor()
self._layout_mapper = QiskitLayoutMapper()

def transpile(
self,
Expand Down Expand Up @@ -52,4 +54,6 @@ def transpile(
),
}

return TranspileResult(transpile_response.qasm, stats, {})
mapping = self._layout_mapper.create_mapping_from_layout(transpiled_circuit)

return TranspileResult(transpile_response.qasm, stats, mapping)
39 changes: 39 additions & 0 deletions src/tranqu/transpiler/qiskit_layout_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from qiskit import QuantumCircuit # type: ignore[import-untyped]


class QiskitLayoutMapper:
"""Maps virtual qubits to physical qubits for Qiskit quantum circuits."""

@staticmethod
def create_mapping_from_layout(
transpiled_circuit: QuantumCircuit,
) -> dict[str, dict[int, int]]:
"""Create a mapping between virtual and physical (qu)bits.
Args:
transpiled_circuit (QuantumCircuit): The transpiled quantum circuit.
Returns:
dict[str, dict[int, int]]: A dictionary containing the mapping between
virtual and physical qubits and classical bits.
"""
mapping: dict[str, dict[int, int]] = {"qubit_mapping": {}, "bit_mapping": {}}

layout = transpiled_circuit.layout
if layout is not None:
final_layout = layout.final_index_layout()
for virtual_bit, physical_bit in enumerate(final_layout):
mapping["qubit_mapping"][virtual_bit] = physical_bit

if transpiled_circuit.num_clbits > 0:
mapping["bit_mapping"] = {
i: i for i in range(transpiled_circuit.num_clbits)
}
else:
for index in range(transpiled_circuit.num_qubits):
mapping["qubit_mapping"][index] = index
for index in range(transpiled_circuit.num_clbits):
mapping["bit_mapping"][index] = index

return mapping
24 changes: 18 additions & 6 deletions src/tranqu/transpiler/qiskit_transpiler.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import copy
from typing import Any

from qiskit import QuantumCircuit # type: ignore[import-untyped]
from qiskit import transpile as qiskit_transpile
from qiskit import transpile as qiskit_transpile # type: ignore[import-untyped]

from tranqu.transpile_result import TranspileResult

from .base_qiskit_transpiler import BaseQiskitTranspiler
from .qiskit_layout_mapper import QiskitLayoutMapper
from .qiskit_stats_extractor import QiskitStatsExtractor
from .transpiler import Transpiler


class QiskitTranspiler(BaseQiskitTranspiler):
class QiskitTranspiler(Transpiler):
"""Transpile quantum circuits using Qiskit.
It optimizes quantum circuits using Qiskit's `transpile()` function.
"""

def __init__(self) -> None:
self._stats_extractor = QiskitStatsExtractor()
self._layout_mapper = QiskitLayoutMapper()

def transpile(
self,
program: QuantumCircuit,
Expand All @@ -36,9 +41,16 @@ def transpile(
and the mapping of virtual qubits to physical qubits.
"""
_options = copy.deepcopy(options or {})
_options = options or {}
if device is not None:
_options["backend"] = device

transpiled_program = qiskit_transpile(program, **_options)
return self._create_transpile_result(program, transpiled_program)

stats = {
"before": self._stats_extractor.extract_stats_from(program),
"after": self._stats_extractor.extract_stats_from(transpiled_program),
}
mapping = self._layout_mapper.create_mapping_from_layout(transpiled_program)

return TranspileResult(transpiled_program, stats, mapping)
3 changes: 3 additions & 0 deletions tests/tranqu/transpiler/test_ouqu_tp_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_transpile_simple_qasm3_program(

assert isinstance(result.transpiled_program, str)
assert result.stats != {}
assert result.virtual_physical_mapping != {}

def test_transpile_qiskit_program(
self, tranqu: Tranqu, simple_device: dict[str, Any]
Expand All @@ -76,6 +77,7 @@ def test_transpile_qiskit_program(

assert isinstance(result.transpiled_program, QuantumCircuit)
assert result.stats != {}
assert result.virtual_physical_mapping != {}

def test_transpile_tket_program(
self, tranqu: Tranqu, simple_device: dict[str, Any]
Expand All @@ -94,3 +96,4 @@ def test_transpile_tket_program(

assert isinstance(result.transpiled_program, TketCircuit)
assert result.stats != {}
assert result.virtual_physical_mapping != {}

0 comments on commit 0bc89b4

Please sign in to comment.