Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run_circuits does not respect BackendV1 and passes Qobj #6280

Closed
ColemanCollins opened this issue Apr 21, 2021 · 4 comments · Fixed by #6299
Closed

run_circuits does not respect BackendV1 and passes Qobj #6280

ColemanCollins opened this issue Apr 21, 2021 · 4 comments · Fixed by #6299
Assignees
Labels
bug Something isn't working mod: algorithms Related to the Algorithms module priority: high stable backport potential The bug might be minimal and/or import enough to be port to stable
Milestone

Comments

@ColemanCollins
Copy link

Information

  • Qiskit Terra version: 0.17
  • Python version: 3.9.2
  • Operating system: macOS 11.2.3 (Big Sur)

What is the current behavior?

This was originally an issue on the qiskit-ionq provider but it's really at core a terra issue.

Steps to reproduce the problem

import numpy as np
from qiskit import QuantumCircuit
from qiskit.utils import QuantumInstance
from qiskit_ionq import IonQProvider

from qiskit.algorithms import EstimationProblem
from qiskit.algorithms.amplitude_estimators import MaximumLikelihoodAmplitudeEstimation
from qiskit_finance.circuit.library.probability_distributions import NormalDistribution
from qiskit_finance.circuit.library.payoff_functions.fixed_income_pricing_objective import FixedIncomePricingObjective

ionq_provider = IonQProvider("token")
ionq_backend = ionq_provider.get_backend("ionq_simulator")
ionq_quantum_instance = QuantumInstance(backend=ionq_backend)

num_qubits = [2, 2]

low = [0, 0]
high = [0.12, 0.24]
mu = [0.12, 0.24]
sigma = 0.01*np.eye(2)
bounds = list(zip(low, high))
u = NormalDistribution(num_qubits, mu, sigma, bounds)

A = np.eye(2)
b = np.zeros(2)
cf = [1.0, 2.0]
c_approx = 0.125

fixed_income = FixedIncomePricingObjective(num_qubits, A, b, cf, c_approx, bounds)
state_preparation = QuantumCircuit(fixed_income.num_qubits)
state_preparation.append(u, range(u.num_qubits))
state_preparation.append(fixed_income, range(fixed_income.num_qubits))
objective_qubit = u.num_qubits

problem = EstimationProblem(state_preparation=state_preparation,
                            objective_qubits=[objective_qubit],
                            post_processing=fixed_income.post_processing,
                            )

ae = MaximumLikelihoodAmplitudeEstimation(evaluation_schedule=2, quantum_instance=ionq_quantum_instance)

print("Running on IonQ simulator")
result = ae.estimate(problem)
print('Estimated value:    \t%.4f' % result.estimation_processed)

produces the following stacktrace:

~/dev/qiskit-ionq/qiskit_ionq/helpers.py in qiskit_circ_to_ionq_circ(input_circuit)
    133     num_meas = 0
--> 134     meas_map = [None] * len(input_circuit.clbits)
    135     for instruction, qargs, cargs in input_circuit.data:

AttributeError: 'QasmQobj' object has no attribute 'clbits'

This is the ionq provider error that stems from expecting only a circuit in this method due to the ionq provider backend being pinned to BackendV1.

Here's the rest of the stacktrace:

During handling of the above exception, another exception occurred:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-1-474ab13aba6b> in <module>
     42 
     43 print("Running on IonQ simulator")
---> 44 ionq_result = ionq_ae.estimate(problem)
     45 print('Estimated value:    \t%.4f' % ionq_result.estimation_processed)

~/anaconda3/envs/qiskit_env/lib/python3.9/site-packages/qiskit/algorithms/amplitude_estimators/mlae.py in estimate(self, estimation_problem)
    277             # run circuit on QASM simulator
    278             circuits = self.construct_circuits(estimation_problem, measurement=True)
--> 279             ret = self._quantum_instance.execute(circuits)
    280 
    281             # get counts and construct MLE input

~/anaconda3/envs/qiskit_env/lib/python3.9/site-packages/qiskit/utils/quantum_instance.py in execute(self, circuits, had_transpiled)
    407 
    408         else:
--> 409             result = run_qobj(qobj, self._backend, self._qjob_config,
    410                               self._backend_options, self._noise_config,
    411                               self._skip_qobj_validation, self._job_callback)

~/anaconda3/envs/qiskit_env/lib/python3.9/site-packages/qiskit/utils/run_circuits.py in run_qobj(qobj, backend, qjob_config, backend_options, noise_config, skip_qobj_validation, job_callback)
    257     job_ids = []
    258     for qob in qobjs:
--> 259         job, job_id = _safe_submit_qobj(qob, backend,
    260                                         backend_options, noise_config, skip_qobj_validation)
    261         job_ids.append(job_id)

~/anaconda3/envs/qiskit_env/lib/python3.9/site-packages/qiskit/utils/run_circuits.py in _safe_submit_qobj(qobj, backend, backend_options, noise_config, skip_qobj_validation)
    178                                "Terra job error: %s ", ex)
    179         except Exception as ex:  # pylint: disable=broad-except
