Quantum Hardware

Hello, how do I run my script on quantum hardware in Pennylane? I saw that in the Pennylane Qiskit plugin, I have to write the backend:

import pennylane as qml
dev = qml.device('qiskit.ibmq', wires=2, backend='ibmq_qasm_simulator', ibmqx_token='XXX', hub='MYHUB', group='MYGROUP', project='MYPROJECT')

Is ibmqx_token from the above line my API token?
So if I have access to ibm_nairobi from my IBM Account, can I just mention it?

Also, in Qiskit, I can transpile my circuit according to quantum hardware. In Pennylane-Qiskit Plugin, how to transpile the circuit?

Hi @mass_of_15,

Thank you for your question!

Yes, ibmqx_token refers to your API token you can include it where it says 'XXX'. Remember to never post this token online.

If you have access to ibm_nairobi you can choose it as your backend. So you would write backend='ibm_nairobi'.

When you refer to transpiling do you mean running on hardware? Or defining a specific coupling for the qubits which represent the hardware? We have a transpile function in PennyLane that allows you to define a specific connectivity. If you don’t need to specifically define this then you can just run your circuit on the device of your choice as-is. Below is an example.

import pennylane as qml
from pennylane import numpy as np
import qiskit_ibm_provider

# Save your API token
IBM_token = 'API Token'# Insert your token here
    qiskit_ibm_provider.IBMProvider.save_account(token=IBM_token, overwrite=True)

# Define the backend that you will run on
b = "ibmq_qasm_simulator"
#b = "ibm_perth"

# Create your device
dev = qml.device(
    shots = 10

# Create a QNode
@qml.transforms.transpile(coupling_map=[(0, 1), (1, 3), (3, 2), (2, 0)]) # Transpile it according to the layout of your circuit
def circuit(phi):
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[2, 3])
    qml.CNOT(wires=[1, 3])
    qml.CNOT(wires=[1, 2])
    qml.CNOT(wires=[2, 3])
    qml.CNOT(wires=[0, 3])
    return qml.probs(wires=[0, 1, 2, 3])


# Print your circuit

Let me know if you have any further questions!

Hello @mass_of_15,

Also note, around November ibm_nairobi is planned to be retired.


Hi, when I meant transpile, I meant that let’s say I have the following circuit:

def CONVCircuit(phi, wires, i=0):
    quantum convolution Node
    # parameter
    theta = np.pi / 2

    qml.RX(phi[0] * np.pi, wires=0)
    qml.RX(phi[1] * np.pi, wires=1)
    qml.RX(phi[2] * np.pi, wires=2)
    qml.RX(phi[3] * np.pi, wires=3)

    qml.CRZ(theta, wires=[1, 0])
    qml.CRZ(theta, wires=[3, 2])
    qml.CRX(theta, wires=[1, 0])
    qml.CRX(theta, wires=[3, 2])
    qml.CRZ(theta, wires=[2, 0])
    qml.CRX(theta, wires=[2, 0])

    # Expectation value
    measurement = qml.expval(qml.PauliZ(wires=0))

    return measurement

Now if I choose ibm_nairobi, it doesn’t have all the connections between qubits. In this case, [2, 0], [2, 3] are the connections not possible in ibm_nairobi. So can it transpile like in Qiskit, which optimizes it for specific hardware?

Hi @mass_of_15,

Thank you for clarifying! PennyLane uses the Qiskit transpiler under the hood so that your circuit can run on the specific backend of your choice. So since ibm_nairobi for example doesn’t have all connections, you can run your code as usual and PennyLane will make sure that it can run on this backend, no extra work from you :raised_hands:.

However, if you want to use specific qubits within that device then you can use PennyLane’s transpile function. In the example I showed before, I mentioned a coupling map, this is the information of which qubits are connected to which others. In my example I was specifying that qubit 0 and 1 were connected, 1 is connected with 3 and so on. Unless you want to use a specific set of qubits you shouldn’t need this.

Let me know if you have any further questions about this!