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.6328411595800771, phi=1.1320505038329223, lmda=0.852361769284454)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X───cirq.circuits.qasm_output.QasmUGate(theta=0.5237913284736395, phi=0.3495246410706257, lmda=1.6552535663563226)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X───cirq.circuits.qasm_output.QasmUGate(theta=1.4430157024556602, phi=1.290412980740981, lmda=1.1831404331076145)────@───cirq.circuits.qasm_output.QasmUGate(theta=1.4790636702289925, phi=0.7516922435959342, lmda=1.9214923738870249)───@───cirq.circuits.qasm_output.QasmUGate(theta=0.42386454981791466, phi=1.4027849924587608, lmda=0.8377005432027568)───X───cirq.circuits.qasm_output.QasmUGate(theta=0.4571764694723979, phi=1.3734510194446103, lmda=1.5258083769105348)────@───cirq.circuits.qasm_output.QasmUGate(theta=1.8826169725468849, phi=0.38948806019286963, lmda=1.2639113257262065)───
                                                                                                                                                                                                                                              │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                │                                                                                                                    │                                                                                                                    │                                                                                                                     │                                                                                                                     │
q_1: ───cirq.circuits.qasm_output.QasmUGate(theta=0.9999999999422161, phi=1.8151457919764802, lmda=0.815145791999278)────X───cirq.circuits.qasm_output.QasmUGate(theta=1.588325239403021, phi=1.627327960519797, lmda=1.9807817809218806)─────@───cirq.circuits.qasm_output.QasmUGate(theta=0.30974412948980906, phi=0.11255225231668624, lmda=1.4038807645162785)───X───cirq.circuits.qasm_output.QasmUGate(theta=1.1617990869331638, phi=0.12701688945142126, lmda=1.4999999990906163)───@───cirq.circuits.qasm_output.QasmUGate(theta=1.5000000004915859, phi=0.8117799041399515, lmda=0.3729831098697982)───────@───cirq.circuits.qasm_output.QasmUGate(theta=1.746357057158145, phi=1.1587889451622326, lmda=1.4774721925259098)────X───cirq.circuits.qasm_output.QasmUGate(theta=0.6260235441531786, phi=1.379807398724606, lmda=1.3850061693506033)─────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                         │                                                                                                                                                                                                                                           │                                                                                                                     │                                                                                                                        │                                                                                                                    │                                                                                                                     │                                                                                                                    │                                                                                                                    │                                                                                                                     │                                                                                                                     │
q_2: ───cirq.circuits.qasm_output.QasmUGate(theta=0.9999999987218069, phi=0.6198434595083692, lmda=0.7930893681745429)───@───cirq.circuits.qasm_output.QasmUGate(theta=1.2414527048020734, phi=1.0275638230980575, lmda=0.4805173944561489)──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────@───cirq.circuits.qasm_output.QasmUGate(theta=1.301306788707919, phi=1.9086170449710764, lmda=0.16631677374754064)────X───cirq.circuits.qasm_output.QasmUGate(theta=0.5000000002241061, phi=1.6192268548436557, lmda=1.2467120690277003e-09)───X───cirq.circuits.qasm_output.QasmUGate(theta=0.9999999987048175, phi=1.2802716901414997, lmda=0.2684929045779192)───@───cirq.circuits.qasm_output.QasmUGate(theta=1.1119566552418418, phi=0.5799422197185474, lmda=0.21964485878922918)───@───cirq.circuits.qasm_output.QasmUGate(theta=1.9707840366013234, phi=1.1114055824488749, lmda=1.9724663628928607)───X───cirq.circuits.qasm_output.QasmUGate(theta=0.4786732871639294, phi=0.9028941364805299, lmda=0.0915094513933935)───X───cirq.circuits.qasm_output.QasmUGate(theta=1.519938926768622, phi=0.9241066726025559, lmda=0.08249202337835415)────@───cirq.circuits.qasm_output.QasmUGate(theta=0.32816756133318176, phi=0.6611136825441359, lmda=1.2496051671218567)───X───cirq.circuits.qasm_output.QasmUGate(theta=0.8504585591925432, phi=1.9730740696720463, lmda=0.6710446778148922)────

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.36      
mitigated    0.51            0.45      
error        0.01            0.05      
depth        10              23        

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#