Skip to content
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

Migrate measure mitigation from ignis for QuantumInstance #6867

Merged
merged 16 commits into from
Oct 1, 2021

Conversation

mtreinish
Copy link
Member

Summary

This commit migrates the measurement mitigation code from qiskit-ignis
into qiskit-terra for use with the QuantumInstance class. The
QuantumInstance class's usage of the measurement mitigation from ignis
is the one thing blocking us from deprecating qiskit-ignis completely.
By embedding the code the quantum instance depends on inside
qiskit.utils users of QuantumInstance (and therefore qiskit.algorithms)
can construct and use measurement mitigation in it's current form. The
use of this migrated module is only supported for use with the
QuantumInstance class and is explicitly documented as internal/private
except for how it gets used by the QuantumInstance.

There is ongoing work to create a standardized mitigation API in #6485
and #6748, this does not preclude that work, but we should adapt this as
part of those efforts to use the standardized interface. Ideally this
would have been made a private interface and not exposed it as user facing
(in deference to the standardization effort), but unfortunately the
QuantumInstance expects classes of these classes as it's public interface
for selecting a mitigation technique which means users need to be able to
use the classes. However, as only the classes are public interfaces we
can adapt this as we come up with a standardized mitigation interface
and rewrite the internals of this and how the QuantumInstance leverages
mitigators to use the new interface.

Details and comments

A good follow-up here would be to adapt the mitigator selection kwarg to
deprecate the use of classes and then we can make things explicitly
private in the migrated code and wait for #6495 and #6748 to be ready
for our user facing API. I opted to not include that in this PR to
minimize changes to just what we migrated from ignis and update usage of
old ignis classes to rely on the migrated version.

@mtreinish mtreinish requested review from manoelmarques, woodsp-ibm and a team as code owners August 4, 2021 14:30
@mtreinish mtreinish force-pushed the integrate-mitigation branch from e671920 to 5f5ea7d Compare August 4, 2021 14:30
@mtreinish mtreinish requested a review from chriseclectic August 4, 2021 14:33
@mtreinish mtreinish force-pushed the integrate-mitigation branch from 5f5ea7d to 148fb4c Compare August 4, 2021 14:54
This commit migrates the measurement mitigation code from qiskit-ignis
into qiskit-terra for use with the QuantumInstance class. The
QuantumInstance class's usage of the measurement mitigation from ignis
is the one thing blocking us from deprecating qiskit-ignis completely.
By embedding the code the quantum instance depends on inside
qiskit.utils users of QuantumInstance (and therefore qiskit.algorithms)
can construct and use measurement mitigation in it's current form. The
use of this migrated module is only supported for use with the
QuantumInstance class and is explicitly documented as internal/private
except for how it gets used by the QuantumInstance.

There is ongoing work to create a standardized mitigation API in Qiskit#6485
and Qiskit#6748, this does not preclude that work, but we should adapt this as
part of those efforts to use the standardized interface. Ideally this
would have been made a private interface and not exposed it as user facing
(in deference to the standardization effort), but unfortunately the
QuantumInstance expects classes of these classes as it's public interface
for selecting a mitigation technique which means users need to be able to
use the classes. However, as only the classes are public interfaces we
can adapt this as we come up with a standardized mitigation interface
and rewrite the internals of this and how the QuantumInstance leverages
mitigators to use the new interface.

A good follow-up here would be to adapt the mitigator selection kwarg to
deprecate the use of classes and then we can make things explicitly
private in the migrated code and wait for Qiskit#6495 and Qiskit#6748 to be ready
for our user facing API. I opted to not include that in this PR to
minimize changes to just what we migrated from ignis and update usage of
old ignis classes to rely on the migrated version.
@mtreinish mtreinish force-pushed the integrate-mitigation branch from 148fb4c to 9e26a10 Compare August 4, 2021 15:06
@mtreinish mtreinish added Changelog: Deprecation Include in "Deprecated" section of changelog Changelog: New Feature Include in the "Added" section of the changelog labels Aug 9, 2021
@mtreinish mtreinish added this to the 0.19 milestone Aug 9, 2021
@omarshehab
Copy link

