Skip to content

Error when executing SamplerQNN on IBM hardware: Sampler job failed: list index out of range #908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
francocirill opened this issue Apr 4, 2025 · 0 comments
Labels
Hardware runtime 💻 Running jobs on IBM quantum devices type: bug 🐞 Something isn't working

Comments

@francocirill
Copy link

Environment

  • Qiskit Machine Learning version: 0.8.2
  • Python version: 3.10
  • Operating system: Ubuntu 24.04.2 LTS
  • qiskit:1.4.2
  • qiskit-ibm-runtime:0.37.0

What is happening?

When I try to run a SamplerQNN on IBM Quantum hardware (ibm_kyiv in my case), I get the following error:

QiskitMachineLearningError: "Sampler job failed: 'Unable to retrieve job result. Error code 3211; Job not valid. list index out of range'"

The error also shows up on the IBM Quantum platform job page.
Interestingly, the code works without issues when I use a local or GenericBackendV2 simulator. So the problem seems specific to the IBM hardware or its interaction with SamplerV2 and SamplerQNN.

How can we reproduce the issue?

To evaluate the error, run the following minimal working example using an IBM Quantum backend such as ibm_kyiv. Make sure to replace "MY_TOKEN" with an IBM Quantum token.

import numpy as np
from qiskit.circuit.library import RealAmplitudes, ZZFeatureMap
from qiskit.circuit import QuantumCircuit
from qiskit_machine_learning.neural_networks import SamplerQNN
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit_ibm_runtime import SamplerV2 as Sampler
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_machine_learning.gradients import ParamShiftSamplerGradient

service1 = QiskitRuntimeService(channel='ibm_quantum', token='MY_TOKEN')
backend = service1.backend('ibm_kyiv')
pass_manager = generate_preset_pass_manager(optimization_level=0, backend=backend)

sampler = Sampler(mode=backend)
sampler_gradient = ParamShiftSamplerGradient(sampler=sampler, pass_manager=pass_manager)

num_qubits = 2

def parity(x):
    return f"{bin(x)}".count("1") % 2

feature_map = ZZFeatureMap(feature_dimension=num_qubits)
ansatz = RealAmplitudes(num_qubits=num_qubits)

qc = QuantumCircuit(num_qubits)
qc.compose(feature_map, inplace=True)
qc.compose(ansatz, inplace=True)

qnn = SamplerQNN(
    circuit=qc,
    input_params=feature_map.parameters,
    weight_params=ansatz.parameters,
    interpret=parity,
    output_shape=2,
    sampler=sampler,
    pass_manager=pass_manager,
)

result = qnn.forward(input_data=[1, 2], weights=[1, 2, 3, 4, 5, 6, 7, 8])
print(result)

This throws the following traceback:

---------------------------------------------------------------------------
RuntimeJobFailureError                    Traceback (most recent call last)
File ~/anaconda3/envs/fraconda/lib/python3.10/site-packages/qiskit_machine_learning/neural_networks/sampler_qnn.py:497, in SamplerQNN._forward(self, input_data, weights)
    496 try:
--> 497     results = job.result()
    498 except Exception as exc:

File ~/anaconda3/envs/fraconda/lib/python3.10/site-packages/qiskit_ibm_runtime/runtime_job_v2.py:137, in RuntimeJobV2.result(self, timeout, decoder)
    136         raise RuntimeJobMaxTimeoutError(error_message)
--> 137     raise RuntimeJobFailureError(f"Unable to retrieve job result. {error_message}")
    138 if self._status == "CANCELLED":

RuntimeJobFailureError: 'Unable to retrieve job result. Error code 3211; Job not valid. list index out of range'

The above exception was the direct cause of the following exception:

QiskitMachineLearningError                Traceback (most recent call last)
Cell In[10], line 29
     17 qnn = SamplerQNN(
     18     circuit=qc,  # Note that this is a QuantumCircuit instance
     19     input_params=feature_map.parameters,
   (...)
     24     pass_manager=pass_manager,
     25 )
     28 # Do a forward pass with input data and custom weights
---> 29 result=qnn.forward(input_data=[1, 2], weights=[1, 2, 3, 4, 5, 6, 7, 8])
     30 print(result)

File ~/anaconda3/envs/fraconda/lib/python3.10/site-packages/qiskit_machine_learning/neural_networks/neural_network.py:229, in NeuralNetwork.forward(self, input_data, weights)
    227 input_, shape = self._validate_input(input_data)
    228 weights_ = self._validate_weights(weights)
--> 229 output_data = self._forward(input_, weights_)
    230 return self._validate_forward_output(output_data, shape)

File ~/anaconda3/envs/fraconda/lib/python3.10/site-packages/qiskit_machine_learning/neural_networks/sampler_qnn.py:499, in SamplerQNN._forward(self, input_data, weights)
    497     results = job.result()
    498 except Exception as exc:
--> 499     raise QiskitMachineLearningError(f"Sampler job failed: {exc}") from exc
    500 result = self._postprocess(num_samples, results)
    501 return result

QiskitMachineLearningError: "Sampler job failed: 'Unable to retrieve job result. Error code 3211; Job not valid. list index out of range'"

What should happen?

The SamplerQNN should successfully run the forward pass and return output probabilities (or other configured values) instead of throwing an exception. Since the same code works with a generic backend, this seems to be an integration issue when running on real IBM hardware.

Any suggestions?

No response

@edoaltamura edoaltamura added type: bug 🐞 Something isn't working Hardware runtime 💻 Running jobs on IBM quantum devices labels Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hardware runtime 💻 Running jobs on IBM quantum devices type: bug 🐞 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants