How do I get a QML program written by Pennylane to run on an IBM Q quantum computer?

Since the IBM Qiskit routines do not describe how to run on an IBM quantum computer, it is more of a local simulation. And Penny has rich QML examples and simple code implementation capabilities, but lacks examples that run directly on IBM quantum computers. How can I put my written Penny program to run on a real IBM quantum computer, and can the Penny-Qiskit plugin do it? How to do it?

Hi @RX1,

Absolutely! All you need to do is change the device to be an IBM device. The example below shows you how to use actual hardware or a simulator. Note that you will need an IBM API token. In the PennyLane-Qiskit plugin docs you can find different ways of specifying your token, which you can get from the IBM website.

b = "ibmq_manila"

dev = qml.device(
    "qiskit.ibmq",
    wires=2,
    backend=b,
    shots=2,
    ibmqx_token="add_your_token_here"
)
#dev = qml.device('qiskit.aer',wires=1,shots=2)

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=0)
    return qml.expval(qml.PauliZ(0))

circuit()

Please let me know if you manage to run circuit! I encourage you to try running it on qiskit.aer first so that you don’t have to wait a long time in the queue. Also, be mindful of the number of shots that you use.

Thank you for your reply. But one question I have is whether QML’s optimizer or gradient descent algorithm will switch as dev becomes IBM’s. I checked the IBM website and the optimizer in the Qiskit machine learning board seems to be different from Pennylane’s, they use vqe{…} function.

So my essential question is whether Pennylane’s optimizer as well as iterative work is compatible with IBM’s and can run on IBM quantum computers as long as the dev is switched to IBM’s.

https://docs.pennylane.ai/projects/qiskit/en/latest/devices/runtime.html

For example, I am puzzled by the content in the above web page. If there is a quantum circuit with parameters, is it only possible to use the “Qiskit Runtime Programs” plugin? If so, it seems that the above web page only optimizes ansatz, not the loss function cost, which often contains classical functions, such as least squares regression or cross-entropy. How can I optimize the cost function, but not ansatz?
@CatalinaAlbornoz @isaacdevlugt @Maria_Schuld

Hi @RX1,

Using Qiskit Runtime can make your programs faster but some things aren’t working as expected as of the latest Qiskit release. It can also be tricky to learn how to use Qiskit runtime with PennyLane.

For what you need I would recommend the approach I suggested before, which is simply creating a PennyLane qnode and choosing an IBM backend as your device. PennyLane’s optimizers will work, regardless of whether you use IBM’s simulators or hardware, or PennyLane’s simulators.

I hope this can clarify things for you!