Running Codebook I.2 code on some free IBMQ hardware

Quantum Codebook is an amazing resource. I really feel now I am learning things well.
I have copied the below example codes (which are amazing) from the Codebook I.2. Please guide me how to modify these code so that I can run it on IBMQ real hardware. I have a account on IBMQ. It would be great if someone can guide me how to run it on some free hardware.

# This creates a device with three wires on which PennyLane can run computations
dev = qml.device("default.qubit", wires=3)


def my_circuit(theta, phi, omega):
    # IMPLEMENT THE CIRCUIT BY ADDING THE GATES

    # Here are two examples, so you can see the format:
    qml.RX(theta, wires =0)
    qml.RY(phi, wires= 1)
    qml.RZ(omega, wires=2)
    qml.CNOT(wires = [0,1])
    qml.CNOT(wires =[1,2])
    qml.CNOT(wires= [2,0])
    return qml.probs(wires=[0, 1, 2])

# This creates a QNode, binding the function and device
my_qnode = qml.QNode(my_circuit, dev)

# We set up some values for the input parameters
theta, phi, omega = 0.1, 0.2, 0.3

# Now we can execute the QNode by calling it like we would a regular function
my_qnode(theta, phi, omega)

dev = qml.device("default.qubit", wires=3)

##################
# YOUR CODE HERE #
##################

# DECORATE THE FUNCTION BELOW TO TURN IT INTO A QNODE
@qml.qnode(dev)
def my_circuit(theta, phi, omega):
    qml.RX(theta, wires=0)
    qml.RY(phi, wires=1)
    qml.RZ(omega, wires=2)
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[1, 2])
    qml.CNOT(wires=[2, 0])
    return qml.probs(wires=[0, 1, 2])


theta, phi, omega = 0.1, 0.2, 0.3
my_circuit(theta, phi, omega)



Hello @Manu_Chaudhary,

‘Specifying providers’ has a couple of examples: IBM Q Experience — PennyLane-Qiskit 0.33.0-dev documentation

Hi @Manu_Chaudhary and @kevinkawchak ,

Thanks Kevin for sharing this link.

Here’s some additional help:

You first need to install the PennyLane-Qiskit plugin. You can use pip install pennylane-qiskit.

Then you can then save your token

import qiskit_ibm_provider
# Save your API token
IBM_token = 'Your Token Goes Here' # Insert your IBM token here
try:
    qiskit_ibm_provider.IBMProvider()
except:
    qiskit_ibm_provider.IBMProvider.save_account(token=IBM_token, overwrite=True)

Now you can choose the device you want to use:

# Choose the device you want to use.
dev = qml.device("lightning.qubit", wires=2, shots=100)
# dev = qml.device('qiskit.aer', wires=2, shots=100)
# dev = qml.device('qiskit.ibmq', wires=2, shots=100, backend='ibmq_qasm_simulator')
# dev = qml.device('qiskit.ibmq', wires=2, shots=100, backend='ibm_lagos')

Be mindful of the number of shots you use when using hardware so you don’t run out of shots.

Let us know if this helps!

Thank you @CatalinaAlbornoz and @kevinkawchak for this great help.

I’m glad this helped @Manu_Chaudhary !