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 [64], 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_SEED = 479

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

bqskit_circuit = cirq_to_bqskit(random_circuit)

print(random_circuit)
                                          ┌──┐
0: ───X───Z───Y───X───────────────iSwap─────@────H───Y───
                  │               │         │
1: ───S───X───────@───Y───iSwap───┼────────S┼────X───X───
          │               │       │         │
2: ───────@───S───Y───Y───iSwap───iSwap─────@────────────
                                          └──┘

Compilation#

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.

Note

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)

print(compiled_circuit)
q_0: ───cirq.circuits.qasm_output.QasmUGate(theta=0.5768269349703968, phi=1.9041575744301413, lmda=1.8085967778965057)───cirq.circuits.qasm_output.QasmUGate(theta=1.9709868212505792, phi=1.4194527309623635, lmda=1.3965547220964476)───cirq.circuits.qasm_output.QasmUGate(theta=1.8164665032090985, phi=0.09519992736885927, lmda=1.3158230574229663)───cirq.circuits.qasm_output.QasmUGate(theta=0.8573240396377699, phi=0.5812182855674989, lmda=1.0325208591558237)───cirq.circuits.qasm_output.QasmUGate(theta=1.568229876911227, phi=0.5260217824533509, lmda=0.7873126816515086)────cirq.circuits.qasm_output.QasmUGate(theta=1.8223437164639278, phi=1.1140619148017235, lmda=1.2499582342371558)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────@───cirq.circuits.qasm_output.QasmUGate(theta=0.49999999999445116, phi=1.7135926842334501, lmda=0.005284008357944393)───@───cirq.circuits.qasm_output.QasmUGate(theta=1.5780756697368934, phi=0.4999999999999798, lmda=1.7864073157685731)────
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                │                                                                                                                       │
q_1: ───cirq.circuits.qasm_output.QasmUGate(theta=1.49540315762184, phi=0.4351074007376275, lmda=0.9990496930013596)─────X────────────────────────────────────────────────────────────────────────────────────────────────────────────────cirq.circuits.qasm_output.QasmUGate(theta=1.6908141451988563, phi=1.4652225173378062, lmda=1.704836367610546)─────cirq.circuits.qasm_output.QasmUGate(theta=1.4107800312652476, phi=1.7314675527477639, lmda=1.7457901668065094)───cirq.circuits.qasm_output.QasmUGate(theta=0.6997645675782334, phi=0.0719442095002357, lmda=0.8467096472307089)───cirq.circuits.qasm_output.QasmUGate(theta=1.6230331053422493, phi=0.03201104649033817, lmda=1.209494882522869)───cirq.circuits.qasm_output.QasmUGate(theta=0.6075933134060512, phi=1.058806759704401, lmda=1.1938131863291892)───X───cirq.circuits.qasm_output.QasmUGate(theta=9.958603486488378e-12, phi=0.49965171442082484, lmda=1.8037498167785915)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                         │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                │                                                                                                                       │
q_2: ───cirq.circuits.qasm_output.QasmUGate(theta=0.5000000000008377, phi=1.9055694648749126, lmda=0.6965984688002106)───@────────────────────────────────────────────────────────────────────────────────────────────────────────────────cirq.circuits.qasm_output.QasmUGate(theta=1.3960243292902283, phi=0.28068194014249637, lmda=1.351944039888782)────cirq.circuits.qasm_output.QasmUGate(theta=0.4826863777111081, phi=1.2492746555974517, lmda=0.5785593279707074)───cirq.circuits.qasm_output.QasmUGate(theta=1.281676135318008, phi=0.3434294974668031, lmda=1.2578783347826896)────cirq.circuits.qasm_output.QasmUGate(theta=1.6580224065238272, phi=0.870791867683944, lmda=0.9287171419733914)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────@───cirq.circuits.qasm_output.QasmUGate(theta=1.521775036743729, phi=1.5611247860876114, lmda=0.5712059188990383)────────cirq.circuits.qasm_output.QasmUGate(theta=0.6281517492765443, phi=0.49979951885570895, lmda=0.7141892569309364)───cirq.circuits.qasm_output.QasmUGate(theta=1.2493175391577545, phi=1.1280154765210526, lmda=0.4328533677980122)───cirq.circuits.qasm_output.QasmUGate(theta=0.6694935156449086, phi=0.24636058243096803, lmda=1.3875001119742931)───cirq.circuits.qasm_output.QasmUGate(theta=0.09257225004734888, phi=0.01425934224068115, lmda=1.7407618762154904)───X───cirq.circuits.qasm_output.QasmUGate(theta=0.7648053074451213, phi=0.21855818994775483, lmda=0.7664128435008941)─────X───cirq.circuits.qasm_output.QasmUGate(theta=1.4179340436996932, phi=0.17303201551253783, lmda=1.0486644562802419)───

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.

Note

There are multiple other techniques described in our Users 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.

Warning

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():
        noisy_circuit.append(op)
        if len(op.qubits) == 2:
            noisy_circuit.append(
                cirq.depolarize(p=noise_level, n_qubits=2)(*op.qubits)
            )

    rho = (
        cirq.DensityMatrixSimulator()
        .simulate(noisy_circuit)
        .final_density_matrix
    )
    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.

compiled

mitigated

uncompiled_noisy_value

uncompiled_mitigated_result

compiled_noisy_value

compiled_mitigated_result

Comparison#

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))
print(
    entry.format(
        "mitigated", uncompiled_mitigated_result, compiled_mitigated_result
    )
)
print(
    entry.format(
        "error",
        abs(uncompiled_ideal_value - uncompiled_mitigated_result),
        abs(compiled_ideal_value - compiled_mitigated_result),
    )
)
print(
    int_entry.format(
        "depth",
        len(random_circuit),
        len(compiled_circuit),
    )
)
            uncompiled      compiled  
ideal        0.50            0.50      
noisy        0.43            0.44      
mitigated    0.54            0.44      
error        0.04            0.06      
depth        10              17        

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.

References#