diff --git a/qiskit_ibm_runtime/qiskit/primitives/backend_sampler_v2.py b/qiskit_ibm_runtime/qiskit/primitives/backend_sampler_v2.py index 31d213ad3..1547c82e2 100644 --- a/qiskit_ibm_runtime/qiskit/primitives/backend_sampler_v2.py +++ b/qiskit_ibm_runtime/qiskit/primitives/backend_sampler_v2.py @@ -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]: diff --git a/test/unit/qiskit/test_backend_sampler_v2.py b/test/unit/qiskit/test_backend_sampler_v2.py index 6a1e27767..1b7ab828b 100644 --- a/test/unit/qiskit/test_backend_sampler_v2.py +++ b/test/unit/qiskit/test_backend_sampler_v2.py @@ -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."""