Metadata handling of SamplerV2 with fake backend does not match behaviour with real backend
Describe the bug
Metadata passed through jobs run with SamplerV2 on a fake backend (or AerSimulator) is in a different format to that received from SamplerV2 on real backends.
After issue #1551 and PR #1596 , circuit metadata is retained in the PrimitiveResult, but it lives in the top-level metadata attribute, and not the circuit_metadata key within this attribute as is the case with real backends.
Steps to reproduce
Run a simple circuit with metadata attached on fake backend and real backend using SamplerV2 primitive:
from qiskit.circuit import QuantumCircuit
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import SamplerV2 as Sampler, QiskitRuntimeService
from qiskit_ibm_runtime.fake_provider import FakeBrisbane
# Bell Circuit
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()
qc.metadata = {0: 1}
# Run the sampler job locally using FakeBrisbane
fake_brisbane = FakeBrisbane()
pm = generate_preset_pass_manager(backend=fake_brisbane, optimization_level=0)
isa_qc = pm.run(qc)
print(f'circuit metadata: {isa_qc.metadata}')
# You can use a fixed seed to get fixed results.
options = {"simulator": {"seed_simulator": 42}}
sampler = Sampler(backend=fake_manila, options=options)
result = sampler.run([isa_qc]).result()
print(f'fake result metadata: {result[0].metadata}')
# Run on real device and compare metadata passthrough
service = QiskitRuntimeService()
backend = service.get_backend("ibm_brisbane")
sampler = Sampler(backend=backend)
job = sampler.run([isa_qc])
result = job.result(timeout=300)
print(f'real result metadata: {result[0].metadata}')
circuit metadata: {0: 1}
fake result metadata: {0: 1}
real result metadata: {'circuit_metadata': {'0': 1}}
Expected behavior
Fake and real result metadata dictionaries should be the same, with the injected circuit metadata living in the circuit_metadata key.
Suggested solutions Modify changes introduced in #1596 to place the metadata:
metadata={"circuit_metadata" : pub.circuit.metadata}
Additional Information
- qiskit-ibm-runtime version: 0.23.0
- Python version: 3.10.14
- Operating system: macOS Sonoma 14.5