Qiskit / qiskit-ibm-runtime

IBM Client for Qiskit Runtime
https://docs.quantum.ibm.com/api/qiskit-ibm-runtime
Apache License 2.0
149 stars 154 forks source link

job tags not passed properly when using Sampler or Estimator #618

Closed cpkurotori closed 1 year ago

cpkurotori commented 1 year ago

Describe the bug

There doesn't seem to be a way to pass job tags when using Sampler or Estimator objects.

Steps to reproduce

instantiate Sampler() or Estimator() and try to run the job while passing job tags.

Expected behavior

there should be a way to create sample/estimator jobs with job tags using SDK interfaces

Suggested solutions

job tags should be passed in like they are in options for service.run() or session.run()

Additional Information

import logging

from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Session, Estimator
from qiskit.test.reference_circuits import ReferenceCircuits
from qiskit.quantum_info import SparsePauliOp

logging.getLogger("root").setLevel(logging.DEBUG)
service = QiskitRuntimeService(name="cloud_stage_lite")

program = "sampler"
backend = "ibmq_qasm_simulator"
options = {"job_tags": ["my-tag"]}

with Session(service=service, backend=backend) as session:
    sampler = Sampler(session=session)
    job = sampler.run(circuits=ReferenceCircuits.bell(), options=options)
    print(f"Job ID using Sampler: {job.job_id()}")

with Session(service=service, backend=backend) as session:
    estimator = Estimator(session=session)
    observable = SparsePauliOp("XZ")
    job = estimator.run(circuits=ReferenceCircuits.bell(), observables=observable, options=options)
    print(f"Job ID using Estimator: {job.job_id()}")

with Session(service=service, backend=backend) as session:
    job = session.run(
        program_id="sampler",
        inputs={"circuits": ReferenceCircuits.bell(), "circuit_indices": [0]},
        options=options,
    )
    print(f"Job ID using service: {job.job_id()}")
Job ID using Sampler: cdq0q5vogrb7m83aqbn0
Job ID using Estimator: cdq0q622r27ic2msqiug
Job ID using service: cdq0q6393ot6pp11sahg
GET https://us-east.quantum-computing.test.cloud.ibm.com/jobs/cdq0q5vogrb7m83aqbn0

HTTP/2 200
date: Tue, 15 Nov 2022 22:06:49 GMT
content-type: application/json; charset=utf-8
content-length: 1038
correlation-id: cdq0qub93ot6pp11saj0
x-envoy-upstream-service-time: 18
content-security-policy: default-src 'self' https://cdnjs.cloudflare.com data:; frame-ancestors 'none'; form-action 'self'
strict-transport-security: max-age=15724800; includeSubDomains
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1
cf-cache-status: DYNAMIC
server: cloudflare
cf-ray: 76ab4bd66adb96ab-SJC

{
  "id": "cdq0q5vogrb7m83aqbn0",
  "backend": "ibmq_qasm_simulator",
  "state": {
    "status": "Completed"
  },
  "params": {
    "transpilation_settings": {
      "basis_gates": null,
      "coupling_map": null,
      "layout_method": null,
      "initial_layout": null,
      "routing_method": null,
      "skip_transpilation": false,
      "approximation_degree": null,
      "optimization_settings": {
        "level": 3
      }
    },
    "options": {
      "job_tags": [
        "my-tag"
      ]
    },
    "circuits": [
      {
        "__type__": "QuantumCircuit",
        "__value__": "eJwL9Az29gxhZRBjYoAAxkIGlkwGDiCLCYpBgAWZnZSakwNlM+SV5uQUMoJlmRgLixhQAWMyXCoZXQrOYsUQQQUe7oklqYVgJhtUiAlTFUg3o3MEXG0hWIwdWRaL2b6picWlRRAdEBeSoIMxGaIGAFUYFYQ="
      }
    ],
    "parameters": [
      []
    ],
    "run_options": {
      "shots": 4000,
      "init_qubits": true,
      "noise_model": null,
      "seed_simulator": null
    },
    "circuit_indices": [
      0
    ],
    "parameter_values": [
      []
    ],
    "resilience_settings": {
      "level": 1,
      "extrapolator": "LinearExtrapolator",
      "noise_factors": [
        1,
        3,
        5
      ],
      "noise_amplifier": "TwoQubitAmplifier"
    }
  },
  "program": {
    "id": "sampler"
  },
  "created": "2022-11-15T22:05:11.018065Z",
  "runtime": "ntc-provider-primitives:latest",
  "session_id": "cdq0q5vogrb7m83aqbn0",
  "cost": 10000,
  "status": "Completed"
}

GET https://us-east.quantum-computing.test.cloud.ibm.com/jobs/cdq0q622r27ic2msqiug

HTTP/2 200
date: Tue, 15 Nov 2022 22:07:00 GMT
content-type: application/json; charset=utf-8
content-length: 1522
correlation-id: cdq0r1393ot6pp11sajg
x-envoy-upstream-service-time: 17
content-security-policy: default-src 'self' https://cdnjs.cloudflare.com data:; frame-ancestors 'none'; form-action 'self'
strict-transport-security: max-age=15724800; includeSubDomains
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1
cf-cache-status: DYNAMIC
server: cloudflare
cf-ray: 76ab4c1d4ba0cee1-SJC

