Now I run a QML programme using the same process.
First run using this code with IBM simulator: qiskit.aer
# Exécuter un Code Pennylane en utilisant un Simulateur Qiskit
import pennylane as qml
from pennylane import numpy as np
import matplotlib.pyplot as plt
# Générer un jeu de données simple
np.random.seed(42)
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0]) # Classes binaires
# Création d'un simulateur quantique avec 2 qubits avec le simulateur Qiskit
**dev = qml.device('qiskit.aer', wires=2)**
@qml.qnode(dev)
def circuit(params, x):
"""Circuit quantique paramétré pour la classification."""
qml.RX(x[0], wires=0)
qml.RX(x[1], wires=1)
qml.RY(params[0], wires=0)
qml.RY(params[1], wires=1)
qml.CNOT(wires=[0, 1])
qml.RZ(params[2], wires=0)
qml.RZ(params[3], wires=1)
return qml.expval(qml.PauliZ(0))
# Fonction de coût
def cost(params):
cost = 0
for xi, yi in zip(X, y):
output = circuit(params, xi)
cost += (output - yi) ** 2
return cost / len(X)
# Initialiser les paramètres
np.random.seed(42)
init_params = np.random.normal(size=(4,))
# Optimiseur
opt = qml.GradientDescentOptimizer(stepsize=0.1)
# Entraînement du circuit
params = init_params
for i in range(100):
params = opt.step(cost, params)
if (i + 1) % 10 == 0:
print(f"Étape {i + 1}: Coût = {cost(params)}")
# Afficher les paramètres optimisés
print("Paramètres optimisés :", params)
# Tester le circuit avec les données d'entraînement
predictions = [circuit(params, xi) for xi in X]
print("Prédictions :", predictions)
# Afficher les résultats
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='bwr', marker='o')
plt.title("Classification binaire avec un circuit quantique")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
The result is correct with qiskit.aer.
Now I use the same code just changing the device using IBM QPU:
import pennylane as qml
from pennylane import numpy as np
import matplotlib.pyplot as plt
# Générer un jeu de données simple
np.random.seed(42)
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0]) # Classes binaires
# Pour accéder aux informations d'identification enregistrées pour le canal quantique IBM et sélectionner une instance
service = QiskitRuntimeService()
# Sélection explicite d'un backend compatible
backend = service.backend(name='ibm_sherbrooke')
# Afficher le backend utilisé
print(backend)
# Utiliser ce backend avec PennyLane (par exemple pour Qiskit sur une machine à 127 qubits)
# Bug fix: ajouter session=backend pour la nouvelle version de IBM Quantum Plateform 1er juillet 2025
#dev = qml.device('qiskit.remote', wires=127, backend=backend)
dev = qml.device('qiskit.remote', wires=127, backend=backend, session=backend) # définit la session pour tn backend
@qml.qnode(dev)
def circuit(params, x):
"""Circuit quantique paramétré pour la classification."""
qml.RX(x[0], wires=0)
qml.RX(x[1], wires=1)
qml.RY(params[0], wires=0)
qml.RY(params[1], wires=1)
qml.CNOT(wires=[0, 1])
qml.RZ(params[2], wires=0)
qml.RZ(params[3], wires=1)
return qml.expval(qml.PauliZ(0))
# Fonction de coût
def cost(params):
cost = 0
for xi, yi in zip(X, y):
output = circuit(params, xi)
cost += (output - yi) ** 2
return cost / len(X)
# Initialiser les paramètres
np.random.seed(42)
init_params = np.random.normal(size=(4,))
# Optimiseur
opt = qml.GradientDescentOptimizer(stepsize=0.1)
# Entraînement du circuit
params = init_params
for i in range(100):
params = opt.step(cost, params)
if (i + 1) % 10 == 0:
print(f"Étape {i + 1}: Coût = {cost(params)}")
# Afficher les paramètres optimisés
print("Paramètres optimisés :", params)
# Tester le circuit avec les données d'entraînement
predictions = [circuit(params, xi) for xi in X]
print("Prédictions :", predictions)
# Afficher les résultats
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='bwr', marker='o')
plt.title("Classification binaire avec un circuit quantique")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
The job is correctly ended.
This code got stuck, why?

Using the qiskit.aer, the code finished under 4 min.
What I did not understand: how Pennylane can run the circuit in my cost function without submit n times the circuit in the job?
If I have to change my code above in the case of using a simulator or a QPU, does this mean that Pennylane is not so interoperable or generic since I have to adapt the code according to the physical device of the provider or the simulator?
Do you have any idea on how to make the code generic so that only the supplier information can be modified regardless of the quantum resource?