Download

Download this file as Jupyter notebook: nox_example.ipynb.

Example: Running Noiseless Output Extrapolation

This example illustrates how to generate noiseless output extrapolation (NOX) circuits and use them to obtain error-mitigated outputs from noisy simulations.

NOX on Qubits

First, we initialize a noisy simulator with depolarizing noise on \(CX\) gates and with readout errors.

[2]:
import trueq as tq
import trueq.simulation as tqs

# make a noisy simulator
sim = tq.Simulator().add_depolarizing(0.03, match=tqs.GateMatch(tq.Gate.cx))
sim.add_readout_error(0.01)
[2]:
<trueq.simulation.simulator.Simulator at 0x7f9cb83554f0>

We are going to use NOX to mitigate these cycles’ errors in a circuit that generates a five-qubit Greenberger–Horne–Zeilinger (GHZ) state. We begin by creating our circuit.

[3]:
# create the circuit
circuit = tq.Circuit(
    [
        {0: tq.Gate.h},
        {(0, 1): tq.Gate.cx},
        {(0, 2): tq.Gate.cx, (1, 3): tq.Gate.cx},
        {(0, 4): tq.Gate.cx},
    ]
).measure_all()

# show the circuit
circuit.draw()
[3]:
0 1 2 3 4 Key: Labels: (0,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (0, 1) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0, 2) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (1, 3) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0, 4) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX 1 Labels: (0,) Name: Meas M Labels: (1,) Name: Meas M Labels: (2,) Name: Meas M Labels: (3,) Name: Meas M Labels: (4,) Name: Meas M

Using the make_nox() function, we generate circuits to amplify the noise in each hard cycle of the circuit.

Note

When it comes to the optional parameters for make_nox(), increasing n_identities can reduce the standard deviation of the estimates, provided that the error on the deeper circuit is not too large. Applying several random compilations to each circuit transforms the noise processes into Pauli channels which is recommended for NOX. We generally expect n_compilations between 15 and 30 to be sufficient.

The circuit shown below amplifies the noise on the first hard cycle, which contains a \(CX\) gate acting on qubits (0, 1).

[4]:
# generate the NOX circuits
nox_circuits = tq.make_nox(circuit, n_identities=2, n_compilations=30)

