Improving the accuracy of BQSKit compiled circuits with error mitigation#

In this tutorial we describe how to use error mitigation capabilities from Mitiq together with the compilation capabilities of BQSKit, a compiler for quantum circuits. BQSKit stands for Berkeley Quantum Synthesis Toolkit and it allows one “to compile quantum programs to efficient physical circuits for any QPU”.

To get started, ensure you have the requisite python packages by running the following install commands.

  • pip install mitiq

  • pip install 'bqskit[ext]'

The main goal of this tutorial is to understand how to use bqskit together with mitiq. To do this, we will

  1. generate a random circuit,

  2. compile it with bqskit,

  3. use error mitigation on the compiled circuit, and

  4. compare the results obtained with and without error mitigation.

After demonstrating the use of the two packages, we can then try and understand how circuit compilation with BQSKit in general interacts with error mitigation by running the process many times and comparing results.

To begin we import many of the required modules and functions.

import bqskit
from bqskit.ext import cirq_to_bqskit, bqskit_to_cirq
import mitiq

import cirq
import numpy as np
from cirq.contrib.qasm_import import circuit_from_qasm

Random circuit generation#

We use cirq’s random_circuit function to generate a random circuit with specified qubit number, depth and density (which refers to the probability of an operation occurring at a given moment). Here we also use a random seed for reproducibility. The random circuit is then converted to BQSKit’s custom internal representation with the cirq_to_bqskit function.

num_qubits = 3
depth = 10
density = 1

