Qiskit / qiskit-ibm-runtime

IBM Client for Qiskit Runtime
https://docs.quantum.ibm.com/api/qiskit-ibm-runtime
Apache License 2.0
151 stars 154 forks source link

KeyError: Qubit(QuantumRegister(2, 'q'), 0) #1969

Closed davide710 closed 20 hours ago

davide710 commented 1 week ago

Describe the bug I have a circuit that works fine on the simulator (even the isa_circuit), but when trying execution on real hardware with qiskit_ibm_runtime.SamplerV2 I get the error: KeyError: Qubit(QuantumRegister(2, 'q'), 0). I first used qiskit-ibm-runtime==0.25.0 and got no output (I could only see that the job had failed on the quantum platform), then updated to version 0.30 and got the error I mentioned.

Steps to reproduce Here is the code to create the circuit:

import numpy as np
from scipy.linalg import expm

from qiskit.circuit.library import UnitaryGate
from qiskit.circuit.classical import expr
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_aer.primitives import Sampler

from qiskit_nature.second_q.drivers import PySCFDriver
from qiskit_nature.second_q.mappers import ParityMapper
from qiskit_nature.second_q.algorithms import GroundStateEigensolver
from qiskit_nature.second_q.circuit.library import HartreeFock

from qiskit_algorithms import IterativePhaseEstimation

from qiskit_ibm_runtime import QiskitRuntimeService

driver = PySCFDriver(
    atom=f'H .0 .0 .0; H .0 .0 0.73',
    basis='sto3g'
)

molecule = driver.run()
mapper = ParityMapper(num_particles=molecule.num_particles)
fer_op = molecule.hamiltonian.second_q_op()
tapered_mapper = molecule.get_tapered_mapper(mapper)
qubit_op = tapered_mapper.map(fer_op)

num_iterations = 4
state_in = HartreeFock(molecule.num_spatial_orbitals, molecule.num_particles, tapered_mapper)
sampler = Sampler()
U = UnitaryGate(expm(1j*qubit_op.to_matrix()))

def x_measurement(qc, qubit, cbit):
    """Measure 'qubit' in the X-basis, and store the result in 'cbit'"""
    qc.h(qubit)
    qc.measure(qubit, cbit)

q = QuantumRegister(2, name='q')
c = ClassicalRegister(num_iterations, name='res')
circuit = QuantumCircuit(q, c)

for k in range(1, num_iterations+1):
    circuit.reset(0)
    circuit.h(0)

    conditions = [expr.lift(c[j]) for j in range(0, k-1)] # phase correction conditions

    for j, cond in enumerate(conditions):
        with circuit.if_test(cond):
            circuit.p(-np.pi/(2**(len(conditions) - j)), 0) # phase correction

    cu = U.power(2**(num_iterations-k)).control(1, f'c-U^{2**(num_iterations-k)}')
    circuit.compose(cu, inplace=True)
    x_measurement(circuit, q[0], c[k-1])

service = QiskitRuntimeService(channel="ibm_quantum")
backend = service.least_busy(operational=True, simulator=False, dynamic_circuits=True, use_fractional_gates=False)
print(backend.name)
target = backend.target
pm = generate_preset_pass_manager(target=target, optimization_level=3)

isa_circuit = pm.run(circuit)

Here is the part for real hardware execution:

from qiskit_ibm_runtime import SamplerV2
sampler = SamplerV2(mode=backend)

job = sampler.run([isa_circuit]) # here is the error

This works as expected:

sampler = Sampler()

job = sampler.run([isa_circuit])
result = job.result()

Expected behavior The job is executed without errors.

Suggested solutions I wonder if the problem is the same as here: in fact, with qiskit_aer==0.14 I got the same error with the simulator, but updating to version 0.15 fixed it.

Additional Information qiskit==1.1.0 qiskit-aer==0.15.0 qiskit-algorithms==0.3.0 qiskit-ibm-runtime==0.30.0 qiskit-machine-learning==0.7.2 qiskit-nature==0.7.2 qiskit-nature-pyscf==0.4.0 qiskit-qasm3-import==0.5.0 qiskit-transpiler-service==0.4.5

kt474 commented 6 days ago

@davide710 I believe this was fixed in the latest release, 0.31 - can you try again

davide710 commented 5 days ago

Hello @kt474 , thank you for the answer, unfortunately updating to qiskit-ibm-runtime==0.31 didn't solve this, now it's gone back to simply show me the failed job on the platform and there I read the error message. I also updated to qiskit==1.2. Also, in #1979 @jwoehr described a similar problem with a dynamic circuit, and he is also using the latest version. To add more context, the error shows up on fake providers too, and I was able to run a dynamic circuit here

kt474 commented 23 hours ago

Sorry, we just pushed another update on the server side to fix this issue - let me know if this job still fails

davide710 commented 20 hours ago

Yes @kt474, now it works! Thank you!