Tags on this page: ucc zne beginner

Using the UCC Compiler with Mitiq’s Error Mitigation#

In this tutorial, we combine circuit compilation from the Unitary Compiler Collection (UCC) with Mitiq’s Zero Noise Extrapolation (ZNE) technique for error mitigation. We’ll explore how compilation affects noise sensitivity and how mitigation can restore accuracy.

🧰 Setup#

Before starting, install the required packages:

pip install mitiq
pip install ucc
pip install cirq

🔁 Workflow#

We’ll be completing the following steps:

  1. Define a quantum circuit.

  2. Compile it with UCC.

  3. Simulate it under a simple noise model.

  4. Apply error mitigation using ZNE.

  5. Compare ideal, noisy, and mitigated expectation values.


Step 1: Imports#

import cirq

import mitiq
import ucc

Step 2: Create a Testing Circuit#

For testing purposes we use a circuit that simulates the dynamics of the Heisenberg model on a square lattice. This circuit is one of the circuits UCC is benchmarked on in the ucc-bench repository.

from cirq.contrib.qasm_import import circuit_from_qasm

with open("resources/square_heisenberge.qasm") as f:
    qasm = f.read()

circuit = circuit_from_qasm(qasm)

print(circuit)
print("circuit depth:", len(circuit))
print(
    "two qubit gate count: ",
    len([op for op in circuit.all_operations() if len(op.qubits) == 2]),
)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ┌──────────────────────┐                ┌──┐              ┌──┐                ┌──┐                                  ┌──┐                ┌──┐                      ┌──┐                ┌──┐              ┌──┐                ┌──┐                                  ┌──┐                ┌──┐                                                                                                                                                                                                                                                                                        ┌────────────┐
q_0: ───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───Rz(-0.154π)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
        │                │              │                │                                  │                │                      │                │              │                │                                  │                │                      │                │              │                │                                  │                │                      │                │              │                │                                  │                │
q_1: ───┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───@──────────────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)────Rz(-0.417π)──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
        │                │              │                │                                  │                │                      │                │              │                │                                  │                │                                                                                                                                                                                                                                                                              │                          │              │                │                                  │                │                      │                │              │                │                                  │                │                      │                │              │                │                                  │                │                      │                │              │                │                                  │                │
q_2: ───┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)────@───────────────────────────────────────@─────Rx(0.5π)────@───────────────────@─────Rx(-0.5π)───Ry(0.5π)───Rx(π)────@───────────────────@─────Ry(0.5π)───Rx(π)────@───────────────────@─────Rx(0.5π)────@───────────────────@─────Rx(-0.5π)───Ry(0.5π)───Rx(π)────@───────────────────@─────Ry(0.5π)───Rx(π)───Rz(-0.451π)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
        │                │              │                │                                  │                │                      │                │              │                │                                  │                │                                                                                                                                                                                                                                                                              │                          │              │                │                                  │                │                      │                │              │                │                                  │                │                                                                                                                                                                                                                                                                               │                                       │                 │                   │                                     │                   │                         │                   │                 │                   │                                     │                   │
