-
Notifications
You must be signed in to change notification settings - Fork 131
Local and correlated readout error characterization experiments #611
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
Changes from all commits
5a349a2
0cac146
f927dea
aee85ca
410774c
20bd3ed
f745194
f2677d2
1a382ab
694c291
3a01c3b
4c12244
949e3a9
d0f8904
9ac0a52
34a3347
85fd567
a2f8629
dfa4a4f
c694110
0359c8d
7c62358
15ee74f
8923a07
8359bc0
c154374
3dd1a25
afa33f8
d9069ba
c1ad8e1
dad27bd
6eabbd2
2952da3
8ee9eea
f7c9a74
1e4ee4b
09bb213
ed1177a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021, 2022. | ||
| # | ||
| # 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. | ||
| """ | ||
| Analysis class to characterize correlated readout error | ||
| """ | ||
| from typing import List, Tuple | ||
| import numpy as np | ||
| import matplotlib.pyplot as plt | ||
| from qiskit.result import CorrelatedReadoutMitigator | ||
| from qiskit_experiments.framework import ExperimentData | ||
| from qiskit_experiments.framework.matplotlib import get_non_gui_ax | ||
| from qiskit_experiments.framework import BaseAnalysis, AnalysisResultData, Options | ||
|
|
||
|
|
||
| class CorrelatedReadoutErrorAnalysis(BaseAnalysis): | ||
| r""" | ||
| Correlated readout error characterization analysis | ||
|
|
||
| # section: overview | ||
|
|
||
| This class generates the full assignment matrix :math:`A` characterizing the | ||
| readout error for the given qubits from the experiment results | ||
| and returns the resulting :class:`~qiskit.result.CorrelatedReadoutMitigator` | ||
|
|
||
| :math:`A` is a :math:`2^n\times 2^n` matrix :math:`A` such that :math:`A_{y,x}` | ||
| is the probability to observe :math:`y` given the true outcome should be :math:`x`. | ||
|
|
||
| In the experiment, for each :math:`x`a circuit is constructed whose expected | ||
| outcome is :math:`x`. From the observed results on the circuit, the probability for | ||
| each :math:`y` is determined, and :math:`A_{y,x}` is set accordingly. | ||
|
|
||
| Analysis Results: | ||
| * "Local Readout Mitigator": The :class:`~qiskit.result.LocalReadoutMitigator`. | ||
|
|
||
| Analysis Figures: | ||
| * (Optional) A figure of the assignment matrix. | ||
|
|
||
| # section: reference | ||
| .. ref_arxiv:: 1 2006.14044 | ||
| """ | ||
|
|
||
| @classmethod | ||
| def _default_options(cls) -> Options: | ||
| """Return default analysis options. | ||
|
|
||
| Analysis Options: | ||
| plot (bool): Set ``True`` to create figure for fit result. | ||
gadial marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ax (AxesSubplot): Optional. A matplotlib axis object to draw. | ||
| """ | ||
| options = super()._default_options() | ||
| options.plot = True | ||
| options.ax = None | ||
| return options | ||
|
|
||
| def _run_analysis( | ||
| self, experiment_data: ExperimentData, **options | ||
| ) -> Tuple[List[AnalysisResultData], List["matplotlib.figure.Figure"]]: | ||
| data = experiment_data.data() | ||
| qubits = experiment_data.metadata["physical_qubits"] | ||
| labels = [datum["metadata"]["state_label"] for datum in data] | ||
| matrix = self._generate_matrix(data, labels) | ||
| result_mitigator = CorrelatedReadoutMitigator(matrix, qubits=qubits) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that trying to save the experiment will crash because
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make these serializable we would to add |
||
| analysis_results = [AnalysisResultData("Correlated Readout Mitigator", result_mitigator)] | ||
| if self.options.plot: | ||
| ax = options.get("ax", None) | ||
| figures = [self._assignment_matrix_visualization(matrix, labels, ax)] | ||
| else: | ||
| figures = [] | ||
| return analysis_results, figures | ||
|
|
||
| def _generate_matrix(self, data, labels) -> np.array: | ||
| list_size = len(labels) | ||
| matrix = np.zeros([list_size, list_size], dtype=float) | ||
| # matrix[i][j] is the probability of counting i for expected j | ||
| for datum in data: | ||
| expected_outcome = datum["metadata"]["state_label"] | ||
| j = labels.index(expected_outcome) | ||
| total_counts = sum(datum["counts"].values()) | ||
| for measured_outcome, count in datum["counts"].items(): | ||
| i = labels.index(measured_outcome) | ||
| matrix[i][j] = count / total_counts | ||
| return matrix | ||
|
|
||
| def _assignment_matrix_visualization( | ||
| self, matrix, labels, ax=None | ||
| ) -> "matplotlib.figure.Figure": | ||
| """ | ||
| Plot the assignment matrix (2D color grid plot). | ||
|
|
||
| Args: | ||
| matrix: assignment matrix to plot | ||
| ax (matplotlib.axes): settings for the graph | ||
|
|
||
| Returns: | ||
| The generated plot of the assignment matrix | ||
|
|
||
| Raises: | ||
| QiskitError: if _cal_matrices was not set. | ||
|
|
||
| ImportError: if matplotlib was not installed. | ||
|
|
||
| """ | ||
|
|
||
| if ax is None: | ||
| ax = get_non_gui_ax() | ||
| figure = ax.get_figure() | ||
| ax.matshow(matrix, cmap=plt.cm.binary, clim=[0, 1]) | ||
| ax.set_xlabel("Prepared State") | ||
gadial marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ax.xaxis.set_label_position("top") | ||
| ax.set_ylabel("Measured State") | ||
| ax.set_xticks(np.arange(len(labels))) | ||
| ax.set_yticks(np.arange(len(labels))) | ||
| ax.set_xticklabels(labels) | ||
| ax.set_yticklabels(labels) | ||
| return figure | ||
Uh oh!
There was an error while loading. Please reload this page.