Qiskit unable to translate

Hello, I recently tried to call the noise model of qiskit through pennylane, but the important quantums such as CNOT CX CZ CY in the circuit could not be compiled by qiskit. My code is as follows:

IBMQ.save_account('my_token', overwrite=True)
# Get the noise model of ibmq_lima
provider = IBMQ.load_account()
provider = IBMQ.get_provider(hub='ibm-q', group='open', project='main')
backends = provider.backends()
backend_names = [backend.name() for backend in backends]
print('backend_names', backend_names)
service = QiskitRuntimeService()
backend = service.backend("ibm_kyiv")
noise_model = NoiseModel.from_backend(backend)

args.device = qml.device('qiskit.aer', wires=args.n_qubits, noise_model=noise_model)
max_iterations = 40
opt = qml.GradientDescentOptimizer(stepsize=0.4)
[dataset] = qml.data.load("qchem", molname="H2", bondlength=0.742, basis="STO-3G")
H = dataset.hamiltonian
args.n_qubits = dataset.hf_state.shape[0]

@qml.qnode(args.device)
def cost_fn_1(theta):
    circuit(theta)
    return qml.expval(H)

def circuit(param):
    qml.BasisState(dataset.hf_state, wires=range(args.n_qubits))
    for layer in range(args.n_layers):
        for i in range(args.n_qubits):
            qml.Hadamard(wires=i)
            qml.RZ(param[layer, i, 0], wires=i)
            qml.RY(param[layer, i, 1], wires=i)
            qml.RZ(param[layer, i, 2], wires=i)
        shape = qml.StronglyEntanglingLayers.shape(n_layers=2, n_wires=4)
        weights = pnp.random.random(size=shape)
        qml.StronglyEntanglingLayers(weights=weights, wires=range(4), ranges=[2, 3], imprimitive=qml.ops.CZ)
theta_1 = pnp.random.random((args.n_layers, args.n_qubits, 3), requires_grad=True)

energies_1 = []
for n in range(max_iterations):
    theta_1, prev_energy_1 = opt.step_and_cost(cost_fn_1, theta_1)
    print('prev_energy_1', prev_energy_1)
    energies_1.append(prev_energy_1)

Correspondingly, my error message is as follows:

File “/home/projector-user/QUEST_real_machine/main.py”, line 92, in get_energy
theta_1, prev_energy_1 = opt.step_and_cost(cost_fn_1, theta_1)
File “/home/ubuntu/aer/lib/python3.10/site-packages/pennylane/optimize/gradient_descent.py”, line 64, in step_and_cost
g, forward = self.compute_grad(objective_fn, args, kwargs, grad_fn=grad_fn)
File “/home/ubuntu/aer/lib/python3.10/site-packages/pennylane/optimize/gradient_descent.py”, line 122, in compute_grad
grad = g(*args, **kwargs)
File “/home/ubuntu/aer/lib/python3.10/site-packages/pennylane/_grad.py”, line 166, in call
grad_value, ans = grad_fn(*args, **kwargs) # pylint: disable=not-callable
File “/home/ubuntu/aer/lib/python3.10/site-packages/autograd/wrap_util.py”, line 20, in nary_f
return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
File “/home/ubuntu/aer/lib/python3.10/site-packages/pennylane/_grad.py”, line 184, in _grad_with_forward
vjp, ans = _make_vjp(fun, x) # pylint: disa…ns in the circuit: [‘h’, ‘ry’, ‘x’, ‘cz’, ‘measure’, ‘rz’] to the backend’s (or manually specified) target basis: [‘set_superop’, ‘save_statevector’, ‘save_matrix_product_state’, ‘set_stabilizer’, ‘save_stabilizer’, ‘for_loop’, ‘id’, ‘if_else’, ‘save_amplitudes’, ‘set_statevector’, ‘rz’, ‘save_probabilities’, ‘save_amplitudes_sq’, ‘roerror’, ‘snapshot’, ‘reset’, ‘barrier’, ‘while_loop’, ‘kraus’, ‘x’, ‘save_unitary’, ‘save_superop’, ‘save_expval_var’, ‘set_matrix_product_state’, ‘qerror_loc’, ‘delay’, ‘save_clifford’, ‘set_density_matrix’, ‘save_expval’, ‘superop’, ‘save_state’, ‘save_statevector_dict’, ‘save_density_matrix’, ‘save_probabilities_dict’, ‘set_unitary’, ‘measure’, ‘sx’, ‘quantum_channel’]. This likely means the target basis is not universal or there are additional equivalence rules needed in the EquivalenceLibrary being used. For more details on this error see: BasisTranslator | IBM Quantum Documentation"

This question is very important to me, and I will be very happy if you can help me!
Best wishes!

Hi @yulianhui ,

Your code seems to be using Qiskit functionality that has now been deprecated by IBM. You could use their V2 fake devices as follows.

First make sure to install PennyLane, the PennyLane-Qiskit plugin, and qiskit_ibm_runtime version 0.29.0

pip install pennylane pennylane-qiskit qiskit_ibm_runtime==0.29.0

Then you can run the code below for example. You can change the circuit to add the gates that you need.

import pennylane as qml
from pennylane import numpy as pnp 
from qiskit_ibm_runtime.fake_provider import FakeManilaV2
 
# Get a fake backend from the fake provider
backend = FakeManilaV2()

dev = qml.device('qiskit.remote', wires=5, backend=backend)

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

print(circuit(pnp.pi/3, shots=1024))

I hope this helps!

Here’s how to do this for a real backend. Make sure to set the number of qubits that the device actually has. Here I just select the least busy device but you can select a specific one if you want.

import pennylane as qml
from pennylane import numpy as pnp 
from qiskit_ibm_runtime import QiskitRuntimeService

QiskitRuntimeService.save_account(channel="ibm_quantum", token=token, overwrite=True)
service = QiskitRuntimeService(channel="ibm_quantum")
backend = service.least_busy(operational=True, simulator=False)

dev = qml.device('qiskit.remote', wires=127, backend=backend)

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

print(circuit(pnp.pi/3, shots=1024))