q_3: ───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───┼──────────────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)────┼──────────Rz(-0.563π)──────────────────┼─────────────────┼───────────────────┼─────────────────────────────────────┼───────────────────┼─────────────────────────┼───────────────────┼─────────────────┼───────────────────┼─────────────────────────────────────┼───────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                                                                                                                                                │                │              │                │                                  │                │                      │                │              │                │                                  │                │                      │                          │              │                │                                  │                │                      │                │              │                │                                  │                │                      │                │              │                │                                  │                │                      │                │              │                │                                  │                │                       │                                       │                 │                   │                                     │                   │                         │                   │                 │                   │                                     │                   │
q_4: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────X─────────────Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)────┼──────────@────────────────────────────┼@────Rx(0.5π)────┼@──────────────────┼@────Rx(-0.5π)───Ry(0.5π)───Rx(π)────┼@──────────────────┼@────Ry(0.5π)───Rx(π)────┼@──────────────────┼@────Rx(0.5π)────┼@──────────────────┼@────Rx(-0.5π)───Ry(0.5π)───Rx(π)────┼@──────────────────┼@────Ry(0.5π)───Rx(π)───@──────────────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)────Rz(-0.497π)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                                                                                                                                                │                │              │                │                                  │                │                      │                │              │                │                                  │                │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 │          │                            ││                ││                  ││                                    ││                  ││                        ││                  ││                ││                  ││                                    ││                  ││                       │                          │              │                │                                  │                │                      │                │              │                │                                  │                │
q_5: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X──────────┼──────────────Rz(0.318π)────X┼────Rx(0.5π)────X┼────Rz(0.318π)────X┼────Rx(-0.5π)───Ry(0.5π)───Rx(π)────X┼────Rz(0.318π)────X┼────Ry(0.5π)───Rx(π)────X┼────Rz(0.318π)────X┼────Rx(0.5π)────X┼────Rz(0.318π)────X┼────Rx(-0.5π)───Ry(0.5π)───Rx(π)────X┼────Rz(0.318π)────X┼────Ry(0.5π)───Rx(π)───X─────────────Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───────────────@─────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───Rz(-0.068π)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                                                                                                                                                │                │              │                │                                  │                │                      │                │              │                │                                  │                │                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            │                             │                 │                   │                                     │                   │                         │                   │                 │                   │                                     │                   │                                                                                                                                                                                                                                                                                                     │                 │              │                │                                  │                │                      │                │              │                │                                  │                │
q_6: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────┼─────────────────┼───────────────────┼─────────────────────────────────────┼───────────────────┼─────────────────────────┼───────────────────┼─────────────────┼───────────────────┼─────────────────────────────────────┼───────────────────┼───────────────────────@──────────────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)────Rz(-0.116π)┼─────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              │                             │                 │                   │                                     │                   │                         │                   │                 │                   │                                     │                   │                       │                          │              │                │                                  │                │                      │                │              │                │                                  │                │                                  │                 │              │                │                                  │                │                      │                │              │                │                                  │                │
q_7: ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X──────────────Rz(0.318π)─────X────Rx(0.5π)─────X────Rz(0.318π)─────X────Rx(-0.5π)───Ry(0.5π)───Rx(π)─────X────Rz(0.318π)─────X────Ry(0.5π)───Rx(π)─────X────Rz(0.318π)─────X────Rx(0.5π)─────X────Rz(0.318π)─────X────Rx(-0.5π)───Ry(0.5π)───Rx(π)─────X────Rz(0.318π)─────X────Ry(0.5π)───Rx(π)───X─────────────Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───────────────┼─────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────┼────────────────┼──────────────┼────────────────┼──────────────────────────────────┼────────────────┼──────────────────────@──────────────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───@────────────────@───Rx(0.5π)───@────────────────@───Rx(-0.5π)───Ry(0.5π)───Rx(π)───@────────────────@───Ry(0.5π)───Rx(π)───Rz(0.266π)───
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                │                 │              │                │                                  │                │                      │                │              │                │                                  │                │                      │                          │              │                │                                  │                │                      │                │              │                │                                  │                │
q_8: ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X────Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X─────────────Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Rx(0.5π)───X───Rz(0.318π)───X───Rx(-0.5π)───Ry(0.5π)───Rx(π)───X───Rz(0.318π)───X───Ry(0.5π)───Rx(π)───Rz(0.105π)───
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  └──────────────────────┘                └──┘              └──┘                └──┘                                  └──┘                └──┘                      └──┘                └──┘              └──┘                └──┘                                  └──┘                └──┘                                                                                                                                                                                                                                                                                        └────────────┘
circuit depth: 241
two qubit gate count:  144

Step 3: Compile with UCC#

UCC will optimize the circuit to reduce the total number of gates. Since noise accrues with circuit depth, shorter circuits are inherently more robust to noise. This example shows a dramatic reduction in the number of gates, but to see the impact of UCC on a variety of circuits see the UCC benchmark repository.

compiled = ucc.compile(circuit)

