Learning Home Catalog Composer
Learning
Home Catalog Composer
Tutorials

Compare transpiler settings

Background

To ensure faster and more efficient results, as of 1 March 2024, circuits and observables need to be transformed to only use instructions supported by the QPU (quantum processing unit) before being submitted to the Qiskit Runtime primitives. We call these instruction set architecture (ISA) circuits and observables. One common way to do this is to use the transpiler's generate_preset_pass_manager function. However, you might choose to follow a more manual process.

For example, you might want to target a specific subset of qubits on a specific device. This tutorial tests the performance of different transpiler settings by completing the full process of creating, transpiling, and submitting circuits.

Requirements

Before starting this tutorial, ensure that you have the following installed:

  • Qiskit SDK 1.0 or later, with visualization support (pip install 'qiskit[visualization]')
  • Qiskit Runtime (pip install qiskit-ibm-runtime) 0.22 or later

Setup

Run
The ability to run code cells is currently disabled.
Copy to clipboard

No output produced

Run
The ability to run code cells is currently disabled.
Copy to clipboard

Output:

'ibm_kyoto'

Step 1: Map classical inputs to a quantum problem

Create a small circuit for the transpiler to try to optimize. This example creates a circuit that carries out Grover's algorithm with an oracle that marks the state 111. Next, simulate the ideal distribution (what you'd expect to measure if you ran this on a perfect quantum computer an infinite number of times) for comparison later.

Run
The ability to run code cells is currently disabled.
Copy to clipboard

Output:

Run
The ability to run code cells is currently disabled.
Copy to clipboard

Output:

Step 2: Optimize problem for quantum execution

Next, transpile the circuits for the QPU. You will compare the performance of the transpiler with optimization_level set to 0 (lowest) against 3 (highest). The lowest optimization level does the bare minimum needed to get the circuit running on the device; it maps the circuit qubits to the device qubits and adds swaps gates to allow all two-qubit operations. The highest optimization level is much smarter and uses lots of tricks to reduce the overall gate count. Since multi-qubit gates have high error rates and qubits decohere over time, the shorter circuits should give better results.

The following cell transpiles qc for both values of optimization_level, prints the number of two-qubit gates, and adds the transpiled circuits to a list. Some of the transpiler's algorithms are randomized, so it sets a seed for reproducibility.

Run
The ability to run code cells is currently disabled.
Copy to clipboard

Output:

Two-qubit gates (optimization_level=0):  27
Two-qubit gates (optimization_level=3):  14

Since CNOTs usually have a high error rate, the circuit transpiled with optimization_level=3 should perform much better.

Another way you can improve performance is through dynamic decoupling, by applying a sequence of gates to idling qubits. This cancels out some unwanted interactions with the environment. The following cell adds dynamic decoupling to the circuit transpiled with optimization_level=3 and adds it to the list.

Run
The ability to run code cells is currently disabled.
Copy to clipboard

No output produced

Run
The ability to run code cells is currently disabled.
Copy to clipboard

Output:

Step 3: Execute using Qiskit Primitives

At this point, you have a list of circuits transpiled for the specified QPU. Next, create an instance of the sampler primitive and start a batched job using the context manager (with ...:), which automatically opens and closes the batch.

Within the context manager, sample the circuits and store the results to result.

Run
The ability to run code cells is currently disabled.
Copy to clipboard

No output produced

Step 4: Post-process, return result in classical format

Finally, plot the results from the device runs against the ideal distribution. You can see the results with optimization_level=3 are closer to the ideal distribution due to the lower gate count, and optimization_level=3 + dd is even closer due to the dynamic decoupling.

Run
The ability to run code cells is currently disabled.
Copy to clipboard

Output:

You can confirm this by computing the Hellinger fidelity between each set of results and the ideal distribution (higher is better, and 1 is perfect fidelity).

Run
The ability to run code cells is currently disabled.
Copy to clipboard

Output:

0.651
0.943
0.973

Run
The ability to run code cells is currently disabled.
Copy to clipboard

Output:

'0.24.1'
Run
The ability to run code cells is currently disabled.
Copy to clipboard

Output:

'1.1.1'

Was this page helpful?