Skip to content

Commit

Permalink
Missing circuit metadata in PubResult from SamplerV2 (#1596)
Browse files Browse the repository at this point in the history
* included the circuit metadata to PubResult

* test metadata in PubResult

* style fixes

* removed debug print

---------

Co-authored-by: Kevin Tian <[email protected]>
  • Loading branch information
ptristan3 and kt474 authored Apr 15, 2024
1 parent 4af7729 commit be51a45
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit_ibm_runtime/qiskit/primitives/backend_sampler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def _run_pub(self, pub: SamplerPub) -> PubResult:
item.creg_name: BitArray(arrays[item.creg_name], item.num_bits) for item in meas_info
}
data_bin = data_bin_cls(**meas)
return PubResult(data_bin, metadata={})
return PubResult(data_bin, metadata=pub.circuit.metadata)


def _analyze_circuit(circuit: QuantumCircuit) -> tuple[list[_MeasureInfo], int]:
Expand Down
18 changes: 18 additions & 0 deletions test/unit/qiskit/test_backend_sampler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ def test_sampler_run(self, backend):
self.assertIsInstance(result[0].data.meas, BitArray)
self._assert_allclose(result[0].data.meas, np.array([target, target, target]))

with self.subTest("with circuit metadata"):
sample_metadata = {
"user_metadata_field_1": "metadata_1",
"user_metadata_field_2": "metadata_2",
}
pqc, _, _ = self._cases[1]
pqc.metadata = sample_metadata
sampler = BackendSamplerV2(backend=backend, options=self._options)
pqc = pm.run(pqc)
job = sampler.run([pqc], shots=self._shots)
result = job.result()
self.assertIsInstance(result, PrimitiveResult)
self.assertIsInstance(result.metadata, dict)
self.assertEqual(len(result), 1)
self.assertIsInstance(result[0], PubResult)
self.assertIsInstance(result[0].metadata, dict)
self.assertEqual(result[0].metadata, sample_metadata)

@combine(backend=BACKENDS)
def test_sampler_run_multiple_times(self, backend):
"""Test run() returns the same results if the same input is given."""
Expand Down

0 comments on commit be51a45

Please sign in to comment.