@ShellyGarion , thanks for mentioning this PR in #6980. Am I correct to understand that this PR is yet to be merged and not specifically dated for client release yet?

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I attempted not to comment much on what the code is actually doing, since it's just copied over from Ignis (though I did leave ~two in that vein).

I reviewed by diffing against the Ignis version, so I've commented wherever there's a difference (barring obvious pylint/black changes and functions being pulled from one file to another). A couple of those comments just say "I think this is ok to change, this is just to make sure we changed it deliberately", but a couple have extra stuff about potential issues with the deprecation procedure.

The tests seems to be amalgamated from a couple of places and chopped and changed a bit more, so my diffing didn't work as cleanly. Other than the comment about test inheritance that seems a bit off, the tests appear to be copied faithfully.

The rest of the comments are just typo fixes or comments on the documentation, and I think this will be ok to bring in shortly.

qiskit/utils/measurement_error_mitigation.py Outdated Show resolved Hide resolved
Comment on lines -212 to 228
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

Copy link
Member

Choose a reason for hiding this comment

The 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 (build_measurement_error_mitigation_qobj) as we did in the previous one (build_measurement_error_mitigation_circuits)?

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

qiskit/utils/mitigation/__init__.py Outdated Show resolved Hide resolved
qiskit/utils/mitigation/circuits.py Outdated Show resolved Hide resolved
Comment on lines 52 to 54
from .circuits import complete_meas_cal, tensored_meas_cal
from .filters import MeasurementFilter, TensoredFilter
from .fitters import CompleteMeasFitter, TensoredMeasFitter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that the MeasurementFilter and TensoredFilter classes are meant to be entirely private, and users (and other parts of Qiskit except the two fitters here) shouldn't be creating or instantiating them at all. For example, there's no generated autosummary pages for them. Is it worth renaming their module to _filters.py, and not importing them in this __init__.py to follow Python conventions of public/private access and make the access pattern clear to consumers of Terra who aren't reading the documentation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's not a bad idea. I was trying to avoid the _module.py pattern mostly because in the dark before times (i.e. before I first started working on Qiskit) pretty much every module was _foo.py and the user api was defined as the re-exported classes from the __init__.py (without __all__). See https://github.com/Qiskit/qiskit-terra/tree/0.5.0/qiskit if you'd like to get a more concrete feel for what I'm describing. It's left a bad taste ever since so I just try to avoid it so people aren't tempted to revert to that "model". But here I think it makes sense I'll update this accordingly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, that's quite the structure. It feels like there's valid intent behind it (perhaps attempting to force a canonical import path?), but yeah, that's gone pretty overboard.

test/python/utils/mitigation/test_meas.py Outdated Show resolved Hide resolved
test/python/utils/mitigation/test_meas.py Outdated Show resolved Hide resolved
test/python/utils/mitigation/test_meas.py Outdated Show resolved Hide resolved
Comment on lines +512 to +533
tests = []
runs = 3
for run in range(runs):
cal_results, state_labels, circuit_results = meas_calibration_circ_execution(
1000, SEED + run
)

meas_cal = CompleteMeasFitter(cal_results, state_labels)
meas_filter = MeasurementFilter(meas_cal.cal_matrix, state_labels)

