Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.09k stars 2.34k forks source link

Qobj header from a job doesn't return the info passed to `execute` function #7149

Closed HuangJunye closed 2 years ago

HuangJunye commented 2 years ago

Information

What is the current behavior?

Qobj header from a job submitted to cloud simulator / local Aer simulator / real quantum system doesn't return the same info passed to qobj_header argument in the execute function, even though the documentation states the following:

qobj_header (QobjHeader or dict) – User input that will be inserted in Qobj header, and will also be copied to the corresponding qiskit.result.Result header. Headers do not affect the run.

What could be the issues?

Steps to reproduce the problem

from qiskit import IBMQ, QuantumCircuit, execute
from qiskit.circuit.random import random_circuit

provider = IBMQ.load_account()
backend = provider.get_backend('ibmq_qasm_simulator')
qc = random_circuit(3, 3)

## https://qiskit.org/documentation/apidoc/execute.html

test_job = execute(
    qc,
    qobj_header={
        'test_header': 'test'
    },
    backend=backend
)

test_job.result().header.to_dict()

returns

{'backend_name': 'ibmq_qasm_simulator', 'backend_version': '0.1.547'}

Same behaviour for Aer QasmSimulator.

backend = QasmSimulator()
backend = provider.get_backend('ibmq_belem')

What is the expected behavior?

The code above should return info passed to the execute function.

{'backend_name': 'ibmq_qasm_simulator', 'backend_version': '0.1.547', 'test_header': 'test'}

Suggested solutions

mtreinish commented 2 years ago

This is because qobj_header is no longer an option on those backends. WIth the migration to BackendV1 backend.run(), which is what execute calls internally, on backends only takes explicit options the backends expose. The explicit qobj_header field for execute() only works with legacy backends that are built off of the deprecated BaseBackend class which used qobj as a payload for backend.run(). You no longer need to set this field to attach metadata to a circuit as this is supported natively by QuantumCircuit since 0.17. To accomplish this with you should be running something like:

from qiskit import IBMQ, QuantumCircuit, execute
from qiskit.circuit.random import random_circuit

provider = IBMQ.load_account()
backend = provider.get_backend('ibmq_qasm_simulator')
qc = random_circuit(3, 3)
qc.metadata = {'test_header': 'test'}
test_job = execute(qc, backend=backend)

of course this attaches the metadata to the circuit and will be in the corresponding experiment header (not the job header) under the metadata field in the output result.

We should update the documentation to outline that the qobj_header kwarg along with a bunch of other options for execute() only work with legacy backend objects.

HuangJunye commented 2 years ago

@mtreinish Thanks for the reply! That's really helpful. We will use the circuit metadata.