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:
Define a quantum circuit.
Compile it with UCC.
Simulate it under a simple noise model.
Apply error mitigation using ZNE.
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 |
❌ |
✅ |
|---|---|---|
❌ |
|
|
✅ |
|
|
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.