3
$\begingroup$

Is it possible to get the information about the step by step transpilation? I have circuits that I know how it is more or less structured after the transpilation, but when I join them to create a bigger circuit it's difficult to see how qiskit arrives at the final result.

$\endgroup$

1 Answer 1

3
$\begingroup$

The standard way to debug transpile is via the callback option. From the documentation:

A callback function that will be called after each pass execution. The function will be called with 5 keyword arguments, * pass_: the pass being run.

  • dag: the dag output of the pass.
  • time: the time to execute the pass.
  • property_set: the property set.
  • count: the index for the pass execution.

[...] For example:

def callback_func(**kwargs):
    pass_ = kwargs['pass_']
    dag = kwargs['dag']
    time = kwargs['time']
    property_set = kwargs['property_set']
    count = kwargs['count']

transpile(circ, callback=callback_func)

You can use callback to inspect intermediate states of the DAGCircuit, which is the internal representation of your circuit.

Example

My usual workflow starts with detecting the interesting "jump". Most of the time count_ops does the trick.

def callback_func(**kwargs):
    dag = kwargs['dag']
    pass_ = kwargs['pass_']
    count = kwargs['count']
    print(count, pass_.name(), dag.count_ops())

When executing with this callback, the output looks something like this:

0 ContainsInstruction {'quantum_volume_[3,4,42]': 1}
1 UnitarySynthesis {'quantum_volume_[3,4,42]': 1}
2 HighLevelSynthesis {'quantum_volume_[3,4,42]': 1}
3 UnrollCustomDefinitions {'u': 32, 'cx': 12}
4 BasisTranslator {'cx': 12, 'rz': 96, 'sx': 64}
...

I might notice that, say, UnrollCustomDefinitions changes the basis is a way I was not expected. Then, I focus on that one, getting the circuit before and after:

from qiskit.converters import dag_to_circuit

def callback_func(**kwargs):
    dag = kwargs['dag']
    count = kwargs['count']
    if 2 <= count <= 3:
        print(dag_to_circuit(dag))

This will print the circuit at count 2 and 3 (before and after UnrollCustomDefinitions).

Important note: Consider setting transpile(..., seed_transpiler=<a number>) to make sure the transpilation process is always the same.

Other tools

Alternatively, you can set a breakpoint inside the callback_func definition to explore the transpilation with your usually debugger. For some situations, this might be the prefer way, instead of rerunning transpile several times, like in the example.

There are other tools, like Harshit Gupta's https://github.com/TheGupta2012/qiskit-timeline-debugger which provide you a more user friendly way to see your circuit evolution.

$\endgroup$
3
  • $\begingroup$ Is it possible to ask for an example? I just want to print all the pipeline from the beginning to the final circuit presented in the notebook. $\endgroup$ Sep 13, 2023 at 18:55
  • 1
    $\begingroup$ example added. Let me know if it helps. $\endgroup$
    – luciano
    Sep 13, 2023 at 19:15
  • $\begingroup$ Many thanks for the example (callbacks is something, since ns3) I wasn't expected to find too often. It seems everything will work pretty well, thanks a lot! $\endgroup$ Sep 15, 2023 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.