From 9060b7f0a76242cc720f85677e1dc62bcb436c0a Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 31 May 2022 14:29:37 -0400 Subject: [PATCH 1/2] Leverage rust marginalization function for marginal_counts() In #8026 we added a new marginalization function marginal_distribution() which was a standalone marginalization function for counts and distribution dictionaries. The core of that new function was written in rust for performance. In that PR we didn't use the rust core because it's behavior was slightly different especially around the order of indices. This commit reworks the internals of the marginal_counts() function to leverage the same rust core function to ensure the ordering is consistent after the migration the index sort is changed. --- qiskit/result/utils.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/qiskit/result/utils.py b/qiskit/result/utils.py index c26a5fec42a2..1a39b64dee69 100644 --- a/qiskit/result/utils.py +++ b/qiskit/result/utils.py @@ -68,9 +68,7 @@ def marginal_counts( for i, experiment_result in enumerate(result.results): counts = result.get_counts(i) new_counts = _marginalize(counts, indices) - new_counts_hex = {} - for k, v in new_counts.items(): - new_counts_hex[_bin_to_hex(k)] = v + new_counts_hex = {_bin_to_hex(k): v for k, v in new_counts.items()} experiment_result.data.counts = new_counts_hex if indices is not None: @@ -189,14 +187,7 @@ def _marginalize(counts, indices=None): # Sort the indices to keep in descending order # Since bitstrings have qubit-0 as least significant bit - indices = sorted(indices, reverse=True) - - # Build the return list - new_counts = Counter() - for key, val in counts.items(): - new_key = "".join([_remove_space_underscore(key)[-idx - 1] for idx in indices]) - new_counts[new_key] += val - return dict(new_counts) + return results_rs.marginal_counts(counts, sorted(indices)) def _format_marginal(counts, marg_counts, indices): From b1ad0405ce94b7f915b8b0f2910faf2efb5bf16a Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 6 Jun 2022 09:11:55 -0400 Subject: [PATCH 2/2] Remove unused import statement --- qiskit/result/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/qiskit/result/utils.py b/qiskit/result/utils.py index 1a39b64dee69..8a56dde0a2d6 100644 --- a/qiskit/result/utils.py +++ b/qiskit/result/utils.py @@ -15,7 +15,6 @@ """Utility functions for working with Results.""" from typing import List, Union, Optional, Dict -from collections import Counter from copy import deepcopy from qiskit.exceptions import QiskitError