How to run circuits on specified qubits of IBM quantum device?

Hi, I am using IBM’s quantum machine service recently.
According to the information provided by IBMQ website, we know that in a certain backend, some qubits have lower error rate than others. To access those qubits, I tried to add the keyword argument ‘initial_layout’:

dev = qml.device('qiskit.ibmq', wires=2, backend=backend, provider=provider,initial_layout=[0,1])

However, I get the error below:

UserWarning: initial_layout is not a recognized runtime option and may be ignored by the backend.
  res = super().batch_execute(circuits, timeout=self.timeout_secs)

Am I using a wrong method?
If so, what is the correct way to access a particular qubit?

My Pennylane version is 0.30.0, and qml.about()shows:

Name: PennyLane
Version: 0.30.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/ubuntu2022/anaconda3/envs/research/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning, PennyLane-qiskit

Platform info:           Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python version:          3.10.9
Numpy version:           1.23.5
Scipy version:           1.10.1
Installed devices:
- qiskit.aer (PennyLane-qiskit-0.30.1)
- qiskit.basicaer (PennyLane-qiskit-0.30.1)
- qiskit.ibmq (PennyLane-qiskit-0.30.1)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.30.1)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.30.1)
- lightning.qubit (PennyLane-Lightning-0.30.0)
- default.gaussian (PennyLane-0.30.0)
- default.mixed (PennyLane-0.30.0)
- default.qubit (PennyLane-0.30.0)
- default.qubit.autograd (PennyLane-0.30.0)
- default.qubit.jax (PennyLane-0.30.0)
- default.qubit.tf (PennyLane-0.30.0)
- default.qubit.torch (PennyLane-0.30.0)
- default.qutrit (PennyLane-0.30.0)
- null.qubit (PennyLane-0.30.0)

Hey @PoJung-Lu!

Can you provide your full error traceback?

Hi @isaacdevlugt !
Do you mean the full error message?
If so, since the error is raised by try except:

try:
    some code...
except Exception as e:
    str_error = str(e)
    print('err:',str_error)

the error message is described as follows:

/home/ubuntu2022/anaconda3/envs/research/lib/python3.10/site-packages/pennylane_qiskit/ibmq.py:91: UserWarning: initial_layout is not a recognized runtime option and may be ignored by the backend.
  res = super().batch_execute(circuits, timeout=self.timeout_secs)

Can you show me what backend and provider are in dev = qml.device('qiskit.ibmq', wires=2, backend=backend, provider=provider, initial_layout=[0,1]) ?

@isaacdevlugt , sure!

I was using ibmq_kolkata backend, and the provider is:

provider = IBMProvider(instance='ibm-q-hub-ntu/ntu-internal/default')

Thanks! I need to chat with some folks internally to see what’s the matter. Sit tight and I’ll get back to you as soon as I can!

1 Like

Hi @PoJung-Lu ,

I see that the error is raised from our batch_execute method, so when you’re using batching the initial_layout keyword argument seems not to be allowed. However I can’t reproduce your problem since I have no idea about your circuit or data.

Can you please share a Minimal Working example as shown in this video, including all of the lines that appear when you get the error (the full error traceback)?

This minimal example should be a very small version of your program that shows the error you’re getting.

Please let me know if you have any questions about this.

Hi @CatalinaAlbornoz .

The code is discribed as below, though queuing for ibm machine might take a lot of time.

import time
import pennylane as qml
from qiskit_ibm_provider import IBMProvider
sleep_time = 5
num_retries = 5
str_error = None

provider = IBMProvider()
dev = qml.device('qiskit.ibmq', wires=2, backend='ibm_cairo', provider=provider,initial_layout=[12,13])#'ibmq_qasm_simulator')#'ibmq_qasm_simulator' )

@qml.qnode(dev)
def circuit(x, y):
    qml.RY(y, wires=[0])
    qml.RX(x, wires=[1])
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(wires=0)),qml.expval(qml.PauliY(wires=1))

data = iter([[[0.1,0.2,0.3],[0.2,0.3,0.4]],[[0.4,0.5,0.6],[0.5,0.6,0.7]]])

