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

Leverage rust marginalization function for marginal_counts() #8122

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
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
14 changes: 2 additions & 12 deletions qiskit/result/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,9 +67,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:
Expand Down Expand Up @@ -190,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))
Comment on lines 188 to +190
Copy link
Member

Choose a reason for hiding this comment

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

The comment probably wants removing as well, I guess?

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 left the comment in place because it just applies to the inline sorted(indices) instead of doing indices = sorted(indices, reverse=True). They're doing the same functional thing to preserve the independence of index of interest ordering from the output (basically don't permute the bits if you did something like [1, 0]).



def _format_marginal(counts, marg_counts, indices):
Expand Down