# show the circuit which amplifies noise on the first hard cycle
nox_circuits[1].draw()
[4]:
0 1 2 3 4 Key: twirl: Paulis on [0, 1, 2, 3, 4] n_marked: 3 protocol: RC seq_label: 721108101 mitigation: ('NOX',) amplification: ((1, 5),) measurement_basis: ZZZZZ Labels: (0,) Name: Gate.cliff13 Aliases: Gate.cliff13 Generators: Z: 127.28 X: -127.28 0.71 -0.71 -0.71 -0.71 13 Labels: (1,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (2,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (3,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (4,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z 1 Labels: (0, 1) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (1,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (2,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (3,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y Labels: (4,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y 2 Labels: (0, 1) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (1,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (2,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (3,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y Labels: (4,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID 3 Labels: (0, 1) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (1,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (2,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (3,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (4,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID 4 Labels: (0, 1) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (1,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (2,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y Labels: (3,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y Labels: (4,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 5 Labels: (0, 1) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (1,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (2,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (3,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (4,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y 6 Labels: (0, 2) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (1, 3) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (1,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (2,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (3,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (4,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y 7 Labels: (0, 4) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (1,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (2,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (3,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (4,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 8 Labels: (0,) Name: Meas M Labels: (1,) Name: Meas M Labels: (2,) Name: Meas M Labels: (3,) Name: Meas M Labels: (4,) Name: Meas M

We now run the circuits on our noisy simulator. We analyze and fit the results by calling the fit() method. This method automatically uses the results of the NOX circuits to improve the estimate of user-specified observables and outcomes. The table shows an estimate of the expected value of each observable and outcome, and its standard deviation.

[5]:
# run the simulation
sim.run(nox_circuits, n_shots=500)

# set which observables and outcomes to estimate
labels = ((0, 1, 2, 3),)
observables = ["0000", "1111", "ZIZI"]

# fit
fit = nox_circuits.fit(labels, observables=observables)
fit
[5]:
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
NOX
Noiseless Output Extrapolation
Circuit #0
Labels (0, 1, 2, 3)
Key:
  • labels: (0, 1, 2, 3)
  • measurement_basis: ZZZZ
  • mitigation: ('NOX',)
  • n_marked: 3
  • protocol: RC
  • seq_label: 721108101
  • twirl: Paulis on [0, 1, 2, 3, 4]
$ev_\texttt{ZIZI}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
9.5e-01 (6.4e-03)
0.9545999999999999, 0.006400359185323021
$ev_\texttt{1111}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
4.8e-01 (7.0e-03)
0.47526666666666656, 0.007048385049127864
$ev_\texttt{0000}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
4.7e-01 (5.9e-03)
0.4659333333333333, 0.005850912755855133

We can plot the estimates from each NOX circuit alongside the mitigated estimates. The point farthest to the left shows the unmitigated estimate. The subsequent points correspond to the estimates for each circuit with one cycle having amplified noise. Finally, the mitigated estimate is on the right.

[6]:
# plot the results for the ZZZZ observable
fit.plot.raw_nox(observables=["0000"])
../../_images/guides_error_suppression_nox_example_10_0.png

We can see the benefits of using NOX by comparing the NOX-mitigated results and the unmitigated results. To retrieve the results from the unmitigated circuits, we select the subset of NOX circuits with no noise amplification. As shown below, the estimated expectation values of the outputs 00000 and 11111 are closer to the correct values for the NOX circuits than for the unmitigated circuits.

[7]:
# unmitigated results
sim.run(circuit, n_shots=15000)
res_unmit = circuit.get_probability(["00000", "11111"])

# nox results
res_nox = nox_circuits.fit(observables=["00000", "11111"]).array("ev_\d*").vals

print("Prob. of output 00000")
print("Correct value: 0.50")
print(f"Unmitigated circuits: {res_unmit[0]:.2f}")
print(f"Mitigated circuits: {res_nox[0]:.2f}")

print("\nProb. of output 11111")
print("Correct value: 0.50")
print(f"Unmitigated circuits: {res_unmit[1]:.2f}")
print(f"Mitigated circuits: {res_nox[1]:.2f}")
Prob. of output 00000
Correct value: 0.50
Unmitigated circuits: 0.43
Mitigated circuits: 0.46

Prob. of output 11111
Correct value: 0.50
Unmitigated circuits: 0.43
Mitigated circuits: 0.47

NOX with RCAL

NOX can be used in tandem with readout calibration (RCAL) to achieve even better results. The noisy simulator we created above suffers from readout errors. To correct these errors, we run RCAL circuits. As we see below, if the NOX circuit collection also contains RCAL circuits, readout errors will automatically be mitigated when we call the fit() method.

[8]:
# create and run RCAL circuits
rcal_circuits = tq.make_rcal([0, 1, 2, 3, 4])
sim.run(rcal_circuits, n_shots=5000)

# add RCAL circuits to circuit collection
nox_circuits.append(rcal_circuits)

# get the results
# the readout calibration correction is automatically applied when we fit
fit = nox_circuits.fit(observables=["00000", "11111", "01100", "ZZIIZ"])
fit
[8]:
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
NOX
Noiseless Output Extrapolation
Circuit #0
Labels (0, 1, 2, 3, 4)
Key:
  • labels: (0, 1, 2, 3, 4)
  • measurement_basis: ZZZZZ
  • mitigation: ('NOX',)
  • n_marked: 3
  • protocol: RC
  • seq_label: 721108101
  • twirl: Paulis on [0, 1, 2, 3, 4]
$ev_\texttt{ZZIIZ}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
1.0e-02 (1.4e-02)
0.010037590707550986, 0.014138747350298015
$ev_\texttt{11111}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
4.9e-01 (7.6e-03)
0.49081835263320966, 0.00760050122892642
$ev_\texttt{01100}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
-2.8e-04 (2.3e-04)
-0.0002829974938339037, 0.00023445690730137557
$ev_\texttt{00000}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
4.9e-01 (6.5e-03)
0.4872980498444942, 0.006524876485652655
RCAL
Readout Calibration
Confusion
(0,) P(0 | 0) = 0.987
P(1 | 1) = 0.988
Confusion Matrix:
0.987 0.012 0.013 0.988
Key:
  • labels: (0, 1, 2, 3, 4)
  • protocol: RCAL
(1,) P(0 | 0) = 0.992
P(1 | 1) = 0.992
Confusion Matrix:
0.992 0.008 0.008 0.992
Key:
  • labels: (0, 1, 2, 3, 4)
  • protocol: RCAL
(2,) P(0 | 0) = 0.986
P(1 | 1) = 0.992
Confusion Matrix:
0.986 0.008 0.014 0.992
Key:
  • labels: (0, 1, 2, 3, 4)
  • protocol: RCAL
(3,) P(0 | 0) = 0.991
P(1 | 1) = 0.990
Confusion Matrix:
0.991 0.010 0.009 0.990
Key:
  • labels: (0, 1, 2, 3, 4)
  • protocol: RCAL
(4,) P(0 | 0) = 0.991
P(1 | 1) = 0.988
Confusion Matrix:
0.991 0.012 0.009 0.988
Key:
  • labels: (0, 1, 2, 3, 4)
  • protocol: RCAL

NOX on Qudits

The make_nox() function extends to higher-dimensional qudits without any modifications. To see this, we consider the analogy of the above GHZ state preparation circuit for qutrits.

[9]:
# set the register dimension to 3 (i.e. qutrits)
tq.settings.set_dim(3)

# make a noisy simulator
sim = tq.Simulator().add_depolarizing(0.01, match=tqs.GateMatch(tq.Gate.cx3))

# create the circuit
circuit = tq.Circuit(
    [
        {(0): tq.Gate.f3},
        {(0, 1): tq.Gate.cx3},
        {(0, 2): tq.Gate.cx3},
        {(0, 3): tq.Gate.cx3},
    ]
)
circuit.measure_all()

# generate the NOX circuits
circuits = tq.make_nox(circuit)

# run the simulation
sim.run(circuits, n_shots=500)

# set which observables and outcomes to estimate
labels = ((0, 1, 2, 3),)
observables = ["W01W01W01W01", "1111", "0000", "2222", "0120"]

# fit
estimate_collection = circuits.fit(labels, observables=observables)
estimate_collection
[9]:
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
NOX
Noiseless Output Extrapolation
Circuit #0
Labels (0, 1, 2, 3)
Key:
  • labels: (0, 1, 2, 3)
  • measurement_basis: W01W01W01W01
  • mitigation: ('NOX',)
  • n_marked: 3
  • protocol: RC
  • seq_label: 431193961
  • twirl: Paulis on [0, 1, 2, 3]
$ev_\texttt{2222}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
3.4e-01+0.0e+00j (9.3e-03)
(0.3365333333333333+0j), 0.009310448133384587
$ev_\texttt{1111}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
3.1e-01+0.0e+00j (1.3e-02)
(0.30696666666666667+0j), 0.012548616568282588
$ev_\texttt{0120}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
1.3e-04+0.0e+00j (1.7e-04)
(0.00013333333333333334+0j), 0.00017109070883990457
$ev_\texttt{0000}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
3.4e-01+0.0e+00j (1.2e-02)
(0.33699999999999986+0j), 0.011858941047101192
$ev_\texttt{W01W01W01W01}$
The expectation value of an output string, of a Pauli/Weyl observable, or of a mixed string.
1.5e-02+2.1e-02j (2.4e-02)
(0.015049999999999791+0.020986682285043294j), 0.024106843116788877

Download

Download this file as Jupyter notebook: nox_example.ipynb.