while True:
    try:
        if not str_error:
            databatch=next(data)
        for x in range(0, num_retries):  
            try:
                rst0 = circuit(*databatch)
                rst = [rst0]
                print('result:',rst)
                str_error = None
            except  Exception as e:
                str_error = str(e)
                print(e)
            if str_error:               # if None->break; if 'some_string'(error happens)->sleep
                time.sleep(sleep_time)  # wait before trying to fetch the data again
                sleep_time *= 3
                print(f'retry {x} times')
            else:
                break
    except StopIteration:
        break

And the result is

/home/ubuntu2022/anaconda3/envs/research/lib/python3.10/site-packages/pennylane_qiskit/ibmq.py:91: UserWarning: initial_layout is not a recognized runtime option and may be ignored by the backend.
  res = super().batch_execute(circuits, timeout=self.timeout_secs)

result: [(array([0.95117188, 0.88476562, 0.83007812]), array([ 0.04296875, -0.04101562, -0.01367188]))]
result: [(array([0.75195312, 0.64648438, 0.5859375 ]), array([ 0.00390625, -0.04101562, -0.00585938]))]

My version of pennylane is:

Name: PennyLane
Version: 0.30.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/ubuntu2022/anaconda3/envs/research/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning, PennyLane-qiskit

Platform info:           Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python version:          3.10.9
Numpy version:           1.23.5
Scipy version:           1.10.1
Installed devices:
- qiskit.aer (PennyLane-qiskit-0.30.1)
- qiskit.basicaer (PennyLane-qiskit-0.30.1)
- qiskit.ibmq (PennyLane-qiskit-0.30.1)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.30.1)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.30.1)
- lightning.qubit (PennyLane-Lightning-0.30.0)
- default.gaussian (PennyLane-0.30.0)
- default.mixed (PennyLane-0.30.0)
- default.qubit (PennyLane-0.30.0)
- default.qubit.autograd (PennyLane-0.30.0)
- default.qubit.jax (PennyLane-0.30.0)
- default.qubit.tf (PennyLane-0.30.0)
- default.qubit.torch (PennyLane-0.30.0)
- default.qutrit (PennyLane-0.30.0)
- null.qubit (PennyLane-0.30.0)

Hi @PoJung-Lu,

I can replicate the warning you’re getting with the newest version of PennyLane. The queue is indeed taking a long time and I can see that the warning doesn’t show up when you use the ibmq_qasm_simulator.

I also identified that this is not related to an incompatibility with batching.

My guess is that the initial layout is not being processed correctly by the hardware backends at the moment of runtime.

I will forward this to our developers who implemented the pennylane-qiskit plugin but it looks to me like this will require some time to figure out a stable solution. In the meantime I will see if we can come up with a workaround. Thank you so much for your patience and I hope we can get back to you with a workaround or solution soon.

If you find any additional information or insights in the following days please let me know here too.

And thank you very much for providing the minimal working example. It was very helpful to identify the possible source of the issue.

1 Like

Hi @CatalinaAlbornoz , thank you for the nice response.

I am trying to use qiskit-runtime to run my circuit recently.

The advantage to use runtime program is that they have many built-in features like error mitigation.
Nevertheless, the most important thing to me is that it can open a session so that I can keep submit jobs to quantum device without queuing again until the session is closed. This is very convenient to me since my circuits are usually short but needed to execute many times.

However, I didn’t find many document introducing this program from pennylane.
According to doc1 & doc2, there are two runtime program: circuit_runner and sampler
I adjust my code as follows:

# minimum qiskit-runtime connect-testing code
import time
import pennylane as qml
from qiskit_ibm_provider import IBMProvider

sleep_time = 5
num_retries = 5
str_error = None
initial_layout = None #[0,1] # 
program_id = 'qiskit.ibmq.circuit_runner' # 'qiskit.ibmq.sampler' #
backend = 'ibmq_qasm_simulator' # or 'ibm_brisbane' #
provider = IBMProvider()
dev = qml.device(program_id, wires=2, backend=backend
                 , provider=provider,initial_layout=initial_layout)

