Qiskit / qiskit-serverless

A programming model for leveraging quantum and classical resources
https://qiskit.github.io/qiskit-serverless/
Apache License 2.0
68 stars 31 forks source link

docs/examples/02_qaoa doesn't work with "USE_RUNTIME_SERVICE = True" #1336

Closed akihikokuroda closed 5 months ago

akihikokuroda commented 6 months ago

Steps to reproduce the problem

Execute docs/examples/02_qaoa with QiskitRuntimeService for an account and USE_RUNTIME_SERVICE = True

What is the current behavior?

cloud simulators and Lab are now retired

So I tried to run the examples/02_qaoa with backend = "ibm_sherbrooke"
I got

IBMInputValueError(\nqiskit_ibm_runtime.exceptions.IBMInputValueError: \'The instruction u3 on qubits (0,) is not supported by the target system. Circuits that do not match the target hardware definition are no longer supported after March 4, 2024.

and

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.

What is the expected behavior?

Job is executed successfully on the quantum system.

akihikokuroda commented 6 months ago

I added the transpiler step like this in qaoa.py:

    if service is not None:
        # if we have service we need to open a session and create sampler
        service = arguments.get("service")
        backend = service.least_busy(operational=True, simulator=False, min_num_qubits=127)
        session = Session(service=service, backend=backend)
        options = Options()
        options.optimization_level = 3
        pm = generate_preset_pass_manager(optimization_level=1, backend=backend)
        ansatz = pm.run(ansatz)

then it's getting:

/tmp/ray/session_2024-05-17_12-34-33_233332_8/runtime_resources/working_dir_files/_ray_pkg6854cd4610ddcb3f/qaoa.py:79: DeprecationWarning: The Sampler and Estimator V1 primitives have been deprecated as of qiskit-ibm-runtime 0.23.0 and will be removed no sooner than 3 months after the release date. Please use the V2 Primitives. See the V2 migration guide <[https://docs.quantum.ibm.com/api/migration-guides/v2-primitives>](https://docs.quantum.ibm.com/api/migration-guides/v2-primitives%3E%60_). for more details\n estimator = Estimator(session=session, options=options)\ncapi_return is NULL\nCall-back cb_calcfc_incobylauser__routines failed.\nTraceback (most recent call last):\n File "/tmp/ray/session_2024-05-17_12-34-33_233332_8/runtime_resources/working_dir_files/_ray_pkg_6854cd4610ddcb3f/qaoa.py", line 84, in \n result = run_qaoa(ansatz, estimator, operator, initial_point, method)\n File "/tmp/ray/session_2024-05-17_12-34-33_233332_8/runtime_resources/working_dir_files/_ray_pkg_6854cd4610ddcb3f/qaoa.py", line 49, in run_qaoa\n return minimize(\n File "/home/ray/anaconda3/lib/python3.10/site-packages/scipy/optimize/_minimize.py", line 702, in minimize\n res = _minimize_cobyla(fun, x0, args, constraints, callback=callback,\n File "/home/ray/anaconda3/lib/python3.10/site-packages/scipy/optimize/_cobyla_py.py", line 34, in wrapper\n return func(*args, *kwargs)\n File "/home/ray/anaconda3/lib/python3.10/site-packages/scipy/optimize/_cobyla_py.py", line 270, in _minimize_cobyla\n xopt, info = cobyla.minimize(calcfc, m=m, x=np.copy(x0), rhobeg=rhobeg,\n File "/home/ray/anaconda3/lib/python3.10/site-packages/scipy/optimize/_cobyla_py.py", line 258, in calcfc\n f = fun(np.copy(x), args)\n File "/tmp/ray/session_2024-05-17_12-34-33_233332_8/runtime_resources/working_dir_files/_ray_pkg_6854cd4610ddcb3f/qaoa.py", line 37, in cost_func\n estimator.run(ansatz, hamiltonian, parameter_values=params).result().values[0]\n File "/home/ray/anaconda3/lib/python3.10/site-packages/qiskit_ibm_runtime/estimator.py", line 290, in run\n return super().run(\n File "/home/ray/anaconda3/lib/python3.10/site-packages/qiskit/primitives/base/base_estimator.py", line 164, in run\n circuits, observables, parameter_values = validation._validate_estimator_args(\n File "/home/ray/anaconda3/lib/python3.10/site-packages/qiskit/primitives/base/validation.py", line 64, in _validate_estimator_args\n _cross_validate_circuits_observables(circuits, observables)\n File "/home/ray/anaconda3/lib/python3.10/site-packages/qiskit/primitives/base/validation.py", line 204, in _cross_validate_circuits_observables\n raise ValueError(\nValueError: The number of qubits of the 0-th circuit (127) does not match the number of qubits of the 0-th observable (5).\n'

akihikokuroda commented 6 months ago

Here is summary of circuit creation code.

import numpy as np
from qiskit.circuit.library import QAOAAnsatz
from qiskit.circuit.library import IQP
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit.quantum_info import SparsePauliOp
from qiskit.circuit.library import IQP
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager

operator = SparsePauliOp.from_list([("IIIZZ", 1), ("IIZIZ", 1), ("IZIIZ", 1), ("ZIIIZ", 1)])
ansatz = QAOAAnsatz(operator, reps=2)
ansatz = ansatz.decompose(reps=3)
ansatz.draw(fold=-1)
service = QiskitRuntimeService(
    channel='ibm_quantum',
    instance='ibm-q/open/main',
    token='<my token>'
)
backend = service.least_busy(operational=True, simulator=False, min_num_qubits=127)
pm = generate_preset_pass_manager(optimization_level=1, backend=backend)
ansatz_t = pm.run(ansatz)
ansatz_t.draw(fold=-1)
akihikokuroda commented 5 months ago

I'll update the sample by following this https://learning.quantum.ibm.com/tutorial/quantum-approximate-optimization-algorithm