Info
Click here to run this notebook interactively on Binder.
Using Custom Transpilers and Converters¶
You can use custom components, such as custom transpilers, custom programs, and custom devices that you have created. This page explains how to create and use custom components.
In Tranqu, you can use the following custom components:
In this page, we assume you are developing a library called "Enigma".
from tranqu import Tranqu
tranqu = Tranqu()
Custom Programs¶
The following code is an example of creating your own quantum program class EnigmaCircuit and a ProgramConverter that converts Qiskit's QuantumCircuit to EnigmaCircuit.
Inherit from the ProgramConverter class and implement the convert function.
from qiskit import QuantumCircuit
from tranqu.program_converter import ProgramConverter
class EnigmaCircuit:
# Implement your own quantum circuit class
pass
class QiskitToEnigmaProgramConverter(ProgramConverter):
def convert(self, program: QuantumCircuit) -> EnigmaCircuit:
# Here, return the conversion of Qiskit's QuantumCircuit to EnigmaCircuit
pass
To use QiskitToEnigmaProgramConverter in Tranqu, call the register_program_converter function.
tranqu.register_program_converter("qiskit", "enigma", QiskitToEnigmaProgramConverter())
Custom Devices¶
The following code is an example of creating your own quantum device class EnigmaDevice and a DeviceConverter that converts Qiskit's BackendV2 to EnigmaDevice.
Inherit from the DeviceConverter class and implement the convert function.
from qiskit.providers import BackendV2
from tranqu.device_converter import DeviceConverter
class EnigmaDevice:
# Implement your own quantum device class
pass
class QiskitToEnigmaDeviceConverter(DeviceConverter):
def convert(self, device: BackendV2) -> EnigmaDevice:
# Here, return the conversion of Qiskit's BackendV2 to EnigmaDevice
pass
To use QiskitToEnigmaDeviceConverter in Tranqu, call the register_device_converter function.
tranqu.register_device_converter("qiskit", "enigma", QiskitToEnigmaDeviceConverter())
Custom Transpilers¶
The following code is an example of creating your own transpiler class EnigmaTraspiler that can transpile EnigmaCircuit.
To do so, inherit from the Transpiler class and implement the transpile function.
from tranqu import TranspileResult
from tranqu.transpiler import Transpiler
class EnigmaTranspiler(Transpiler):
def transpile(
self,
program: EnigmaCircuit,
options: dict | None = None,
device: EnigmaDevice | None = None,
) -> TranspileResult:
# Implement custom transpile logic here
pass
To use EnigmaTranspiler in Tranqu, call the register_transpiler function.
tranqu.register_transpiler("enigma", EnigmaTranspiler())
Run transpile function¶
When you execute the transpile function of a Tranqu instance with registered custom components, Tranqu follows the steps explained in "How it Works" to convert the program and device information into the Enigma format and perform transpilation using Enigma.
Warning
To make this code actually work, you need to implement the parts where "pass" is used.
from qiskit_ibm_runtime.fake_provider import FakeManilaV2
backend = FakeManilaV2()
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.measure_all()
result = tranqu.transpile(
program=circuit,
program_lib="qiskit",
transpiler_lib="enigma",
device=backend,
device_lib="qiskit",
)