Qiskit / qiskit-ibm-runtime

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

ISA checks inside control flow operations sometimes fail for valid circuits #1843

Open yaelbh opened 1 month ago

yaelbh commented 1 month ago

Describe the bug

Recently, in #1784, we added ISA checks inside bodies of control flow operations. @ashsaki found an example similar to the one below, where a valid circuit is labeled as invalid by this new ISA check.

Steps to reproduce

from qiskit import QuantumCircuit

from qiskit_ibm_runtime import SamplerV2
from qiskit_ibm_runtime.fake_provider import FakeCairoV2

backend = FakeCairoV2()
sampler = SamplerV2(mode=backend)

circ = QuantumCircuit(5, 1)
circ.x(0)
circ.measure(0, 0)
with circ.if_test((0, 1)):
    circ.cx(1, 2)

res = sampler.run([circ]).result()

Output is:

  File "/mnt/wsl/balagan/saki.py", line 16, in <module>
    res = sampler.run([circ]).result()
  File "/root/miniforge3/envs/env2/lib/python3.10/site-packages/qiskit_ibm_runtime/sampler.py", line 159, in run
    return self._run(coerced_pubs)  # type: ignore[arg-type]
  File "/root/miniforge3/envs/env2/lib/python3.10/site-packages/qiskit_ibm_runtime/base_primitive.py", line 174, in _run
    validate_isa_circuits([pub.circuit], self._backend.target)
  File "/root/miniforge3/envs/env2/lib/python3.10/site-packages/qiskit_ibm_runtime/utils/validations.py", line 89, in validate_isa_circuits
    raise IBMInputValueError(
qiskit_ibm_runtime.exceptions.IBMInputValueError: 'The instruction cx on qubits (0, 1) is not supported by the target system. Circuits that do not match the target hardware definition are no longer supported after March 4, 2024. See the transpilation documentation (https://docs.quantum.ibm.com/transpile) for instructions to transform circuits and the primitive examples (https://docs.quantum.ibm.com/run/primitives-examples) to see this coupled with operator transformations.'

Expected behavior

Well, don't crash and let the circuit pass the ISA test.

Suggested solutions

Well, debug and fix :smile:.

Additional Information

ashsaki commented 1 month ago

It happens on real backends as well. Switching the backend to ibm_sherbrooke threw me 'The instruction ecr on qubits (0, 1) is not supported by the target system.

Example:

from qiskit import QuantumCircuit, transpile

from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2
from qiskit_ibm_runtime.fake_provider import FakeCairoV2

service = QiskitRuntimeService()
backend = service.backend("ibm_sherbrooke")
sampler = SamplerV2(mode=backend)

circ = QuantumCircuit(5, 1)
circ.x(0)
circ.measure(0, 0)
with circ.if_test((0, 1)):
    circ.cx(1, 2)

trans_qc = transpile(circ, backend=backend, optimization_level=1)

res = sampler.run([trans_qc]).result()
yaelbh commented 1 month ago

Consider replacing the entire ISA check implementation with qiskit's transpiler pass: https://github.com/Qiskit/qiskit/issues/12916#issuecomment-2275729419.

yaelbh commented 1 day ago

Inside the control flow operation, when the ISA check checks cx(1, 2), it wrongly checks cx(0, 1), because of a bug. Since (0, 1) is not connected in Sherbrooke (only (1, 0) is connected - the other direction), the ISA check fails, in spite of (1, 2) being connected and valid.

This bug occurs also on the other way around: in Cusco, (0, 1) is connected but (1, 2) is not connected. Because of the bug, cx(1, 2) wrongly passes the ISA check, and the failure is deferred must later down the stack:

from qiskit import QuantumCircuit

from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2

service = QiskitRuntimeService()
backend = service.backend("ibm_cusco")
sampler = SamplerV2(mode=backend)

circ = QuantumCircuit(5, 1)
circ.x(0)
circ.measure(0, 0)
with circ.if_test((0, 1)):
    circ.ecr(1, 2)

job = sampler.run([circ])
print(job.job_id())
res = job.result()

print(res)

Output:

    raise RuntimeJobFailureError(f"Unable to retrieve job result. {error_message}")
qiskit_ibm_runtime.exceptions.RuntimeJobFailureError: 'Unable to retrieve job result. Error code 6103; Internal OpenQASM3 compilation error'