{
  "id": "cdq0q622r27ic2msqiug",
  "backend": "ibmq_qasm_simulator",
  "state": {
    "status": "Failed"
  },
  "params": {
    "circuit_indices": [
      0
    ],
    "parameter_values": [
      []
    ],
    "circuits": [
      {
        "__type__": "QuantumCircuit",
        "__value__": "eJwL9Az29gxhZRBjYoAAxkIGlkwGDiCLCYpBgAWZnZSakwNlM+SV5uQUMoJlmRgLixhQAWMyXCoZXQrOYsUQQQUe7oklqYVgJhtUiAlTFUg3o3MEXG0hWIwdWRaL2b6picWlRRAdEBeSoIMxGaIGAFUYFYQ="
      }
    ],
    "parameters": [
      []
    ],
    "run_options": {
      "init_qubits": true,
      "noise_model": null,
      "seed_simulator": null,
      "shots": 4000
    },
    "observable_indices": [
      0
    ],
    "resilience_settings": {
      "level": 1,
      "extrapolator": "LinearExtrapolator",
      "noise_factors": [
        1,
        3,
        5
      ],
      "noise_amplifier": "TwoQubitAmplifier"
    },
    "transpilation_settings": {
      "routing_method": null,
      "skip_transpilation": false,
      "approximation_degree": null,
      "optimization_settings": {
        "level": 3
      },
      "basis_gates": null,
      "coupling_map": null,
      "layout_method": null,
      "initial_layout": null
    },
    "options": {
      "job_tags": [
        "my-tag"
      ]
    },
    "observables": [
      {
        "__type__": "settings",
        "__class__": "SparsePauliOp",
        "__value__": {
          "data": {
            "__type__": "settings",
            "__class__": "PauliList",
            "__value__": {
              "data": [
                "XZ"
              ]
            },
            "__module__": "qiskit.quantum_info.operators.symplectic.pauli_list"
          },
          "coeffs": {
            "__type__": "ndarray",
            "__value__": "eJyb7BfqGxDJyFDGUK2eklqcXKRupaBuk2xopq6joJ6WX1RSlJgXn1+UkgqScEvMKU4FihdnJBakAvkahjqaOgq1CuQDLgYw+GDPAAUAE2Uclw=="
          }
        },
        "__module__": "qiskit.quantum_info.operators.symplectic.sparse_pauli_op"
      }
    ]
  },
  "program": {
    "id": "estimator"
  },
  "created": "2022-11-15T22:05:12.405166Z",
  "runtime": "ntc-provider-primitives:latest",
  "session_id": "cdq0q622r27ic2msqiug",
  "cost": 10800,
  "status": "Failed"
}

GET https://us-east.quantum-computing.test.cloud.ibm.com/jobs/cdq0q6393ot6pp11sahg

HTTP/2 200
date: Tue, 15 Nov 2022 22:07:20 GMT
content-type: application/json; charset=utf-8
content-length: 535
correlation-id: cdq0r622r27ic2msqivg
x-envoy-upstream-service-time: 17
content-security-policy: default-src 'self' https://cdnjs.cloudflare.com data:; frame-ancestors 'none'; form-action 'self'
strict-transport-security: max-age=15724800; includeSubDomains
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1
cf-cache-status: DYNAMIC
server: cloudflare
cf-ray: 76ab4c94ab5c9e64-SJC

{
  "id": "cdq0q6393ot6pp11sahg",
  "backend": "ibmq_qasm_simulator",
  "state": {
    "status": "Completed"
  },
  "params": {
    "circuits": {
      "__type__": "QuantumCircuit",
      "__value__": "eJwL9Az29gxhZRBjYoAAxkIGlkwGDiCLCYpBgAWZnZSakwNlM+SV5uQUMoJlmRgLixhQAWMyXCoZXQrOYsUQQQUe7oklqYVgJhtUiAlTFUg3o3MEXG0hWIwdWRaL2b6picWlRRAdEBeSoIMxGaIGAFUYFYQ="
    },
    "circuit_indices": [
      0
    ]
  },
  "program": {
    "id": "sampler"
  },
  "created": "2022-11-15T22:05:12.718877Z",
  "runtime": "ntc-provider-primitives:latest",
  "tags": [
    "my-tag"
  ],
  "session_id": "cdq0q6393ot6pp11sahg",
  "cost": 10000,
  "status": "Completed"
}
kt474 commented 1 year ago

job tags can be passed in directly to estimator.run() or sampler.run()

with Session(service=service, backend=backend) as session:
    sampler = Sampler(session=session)
    job = sampler.run(circuits=ReferenceCircuits.bell(), job_tags=['test_tag123'])

with Session(service=service, backend=backend) as session:
    estimator = Estimator(session=session)
    observable = SparsePauliOp("XZ")
    job = estimator.run(circuits=ReferenceCircuits.bell(), observables=observable, job_tags=['test_tag123'])
mriedem commented 1 year ago

job tags can be passed in directly to estimator.run() or sampler.run()

Reading the docs https://qiskit.org/documentation/partners/qiskit_ibm_runtime/stubs/qiskit_ibm_runtime.Sampler.run.html#qiskit_ibm_runtime.Sampler.run I guess this:

**kwargs – Individual options to overwrite the default primitive options.

Is what you mean? And an example of an individual option in this case is job_tags and the "primitive options" refer to RuntimeOptions?

If so, it feels like at least the docs should be a bit more clear about that for Estimator.run and Sampler.run.

Otherwise I assume it'd be a feature request to unpack an options kwarg in those run methods and pass those through? For example:

def run(circuits, parameter_values=None, **kwargs):
    if 'options' in kwargs:
        kwargs.update(kwargs['options'])
    # ...
kt474 commented 1 year ago

Is what you mean? And an example of an individual option in this case is job_tags and the "primitive options" refer to RuntimeOptions?

Yeah that's correct - I can update the docs