Skip to content

Conversation

ShellyGarion
Copy link
Member

@ShellyGarion ShellyGarion commented May 30, 2021

Summary

#6484

Add Readout Mitigator classes bases on the current code in ignis.

Details and comments

  • BaseReadoutMitgator
  • CorrelatedReadoutMitigator
  • LocalReadoutMitigator
  • Update qiskit.result classes only if necessary (Counts, ProbDitribution, QuasiDistribution)
  • Add tests

co-authored by @gadial

@ShellyGarion ShellyGarion requested review from chriseclectic and a team as code owners May 30, 2021 09:32
@ShellyGarion ShellyGarion added mod: quantum info Related to the Quantum Info module (States & Operators) Changelog: New Feature Include in the "Added" section of the changelog labels May 30, 2021
@ShellyGarion ShellyGarion changed the title Readout mitigator classes [WIP] Readout mitigator classes May 30, 2021
@@ -123,3 +123,11 @@ def hex_probabilities(self):
format ``"0x1a"``
"""
return {hex(key): value for key, value in self.items()}

def stddev(self, shots):
Copy link
Contributor

Choose a reason for hiding this comment

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

This does not work here. The runtime mitigation method will soon return the mitigation_overhead that, together with the number of shots, gives an estimate (usually exact) of this value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you @nonhermitian for the feedback! So where should I add the stddev function?
Should I add it to the ReadoutMitigator class?

Copy link
Contributor

Choose a reason for hiding this comment

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

It makes sense but needs to be computed differently.

@nonhermitian
Copy link
Contributor

Here is the basic way M3 works:

# Grab a mitigator instance and target a given backend
mit = mthree.M3Mitigation(backend)

# calibrate over the desired number of qubits (can be renamed to something more generic)
mit.tensored_cals_from_system(qubits)

# apply correction
quasi_probs = mit.apply_correction(real_counts, qubits=measured_qubits)

@nonhermitian
Copy link
Contributor

The M3 code is up here #6748. So thoughts on this PR:

I think the mitigation classes should be simpler. Namely, the bare minimum one needs is:

mit = Mitigator(backend)
mit.calibrations_from_system(qubits)
mitigated_values = mit.apply_correction(raw_counts, qubits, **kwargs)

The mitigator itself should in charge of executing the calibration circuits because at some point the backends should just compute this info on a regular basis and cache it at which point calibrations_from_system might just be an API call, or another method like calibrations_from_remote may exist.

Things like plotting the A-matrix should not be here, just simply return an array for the given matrix.

Returning the mitigation matrix should not be in the base class as computing it is optional depending on the solution method.

I am also not a big fan of having expectation_value attached to the mitigator as it is not really a property of the mitigator but rather of the return values and additional info attached to them. I do however understand that the CTMP works this was, but, in my opinion, that should be refactored to return quasi-probs like the others do.

mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Aug 4, 2021
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.
@chriseclectic chriseclectic added this to the 0.19 milestone Aug 24, 2021
@ShellyGarion
Copy link
Member Author

@nonhermitian @chriseclectic @mtreinish - I updated this PR following your comments and discussions.
Now, the BaseReadoutMitgator class contains only 2 methods: quasi_probabilities and expectation_value.
I also didn't change the qiskit.result classes, except of adding the methods num_qubits and shots to Counts, which I think are useful (but can be removed if you don't like them).
Could you please review the code and let me know if you agree with the design of the base class (which should also be relevant for the M3Mitigation)?

Copy link
Member

@chriseclectic chriseclectic left a comment

Choose a reason for hiding this comment

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

Some initial comments

_HAS_MATPLOTLIB = False


class BaseReadoutMitigator(ABC):
Copy link
Member

Choose a reason for hiding this comment

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

We might want to include something in the base class for what physical qubits it is defined on.

Copy link
Member Author

Choose a reason for hiding this comment

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

could you please elaborate?


# Compute variance
sq_expval = (coeffs ** 2).dot(probs)
variance = (sq_expval - expval ** 2) / shots
Copy link
Member

Choose a reason for hiding this comment

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

I think this method under-estimates error since it doesn't take into account the mitigation overhead from the CTMP paper. In that paper they use an upper bound for the variance based on the A-matrix which comes from uncertainty in the estimates of the true error probabilities.

Copy link
Member Author

Choose a reason for hiding this comment

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

Does the CTMP mitigation overhead estimator work for all mitigation methods? If so - perhaps we should add mitigation_overhead to the base class?

Copy link
Member Author

Choose a reason for hiding this comment

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

I took the code for the complete mitigator method from the previous code in ignis - does it mean that the code there is not accurate enough?
https://github.com/Qiskit/qiskit-ignis/blob/f0728b6b6785b68693a0d31c54c51f9826ddde34/qiskit/ignis/mitigation/expval/utils.py#L244-#L246

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

so perhaps we could return only gamma and not the entire expectation_value ?

Copy link
Member Author

@ShellyGarion ShellyGarion Oct 12, 2021

Choose a reason for hiding this comment

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

I added the functions _stddev_upper_bound and _gamma


def quasi_probabilities(
self, data: Counts, qubits: Iterable[int] = None, shots: Optional[int] = None
) -> (QuasiDistribution, Dict[str, float]):
Copy link
Member Author

@ShellyGarion ShellyGarion Sep 5, 2021

Choose a reason for hiding this comment

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

@chriseclectic - Is it better to return the standard deviation also as a QuasiDistribution object (instead of a Dict)?

else:
qubits = tuple(sorted(qubits))

# Check for cached mitigation matrix
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use a built-in Python caching mechanism such as the @lru_cache decorator?

Copy link
Member Author

Choose a reason for hiding this comment

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

Should discuss where we can store the cached mitigation matrix(es)

kdk pushed a commit that referenced this pull request Oct 1, 2021
* 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 #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.

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.

* 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>
@ShellyGarion
Copy link
Member Author

ShellyGarion commented Oct 6, 2021

@ShellyGarion ShellyGarion force-pushed the readout_mitigator branch 3 times, most recently from ac638fd to 6549830 Compare October 11, 2021 11:48
@mergify mergify bot merged commit ae55844 into Qiskit:main Dec 2, 2021
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: New Feature Include in the "Added" section of the changelog mod: quantum info Related to the Quantum Info module (States & Operators)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants