Skip to content

Commit

Permalink
Fix shots argument of local_readout_mitigator (#9155)
Browse files Browse the repository at this point in the history
* fix shots argument of local_readout_mitigator

* fix correlated_readout_mitigator as well

* add test

* docstring

* black

* lint

* add release notes

* Fix up release note

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
  • Loading branch information
3 people committed Nov 24, 2022
1 parent c063c91 commit 11eb891
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
9 changes: 6 additions & 3 deletions qiskit/result/mitigation/correlated_readout_mitigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,16 @@ def quasi_probabilities(
data: Counts,
qubits: Optional[List[int]] = None,
clbits: Optional[List[int]] = None,
shots: Optional[bool] = False,
shots: Optional[int] = None,
) -> QuasiDistribution:
"""Compute mitigated quasi probabilities value.
Args:
data: counts object
qubits: qubits the count bitstrings correspond to.
clbits: Optional, marginalize counts to just these bits.
shots: the number of shots.
shots: Optional, the total number of shots, if None shots will
be calculated as the sum of all counts.
Returns:
QuasiDistibution: A dictionary containing pairs of [output, mean] where "output"
Expand All @@ -151,9 +152,11 @@ def quasi_probabilities(
"""
if qubits is None:
qubits = self._qubits
probs_vec, shots = counts_probability_vector(
probs_vec, calculated_shots = counts_probability_vector(
data, qubit_index=self._qubit_index, clbits=clbits, qubits=qubits
)
if shots is None:
shots = calculated_shots

# Get qubit mitigation matrix and mitigate probs
mit_mat = self.mitigation_matrix(qubits)
Expand Down
11 changes: 7 additions & 4 deletions qiskit/result/mitigation/local_readout_mitigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,16 @@ def quasi_probabilities(
data: Counts,
qubits: Optional[List[int]] = None,
clbits: Optional[List[int]] = None,
shots: Optional[bool] = False,
shots: Optional[int] = None,
) -> QuasiDistribution:
"""Compute mitigated quasi probabilities value.
Args:
data: counts object
qubits: qubits the count bitstrings correspond to.
clbits: Optional, marginalize counts to just these bits.
shots: the number of shots.
shots: Optional, the total number of shots, if None shots will
be calculated as the sum of all counts.
Returns:
QuasiDistibution: A dictionary containing pairs of [output, mean] where "output"
Expand All @@ -193,9 +194,11 @@ def quasi_probabilities(

num_qubits = len(qubits)

probs_vec, shots = counts_probability_vector(
probs_vec, calculated_shots = counts_probability_vector(
data, qubit_index=self._qubit_index, clbits=clbits, qubits=qubits
)
if shots is None:
shots = calculated_shots

# Get qubit mitigation matrix and mitigate probs
qubit_indices = [self._qubit_index[qubit] for qubit in qubits]
Expand All @@ -214,7 +217,7 @@ def quasi_probabilities(
probs_dict[index] = probs_vec[index]

quasi_dist = QuasiDistribution(
probs_dict, stddev_upper_bound=self.stddev_upper_bound(shots, qubits)
probs_dict, shots=shots, stddev_upper_bound=self.stddev_upper_bound(shots, qubits)
)
return quasi_dist

Expand Down
1 change: 1 addition & 0 deletions qiskit/result/mitigation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def counts_probability_vector(
Returns:
np.ndarray: a probability vector for all count outcomes.
int: Number of shots in the counts
"""
counts = marganalize_counts(counts, qubit_index, qubits, clbits)
if qubits is not None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed an issue with the :meth:`.LocalReadoutMitigator.quasi_probabilities` method where
the ``shots`` argument was not used. It is now used to set the number of shots
in the return object.
14 changes: 14 additions & 0 deletions test/python/result/test_mitigators.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,20 @@ def test_expectation_value_endian(self):
expval, _ = mitigator.expectation_value(counts, diagonal="IZ", qubits=[0, 1])
self.assertAlmostEqual(expval, -1.0, places=0)

def test_quasi_probabilities_shots_passing(self):
"""Test output of LocalReadoutMitigator.quasi_probabilities
We require the number of shots to be set in the output.
"""
mitigator = LocalReadoutMitigator([np.array([[0.9, 0.1], [0.1, 0.9]])], qubits=[0])
counts = Counts({"10": 3, "11": 24, "00": 74, "01": 923})
quasi_dist = mitigator.quasi_probabilities(counts)
self.assertEqual(quasi_dist.shots, sum(counts.values()))

# custom number of shots
quasi_dist = mitigator.quasi_probabilities(counts, shots=1025)
self.assertEqual(quasi_dist.shots, 1025)


class TestLocalReadoutMitigation(QiskitTestCase):
"""Tests specific to the local readout mitigator"""
Expand Down

0 comments on commit 11eb891

Please sign in to comment.