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

Annotate and clarify that metadata keys should be strings (backport #11736) #11738

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion qiskit/primitives/containers/primitive_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def __init__(self, pub_results: Iterable[T], metadata: dict[str, Any] | None = N
"""
Args:
pub_results: Pub results.
metadata: Any metadata that doesn't make sense to put inside of pub results.
metadata: Metadata that is common to all pub results; metadata specific to particular
pubs should be placed in their metadata fields. Keys are expected to be strings.
"""
self._pub_results = list(pub_results)
self._metadata = metadata or {}
Expand Down
8 changes: 5 additions & 3 deletions qiskit/primitives/containers/pub_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from __future__ import annotations

from typing import Any

from .data_bin import DataBin


Expand All @@ -24,12 +26,12 @@ class PubResult:

__slots__ = ("_data", "_metadata")

def __init__(self, data: DataBin, metadata: dict | None = None):
def __init__(self, data: DataBin, metadata: dict[str, Any] | None = None):
"""Initialize a pub result.

Args:
data: result data bin.
metadata: metadata dictionary.
data: Result data.
metadata: Metadata specific to this pub. Keys are expected to be strings.
"""
self._data = data
self._metadata = metadata or {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ def test_primitive_result(self):
PubResult(data_bin_cls(alpha, beta)),
PubResult(data_bin_cls(alpha, beta)),
]
result = PrimitiveResult(pub_results, {1: 2})
result = PrimitiveResult(pub_results, {"x": 2})

self.assertTrue(result[0] is pub_results[0])
self.assertTrue(result[1] is pub_results[1])
self.assertTrue(list(result)[0] is pub_results[0])
self.assertEqual(len(result), 2)
self.assertEqual(result.metadata, {"x": 2})
Loading