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

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

Merged
merged 2 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions qiskit/result/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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>`__
10 changes: 10 additions & 0 deletions test/python/result/test_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import unittest

import numpy as np

from qiskit.result import counts
from qiskit import exceptions
from qiskit.result import utils
Expand Down Expand Up @@ -50,6 +52,14 @@ def test_marginal_distribution(self):
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}
Expand Down