Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.03k stars 2.32k forks source link

Error when using measurement error mitigation with VQE runtime #8315

Closed LenaPer closed 1 year ago

LenaPer commented 2 years ago

Environment

What is happening?

When I use VQE via on some backends with measurement_error_mitigation=True (when set to false it runs without issues) and try to solve on a huge qubit operator (around 500 terms), it throws me this error : 2022-07-11T09:56:28.803438232Z qiskit.exceptions.2022-07-11T09:56:28.803447210Z QiskitError: 'The used qubit index are different. ([14, 13, 12, 16, 4]) vs ([13, 14, 11, 12, 16]).\nCurrently, we only support all circuits using the same set of qubits regardless qubit order.'2022-07-11T09:56:28.803455294Z

Some job ids that threw me the error : cajf9ie8jrdfemu9plp0 and cb5v7bglsmdphf92c850 on Auckland, cajh753762hnc4c59u40 on Mumbai, cakp7om8jrdfemub7pp0 on Kolkata.

How can we reproduce the issue?

After loading a huge operator (around 500 terms), do

import numpy as np

from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit.algorithms.optimizers import L_BFGS_B, SPSA, COBYLA ###
from qiskit.circuit.library import TwoLocal
from qiskit.opflow import I, X, Y, Z

service = QiskitRuntimeService(channel="ibm_quantum")
backend = service.backend("ibm_auckland")

optimizer = SPSA(maxiter=2)
ansatz = TwoLocal(5, ['ry','rz'], 'cz', 'linear', reps=1)

initial_point = [0.0, 0.0, 0.0]

vqe_inputs = {
    "ansatz": ansatz,
    "operator": qubit_op,
    "optimizer": optimizer,  # let's only do a few iterations!
    "initial_point": initial_point,
    "measurement_error_mitigation": True,
    "shots": 10,
}

backend_options = {"backend_name": name}

intermediate_info = {"nfev": [], "parameters": [], "energy": [], "stddev": []}

def raw_callback(*args):
    # check if interim results, since both interim results (list) and final results (dict) are returned
    if type(args[1]) is list:
        job_id, (nfev, parameters, energy, stddev) = args
        intermediate_info["nfev"].append(nfev)
        intermediate_info["parameters"].append(parameters)
        intermediate_info["energy"].append(energy)
        intermediate_info["stddev"].append(stddev)

job = service.run(program_id="vqe", 
                  inputs=vqe_inputs, 
                  options=backend_options, 
                  callback=raw_callback )
print('\n\n',name)
print("Job ID:", job.job_id)

What should happen?

The code should run without any error.

Any suggestions?

No response

Cryoris commented 2 years ago

This is something we also encountered with @eggerdj with the QAOA runtime and seems to be related to the error mitigation. We're looking into it 🙂

eggerdj commented 2 years ago

The error that @Cryoris and myself have been running into relates to this: https://github.com/Qiskit/qiskit-terra/blob/16169de38ba858747e796a2acb5c96f8f37cb3b9/test/python/algorithms/test_measure_error_mitigation.py#L84-L90

nathanschnitzerNNL commented 2 years ago

I have also experienced this issue on cairo, mumbai, and auckland.

Cryoris commented 2 years ago

@nathanschnitzerNNL the one that @eggerdj posted or the one from @LenaPer?

nathanschnitzerNNL commented 2 years ago

I am referring to the one from @LenaPer . Here is the log error output that I got from my latest job cb620trshjg0cvokdsb0 (Auckland). Sorry for the formatting...

