Fix handling of numpy arrays for indices in marginal_distribution (#8288)

* Fix handling of numpy arrays for indices in marginal_distribution

This commit fixes an issue with the marginal_distribution() function
where it would previously error if a numpy arrray of ints was passed in
for the indices parameter. This was caused by the input validation
looking for empty input by checking `not indices` which isn't valid for
numpy arrays. This commit fixes this by adjusting the check for
empty lists to work with any sequence input.

Fixes #8283

* Update releasenotes/notes/fix-numpy-indices-marginal-dist-45889e49ba337d84.yaml

Co-authored-by: Jim Garrison <jim@garrison.cc>

Co-authored-by: Jim Garrison <jim@garrison.cc>
This commit is contained in:
Matthew Treinish 2022-07-25 11:12:46 -04:00 committed by GitHub
parent aa6d359612
commit f8681295dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

View File

@ -14,7 +14,7 @@
"""Utility functions for working with Results."""
from typing import List, Union, Optional, Dict
from typing import Sequence, Union, Optional, Dict, List
from collections import Counter
from copy import deepcopy
@ -199,7 +199,9 @@ def marginal_memory(
def marginal_distribution(
counts: dict, indices: Optional[List[int]] = None, format_marginal: bool = False
counts: dict,
indices: Optional[Sequence[int]] = None,
format_marginal: bool = False,
) -> Dict[str, int]:
"""Marginalize counts from an experiment over some indices of interest.
@ -222,7 +224,7 @@ def marginal_distribution(
is invalid.
"""
num_clbits = len(max(counts.keys()).replace(" ", ""))
if indices is not None and (not indices or not set(indices).issubset(range(num_clbits))):
if indices is not None and (len(indices) == 0 or not set(indices).issubset(range(num_clbits))):
raise QiskitError(f"indices must be in range [0, {num_clbits - 1}].")
if isinstance(counts, Counts):

View File

@ -0,0 +1,7 @@
---
fixes:
- |
Fixed an issue with the :func:`~.marginal_distribution` function: when
a numpy array was passed in for the ``indices`` argument the function would
raise an error.
Fixed `#8283 <https://github.com/Qiskit/qiskit-terra/issues/8283>`__

View File

@ -16,6 +16,8 @@
import unittest
import numpy as np
from qiskit.result import counts
from qiskit import exceptions
from qiskit.result import utils
@ -50,6 +52,14 @@ class TestCounts(unittest.TestCase):
result = utils.marginal_distribution(counts_obj, [0, 1])
self.assertEqual(expected, result)
def test_marginal_distribution_numpy_indices(self):
raw_counts = {"0x0": 4, "0x1": 7, "0x2": 10, "0x6": 5, "0x9": 11, "0xD": 9, "0xE": 8}
expected = {"00": 4, "01": 27, "10": 23}
indices = np.asarray([0, 1])
counts_obj = counts.Counts(raw_counts, creg_sizes=[["c0", 4]], memory_slots=4)
result = utils.marginal_distribution(counts_obj, indices)
self.assertEqual(expected, result)
def test_int_outcomes(self):
raw_counts = {"0x0": 21, "0x2": 12, "0x3": 5, "0x2E": 265}
expected = {0: 21, 2: 12, 3: 5, 46: 265}