@qml.qnode(dev)
def circuit(x, y):
    qml.RY(y, wires=[0])
    qml.RX(x, wires=[0])
    qml.CNOT(wires=[0,1])
    return qml.expval(qml.PauliZ(wires=0)),qml.expval(qml.PauliY(wires=1))

data = iter([[[0.1,0.2,0.3],[0.2,0.3,0.4]],[[0.4,0.5,0.6],[0.5,0.6,0.7]]])

while True:
    try:
        if not str_error:
            databatch=next(data)
        for x in range(0, num_retries):  
            try:
                rst = circuit(*databatch)
                print('result:',rst)
                str_error = None
            except  Exception as e:
                str_error = str(e)
                print(e)
            if str_error:               # if None->break; if 'some_string'(error happens)->sleep
                time.sleep(sleep_time)  # wait before trying to fetch the data again
                sleep_time *= 3
                print(f'retry {x} times')
            else:
                break
    except StopIteration:
        break

Through this code, I tested different situation as follows

  • A.1 If circuit_runner program is used and initial_layout=None, this code runs fine on both simulator and real device.
  • A.2 If circuit_runner program is used and initial_layout is specified, both simulator and real device show error:
RuntimeJobFailureError: 'Unable to retrieve job result. TRANSPILERERROR: "\'INITIAL_LAYOUT\' (2) AND CIRCUIT (32) HAD DIFFERENT NUMBERS OF QUBITS'
  • A.3 If sampler program is used and initial_layout=None, this code runs fine on both simulator and real device.

  • A.4 If sampler program is used and initial_layout is specified, this code runs fine on simulator; as for real device, it usually runs fine but sometimes shows errors:

ValueError: probabilities are not non-negative
  • A.5 Another wierd thing is that connection warning (see below) often occurs when using pennylane-qiskit plugins, but I never see this kind of warning when i was using qiskit packages.
WARNING: urllib3.connectionpool:Retrying (PostForcelistRetry(total=4, connect=3, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))': /jobs/cjfo31d7ui1f8qb4o8g0

The table below should conclude my experiments.
image

I have checked document of pennylane_qiskit.runtime(doc2) and QiskitRuntimeService, it seems that qiskit has update the sampler program.
Sampler is now a indivial class under QiskitRuntimeService.
Instead of calling QiskitRuntimeService.run function and specify program_id=sampler (old way), we can simply import Sampler and run circuit with many built-in features now (new way).

Given that the current version of pennylane_qiskit.runtime is not working properly, maybe pennylane can fix these problems by changing the ‘old way’ of implementing qiskit runtime to the ‘new way’.

Since features like error mitigation and session-opening are very important to my works, and they are not available from current version of sampler program, I really hope pennylane can fix these issues soon.

Plese let me know if fixing these runtime issues in the plan of Xandadu in the near future. If not, I think I would need to rewrite my code into qiskit version. Many thanks!

Hi @PoJung-Lu ,

Thank you very much for your detailed post!
We are indeed looking into increasing the compatibility between PennyLane and Qiskit to make it easier to use runtime for example. This takes some time and I don’t have a date yet but I can assure you that this is well in our minds and on our plans. Your posts here are very important to guide our work on this front.

We will be making a new PennyLane release around August 29 so make sure to update your PennyLane and PennyLane-qiskit versions then to see if some of your problems get fixed. You can also use the development version if you prefer not to wait.

I will take a deeper look at your issues next week and get back to you with more information. Thank you very much for helping us improve!

1 Like

@CatalinaAlbornoz , I see. I will look forward to the update!

3 Likes

Hi @PoJung-Lu,

Thank you once again for your detailed post. It seems that Qiskit completely changed their runtime service so this will require some big work to make the plugin work well for all of your use cases. If you could try running your experiments late next week after the release of PennyLane v0.32 it can help us see if some of the issues are resolved. If it’s not the case then we may need to wait until PennyLane v0.34 or PennyLane v0.35 so around the end of the year or the beginning of next year.

Please let us know if you have any questions about this and thank you for being part of the community and help us improve!

1 Like