print(compiled)
print("compiled depth:", len(compiled))
print(
    "two qubit gate count: ",
    len([op for op in compiled.all_operations() if len(op.qubits) == 2]),
)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ┌──┐                                         ┌──┐                                            ┌──┐
q_0: ───Ry(-0.5π)─────Rz(0.5π)───@───Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)────@───Rz(-0.5π)─────Ry(-0.363π)────────────────@───Rz(-0.5π)───Ry(0.5π)──────Ry(-0.5π)───Rz(0.5π)───@───Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)────@───Rz(-0.5π)─────Ry(-0.363π)────────────────@───Rz(-0.5π)───Ry(0.5π)──────Rz(-0.154π)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                 │                                         │                                            │                                                    │                                         │                                            │
q_1: ───Ry(-0.321π)───Rz(0.5π)───┼─────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────X───Rz(0.29π)───Ry(0.647π)───Rz(0.894π)───X───Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)───X───Ry(0.5π)────Rz(-0.679π)───Ry(-0.5π)─────Rz(0.5π)───@───Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)────@───Rz(-0.5π)─────Ry(-0.363π)────────────────@───Rz(-0.5π)───Ry(0.5π)──────Ry(-0.5π)─────Rz(0.5π)───@───Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)────@───Rz(-0.5π)─────Ry(-0.363π)────────────────@───Rz(-0.5π)───Ry(0.5π)──────Rz(-0.417π)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                 │                                         │                                            │                                                                                                                                                                                                  │                                         │                                            │                                                      │                                         │                                            │
q_2: ───Ry(-0.321π)───Rz(0.5π)───┼─────────────────────────────────────────┼────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────┼────────────────────────────────────────────┼──────────────────────────────────────────────────────X───Rz(0.29π)───Ry(0.647π)───Rz(0.894π)───X───Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)───X───Ry(0.5π)────Rz(-0.679π)───Ry(-0.5π)─────Rz(0.5π)────@─────Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)─────@─────Rz(-0.5π)─────Ry(-0.363π)─────────────────@─────Rz(-0.5π)───Ry(0.5π)──────Rz(-0.451π)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                 │                                         │                                            │                                                                                                                                                                                                  │                                         │                                            │                                                                                                                                                                                                     │                                            │                                               │
q_3: ───Ry(-0.321π)───Rz(0.5π)───X───Rz(0.29π)───Ry(0.647π)───Rz(0.894π)───X───Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)───X───Ry(0.5π)────Rz(-0.679π)───Ry(-0.5π)───Rz(0.5π)───@───Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)────@───Rz(-0.5π)─────Ry(-0.363π)────────────────@───Rz(-0.5π)───Ry(0.5π)──────Ry(-0.5π)─────Rz(0.5π)───┼─────────────────────────────────────────┼────────────────────────────────────────────┼──────────────────────────────────────────────────────@───Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)────@───Rz(-0.5π)─────Ry(-0.363π)────────────────@───Rz(-0.5π)───Ry(0.5π)──────Rz(-0.563π)───────────────┼────────────────────────────────────────────┼───────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                                                             │                                         │                                            │                                                      │                                         │                                            │                                                      │                                         │                                            │                                                       │                                            │                                               │
q_4: ───Ry(-0.321π)───Rz(0.5π)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────┼────────────────────────────────────────────┼──────────────────────────────────────────────────────X───Rz(0.29π)───Ry(0.647π)───Rz(0.894π)───X───Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)───X───Ry(0.5π)────Rz(-0.679π)───Ry(-0.321π)───Rz(0.5π)───X───Rz(0.29π)───Ry(0.647π)───Rz(0.894π)───X───Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)───X───Ry(0.5π)────Rz(-0.679π)───Ry(-0.5π)─────Rz(0.5π)────┼@────Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)─────┼@────Rz(-0.5π)─────Ry(-0.363π)─────────────────┼@────Rz(-0.5π)───Ry(0.5π)──────Ry(-0.5π)─────Rz(0.5π)───@───Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)────@───Rz(-0.5π)─────Ry(-0.363π)────────────────@───Rz(-0.5π)───Ry(0.5π)──────Rz(-0.497π)──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                                                             │                                         │                                            │                                                                                                                                                                                                                                                                                                                                                   ││                                           ││                                              ││                                                       │                                         │                                            │
q_5: ───Ry(-0.321π)───Rz(0.5π)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────────────────┼────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X┼────Rz(0.29π)───Ry(0.647π)───Rz(0.894π)────X┼────Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)────X┼────Ry(0.5π)────Rz(-0.679π)───Ry(-0.321π)───Rz(0.5π)───X───Rz(0.29π)───Ry(0.647π)───Rz(0.894π)───X───Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)───X───Ry(0.5π)────Rz(-0.679π)───Ry(-0.5π)─────Rz(0.5π)───@───Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)────@───Rz(-0.5π)─────Ry(-0.363π)────────────────@───Rz(-0.5π)───Ry(0.5π)──────Rz(-0.068π)────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                                                             │                                         │                                            │                                                                                                                                                                                                                                                                                                                                                    │                                            │                                               │                                                                                                                                                                                                     │                                         │                                            │
q_6: ───Ry(-0.321π)───Rz(0.5π)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X───Rz(0.29π)───Ry(0.647π)───Rz(0.894π)───X───Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)───X───Ry(0.5π)────Rz(-0.679π)───Ry(-0.5π)─────Rz(0.5π)─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────────────────────────────────────┼───────────────────────────────────────────────┼───────────────────────────────────────────────────────@───Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)────@───Rz(-0.5π)─────Ry(-0.363π)────────────────@───Rz(-0.5π)───Ry(0.5π)──────Rz(-0.116π)──────────────┼─────────────────────────────────────────┼────────────────────────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         │                                            │                                               │                                                       │                                         │                                            │                                                      │                                         │                                            │
q_7: ───Ry(-0.321π)───Rz(0.5π)───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X────Rz(0.29π)───Ry(0.647π)───Rz(0.894π)─────X────Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)─────X────Ry(0.5π)────Rz(-0.679π)───Ry(-0.321π)───Rz(0.5π)───X───Rz(0.29π)───Ry(0.647π)───Rz(0.894π)───X───Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)───X───Ry(0.5π)────Rz(-0.679π)───Ry(-0.5π)─────Rz(0.5π)───┼─────────────────────────────────────────┼────────────────────────────────────────────┼──────────────────────────────────────────────────────@───Rz(-0.5π)───Ry(0.363π)───Rz(-0.5π)────@───Rz(-0.5π)─────Ry(-0.363π)────────────────@───Rz(-0.5π)───Ry(0.5π)──────Rz(0.266π)───
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            │                                         │                                            │                                                      │                                         │                                            │
q_8: ───Ry(-0.321π)───Rz(0.5π)──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────X───Rz(0.29π)───Ry(0.647π)───Rz(0.894π)───X───Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)───X───Ry(0.5π)────Rz(-0.679π)───Ry(-0.321π)───Rz(0.5π)───X───Rz(0.29π)───Ry(0.647π)───Rz(0.894π)───X───Rz(-0.703π)───Ry(0.351π)────Rz(0.103π)───X───Ry(0.5π)────Rz(-0.573π)────────────────
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       └──┘                                         └──┘                                            └──┘
compiled depth: 105
two qubit gate count:  36