--> 180             logger.warning("FAILURE: Can not get job id, Resubmit the qobj to get job id."
    181                            "Error: %s ", ex)
    182 

(it actually iteratively produces a couple hundred of these terra job errors/traces as it runs the circuits, but they're all the same)

What is the expected behavior?

The helper methods pass circuits instead of qobjs to the job run method like the contract for BackendV1 implies, and this estimation runs successfully.

Suggested solutions

I don't have a ton of context on this part of the codebase or the plans for versioned backends/deprecation of qobj etc, but from my uninformed perspective, it looks like the run_circuits helper method needs to be refactored to support versioned backends. Not exactly a complete suggestion but hopefully a start and enough background for someone who has a bit more context.

@ColemanCollins ColemanCollins added the bug Something isn't working label Apr 21, 2021
@mtreinish mtreinish added mod: algorithms Related to the Algorithms module priority: high stable backport potential The bug might be minimal and/or import enough to be port to stable labels Apr 21, 2021
@mtreinish mtreinish added this to the 0.18 milestone Apr 21, 2021
@mtreinish
Copy link
Member

Yeah, your suggested solution is the path forward here. This needs to be fixed as the current state means that run_circuits doesn't respect the backends interface. It was missed because all of the providers/backend objects used for testing (ibmq, aer, and basicaer) still support qobj (albeit with a deprecation warning or pending deprecation warning) for backwards compatibility. But, we need to fix this asap, because besides ionq other backends don't support qobj and the ones tested won't support qobj in the near future.

@amilstead
Copy link

amilstead commented Apr 22, 2021

@mtreinish Not sure if this is the right ticket or if you would prefer a new one, but another issue that has cropped up out of this exploration--

https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/utils/run_circuits.py#L28 expects a Job to have the method queue_position, which appears to only exist on the IBMQ provider, and nowhere in the JobV1 class definition.

@mtreinish
Copy link
Member

It's fine here, it's really the same bug and should be fixed as part of this too. It's basically the same bug, just in a different part of that code path. It's basically that QuantumInstance/run_circuits is hardcoded to assume it's either running with only aer, basicaer, or ibmq and doesn't take any other providers into account. That's why I labeled this as high priority because this basically prevents anyone from running an algorithm class on new backends. Hopefully we'll have fix up shortly and I think this warrants a terra 0.17.2 release as soon as it is up too.

As you say queue_position isn't part of the job api (it wasn't even part of the legacy BaseJob class either) because it doesn't make sense on every provider/backend (and even if it did the semantics would be different depending on how a job queue was implemented). So that shouldn't be in the general call path anywhere in terra.

mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Apr 22, 2021
As was reported in Qiskit#6280 the algorithms path doesn't work on minimal
examples of the Backend and Jobs interface and instead only works in
practice on ibmq, aer, or basic aer backends. Part of the reason for
this was that all the testing of algorithms is done exclusively on those
providers and there wasn't a minimal example to test against that
doesn't have legacy overhead (like qobj, etc). This commit updates the
fake providers and fake backends to use the latest version of the
provider, backend, and job interfaces and to be strictly compatible (ie
no qobj support). Besides exercising the new interface version it also
enables us to test the eventual fix for Qiskit#6280 when it's available.
@amilstead
Copy link

Looks like this is getting addressed in the PR, but dropping this here for posterity-- https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/utils/run_circuits.py#L309

mergify bot pushed a commit that referenced this issue Apr 29, 2021
* Update fake backend and fake provider to use versioned interface

As was reported in #6280 the algorithms path doesn't work on minimal
examples of the Backend and Jobs interface and instead only works in
practice on ibmq, aer, or basic aer backends. Part of the reason for
this was that all the testing of algorithms is done exclusively on those
providers and there wasn't a minimal example to test against that
doesn't have legacy overhead (like qobj, etc). This commit updates the
fake providers and fake backends to use the latest version of the
provider, backend, and job interfaces and to be strictly compatible (ie
no qobj support). Besides exercising the new interface version it also
enables us to test the eventual fix for #6280 when it's available.

* Fix lint

* Add legacy fake backends using BaseBackend
@mergify mergify bot closed this as completed in #6299 May 3, 2021
ElePT pushed a commit to ElePT/qiskit-ibm-provider that referenced this issue Oct 9, 2023
…kit/qiskit#6286)

* Update fake backend and fake provider to use versioned interface

As was reported in Qiskit/qiskit#6280 the algorithms path doesn't work on minimal
examples of the Backend and Jobs interface and instead only works in
practice on ibmq, aer, or basic aer backends. Part of the reason for
this was that all the testing of algorithms is done exclusively on those
providers and there wasn't a minimal example to test against that
doesn't have legacy overhead (like qobj, etc). This commit updates the
fake providers and fake backends to use the latest version of the
provider, backend, and job interfaces and to be strictly compatible (ie
no qobj support). Besides exercising the new interface version it also
enables us to test the eventual fix for Qiskit/qiskit#6280 when it's available.

* Fix lint

* Add legacy fake backends using BaseBackend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: algorithms Related to the Algorithms module priority: high stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
6 participants