# Calculate the results after mitigation
results_pseudo_inverse = meas_filter.apply(circuit_results, method="pseudo_inverse")
results_least_square = meas_filter.apply(circuit_results, method="least_squares")
tests.append(
{
"cal_matrix": convert_ndarray_to_list_in_data(meas_cal.cal_matrix),
"fidelity": meas_cal.readout_fidelity(),
"results": circuit_results,
"results_pseudo_inverse": results_pseudo_inverse,
"results_least_square": results_least_square,
}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Ignis version of this test, these results are loaded from a saved file. I timed it on my machine, and this whole test takes under a second to run, so I think we're fine to generate the results like this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's an annoying pattern in ignis tests where they use json files committed to the repo for testing the fitters where the generation of the data is quite quick. I tried to remove all of that whenever I encountered it in the tests since it was unnecessary complexity (and git repo bloat).

Comment on lines +597 to +605
saved_info = {
"cal_results": cal_results.to_dict(),
"results": circuit_results.to_dict(),
"mit_pattern": mit_pattern,
"meas_layout": meas_layout,
"fidelity": meas_cal.readout_fidelity(),
"results_pseudo_inverse": results_pseudo_inverse,
"results_least_square": results_least_square,
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, in Ignis this test loads from a known file, but generating the results like this takes well under a second, so I think we're fine.

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy this is a correct import of the code from Ignis, and we're handling exposing it on the Terra side fine. The code itself likely wouldn't pass Terra review, but that's beyond the scope of this PR, since we just need this in place to completely deprecate Ignis.

@kdk kdk merged commit 75c0e46 into Qiskit:main Oct 1, 2021
mtreinish added a commit to mtreinish/qiskit-ignis that referenced this pull request Oct 1, 2021
With the introduction of Qiskit Experiments development on Qiskit Ignis
has pretty much stopped. It's been in maintenance only mode for the past
6-8 months. Now that the only other use of Ignis inside Qiskit has been
worked around with Qiskit/qiskit#6867 nothin depends on Qiskit Ignis
moving forward. This commit officially marks the deprecation of the project
and it will be retired and archived after the deprecation period
finishes.
mtreinish added a commit to mtreinish/qiskit-ignis that referenced this pull request Oct 1, 2021
With the introduction of Qiskit Experiments development on Qiskit Ignis
has pretty much stopped. It's been in maintenance only mode for the past
6-8 months. Now that the only other use of Ignis inside Qiskit has been
worked around with Qiskit/qiskit#6867 nothin depends on Qiskit Ignis
moving forward. This commit officially marks the deprecation of the project
and it will be retired and archived after the deprecation period
finishes.
@mtreinish mtreinish deleted the integrate-mitigation branch October 4, 2021 18:12
ElePT pushed a commit to ElePT/qiskit that referenced this pull request Jun 27, 2023
* Migrate measure mitigation from ignis for QuantumInstance

This commit migrates the measurement mitigation code from qiskit-ignis
into qiskit-terra for use with the QuantumInstance class. The
QuantumInstance class's usage of the measurement mitigation from ignis
is the one thing blocking us from deprecating qiskit-ignis completely.
By embedding the code the quantum instance depends on inside
qiskit.utils users of QuantumInstance (and therefore qiskit.algorithms)
can construct and use measurement mitigation in it's current form. The
use of this migrated module is only supported for use with the
QuantumInstance class and is explicitly documented as internal/private
except for how it gets used by the QuantumInstance.

There is ongoing work to create a standardized mitigation API in Qiskit#6485
and Qiskit#6748, this does not preclude that work, but we should adapt this as
part of those efforts to use the standardized interface. Ideally this
would have been made a private interface and not exposed it as user facing
(in deference to the standardization effort), but unfortunately the
QuantumInstance expects classes of these classes as it's public interface
for selecting a mitigation technique which means users need to be able to
use the classes. However, as only the classes are public interfaces we
can adapt this as we come up with a standardized mitigation interface
and rewrite the internals of this and how the QuantumInstance leverages
mitigators to use the new interface.

A good follow-up here would be to adapt the mitigator selection kwarg to
deprecate the use of classes and then we can make things explicitly
private in the migrated code and wait for Qiskit#6495 and Qiskit#6748 to be ready
for our user facing API. I opted to not include that in this PR to
minimize changes to just what we migrated from ignis and update usage of
old ignis classes to rely on the migrated version.

* Update releasenotes/notes/ignis-mitigators-70492690cbcf99ca.yaml

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

* Finish release notes

* Move API stability warning to the top

* Apply suggestions from code review

Co-authored-by: Jake Lishman <jake@binhbar.com>

* Remove unecessary test subclassing

* Fix skip logic in algorithm mitigation tests

* Fix exception handling on missing ignis in algorithms

* Assert deprecation message for ignis use mentions ignis

* Make filters module properly private

* Fix lint

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: Jake Lishman <jake@binhbar.com>
ElePT pushed a commit to ElePT/qiskit-algorithms-test that referenced this pull request Jul 17, 2023
…kit#6867)

* Migrate measure mitigation from ignis for QuantumInstance

This commit migrates the measurement mitigation code from qiskit-ignis
into qiskit-terra for use with the QuantumInstance class. The
QuantumInstance class's usage of the measurement mitigation from ignis
is the one thing blocking us from deprecating qiskit-ignis completely.
By embedding the code the quantum instance depends on inside
qiskit.utils users of QuantumInstance (and therefore qiskit.algorithms)
can construct and use measurement mitigation in it's current form. The
use of this migrated module is only supported for use with the
QuantumInstance class and is explicitly documented as internal/private
except for how it gets used by the QuantumInstance.

There is ongoing work to create a standardized mitigation API in Qiskit/qiskit#6485
and Qiskit/qiskit#6748, this does not preclude that work, but we should adapt this as
part of those efforts to use the standardized interface. Ideally this
would have been made a private interface and not exposed it as user facing
(in deference to the standardization effort), but unfortunately the
QuantumInstance expects classes of these classes as it's public interface
for selecting a mitigation technique which means users need to be able to
use the classes. However, as only the classes are public interfaces we
can adapt this as we come up with a standardized mitigation interface
and rewrite the internals of this and how the QuantumInstance leverages
mitigators to use the new interface.

A good follow-up here would be to adapt the mitigator selection kwarg to
deprecate the use of classes and then we can make things explicitly
private in the migrated code and wait for Qiskit/qiskit#6495 and Qiskit/qiskit#6748 to be ready
for our user facing API. I opted to not include that in this PR to
minimize changes to just what we migrated from ignis and update usage of
old ignis classes to rely on the migrated version.

* Update releasenotes/notes/ignis-mitigators-70492690cbcf99ca.yaml

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

* Finish release notes

* Move API stability warning to the top

* Apply suggestions from code review

Co-authored-by: Jake Lishman <jake@binhbar.com>

* Remove unecessary test subclassing

* Fix skip logic in algorithm mitigation tests

* Fix exception handling on missing ignis in algorithms

* Assert deprecation message for ignis use mentions ignis

* Make filters module properly private

* Fix lint

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: Jake Lishman <jake@binhbar.com>
ElePT pushed a commit to ElePT/qiskit-algorithms that referenced this pull request Jul 27, 2023
…kit#6867)

* Migrate measure mitigation from ignis for QuantumInstance

This commit migrates the measurement mitigation code from qiskit-ignis
into qiskit-terra for use with the QuantumInstance class. The
QuantumInstance class's usage of the measurement mitigation from ignis
is the one thing blocking us from deprecating qiskit-ignis completely.
By embedding the code the quantum instance depends on inside
qiskit.utils users of QuantumInstance (and therefore qiskit.algorithms)
can construct and use measurement mitigation in it's current form. The
use of this migrated module is only supported for use with the
QuantumInstance class and is explicitly documented as internal/private
except for how it gets used by the QuantumInstance.

There is ongoing work to create a standardized mitigation API in Qiskit/qiskit#6485
and Qiskit/qiskit#6748, this does not preclude that work, but we should adapt this as
part of those efforts to use the standardized interface. Ideally this
would have been made a private interface and not exposed it as user facing
(in deference to the standardization effort), but unfortunately the
QuantumInstance expects classes of these classes as it's public interface
for selecting a mitigation technique which means users need to be able to
use the classes. However, as only the classes are public interfaces we
can adapt this as we come up with a standardized mitigation interface
and rewrite the internals of this and how the QuantumInstance leverages
mitigators to use the new interface.

A good follow-up here would be to adapt the mitigator selection kwarg to
deprecate the use of classes and then we can make things explicitly
private in the migrated code and wait for Qiskit/qiskit#6495 and Qiskit/qiskit#6748 to be ready
for our user facing API. I opted to not include that in this PR to
minimize changes to just what we migrated from ignis and update usage of
old ignis classes to rely on the migrated version.

* Update releasenotes/notes/ignis-mitigators-70492690cbcf99ca.yaml

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>

* Finish release notes

* Move API stability warning to the top

* Apply suggestions from code review

Co-authored-by: Jake Lishman <jake@binhbar.com>

* Remove unecessary test subclassing

* Fix skip logic in algorithm mitigation tests

* Fix exception handling on missing ignis in algorithms

* Assert deprecation message for ignis use mentions ignis

* Make filters module properly private

* Fix lint

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Co-authored-by: Jake Lishman <jake@binhbar.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Deprecation Include in "Deprecated" section of changelog Changelog: New Feature Include in the "Added" section of the changelog priority: high
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants