- Notifications
You must be signed in to change notification settings - Fork 1.2k
Add cirq.TensoredConfusionMatrices for readout error mitigation. #4854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
15 commits Select commit Hold shift + click to select a range
7f3998b Add for readout error mitigation.
tanujkhattar 5ac4f45 Merge branch 'master' of https://github.com/quantumlib/cirq into conf…
tanujkhattar 05a036c Merge branch 'master' of https://github.com/quantumlib/cirq into conf…
tanujkhattar 0c3ec58 Improve docstrings, switch to alternate einsum API, raise error for r…
tanujkhattar aab3a14 Fix lint and typo
tanujkhattar a91e120 Merge branch 'master' of https://github.com/quantumlib/cirq into conf…
tanujkhattar 802fbc2 Address comments
tanujkhattar 09f9242 Merge branch 'master' of https://github.com/quantumlib/cirq into conf…
tanujkhattar 30d8035 Address comments
tanujkhattar fee5e0d Fix mypy issues
tanujkhattar a68a49a Merge branch 'master' of https://github.com/quantumlib/cirq into conf…
tanujkhattar a237a26 Raise instead of warning if optimization fails
tanujkhattar dddfc89 Merge branch 'master' into confusion_matrix
CirqBot c9a4e49 Merge branch 'master' into confusion_matrix
CirqBot 40544f7 Merge branch 'master' into confusion_matrix
CirqBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,310 @@ | ||
| # Copyright 2022 The Cirq Developers | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| | ||
| """Utilities to compute readout confusion matrix and use it for readout error mitigation.""" | ||
| | ||
| import functools | ||
| from typing import Any, Dict, Union, Sequence, List, Tuple, TYPE_CHECKING, Optional, cast | ||
| | ||
| import numpy as np | ||
| import scipy.optimize | ||
| from cirq import circuits, ops, vis | ||
| from cirq._compat import proper_repr | ||
| | ||
| if TYPE_CHECKING: | ||
| import cirq | ||
| | ||
| | ||
| class ReadoutConfusionMatrix: | ||
| """Store and use confusion matrices for readout error mitigation on sets of qubits. | ||
| | ||
| The confusion matrix (CM) for two qubits is the following matrix: | ||
| | ||
| ⎡ Pr(00o|00a) Pr(01o|00a) Pr(10o|00a) Pr(11o|00a) ⎤ | ||
| ⎢ Pr(00o|01a) Pr(01o|01a) Pr(10o|01a) Pr(11o|01a) ⎥ | ||
| ⎢ Pr(00o|10a) Pr(01o|10a) Pr(10o|10a) Pr(11o|10a) ⎥ | ||
| ⎣ Pr(00o|11a) Pr(01o|11a) Pr(10o|11a) Pr(11o|11a) ⎦ | ||
| | ||
| where Pr(ij | pq) = Probability of observing “ij” given state “pq” was prepared. | ||
tanujkhattar marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| This class can be used to | ||
| - Store a list of confusion matrices computed for a list of qubit patterns. | ||
| - Build a single confusion / correction matrix for entire set of calibrated qubits using the | ||
| smaller individual confusion matrices for specific qubit patterns. | ||
| - Apply readout corrections to observed frequencies / output probabilities. | ||
| | ||
| Use `cirq.measure_confusion_matrix(sampler, qubits, repetitions)` to perform | ||
| an experiment on `sampler` and construct the `cirq.ReadoutConfusionMatrix` object. | ||
| """ | ||
| | ||
| def __init__( | ||
| self, | ||
| confusion_matrices: Union[np.ndarray, Sequence[np.ndarray]], | ||
| measure_qubits: Union[Sequence['cirq.Qid'], Sequence[Sequence['cirq.Qid']]], | ||
| ): | ||
| """Initializes `cirq.ReadoutConfusionMatrix`. | ||
| | ||
| `confusion_matrices[i]` should correspond to the qubit sequence `measure_qubits[i]`. | ||
| | ||
| Args: | ||
| confusion_matrices: Sequence of Confusion matrices, computed for qubit patterns present | ||
| in `measure_qubits`. A single confusion matrix is also accepted. | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| measure_qubits: Sequence of smaller qubit patterns, for which the confusion matrices | ||
| were computed. A single qubit pattern is also accepted. | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| Raises: | ||
| ValueError: If length of `confusion_matrices` and `measure_qubits` is different or if | ||
| the shape of any confusion matrix does not match the corresponding qubit | ||
| pattern. | ||
| """ | ||
| if isinstance(confusion_matrices, np.ndarray): | ||
| confusion_matrices = [confusion_matrices] | ||
| measure_qubits = cast( | ||
| Sequence[Sequence['cirq.Qid']], | ||
| [measure_qubits] if isinstance(measure_qubits[0], ops.Qid) else measure_qubits, | ||
tanujkhattar marked this conversation as resolved. Show resolved Hide resolved | ||
| ) | ||
| if len(confusion_matrices) != len(measure_qubits): | ||
| raise ValueError( | ||
| f"len(confusion_matrices): {len(confusion_matrices)} should be equal to " | ||
| f"len(measure_qubits): {len(measure_qubits)}" | ||
| ) | ||
| for i, (cm, q) in enumerate(zip(confusion_matrices, measure_qubits)): | ||
| if cm.shape != (2 ** len(q),) * 2: | ||
| raise ValueError( | ||
| f"Shape mismatch for confusion matrix {cm} at index {i} corresponding to {q}." | ||
| f"Confusion Matrix shape {cm.shape} should match {(2 ** len(q),) * 2}" | ||
| ) | ||
| self._confusion_matrices = list(confusion_matrices) | ||
| self._measure_qubits = [list(q) for q in measure_qubits] | ||
| self._qubits = sorted(set(q for ql in measure_qubits for q in ql)) | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| @property | ||
| def confusion_matrices(self) -> List[np.ndarray]: | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| """List of confusion matrices corresponding to `measure_qubits` qubit pattern.""" | ||
| return self._confusion_matrices | ||
| | ||
| @property | ||
| def measure_qubits(self) -> List[List['cirq.Qid']]: | ||
| """Calibrated qubit pattern for which individual confusion matrices were computed.""" | ||
| return self._measure_qubits | ||
| | ||
| @property | ||
| def qubits(self) -> List['cirq.Qid']: | ||
| """Sorted list of all calibrated qubits.""" | ||
| return self._qubits | ||
| | ||
| def _get_vars(self, qubit_pattern: Optional[Sequence[Sequence['cirq.Qid']]] = None): | ||
| if qubit_pattern is None: | ||
| qubit_pattern = self.measure_qubits | ||
| abcd = "abcdefghijklmnopqrstuvwxyz" | ||
| | ||
| def qubits_to_abcd(qs: Sequence['cirq.Qid']): | ||
| assert len(qs) <= len(abcd), "No. of qubits should be <= 26." | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ret = ''.join(abcd[self.qubits.index(q)] for q in qs) | ||
| return ret + ret.upper() | ||
| | ||
| return ','.join(qubits_to_abcd(qs) for qs in qubit_pattern) | ||
| | ||
| @functools.lru_cache() | ||
| def _confusion_matrix(self, qubits: Tuple['cirq.Qid']) -> np.ndarray: | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ret = np.einsum( | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| f'{self._get_vars()}->{self._get_vars([qubits])}', | ||
| *[ | ||
| cm.reshape((2, 2) * len(qs)) | ||
| for qs, cm in zip(self.measure_qubits, self.confusion_matrices) | ||
| ], | ||
| ).reshape((2 ** len(qubits),) * 2) | ||
| return ret / ret.sum(axis=1) | ||
| | ||
| def confusion_matrix(self, qubits: Optional[Sequence['cirq.Qid']] = None) -> np.ndarray: | ||
| """Returns a single confusion matrix constructed for the given set of qubits. | ||
| | ||
| The single `2 ** len(qubits) x 2 ** len(qubits)` confusion matrix is constructed | ||
| using the individual smaller `self.confusion_matrices` by applying necessary | ||
tanujkhattar marked this conversation as resolved. Show resolved Hide resolved | ||
| matrix transpose / kron / partial trace operations. | ||
| | ||
| Args: | ||
| qubits: The qubits representing the subspace for which a confusion matrix should be | ||
| constructed. By default, uses all qubits in sorted order, i.e. `self.qubits`. | ||
| | ||
| Returns: | ||
| Confusion matrix for subspace corresponding to `qubits`. | ||
| | ||
| Raises: | ||
| ValueError: If `qubits` is not a subset of `self.qubits`. | ||
| """ | ||
| | ||
| if qubits is None: | ||
| qubits = self.qubits | ||
| if any(q not in self.qubits for q in qubits): | ||
| raise ValueError(f"qubits {qubits} should be a subset of self.qubits {self.qubits}.") | ||
| return self._confusion_matrix(tuple(qubits)) | ||
| | ||
| def correction_matrix(self, qubits: Optional[Sequence['cirq.Qid']] = None) -> np.ndarray: | ||
| """Returns a single correction matrix constructed for the given set of qubits. | ||
| | ||
| A correction matrix is the inverse of confusion matrix and can be used to apply corrections | ||
| to observed frequencies / probabilities to compensate for the readout error. | ||
| A Moore–Penrose Pseudo inverse of the confusion matrix is computed to get the correction | ||
| matrix. | ||
| | ||
| Args: | ||
| qubits: The qubits representing the subspace for which a correction matrix should be | ||
| constructed. By default, uses all qubits in sorted order, i.e. `self.qubits`. | ||
| | ||
| Returns: | ||
| Correction matrix for subspace corresponding to `qubits`. | ||
| | ||
| Raises: | ||
| ValueError: If `qubits` is not a subset of `self.qubits`. | ||
| """ | ||
| | ||
| if qubits is None: | ||
| qubits = self.qubits | ||
| if any(q not in self.qubits for q in qubits): | ||
| raise ValueError(f"qubits {qubits} should be a subset of self.qubits {self.qubits}.") | ||
| return np.linalg.pinv(self.confusion_matrix(qubits)) | ||
| | ||
| def apply( | ||
| self, | ||
| result: np.ndarray, | ||
| qubits: Optional[Sequence['cirq.Qid']] = None, | ||
| *, | ||
| method='least_squares', | ||
| ) -> np.ndarray: | ||
| """Applies corrections to the observed `result` to compensate for readout error on qubits. | ||
| | ||
| The compensation is applied by multiplying the result with the correction matrix | ||
| corresponding to the subspace defined by `qubits`. | ||
| | ||
| Args: | ||
| result: `(2 ** len(qubits), )` shaped numpy array containing observed frequencies / | ||
| probabilities. | ||
| qubits: Sequence of qubits used for sampling to get `result`. By default, uses all | ||
| qubits in sorted order, i.e. `self.qubits`. | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| method: Correction Method. Should be either 'pseudo_inverse' or 'least_squares'. | ||
tanujkhattar marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| Returns: | ||
| `(2 ** len(qubits), )` shaped numpy array corresponding to `result` with corrections. | ||
| | ||
| Raises: | ||
| ValueError: if `result.shape` != `(2 ** len(qubits),)`. | ||
| """ | ||
| if qubits is None: | ||
| qubits = self.qubits | ||
| if result.shape != (2 ** len(qubits),): | ||
| raise ValueError(f"result.shape {result.shape} should be {(2 ** len(qubits),)}.") | ||
| if method not in ['pseudo_inverse', 'least_squares']: | ||
| raise ValueError(f"method: {method} should be 'pseudo_inverse' or 'least_squares'.") | ||
| | ||
| if method == 'pseudo_inverse': | ||
| return result @ self.correction_matrix(qubits) # coverage: ignore | ||
| | ||
| # Least squares minimization. | ||
| cm = self.confusion_matrix(qubits) | ||
| | ||
| def func(x): | ||
| print(x.shape) | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| return np.sum((result - x @ cm) ** 2) | ||
| | ||
| constraints = {'type': 'eq', 'fun': lambda x: sum(result) - sum(x)} | ||
| bounds = tuple((0, sum(result)) for _ in result) | ||
| res = scipy.optimize.minimize( | ||
| func, result, method='SLSQP', constraints=constraints, bounds=bounds | ||
| ) | ||
| return res.x | ||
tanujkhattar marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| def __repr__(self) -> str: | ||
| return ( | ||
| f"cirq.ReadoutConfusionMatrix(" | ||
| f"[{','.join([proper_repr(cm) for cm in self.confusion_matrices])}]," | ||
| f"{self.measure_qubits}" | ||
| f")" | ||
| ) | ||
| | ||
| def _json_dict_(self) -> Dict[str, Any]: | ||
| return { | ||
| 'confusion_matrices': self.confusion_matrices, | ||
| 'measure_qubits': self.measure_qubits, | ||
| } | ||
| | ||
| @classmethod | ||
| def _from_json_dict_( | ||
| cls, confusion_matrices, measure_qubits, **kwargs | ||
| ) -> 'ReadoutConfusionMatrix': | ||
| return cls([np.asarray(cm) for cm in confusion_matrices], measure_qubits) | ||
| | ||
| def _approx_eq_(self, other: Any, atol: float) -> bool: | ||
| if not isinstance(other, type(self)): | ||
| return NotImplemented | ||
| return self.qubits == other.qubits and all( | ||
| np.allclose(cm, ocm, atol=atol) | ||
| for cm, ocm in zip(self.confusion_matrices, other.confusion_matrices) | ||
| ) | ||
| | ||
| def __eq__(self, other: Any) -> bool: | ||
| if not isinstance(other, type(self)): | ||
| return NotImplemented | ||
| return self.qubits == other.qubits and all( | ||
| np.array_equal(cm, ocm) | ||
| for cm, ocm in zip(self.confusion_matrices, other.confusion_matrices) | ||
| ) | ||
| | ||
| def __ne__(self, other: Any) -> bool: | ||
| return not self == other | ||
| | ||
| def __hash__(self) -> int: | ||
| vals = tuple(v for cm in self.confusion_matrices for _, v in np.ndenumerate(cm)) | ||
| return hash((ReadoutConfusionMatrix, vals, tuple(self.qubits))) | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| | ||
| def measure_confusion_matrix( | ||
| sampler: 'cirq.Sampler', | ||
| qubits: Union[Sequence['cirq.Qid'], Sequence[Sequence['cirq.Qid']]], | ||
| repetitions: int = 1000, | ||
| ) -> ReadoutConfusionMatrix: | ||
| """Prepares `ReadoutConfusionMatrix` for the n qubits in the input. | ||
| | ||
| The confusion matrix (CM) for two qubits is the following matrix: | ||
| | ||
| ⎡ Pr(00o|00a) Pr(01o|00a) Pr(10o|00a) Pr(11o|00a) ⎤ | ||
| ⎢ Pr(00o|01a) Pr(01o|01a) Pr(10o|01a) Pr(11o|01a) ⎥ | ||
| ⎢ Pr(00o|10a) Pr(01o|10a) Pr(10o|10a) Pr(11o|10a) ⎥ | ||
| ⎣ Pr(00o|11a) Pr(01o|11a) Pr(10o|11a) Pr(11o|11a) ⎦ | ||
| | ||
| where Pr(ij | pq) = Probability of observing “ij” given state “pq” was prepared. | ||
tanujkhattar marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| Args: | ||
| sampler: Sampler to collect the data from. | ||
| qubits: Qubits for which the confusion matrix should be measured. | ||
| repetitions: Number of times to sample each circuit for a confusion matrix row. | ||
| """ | ||
| qubits = cast( | ||
| Sequence[Sequence['cirq.Qid']], [qubits] if isinstance(qubits[0], ops.Qid) else qubits | ||
| ) | ||
| confusion_matrices = [] | ||
| for qs in qubits: | ||
| results = sampler.run_batch( | ||
| [ | ||
| circuits.Circuit( | ||
| [ops.X(q) ** ((state >> i) & 1) for i, q in enumerate(qs[::-1])], | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ops.measure(*qs), | ||
| ) | ||
| for state in range(2 ** len(qs)) | ||
| ], | ||
| repetitions=repetitions, | ||
| ) | ||
| confusion_matrices.append( | ||
| np.asarray([vis.get_state_histogram(r[0]) for r in results], dtype=float) / repetitions | ||
tanujkhattar marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ) | ||
| return ReadoutConfusionMatrix(confusion_matrices, qubits) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.