/
correlated_readout_mitigator.py
269 lines (224 loc) · 10.3 KB
/
correlated_readout_mitigator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
Readout mitigator class based on the A-matrix inversion method
"""
import math
from typing import Optional, List, Tuple, Iterable, Callable, Union, Dict
import numpy as np
from qiskit.exceptions import QiskitError
from ..distributions.quasi import QuasiDistribution
from ..counts import Counts
from .base_readout_mitigator import BaseReadoutMitigator
from .utils import counts_probability_vector, z_diagonal, str2diag
class CorrelatedReadoutMitigator(BaseReadoutMitigator):
"""N-qubit readout error mitigator.
Mitigates :meth:`expectation_value` and :meth:`quasi_probabilities`.
The mitigation_matrix should be calibrated using qiskit experiments.
This mitigation method should be used in case the readout errors of the qubits
are assumed to be correlated. The mitigation_matrix of *N* qubits is of size
:math:`2^N x 2^N` so the mitigation complexity is :math:`O(4^N)`.
"""
def __init__(self, assignment_matrix: np.ndarray, qubits: Optional[Iterable[int]] = None):
"""Initialize a CorrelatedReadoutMitigator
Args:
assignment_matrix: readout error assignment matrix.
qubits: Optional, the measured physical qubits for mitigation.
Raises:
QiskitError: matrix size does not agree with number of qubits
"""
if np.any(assignment_matrix < 0) or not np.allclose(np.sum(assignment_matrix, axis=0), 1):
raise QiskitError("Assignment matrix columns must be valid probability distributions")
assignment_matrix = np.asarray(assignment_matrix, dtype=float)
matrix_qubits_num = int(math.log2(assignment_matrix.shape[0]))
if qubits is None:
self._num_qubits = matrix_qubits_num
self._qubits = range(self._num_qubits)
else:
if len(qubits) != matrix_qubits_num:
raise QiskitError(
"The number of given qubits ({}) is different than the number of "
"qubits inferred from the matrices ({})".format(len(qubits), matrix_qubits_num)
)
self._qubits = qubits
self._num_qubits = len(self._qubits)
self._qubit_index = dict(zip(self._qubits, range(self._num_qubits)))
self._assignment_mat = assignment_matrix
self._mitigation_mats = {}
@property
def settings(self) -> Dict:
"""Return settings."""
return {"assignment_matrix": self._assignment_mat, "qubits": self._qubits}
def expectation_value(
self,
data: Counts,
diagonal: Union[Callable, dict, str, np.ndarray] = None,
qubits: Iterable[int] = None,
clbits: Optional[List[int]] = None,
shots: Optional[int] = None,
) -> Tuple[float, float]:
r"""Compute the mitigated expectation value of a diagonal observable.
This computes the mitigated estimator of
:math:`\langle O \rangle = \mbox{Tr}[\rho. O]` of a diagonal observable
:math:`O = \sum_{x\in\{0, 1\}^n} O(x)|x\rangle\!\langle x|`.
Args:
data: Counts object
diagonal: Optional, the vector of diagonal values for summing the
expectation value. If ``None`` the default value is
:math:`[1, -1]^\otimes n`.
qubits: Optional, the measured physical qubits the count
bitstrings correspond to. If None qubits are assumed to be
:math:`[0, ..., n-1]`.
clbits: Optional, if not None marginalize counts to the specified bits.
shots: the number of shots.
Returns:
(float, float): the expectation value and an upper bound of the standard deviation.
Additional Information:
The diagonal observable :math:`O` is input using the ``diagonal`` kwarg as
a list or Numpy array :math:`[O(0), ..., O(2^n -1)]`. If no diagonal is specified
the diagonal of the Pauli operator
:math`O = \mbox{diag}(Z^{\otimes n}) = [1, -1]^{\otimes n}` is used.
The ``clbits`` kwarg is used to marginalize the input counts dictionary
over the specified bit-values, and the ``qubits`` kwarg is used to specify
which physical qubits these bit-values correspond to as
``circuit.measure(qubits, clbits)``.
"""
if qubits is None:
qubits = self._qubits
probs_vec, shots = counts_probability_vector(
data, qubit_index=self._qubit_index, clbits=clbits, qubits=qubits
)
# Get qubit mitigation matrix and mitigate probs
mit_mat = self.mitigation_matrix(qubits)
# Get operator coeffs
if diagonal is None:
diagonal = z_diagonal(2**self._num_qubits)
elif isinstance(diagonal, str):
diagonal = str2diag(diagonal)
# Apply transpose of mitigation matrix
coeffs = mit_mat.T.dot(diagonal)
expval = coeffs.dot(probs_vec)
stddev_upper_bound = self.stddev_upper_bound(shots)
return (expval, stddev_upper_bound)
def quasi_probabilities(
self,
data: Counts,
qubits: Optional[List[int]] = None,
clbits: Optional[List[int]] = None,
shots: Optional[int] = None,
) -> QuasiDistribution:
"""Compute mitigated quasi probabilities value.
Args:
data: counts object
qubits: qubits the count bitstrings correspond to.
clbits: Optional, marginalize counts to just these bits.
shots: Optional, the total number of shots, if None shots will
be calculated as the sum of all counts.
Returns:
QuasiDistribution: A dictionary containing pairs of [output, mean] where "output"
is the key in the dictionaries,
which is the length-N bitstring of a measured standard basis state,
and "mean" is the mean of non-zero quasi-probability estimates.
"""
if qubits is None:
qubits = self._qubits
probs_vec, calculated_shots = counts_probability_vector(
data, qubit_index=self._qubit_index, clbits=clbits, qubits=qubits
)
if shots is None:
shots = calculated_shots
# Get qubit mitigation matrix and mitigate probs
mit_mat = self.mitigation_matrix(qubits)
# Apply transpose of mitigation matrix
probs_vec = mit_mat.dot(probs_vec)
probs_dict = {}
for index, _ in enumerate(probs_vec):
probs_dict[index] = probs_vec[index]
quasi_dist = QuasiDistribution(
probs_dict, stddev_upper_bound=self.stddev_upper_bound(shots)
)
return quasi_dist
def mitigation_matrix(self, qubits: List[int] = None) -> np.ndarray:
r"""Return the readout mitigation matrix for the specified qubits.
The mitigation matrix :math:`A^{-1}` is defined as the inverse of the
:meth:`assignment_matrix` :math:`A`.
Args:
qubits: Optional, qubits being measured.
Returns:
np.ndarray: the measurement error mitigation matrix :math:`A^{-1}`.
"""
if qubits is None:
qubits = self._qubits
qubits = tuple(sorted(qubits))
# Check for cached mitigation matrix
# if not present compute
if qubits not in self._mitigation_mats:
marginal_matrix = self.assignment_matrix(qubits)
try:
mit_mat = np.linalg.inv(marginal_matrix)
except np.linalg.LinAlgError:
# Use pseudo-inverse if matrix is singular
mit_mat = np.linalg.pinv(marginal_matrix)
self._mitigation_mats[qubits] = mit_mat
return self._mitigation_mats[qubits]
def assignment_matrix(self, qubits: List[int] = None) -> np.ndarray:
r"""Return the readout assignment matrix for specified qubits.
The assignment matrix is the stochastic matrix :math:`A` which assigns
a noisy readout probability distribution to an ideal input
readout distribution: :math:`P(i|j) = \langle i | A | j \rangle`.
Args:
qubits: Optional, qubits being measured.
Returns:
np.ndarray: the assignment matrix A.
"""
if qubits is None:
qubits = self._qubits
if qubits == self._num_qubits:
return self._assignment_mat
if isinstance(qubits, int):
qubits = [qubits]
qubit_indices = [self._qubit_index[qubit] for qubit in qubits]
# Compute marginal matrix
axis = tuple(
self._num_qubits - 1 - i for i in set(range(self._num_qubits)).difference(qubit_indices)
)
num_qubits = len(qubits)
new_amat = np.zeros(2 * [2**num_qubits], dtype=float)
for i, col in enumerate(self._assignment_mat.T[self._keep_indexes(qubit_indices)]):
new_amat[i] = (
np.reshape(col, self._num_qubits * [2]).sum(axis=axis).reshape([2**num_qubits])
)
new_amat = new_amat.T
return new_amat
@staticmethod
def _keep_indexes(qubits):
indexes = [0]
for i in sorted(qubits):
indexes += [idx + (1 << i) for idx in indexes]
return indexes
def _compute_gamma(self):
"""Compute gamma for N-qubit mitigation"""
mitmat = self.mitigation_matrix(qubits=self._qubits)
return np.max(np.sum(np.abs(mitmat), axis=0))
def stddev_upper_bound(self, shots: int):
"""Return an upper bound on standard deviation of expval estimator.
Args:
shots: Number of shots used for expectation value measurement.
Returns:
float: the standard deviation upper bound.
"""
gamma = self._compute_gamma()
return gamma / math.sqrt(shots)
@property
def qubits(self) -> Tuple[int]:
"""The device qubits for this mitigator"""
return self._qubits