--- Logging error ---
2022-07-11T13:10:33.708114755Z Traceback (most recent call last):
2022-07-11T13:10:33.708114755Z   File "/provider/programruntime/program_starter_wrapper.py", line 75, in execute
2022-07-11T13:10:33.708114755Z     final_result = self.main(backend, self.messenger, **self.user_params)
2022-07-11T13:10:33.708114755Z   File "/code/program.py", line 118, in main
2022-07-11T13:10:33.708114755Z     result = vqe.compute_minimum_eigenvalue(operator, aux_operators)
2022-07-11T13:10:33.708114755Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/minimum_eigen_solvers/vqe.py", line 530, in compute_minimum_eigenvalue
2022-07-11T13:10:33.708114755Z     opt_result = self.optimizer.minimize(
2022-07-11T13:10:33.708114755Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/optimizers/spsa.py", line 512, in minimize
2022-07-11T13:10:33.708114755Z     get_eta, get_eps = self.calibrate(fun, x0, max_evals_grouped=self._max_evals_grouped)
2022-07-11T13:10:33.708149554Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/optimizers/spsa.py", line 333, in calibrate
2022-07-11T13:10:33.708149554Z     losses = _batch_evaluate(loss, points, max_evals_grouped)
2022-07-11T13:10:33.708149554Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/optimizers/spsa.py", line 718, in _batch_evaluate
2022-07-11T13:10:33.708149554Z     return [
2022-07-11T13:10:33.708149554Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/optimizers/spsa.py", line 719, in <listcomp>
2022-07-11T13:10:33.708149554Z     function(*point) if isinstance(point, tuple) else function(point) for point in points
2022-07-11T13:10:33.708149554Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/minimum_eigen_solvers/vqe.py", line 604, in energy_evaluation
2022-07-11T13:10:33.708149554Z     sampled_expect_op = self._circuit_sampler.convert(expect_op, params=param_bindings)
2022-07-11T13:10:33.708149554Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/opflow/converters/circuit_sampler.py", line 216, in convert
2022-07-11T13:10:33.708149554Z     sampled_statefn_dicts = self.sample_circuits(circuit_sfns=circs, param_bindings=p_b)
2022-07-11T13:10:33.708149554Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/opflow/converters/circuit_sampler.py", line 335, in sample_circuits
2022-07-11T13:10:33.708149554Z     results = self.quantum_instance.execute(
2022-07-11T13:10:33.708149554Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/utils/quantum_instance.py", line 531, in execute
2022-07-11T13:10:33.708149554Z     qubit_index, qubit_mappings = get_measured_qubits(circuits)
2022-07-11T13:10:33.708149554Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/utils/measurement_error_mitigation.py", line 69, in get_measured_qubits
2022-07-11T13:10:33.708149554Z     raise QiskitError(
2022-07-11T13:10:33.708165063Z qiskit.exceptions.QiskitError: \'The used qubit index are different. ([7, 4, 1, 2, 3, 5, 8, 11, 14, 16, 19, 22, 25, 26]) vs ([13, 12, 10, 7, 4, 1, 2, 3, 5, 8, 11, 14, 16, 19]).\
Currently, we only support all circuits using the same set of qubits regardless qubit order.\'
2022-07-11T13:10:33.708175386Z 

2022-07-11T13:10:33.708175386Z During handling of the above exception, another exception occurred:
2022-07-11T13:10:33.708175386Z 
2022-07-11T13:10:33.708175386Z Traceback (most recent call last):
2022-07-11T13:10:33.708185366Z   File "/usr/lib64/python3.8/logging/__init__.py", line 1085, in emit
2022-07-11T13:10:33.708185366Z     msg = self.format(record)
2022-07 11T13:10:33.708194274Z   File "/usr/lib64/python3.8/logging/__init__.py", line 929, in format
2022-07-11T13:10:33.708194274Z     return fmt.format(record)
2022-07-11T13:10:33.708194274Z   File "/usr/lib64/python3.8/logging/__init__.py", line 668, in format
2022-07-11T13:10:33.708194274Z     record.message = record.getMessage()
2022-07-11T13:10:33.708207512Z   File "/usr/lib64/python3.8/logging/__init__.py", line 373, in getMessage
2022-07-11T13:10:33.708207512Z     msg = msg % self.args
2022-07-11T13:10:33.708207512Z TypeError: not all arguments converted during string formatting
2022-07-11T13:10:33.708216638Z Call stack:
2022-07-11T13:10:33.708342746Z   File "/code/program_starter.py", line 76, in <module>
2022-07-11T13:10:33.708342746Z     starter.execute()
2022-07-11T13:10:33.708342746Z   File "/provider/programruntime/program_starter_wrapper.py", line 79, in execute
2022-07-11T13:10:33.708342746Z     
logger.error(f"Error executing program job {self.provider.execution_id}", ex)
2022-07-11T13:10:33.708356042Z Message: \'Error executing program job cb620trshjg0cvokdsb0\'
2022-07-11T13:10:33.708356042Z Arguments: (QiskitError(\'The used qubit index are different. ([7, 4, 1, 2, 3, 5, 8, 11, 14, 16, 19, 22, 25, 26]) vs ([13, 12, 10, 7, 4, 1, 2, 3, 5, 8, 11, 14, 16, 19]).\
Currently, we only support all circuits using the same set of qubits regardless qubit order.\'),)
2022-07-11T13:10:33.711885579Z 07/11/2022 01:10:33 PM - jaeger_tracing - INFO Span publisher exited
2022-07-11T13:10:33.712102320Z Traceback (most recent call last):
2022-07-11T13:10:33.712117763Z   File "/code/program_starter.py", line 76, in <module>
2022-07-11T13:10:33.712227609Z     starter.execute()
2022-07-11T13:10:33.712241775Z   File "/provider/programruntime/program_starter_wrapper.py", line 81, in execute
2022-07-11T13:10:33.712290215Z     raise ex
2022-07-11T13:10:33.712302354Z   File "/provider/programruntime/program_starter_wrapper.py", line 75, in execute
2022-07-11T13:10:33.712339406Z     final_result = self.main(backend, self.messenger, **self.user_params)2022-07-11T13:10:33.712349231Z 

2022-07-11T13:10:33.712349231Z   File "/code/program.py", line 118, in main
2022-07-11T13:10:33.712422115Z     result = vqe.compute_minimum_eigenvalue(operator, aux_operators)
2022-07-11T13:10:33.712431854Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/minimum_eigen_solvers/vqe.py", line 530, in compute_minimum_eigenvalue
2022-07-11T13:10:33.712548295Z     opt_result = self.optimizer.minimize(
2022-07-11T13:10:33.712557326Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/optimizers/spsa.py", line 512, in minimize
2022-07-11T13:10:33.712676788Z     get_eta, get_eps = self.calibrate(fun, x0, max_evals_grouped=self._max_evals_grouped)
2022-07-11T13:10:33.712685477Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/optimizers/spsa.py", line 333, in calibrate

2022-07-11T13:10:33.712776161Z     losses = _batch_evaluate(loss, points, max_evals_grouped)
2022-07-11T13:10:33.712785352Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/optimizers/spsa.py", line 718, in _batch_evaluate
2022-07-11T13:10:33.712916701Z     return [
2022-07-11T13:10:33.712925118Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/optimizers/spsa.py", line 719, in <listcomp>
2022-07-11T13:10:33.713054414Z     function(*point) if isinstance(point, tuple) else function(point) for point in points
2022-07-11T13:10:33.713062397Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/algorithms/minimum_eigen_solvers/vqe.py", line 604, in energy_evaluation
2022-07-11T13:10:33.713167319Z     sampled_expect_op = self._circuit_sampler.convert(expect_op, params=param_bindings)
2022-07-11T13:10:33.713176113Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/opflow/converters/circuit_sampler.py", line 216, in convert
2022-07-11T13:10:33.713244386Z     sampled_statefn_dicts = self.sample_circuits(circuit_sfns=circs, param_bindings=p_b)

2022-07-11T13:10:33.713253412Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/opflow/converters/circuit_sampler.py", line 335, in sample_circuits
2022-07-11T13:10:33.713328296Z     results = self.quantum_instance.execute(
2022-07-11T13:10:33.713336584Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/utils/quantum_instance.py", line 531, in execute

2022-07-11T13:10:33.713447590Z     qubit_index, qubit_mappings = get_measured_qubits(circuits)
2022-07-11T13:10:33.713457192Z   File "/opt/app-root/lib64/python3.8/site-packages/qiskit/utils/measurement_error_mitigation.py", line 69, in get_measured_qubits
2022-07-11T13:10:33.713505479Z     raise QiskitError(
2022-07-11T13:10:33.713519219Z qiskit.exceptions.2022-07-11T13:10:33.713527464Z QiskitError: \'The used qubit index are different. ([7, 4, 1, 2, 3, 5, 8, 11, 14, 16, 19, 22, 25, 26]) vs ([13, 12, 10, 7, 4, 1, 2, 3, 5, 8, 11, 14, 16, 19]).\
Currently, we only support all circuits using the same set of qubits regardless qubit order.\'2022-07-11T13:10:33.713536028Z 
'
MarcoBarroca commented 2 years ago

I'm also having this issue. I can run measurement error mitigation without any issues using either the VQEClient and QiskitRuntimeService on open systems like manilla and nairobi, but if I try any of the closed ones I get this error.

MarcoBarroca commented 2 years ago

Not sure how but a job with measurement error mitigation on ibmq_montreal completed without any issues.

Here is the log for the job.

woodsp-ibm commented 2 years ago

The behavior is noted in an issue here #5730 As noted there it can be device specific. It even recently changed for a device that had worked that later did not (discussions out on public Qiskit slack)

LenaPer commented 1 year ago

I saw it was removed from the 0.22 milestone, do you have an idea on when it will be solved? @1ucian0 @mtreinish

nonhermitian commented 1 year ago

I do not think that this is something that should be solved on the Terra side of things. Namely, this is an issue with the limitations of the QuantumInstance; a class that is being deprecated in favor of runtime primitives. The primitives do not have the same issue because they use improved mitigation methods that the QuantumInstance cannot handle due to lack of flexibility. In principle, those who wrote the VQE runtime can refactor it on their side and fix the issue without any changes to Terra. Indeed, this is one of the benefits of the abstraction into the cloud.

woodsp-ibm commented 1 year ago

The VQE Runtime is now deprecated, along with QuantumInstance which never supported circuits on different qubits (hence the error message it raised). The solution/workaround is to move to using the Estimator primitive from IBM Qiskit Runtime which supports various error mitigation capabilities via a resilience_level. Refer to the link for more information and code examples etc. This issue is therefore being closed.