Qiskit / qiskit-ibm-runtime

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

RuntimeJobFailureError when using Sampler primitives with Grover's Algo + AmplificationProblem #688

Closed svijaymurugan closed 1 year ago

svijaymurugan commented 1 year ago

Environment

What is happening?

When using primitives to run Grover's Algorithm with AmplificationProblem and 6 or more qubits, a RuntimeJobFailureError within qiskit_ibm_runtime. The error refers to "ERROR Failed to execute program: init() missing 1 required positional argument: \'num_ctrl_qubits\" which I believe to be the source of the issue.

After posting this on the Qiskit Slack, I came to know that there was an identical issue in the version prior to Qiskit Terra 0.23.0. (Here is the link: https://github.com/Qiskit/qiskit-terra/issues/9384).

There are no issues when using up to 5 qubits.

Here is the full error: "RuntimeJobFailureError: 'Unable to retrieve job result. Job cfbekknp2bp10gcfm4rg has failed:\n2023-01-29T21:49:06.663349527Z Setting up watches.\n2023-01-29T21:49:06.663401212Z Watches established.\n2023-01-29T21:49:08.604731099Z INFO: Started server process [7]\n2023-01-29T21:49:08.604771632Z INFO: Waiting for application startup.\n2023-01-29T21:49:08.604779753Z INFO: Application startup complete.\n2023-01-29T21:49:08.630436854Z INFO: Uvicorn running on http://127.0.0.1:8081/ (Press CTRL+C to quit)\n2023-01-29T21:51:14.353659088Z INFO: 127.0.0.1:40146 - "POST /run HTTP/1.1" 202 Accepted\n2023-01-29T21:51:14.416559828Z /opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/qpy/interface.py:255: UserWarning: The qiskit version used to generate the provided QPY file, 0.23.0, is newer than the current qiskit version 0.22.4. This may result in an error if the QPY file uses instructions not present in this current qiskit version\n2023-01-29T21:51:14.416605057Z warnings.warn(\n2023-01-29T21:51:14.421473169Z webserver-starter - ERROR Failed to execute program: init() missing 1 required positional argument: \'num_ctrl_qubits\'\n2023-01-29T21:51:14.421514588Z Traceback (most recent call last):\n2023-01-29T21:51:14.421524175Z File "/provider/server/main.py", line 60, in execute_program\n2023-01-29T21:51:14.421544270Z user_params=load_user_params(body.program_input_parameters_file, logger),\n2023-01-29T21:51:14.421555534Z File "/provider/programruntime/utils.py", line 49, in load_user_params\n2023-01-29T21:51:14.421565367Z user_params = json.loads(params, cls=ProgramRuntimeDecoder)\n2023-01-29T21:51:14.421575405Z File "/usr/lib64/python3.9/json/init.py", line 359, in loads\n2023-01-29T21:51:14.421584994Z return cls(kw).decode(s)\n2023-01-29T21:51:14.421596182Z File "/usr/lib64/python3.9/json/decoder.py", line 337, in decode\n2023-01-29T21:51:14.421605648Z obj, end = self.raw_decode(s, idx=_w(s, 0).end())\n2023-01-29T21:51:14.421615083Z File "/usr/lib64/python3.9/json/decoder.py", line 353, in raw_decode\n2023-01-29T21:51:14.421626281Z obj, end = self.scan_once(s, idx)\n2023-01-29T21:51:14.421636027Z File "/provider/programruntime/utils.py", line 221, in object_hook\n2023-01-29T21:51:14.421645947Z return super().object_hook(obj)\n2023-01-29T21:51:14.421655067Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/utils/json.py", line 308, in object_hook\n2023-01-29T21:51:14.421664887Z return _decode_and_deserialize(obj_val, load)[0]\n2023-01-29T21:51:14.421674514Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/utils/json.py", line 134, in _decode_and_deserialize\n2023-01-29T21:51:14.421683715Z return deserializer(buff)\n2023-01-29T21:51:14.421494634Z /pod-data/ CLOSE_WRITE,CLOSE terminated\n2023-01-29T21:51:14.421693215Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/qpy/interface.py", line 278, in load\n2023-01-29T21:51:14.421725359Z loader( # type: ignore[no-untyped-call]\n2023-01-29T21:51:14.422198799Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/qpy/binary_io/circuits.py", line 1020, in read_circuit\n2023-01-29T21:51:14.422217879Z custom_operations = _read_custom_operations(file_obj, version, vectors)\n2023-01-29T21:51:14.422227202Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/qpy/binary_io/circuits.py", line 429, in _read_custom_operations\n2023-01-29T21:51:14.422237610Z definition_circuit = common.data_from_binary(\n2023-01-29T21:51:14.422247312Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/qpy/common.py", line 269, in data_from_binary\n2023-01-29T21:51:14.422256268Z obj = deserializer(container, kwargs)\n2023-01-29T21:51:14.422265970Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/qpy/binary_io/circuits.py", line 1020, in read_circuit\n2023-01-29T21:51:14.422304557Z custom_operations = _read_custom_operations(file_obj, version, vectors)\n2023-01-29T21:51:14.422312045Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/qpy/binary_io/circuits.py", line 429, in _read_custom_operations\n2023-01-29T21:51:14.422320424Z definition_circuit = common.data_from_binary(\n2023-01-29T21:51:14.422328869Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/qpy/common.py", line 269, in data_from_binary\n2023-01-29T21:51:14.422336372Z obj = deserializer(container, *kwargs)\n2023-01-29T21:51:14.422344082Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/qpy/binary_io/circuits.py", line 1022, in read_circuit\n2023-01-29T21:51:14.422351224Z _read_instruction(\n2023-01-29T21:51:14.422359832Z File "/opt/app-root/lib64/python3.9/site-packages/qiskit_ibm_runtime/qpy/binary_io/circuits.py", line 281, in _read_instruction\n2023-01-29T21:51:14.422367789Z gate = gate_class(params)\n2023-01-29T21:51:14.422375925Z TypeError: init() missing 1 required positional argument: \'num_ctrl_qubits\'\n2023-01-29T21:51:14.424997165Z Termination marker file found. Kill process (7).\n2023-01-29T21:51:14.440811484Z Termination signal received, exited.\n2023-01-29T21:51:14.440851788Z /bin/bash: line 3: 7 Killed python -m uvicorn server.main:app --port 8081\n'"

How can we reproduce the issue?

from qiskit.quantum_info import Statevector
from qiskit.algorithms import AmplificationProblem
from qiskit.algorithms import Grover
from qiskit_ibm_runtime import QiskitRuntimeService, Sampler, Session, Options
from qiskit import IBMQ

service = QiskitRuntimeService()
my_token = [API Token]
provider = IBMQ.enable_account(my_token)
QiskitRuntimeService.save_account(channel='ibm_quantum', token=my_token, overwrite=True)

simulator_backend = service.backends(simulator=True)[0]

options = Options(resilience_level=0, optimization_level=0)

secret = 6 
secret_string = format(secret, f'0{secret}b')
oracle = Statevector.from_label(secret_string)

problem = AmplificationProblem(oracle, is_good_state=secret_string, objective_qubits=[i - 1])
iterations = Grover.optimal_num_iterations(1, i)

grover = Grover(iterations=iterations)

circuit = grover.construct_circuit(problem)
circuit.measure_all()

with Session(service=service, backend=simulator_backend) as session:
    sampler = Sampler(session=session, options=options)
    job = sampler.run(circuits=circuit, shots=1000)
    result = job.result()

What should happen?

This should run Grover's Algorithm on a database of 64 elements, searching for the "6" (or 000110) with no Error Mitigation or Optimization.

The end result should be a SamplerResult() object.

Any suggestions?

My only guess for where a num_ctrl_qubits parameter could not have been passed is somewhere within the MCXGate Class (as that's where I seemed to find reference to that parameter after some searching).

Cryoris commented 1 year ago

Since this occurs only with the runtime Sampler and not the reference Sampler running locally (qiskit.primitives.Sampler) I would assume that the fix for Qiskit/qiskit-terra#9384 is not uploaded on the server side yet. For that fix to be effective it needs to be installed both locally, where the circuit is constructed and serialized, and on the server side, where the circuit is deserialized.

@jyu00 do you know if the server for runtime is running Terra 0.23 already? 🙂

jakelishman commented 1 year ago

Runtime has a vendored version of QPY that's currently a bit out-of-date (see Qiskit/qiskit-ibm-runtime#577). The likely fix is for Runtime to pull Terra's updates in this case - @1ucian0, if you could transfer this issue over to Runtime that'd be great thanks.

jyu00 commented 1 year ago

fyi @ChitsimranGill Not pulse gate but another issue with downlevel qpy

kt474 commented 1 year ago

The server side qpy has been updated - tested the code snippet above and it's working now