random_circuit = cirq.testing.random_circuit(
    num_qubits, depth, density, random_state=RANDOM_SEED

bqskit_circuit = cirq_to_bqskit(random_circuit)

0: ───X───Z───Y───X───────────────iSwap─────@────H───Y───
                  │               │         │
1: ───S───X───────@───Y───iSwap───┼────────S┼────X───X───
          │               │       │         │
2: ───────@───S───Y───Y───iSwap───iSwap─────@────────────


With the circuit initialized we can compile it with BQSKit. The default optimization of this compiler is to reduce circuit depth, and in doing the compilation assumes an all-to-all connected hardware with \(\mathsf{CNOT}\) and \(\mathsf{U3}\) as native gates.


If you are compiling a circuit to run on a specific QPU, you can use the compile functions model argument to pass a connectivity graph, and native gateset. This allows for one to skip the second pass of compilation that is usually required when first optimizing, and then compiling to hardware.

compiled = bqskit.compile(bqskit_circuit)
compiled_circuit = bqskit_to_cirq(compiled)

q_0: ───cirq.circuits.qasm_output.QasmUGate(theta=1.6905328824328205, phi=0.08537404559363616, lmda=1.6442580548633938)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X───cirq.circuits.qasm_output.QasmUGate(theta=0.5465610735917942, phi=1.7130766537795952, lmda=1.651858626843597)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X───cirq.circuits.qasm_output.QasmUGate(theta=0.40477261161112793, phi=1.2327812488903613, lmda=1.0863062255652722)────@───cirq.circuits.qasm_output.QasmUGate(theta=0.49901364312132607, phi=1.1532675175054756, lmda=1.5204366081180278)─────@───cirq.circuits.qasm_output.QasmUGate(theta=0.5497392058053334, phi=1.9612420406712305, lmda=0.06434188692811382)───X───cirq.circuits.qasm_output.QasmUGate(theta=0.10159732698711757, phi=0.4865594257254381, lmda=0.9648993731972517)───
                                                                                                                                                                                                                                                │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         │                                                                                                                      │                                                                                                                       │                                                                                                                     │
q_1: ───cirq.circuits.qasm_output.QasmUGate(theta=0.9999999998931389, phi=1.0825468908509526, lmda=1.0825468909099503)────X───cirq.circuits.qasm_output.QasmUGate(theta=0.5767723926739627, phi=0.09289162298618514, lmda=1.9819672028259259)───@───cirq.circuits.qasm_output.QasmUGate(theta=0.0563891173717348, phi=1.04339069111018, lmda=1.8399261251982975)────X───cirq.circuits.qasm_output.QasmUGate(theta=1.5000000054756693, phi=0.6027696543921155, lmda=1.9999999984373364)───@───cirq.circuits.qasm_output.QasmUGate(theta=1.5000000008412409, phi=0.2052814980323162, lmda=1.7365629295379708)───@───cirq.circuits.qasm_output.QasmUGate(theta=1.3160951131300194, phi=0.49999999210238455, lmda=0.2947184916797226)───X───cirq.circuits.qasm_output.QasmUGate(theta=0.9038929467707614, phi=0.2821994314557334, lmda=0.27328170079784236)───┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                          │                                                                                                                                                                                                                                         │                                                                                                                    │                                                                                                                    │                                                                                                                     │                                                                                                                     │                                                                                                                      │                                                                                                                       │                                                                                                                     │
q_2: ───cirq.circuits.qasm_output.QasmUGate(theta=0.9999999988954114, phi=0.22663566768871024, lmda=1.6654436888701387)───@───cirq.circuits.qasm_output.QasmUGate(theta=0.801970541657027, phi=1.7710631497359504, lmda=0.25434179761357356)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────@───cirq.circuits.qasm_output.QasmUGate(theta=0.9999999994260816, phi=0.5858212273831289, lmda=0.6212204476076704)───X───cirq.circuits.qasm_output.QasmUGate(theta=1.4999999999919442, phi=0.3967350406690477, lmda=0.5000000006623446)───X───cirq.circuits.qasm_output.QasmUGate(theta=0.999999998957598, phi=1.2216036197970885, lmda=1.1801174795011866)─────@───cirq.circuits.qasm_output.QasmUGate(theta=1.9315817145405105, phi=1.9142001860387903, lmda=0.6790648366558075)────@───cirq.circuits.qasm_output.QasmUGate(theta=0.31425135965993034, phi=0.9808004173540544, lmda=0.16116332803988884)───X───cirq.circuits.qasm_output.QasmUGate(theta=0.22797738866734504, phi=0.016743312253470588, lmda=1.1976802596539162)───X───cirq.circuits.qasm_output.QasmUGate(theta=0.4191494990509067, phi=1.6718390255746296, lmda=1.5012620102834067)────@───cirq.circuits.qasm_output.QasmUGate(theta=1.4564196528984579, phi=1.4091061535097449, lmda=1.462673523108384)─────

We now have two cirq circuits: random_circuit and compiled_circuit. Both represent the same (or very close to the same) unitary operation, but with different gatesets, and with different structure. Now we mitigate them!

Error Mitigation#

Using mitiq’s simplest, and easiest to use method of Zero Noise Extrapolation (ZNE) we can obtain more accurate results than we would otherwise.


There are multiple other techniques described in our user guide which could be used as well.

In this tutorial we assume a simple error model of depolarizing noise on two-qubit gates. To use this method, we need to define a function (in mitiq this is often referred to as an executor) which takes as input a circuit, and returns some sort of expectation value, or probability. We define a function execute which adds a tunable noise parameter, which controls the strength of the simulated noise. This function performs a density matrix simulation along with measuring the probability of observing the system in the ground state.


This error model is not entirely realistic. Two-qubit gates are generally much noisier than single qubit gates, but real quantum hardware is often afflicted with many other types of errors that this model will not account for.

def execute(circuit, noise_level=0.05):
    noisy_circuit = cirq.Circuit()
    for op in circuit.all_operations():
        if len(op.qubits) == 2:
                cirq.depolarize(p=noise_level, n_qubits=2)(*op.qubits)

    rho = (
    return rho[0, 0].real

Since we’d like to see how compilation effects error mitigation, we first simulate the ideal and noisy values using the simulator defined above.

uncompiled_ideal_value = execute(random_circuit, noise_level=0.0)
uncompiled_noisy_value = execute(random_circuit)

compiled_ideal_value = execute(compiled_circuit, noise_level=0.0)
compiled_noisy_value = execute(compiled_circuit)

With these values taken, we are now ready to use ZNE — on both the random, and compiled circuit — to obtain mitigated expectation values.

from mitiq import zne

uncompiled_mitigated_result = zne.execute_with_zne(random_circuit, execute)
compiled_mitigated_result = zne.execute_with_zne(compiled_circuit, execute)

Thus we have four variables which we can compare against ideal values to see how performance varies for this circuit across compilation and mitigation.








These data are then summarized in the following table printed below.

header = "{:<11} {:<15} {:<10}"
entry = "{:<11}  {:<15.2f} {:<10.2f}"
int_entry = "{:<11}  {:<15} {:<10}"
print(header.format("", "uncompiled", "compiled"))
print(entry.format("ideal", uncompiled_ideal_value, compiled_ideal_value))
print(entry.format("noisy", uncompiled_noisy_value, compiled_noisy_value))
        "mitigated", uncompiled_mitigated_result, compiled_mitigated_result
        abs(uncompiled_ideal_value - uncompiled_mitigated_result),
        abs(compiled_ideal_value - compiled_mitigated_result),
            uncompiled      compiled  
ideal        0.50            0.50      
noisy        0.43            0.36      
mitigated    0.54            0.49      
error        0.04            0.01      
depth        10              21        

Hence for this particular random circuit we see that using both compilation and error mitigation combine for the most accurate result. Note that despite using BQSKit to compile the circuit, the depth has actually increased. This can occasionally happen when the random circuit contains gates that are harder to compile into BQSKit’s default gateset.

More random circuits#

We can now repeat the above procedure with many random circuits to get a better understanding of how these two technologies interact in a more general setting. To do this we execute the above code many times, each iteration using a new random circuit on 4 qubits with depth 40. Because compiling many large circuits is computationally expensive, we leave the code our from this notebook, but it can be accessed in our research repository.

Once the errors are computed for each circuit we can collect the results in a histogram to get an idea of how compilation and mitigation affects accuracy more generally.

Histograms of circuit accuracy with and without compilation, and error mitigation.

These results show that using error mitigation improves the accuracy of both uncompiled, and compiled circuits. The tutorial in the research repository shows further that error mitigation both reduces the mean, and standard deviation of these distributions.

In this tutorial we’ve seen how one can use error mitigation in conjunction with circuit compilation. For more information check out the bqskit and mitiq documentation.