# What additional options are available when using REM?#

## Overview#

The main options the user has when using REM concern how to specify the inverse confusion matrix that is used to mitigate errors in the raw measurement (or “readout”) results. Currently Mitiq does not implement methods for estimating readout-error confusion matrices (which is a form of measurement noise calibration and therefore a device specific task), so the user must provide enough information to allow Mitiq to construct one. As described below, Mitiq’s options support the differing levels of information a user may have about the readout-error characteristics of their device. After the confusion matrix has been constructed, the remaining steps of standard REM are straightforward (compute the pseudoinverse of the confusion matrix and then apply this to the raw measurement results). For more information on what a confusion matrix is, see What is the theory behind REM?.

## Options for specifying the inverse confusion matrix#

Mitiq provides two utility functions for constructing an inverse confusion matrix from user-provided information about a device’s confusion matrix. We describe these functions below and illustrate with toy examples. (Note that everything that follows is circuit-agnostic; it concerns how to represent a device’s noise model in the form required by REM).

### Inverse confusion matrix from single-qubit noise model#

The function generate_inverse_confusion_matrix(num_qubits, p0, p1) embodies the simplest noise model possible, where one assumes that noise affects the measurement of each qubit independently and with the same confusion probabilities, specified by $$p_0 = Pr(1|0)$$, the probability $$|0\rangle$$ gets flipped to $$|1\rangle$$ when measured, and $$p_1 = Pr(0|1)$$, the probability $$|1\rangle$$ gets flipped to $$|0\rangle$$. The $$2 \times 2$$ confusion matrix $$A_1$$ for the $$1$$st qubit (and every other qubit) is then

$\begin{split} \begin{bmatrix} 1-p_0 & p_1 \\ p_0 & 1-p_1 \end{bmatrix} \end{split}$

and the joint $$2^n \times 2^n$$ confusion matrix $$A$$ for all $$n$$ qubits is just $$n$$ copies of $$A_1$$ tensored together: $$A = A_1 \otimes \dots \otimes A_1 = A_1^{\otimes n}$$.

To construct an inverse confusion matrix with generate_inverse_confusion_matrix() the user supplies the number of qubits and the single-qubit confusion matrix parameters $$p_0$$ and $$p_1$$. Here is an example with two qubits.

from functools import reduce

import numpy as np
from mitiq.rem import generate_inverse_confusion_matrix

# Confusion matrix for qubit 1
A1_entries = [
0.9, 0.2,
0.1, 0.8
]
A1 = np.array(A1_entries).reshape(2,2)

# Overall 2-qubit confusion matrix (tensor two copies of A1)
A = np.kron(A1, A1)

# Generate inverse confusion matrix.
# For this simple error model the user only has to
# specify the single qubit flip probabilities p0 and p1
A_pinv = generate_inverse_confusion_matrix(2, p0=0.1, p1=0.2)

print(f"Confusion matrix:\n{A}\n")
print(f"Column-wise sums of confusion matrix:\n{A.sum(axis=0)}\n")
print(f"Inverse confusion matrix:\n{A_pinv}")

Confusion matrix:
[[0.81 0.18 0.18 0.04]
[0.09 0.72 0.02 0.16]
[0.09 0.02 0.72 0.16]
[0.01 0.08 0.08 0.64]]

Column-wise sums of confusion matrix:
[1. 1. 1. 1.]

Inverse confusion matrix:
[[ 1.30612245 -0.32653061 -0.32653061  0.08163265]
[-0.16326531  1.46938776  0.04081633 -0.36734694]
[-0.16326531  0.04081633  1.46938776 -0.36734694]
[ 0.02040816 -0.18367347 -0.18367347  1.65306122]]


Note

In each code example we show an explicit computation of the full $$2^n \times 2^n$$ confusion matrix $$A$$ from the smaller, local confusion matrices supplied by the user, but this is solely for expository purposes. When applying REM in practice only the pseudoinverse $$A^{+}$$ needs to be computed: the user supplies one or more local confusion matrices, and Mitiq’s utility functions can directly compute $$A^{+}$$ from these.

### Inverse confusion matrix from $$k$$ local confusion matrices#

The function generate_tensored_inverse_confusion_matrix(num_qubits, confusion_matrices) can be applied to any $$n$$-qubit confusion matrix $$A$$ which is factorized into the tensor product of $$k$$ smaller, local confusion matrices (supplied by the user in confusion_matrices), one for each subset in a partition of the $$n$$ qubits into $$k$$ smaller subsets. The factorization encodes the assumption that there are $$k$$ independent/uncorrelated noise processes affecting the $$k$$ disjoint subsets of qubits (possibly of different sizes), but within each subset noise may be correlated between qubits in that subset. This model includes the simplest noise model above as the special case where $$k=n$$ and each of the $$n$$ single-qubit subsets has the same confusion matrix $$A_1$$:

$A = A^{(1)}_1 \otimes \dots \otimes A^{(n)}_1.$

For a slightly more nuanced model, one could still assume independent noise across qubits, but specify different $$2 \times 2$$ confusion matrices for each qubit:

$A = A^{(1)}_1 \otimes \dots \otimes A^{(n)}_n.$

Here is an example with two qubits.

from mitiq.rem import generate_tensored_inverse_confusion_matrix

# Confusion matrix for qubit 1 (same as above)
A1_entries = [
0.9, 0.2,
0.1, 0.8
]
A1 = np.array(A1_entries).reshape(2,2)