Step 4: Define a Noisy Simulator#

The dominant source of noise on modern devices is associated with two-qubit gates. For simplicity, we define a simulator that adds depolarizing noise after each two-qubit gate, but no noise after single-qubit gates.

def execute(circuit, noise_level=0.01):
    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)
            )
    result = cirq.DensityMatrixSimulator(seed=42).simulate(noisy_circuit)
    return result.final_density_matrix[0, 0].real  # Probability of |00⟩

Baseline: Ideal vs Noisy#

ideal_uncompiled = execute(circuit, noise_level=0.0)
noisy_uncompiled = execute(circuit)

ideal_compiled = execute(compiled, noise_level=0.0)
noisy_compiled = execute(compiled)

Step 5: Apply ZNE#

Here ZNE is applied using the default parameters (Richardson extrapolation with scale factors 1, 3, and 5, and random unitary folding for the noise scaling method).

mitigated_uncompiled = mitiq.zne.execute_with_zne(circuit, execute)
mitigated_compiled = mitiq.zne.execute_with_zne(compiled, execute)

Step 6: Compare the Results#

We can now visualize the 4 methods of execution:

compiled\mitigated

noisy_uncompiled

mitigated_uncompiled

noisy_compiled

mitigated_compiled

Hide code cell source

import matplotlib.pyplot as plt

categories = ["Uncompiled", "Compiled"]
noisy = [noisy_uncompiled, noisy_compiled]
mitigated = [
    mitigated_uncompiled - noisy_uncompiled,
    mitigated_compiled - noisy_compiled,
]  # Delta from noisy
ideal = [ideal_uncompiled, ideal_compiled]

fig, ax = plt.subplots(figsize=(6, 3))

y = range(len(categories))
bar_width = 0.55

ax.barh(y, noisy, height=bar_width, color="lightcoral", label="Noisy")

ax.barh(
    y,
    mitigated,
    left=noisy,
    height=bar_width,
    hatch="//",
    edgecolor="firebrick",
    facecolor="none",
    label="Mitigated",
)

ax.axvline(x=ideal_uncompiled, color="maroon", linestyle="--", linewidth=1)
ax.text(
    ideal_uncompiled,
    0.4,
    "Ideal",
    color="maroon",
    ha="right",
)

ax.set_yticks(y)
ax.set_yticklabels(categories)
ax.set_xlabel("Expectation Value")
ax.set_title("Impact of Mitigation on pre- and post-compiled circuit")

ax.legend()
plt.show()
../_images/a75bb09e383a55bc754dbbb48a86a98f1b81c75ffec6ccf35b9882c2162cf99e.png

Warning

The above plot is a single instance of a given circuit. The behavior of compilation and mitigation acting beneficially is a common phenomenon, but not always guaranteed.

Conclusions#

  • Compilation minimizes the potential for errors to accrue.

  • Error mitigation remains effective on compiled circuits.

  • Combined use of UCC and ZNE gives significantly better results under noise.

Try running this pipeline over many random circuits to get a better statistical picture.

References#