-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Migrate measure mitigation from ignis for QuantumInstance #6867
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
9e26a10
3223996
701b2de
733417d
aac56bd
acc9e77
7386576
1ace6b0
61bbf56
5a6070f
d04d405
0977d10
508d601
bffd7b3
9cb0797
d893c7e
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 |
|---|---|---|
|
|
@@ -33,5 +33,6 @@ Qiskit Terra API Reference | |
| transpiler_preset | ||
| transpiler_plugins | ||
| utils | ||
| utils_mitigation | ||
| opflow | ||
| algorithms | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| .. _qiskit-utils-mitigation: | ||
|
|
||
| .. automodule:: qiskit.utils.mitigation | ||
| :no-members: | ||
| :no-inherited-members: | ||
| :no-special-members: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,12 +14,19 @@ | |
|
|
||
| import copy | ||
| from typing import List, Optional, Tuple, Dict, Callable | ||
|
|
||
| from qiskit import compiler | ||
| from qiskit.providers import BaseBackend | ||
| from qiskit.circuit import QuantumCircuit | ||
| from qiskit.qobj import QasmQobj | ||
| from qiskit.assembler.run_config import RunConfig | ||
| from ..exceptions import QiskitError, MissingOptionalLibraryError | ||
| from qiskit.exceptions import QiskitError | ||
| from qiskit.utils.mitigation import ( | ||
| complete_meas_cal, | ||
| tensored_meas_cal, | ||
| CompleteMeasFitter, | ||
| TensoredMeasFitter, | ||
| ) | ||
|
|
||
| # pylint: disable=invalid-name | ||
|
|
||
|
|
@@ -135,37 +142,43 @@ def build_measurement_error_mitigation_circuits( | |
| the labels of the calibration circuits | ||
| Raises: | ||
| QiskitError: when the fitter_cls is not recognizable. | ||
| MissingOptionalLibraryError: Qiskit-Ignis not installed | ||
| """ | ||
| try: | ||
| from qiskit.ignis.mitigation.measurement import ( | ||
| complete_meas_cal, | ||
| tensored_meas_cal, | ||
| CompleteMeasFitter, | ||
| TensoredMeasFitter, | ||
| ) | ||
| except ImportError as ex: | ||
| raise MissingOptionalLibraryError( | ||
| libname="qiskit-ignis", | ||
| name="build_measurement_error_mitigation_qobj", | ||
| pip_install="pip install qiskit-ignis", | ||
| ) from ex | ||
|
|
||
| circlabel = "mcal" | ||
|
|
||
| if not qubit_list: | ||
| raise QiskitError("The measured qubit list can not be [].") | ||
|
|
||
| run = False | ||
| if fitter_cls == CompleteMeasFitter: | ||
| meas_calibs_circuits, state_labels = complete_meas_cal( | ||
| qubit_list=range(len(qubit_list)), circlabel=circlabel | ||
| ) | ||
| run = True | ||
| elif fitter_cls == TensoredMeasFitter: | ||
| meas_calibs_circuits, state_labels = tensored_meas_cal( | ||
| mit_pattern=mit_pattern, circlabel=circlabel | ||
| ) | ||
| else: | ||
| raise QiskitError(f"Unknown fitter {fitter_cls}") | ||
| run = True | ||
| if not run: | ||
| try: | ||
| from qiskit.ignis.mitigation.measurement import ( | ||
| CompleteMeasFitter as CompleteMeasFitter_IG, | ||
| TensoredMeasFitter as TensoredMeasFitter_IG, | ||
| ) | ||
| except ImportError as ex: | ||
| # If ignis can't be imported we don't have a valid fitter | ||
| # class so just fail here with an appropriate error message | ||
| raise QiskitError(f"Unknown fitter {fitter_cls}") from ex | ||
| if fitter_cls == CompleteMeasFitter_IG: | ||
| meas_calibs_circuits, state_labels = complete_meas_cal( | ||
| qubit_list=range(len(qubit_list)), circlabel=circlabel | ||
| ) | ||
| elif fitter_cls == TensoredMeasFitter_IG: | ||
| meas_calibs_circuits, state_labels = tensored_meas_cal( | ||
| mit_pattern=mit_pattern, circlabel=circlabel | ||
| ) | ||
| else: | ||
| raise QiskitError(f"Unknown fitter {fitter_cls}") | ||
|
|
||
| # the provided `qubit_list` would be used as the initial layout to | ||
| # assure the consistent qubit mapping used in the main circuits. | ||
|
|
@@ -209,19 +222,6 @@ def build_measurement_error_mitigation_qobj( | |
| QiskitError: when the fitter_cls is not recognizable. | ||
| MissingOptionalLibraryError: Qiskit-Ignis not installed | ||
| """ | ||
| try: | ||
| from qiskit.ignis.mitigation.measurement import ( | ||
| complete_meas_cal, | ||
| tensored_meas_cal, | ||
| CompleteMeasFitter, | ||
| TensoredMeasFitter, | ||
| ) | ||
| except ImportError as ex: | ||
| raise MissingOptionalLibraryError( | ||
| libname="qiskit-ignis", | ||
| name="build_measurement_error_mitigation_qobj", | ||
| pip_install="pip install qiskit-ignis", | ||
| ) from ex | ||
|
|
||
|
Comment on lines
212
to
225
Member
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. Do we need the same attempt to import from Ignis for this function (
Member
Author
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. it's not strictly necessary because the calibration circuits are the same. Above where we handle the ignis path we're actually just passing the terra version of the classes here. If ignis were going to continue active feature development I would have added handling for calling ignis here just in case the cal circuits diverged, but since everything is pretty much froze and this is just for compat while the api for mitigation in algorithms is being redone I think this is fine. |
||
| circlabel = "mcal" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2019. | ||
| # | ||
| # 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. | ||
|
|
||
| # This code was originally copied from the qiskit-ignis repsoitory see: | ||
| # https://github.com/Qiskit/qiskit-ignis/blob/b91066c72171bcd55a70e6e8993b813ec763cf41/qiskit/ignis/mitigation/measurement/__init__.py | ||
| # it was migrated as qiskit-ignis is being deprecated | ||
|
|
||
| """ | ||
| ============================================================= | ||
| Measurement Mitigation Utils (:mod:`qiskit.utils.mitigation`) | ||
| ============================================================= | ||
|
|
||
| .. currentmodule:: qiskit.utils.mitigation | ||
|
|
||
| .. warning:: | ||
|
|
||
| The user-facing API stability of this module is not guaranteed except for | ||
| its use with the :class:`~qiskit.utils.QuantumInstance` (i.e. using the | ||
| :class:`~qiskit.utils.mitigation.CompleteMeasFitter` or | ||
| :class:`~qiskit.utils.mitigation.TensoredMeasFitter` classes as values for the | ||
| ``meas_error_mitigation_cls``). The rest of this module should be treated as | ||
| an internal private API that can not be relied upon. | ||
|
|
||
| Measurement correction | ||
| ====================== | ||
|
|
||
| The measurement calibration is used to mitigate measurement errors. | ||
| The main idea is to prepare all :math:`2^n` basis input states and compute | ||
| the probability of measuring counts in the other basis states. | ||
| From these calibrations, it is possible to correct the average results | ||
| of another experiment of interest. These tools are intended for use solely | ||
| with the :class:`~qiskit.utils.QuantumInstance` class as part of | ||
| :mod:`qiskit.algorithms` and :mod:`qiskit.opflow`. | ||
|
|
||
| .. autosummary:: | ||
| :toctree: ../stubs/ | ||
|
|
||
| CompleteMeasFitter | ||
| TensoredMeasFitter | ||
| """ | ||
|
|
||
| # Measurement correction functions | ||
| from .circuits import complete_meas_cal, tensored_meas_cal | ||
| from .fitters import CompleteMeasFitter, TensoredMeasFitter |
Uh oh!
There was an error while loading. Please reload this page.