# A different confusion matrix for qubit 2
A2_entries = [
0.7, 0.4,
0.3, 0.6
]
A2 = np.array(A2_entries).reshape(2,2)

# Overall 2-qubit confusion matrix (A1 tensor A2)
A = np.kron(A1, A2)

# Generate inverse confusion matrix.
A_pinv = generate_tensored_inverse_confusion_matrix(2, confusion_matrices=[A1, A2])

print(f"Confusion matrix:\n{A}\n")
print(f"Column-wise sums of confusion matrix:\n{A.sum(axis=0)}\n")
print(f"Inverse confusion matrix:\n{A_pinv}")

Confusion matrix:
[[0.63 0.36 0.14 0.08]
[0.27 0.54 0.06 0.12]
[0.07 0.04 0.56 0.32]
[0.03 0.06 0.24 0.48]]

Column-wise sums of confusion matrix:
[1. 1. 1. 1.]

Inverse confusion matrix:
[[ 2.28571429 -1.52380952 -0.57142857  0.38095238]
[-1.14285714  2.66666667  0.28571429 -0.66666667]
[-0.28571429  0.19047619  2.57142857 -1.71428571]
[ 0.14285714 -0.33333333 -1.28571429  3.        ]]


More generally, one can provide generate_tensored_inverse_confusion_matrix() with a list of $$k$$ confusion matrices of any size (for any $$k$$, $$1\leq k \leq n$$), as long as their dimensions when tensored together give an overall confusion matrix of the correct dimension $$2^{n} \times 2^{n}$$. For instance, the first confusion matrix might apply to qubits $$1$$ and $$2$$ while the $$k$$th applies to qubits $$n-2, n-1, n$$:

$A = A^{(1)}_{1,2} \otimes \dots \otimes A^{(k)}_{n-2, n-1, n}.$

Here is an example with three qubits. We represent a stochastic noise model in which errors on qubit $$1$$ are independent of errors on qubits $$2$$ and $$3$$, but errors on qubits $$2$$ and $$3$$ are correlated with each other. So the confusion matrix factorizes into two differently sized sub-matrices $$A = A^{(1)}_1 \otimes A^{(2)}_{2,3}$$.

# Confusion matrix for qubit 1 (same as above)
A1_entries = [
0.9, 0.2,
0.1, 0.8
]
A1 = np.array(A1_entries).reshape(2,2)

# Generate a random 4x4 confusion matrix (square
# with columns summing to 1) to represent a
# a correlated error model on qubits 2 and 3
matrix = np.random.rand(4,4)
A23 = matrix/(matrix.sum(axis=0)[None,:])

# Overall 3-qubit confusion matrix (A1 tensor A23)
A = np.kron(A1, A23)

# Generate inverse confusion matrix.
A_pinv = generate_tensored_inverse_confusion_matrix(3, [A1, A23])

print(f"Confusion matrix:\n{A}\n")
print(f"Column-wise sums of confusion matrix:\n{A.sum(axis=0)}\n")
print(f"Inverse confusion matrix:\n{A_pinv}")

Confusion matrix:
[[0.32577486 0.28820653 0.33489107 0.25263015 0.07239441 0.0640459
0.07442024 0.05614003]
[0.14472218 0.17264411 0.30374331 0.20423054 0.03216049 0.03836536
0.06749851 0.04538456]
[0.29722129 0.23607676 0.11453035 0.21041839 0.06604918 0.0524615
0.02545119 0.04675964]
[0.13228167 0.2030726  0.14683528 0.23272092 0.02939593 0.04512724
0.03263006 0.05171576]
[0.03619721 0.03202295 0.03721012 0.02807002 0.28957765 0.25618358
0.29768095 0.22456013]
[0.01608024 0.01918268 0.03374926 0.02269228 0.12864194 0.15346143
0.26999405 0.18153826]
[0.03302459 0.02623075 0.01272559 0.02337982 0.2641967  0.209846
0.10180475 0.18703857]
[0.01469796 0.02256362 0.01631503 0.02585788 0.11758371 0.18050898
0.13052025 0.20686304]]

Column-wise sums of confusion matrix:
[1. 1. 1. 1. 1. 1. 1. 1.]

Inverse confusion matrix:
[[-10.09684908  11.62021762  14.27364499 -12.14275152   2.52421227
-2.9050544   -3.56841125   3.03568788]
[ 35.14534458 -38.38108913 -29.8599261   22.52867807  -8.78633614
9.59527228   7.46498153  -5.63216952]
[  2.77213249   2.75686978  -3.28775401  -2.45597742  -0.69303312
-0.68921745   0.8219385    0.61399436]
[-26.67777084  25.14685888  20.01689226  -6.78709199   6.66944271
-6.28671472  -5.00422307   1.696773  ]
[  1.26210614  -1.4525272   -1.78420562   1.51784394 -11.35895522
13.07274482  16.05785062 -13.66059547]
[ -4.39316807   4.79763614   3.73249076  -2.81608476  39.53851265
-43.17872527 -33.59241687  25.34476283]
[ -0.34651656  -0.34460872   0.41096925   0.30699718   3.11864905
3.10147851  -3.69872326  -2.7629746 ]
[  3.33472135  -3.14335736  -2.50211153   0.8483865  -30.01249219
28.29021624  22.5190038   -7.63547848]]