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.18k stars 2.35k forks source link

Issue mismatch between parameters trying to run minimum eigen optimizer on real quantum hardware. #11207

Open bert799 opened 11 months ago

bert799 commented 11 months ago

Environment

What is happening?

when running the following code I get the error: QiskitError: 'Mismatch between run_config.parameter_binds and all circuit parameters. Parameter binds: [] Circuit parameters: [ParameterView([ParameterVectorElement(θ[0]), ParameterVectorElement(θ[1]), ParameterVectorElement(θ[2]), ParameterVectorElement(θ[3]), ParameterVectorElement(θ[4]), ParameterVectorElement(θ[5]), ParameterVectorElement(θ[6]), ParameterVectorElement(θ[7]), ParameterVectorElement(θ[8]), ParameterVectorElement(θ[9]), ParameterVectorElement(θ[10]), ParameterVectorElement(θ[11]), ParameterVectorElement(θ[12]), ParameterVectorElement(θ[13]), ParameterVectorElement(θ[14]), ParameterVectorElement(θ[15]), ParameterVectorElement(θ[16]), ParameterVectorElement(θ[17]), ParameterVectorElement(θ[18]), ParameterVectorElement(θ[19]), ParameterVectorElement(θ[20]), ParameterVectorElement(θ[21]), ParameterVectorElement(θ[22]), ParameterVectorElement(θ[23]), ParameterVectorElement(θ[24]), ParameterVectorElement(θ[25]), ParameterVectorElement(θ[26]), ParameterVectorElement(θ[27]), ParameterVectorElement(θ[28]), ParameterVectorElement(θ[29]), ParameterVectorElement(θ[30]), ParameterVectorElement(θ[31]), ParameterVectorElement(θ[32]), ParameterVectorElement(θ[33]), ParameterVectorElement(θ[34]), ParameterVectorElement(θ[35])])]'

How can we reproduce the issue?

import numpy as np
from qiskit import IBMQ

IBMQ.load_account()
provider = IBMQ.get_provider(hub='ibm-q')
backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= 2 
                                       and not x.configuration().simulator 
                                       and x.status().operational==True))
print("least busy backend: ", backend)

num_qubits = 2
ansatz = TwoLocal(num_qubits, 'ry', 'cz')

optimizer = COBYLA()
vqe = SamplingVQE(sampler=backend, ansatz=ansatz, optimizer=optimizer)
meo = MinimumEigenOptimizer(min_eigen_solver= vqe)
result = meo.solve(qp)

#t_qc = transpile(qc, backend, optimization_level=3)
#job = backend.run(t_qc)

print('result:\n', result.prettyprint())
print(type(result))
print('\n index of the chosen items:', interpret(result)) ````

What should happen?

Given that running a very similar piece of code, substituting the real hardware with an aer simulator the code runs smoothly and returns the expected answer.

Any suggestions?

No response

woodsp-ibm commented 11 months ago

You are supplying a backend type to SamplerVQE where it needs a Sampler (something of type BaseSampler).

sampler (BaseSampler) – The sampler primitive to sample the circuits.

You would need to use the Sampler from the Qiskit Runtime to work with real devices (or the remote simulator). See https://qiskit.org/ecosystem/ibm-runtime/tutorials/how-to-getting-started-with-sampler.html

For what you are doing there is a similar example in Qiskit Optimization tutorials here. Its using the reference Sampler from Qiskit in this case but you would use the Sampler from Runtime, ideally using a runtime Session. The runtime docs have further information.

Lihsayuri commented 11 months ago

Hello! I'm currently working on the project with @bert799 , and we've encountered an issue while running the provided code snippet. We tried implementing the Sampler from Qiskit Runtime, as suggested in the documentation. However, when attempting to execute the code, it seems to get stuck in an infinite loop while running on the IBM simulator.

Below is the code snippet we're using:

#from qiskit.primitives import Sampler
from qiskit_ibm_runtime import QiskitRuntimeService, Sampler
from qiskit.algorithms.optimizers import COBYLA
from qiskit.algorithms.minimum_eigensolvers import VQE, SamplingVQE
from qiskit.circuit.library import TwoLocal
from qiskit.primitives import Estimator
from qiskit_optimization.converters import QuadraticProgramToQubo

values = [13, 40, 18]
weights = [2, 4, 6]
max_weight = 10

qp = knapsack_quadratic_program(values, weights, max_weight)
print(qp)

seed = 123
algorithm_globals.random_seed = seed
service = QiskitRuntimeService(channel="ibm_quantum")
options = Options(resilience_level=1)

num_qubits = 2
ansatz = TwoLocal(num_qubits, 'ry', 'cz')

backend = service.backend("ibmq_qasm_simulator")

sampler = Sampler(backend=backend) 
optimizer = COBYLA()

vqe = SamplingVQE(sampler=sampler, ansatz=ansatz, optimizer=optimizer)
meo = MinimumEigenOptimizer(min_eigen_solver=vqe)
result = meo.solve(qp)

print('result:\n', result.prettyprint())
print(type(result))
print('\n index of the chosen items:', interpret(result)) 

Additionally, here are the logs that the code snippet is generating:

maximize 13*x_0 + 40*x_1 + 18*x_2 (3 variables, 1 constraints, 'Knapsack')
base_primitive._run_primitive:INFO:2023-11-16 17:45:43,772: Submitting job using options {'optimization_level': 1, 'resilience_level': 0, 'max_execution_time': None, 'transpilation': {'skip_transpilation': False, 'initial_layout': None, 'layout_method': None, 'routing_method': None, 'approximation_degree': None, 'optimization_settings': {'level': 1}, 'coupling_map': None, 'basis_gates': None}, 'resilience': {'noise_amplifier': 'TwoQubitAmplifier', 'noise_factors': (1, 3, 5), 'extrapolator': 'LinearExtrapolator', 'level': 0}, 'execution': {'shots': 4000, 'init_qubits': True, 'noise_model': None, 'seed_simulator': None}, 'environment': {'log_level': 'WARNING', 'callback': None, 'job_tags': []}, 'simulator': {'noise_model': None, 'seed_simulator': None, 'coupling_map': None, 'basis_gates': None}}
base_primitive._run_primitive:INFO:2023-11-16 17:45:45,552: Submitting job using options {'optimization_level': 1, 'resilience_level': 0, 'max_execution_time': None, 'transpilation': {'skip_transpilation': False, 'initial_layout': None, 'layout_method': None, 'routing_method': None, 'approximation_degree': None, 'optimization_settings': {'level': 1}, 'coupling_map': None, 'basis_gates': None}, 'resilience': {'noise_amplifier': 'TwoQubitAmplifier', 'noise_factors': (1, 3, 5), 'extrapolator': 'LinearExtrapolator', 'level': 0}, 'execution': {'shots': 4000, 'init_qubits': True, 'noise_model': None, 'seed_simulator': None}, 'environment': {'log_level': 'WARNING', 'callback': None, 'job_tags': []}, 'simulator': {'noise_model': None, 'seed_simulator': None, 'coupling_map': None, 'basis_gates': None}}
base_primitive._run_primitive:INFO:2023-11-16 17:45:47,340: Submitting job using options {'optimization_level': 1, 'resilience_level': 0, 'max_execution_time': None, 'transpilation': {'skip_transpilation': False, 'initial_layout': None, 'layout_method': None, 'routing_method': None, 'approximation_degree': None, 'optimization_settings': {'level': 1}, 'coupling_map': None, 'basis_gates': None}, 'resilience': {'noise_amplifier': 'TwoQubitAmplifier', 'noise_factors': (1, 3, 5), 'extrapolator': 'LinearExtrapolator', 'level': 0}, 'execution': {'shots': 4000, 'init_qubits': True, 'noise_model': None, 'seed_simulator': None}, 'environment': {'log_level': 'WARNING', 'callback': None, 'job_tags': []}, 'simulator': {'noise_model': None, 'seed_simulator': None, 'coupling_map': None, 'basis_gates': None}}
base_primitive._run_primitive:INFO:2023-11-16 17:45:49,367: Submitting job using options {'optimization_level': 1, 'resilience_level': 0, 'max_execution_time': None, 'transpilation': {'skip_transpilation': False, 'initial_layout': None, 'layout_method': None, 'routing_method': None, 'approximation_degree': None, 'optimization_settings': {'level': 1}, 'coupling_map': None, 'basis_gates': None}, 'resilience': {'noise_amplifier': 'TwoQubitAmplifier', 'noise_factors': (1, 3, 5), 'extrapolator': 'LinearExtrapolator', 'level': 0}, 'execution': {'shots': 4000, 'init_qubits': True, 'noise_model': None, 'seed_simulator': None}, 'environment': {'log_level': 'WARNING', 'callback': None, 'job_tags': []}, 'simulator': {'noise_model': None, 'seed_simulator': None, 'coupling_map': None, 'basis_gates': None}}
base_primitive._run_primitive:INFO:2023-11-16 17:45:50,888: Submitting job using options {'optimization_level': 1, 'resilience_level': 0, 'max_execution_time': None, 'transpilation': {'skip_transpilation': False, 'initial_layout': None, 'layout_method': None, 'routing_method': None, 'approximation_degree': None, 'optimization_settings': {'level': 1}, 'coupling_map': None, 'basis_gates': None}, 'resilience': {'noise_amplifier': 'TwoQubitAmplifier', 'noise_factors': (1, 3, 5), 'extrapolator': 'LinearExtrapolator', 'level': 0}, 'execution': {'shots': 4000, 'init_qubits': True, 'noise_model': None, 'seed_simulator': None}, 'environment': {'log_level': 'WARNING', 'callback': None, 'job_tags': []}, 'simulator': {'noise_model': None, 'seed_simulator': None, 'coupling_map': None, 'basis_gates': None}}
base_primitive._run_primitive:INFO:2023-11-16 17:45:52,450: Submitting job using options {'optimization_level': 1, 'resilience_level': 0, 'max_execution_time': None, 'transpilation': {'skip_transpilation': False, 'initial_layout': None, 'layout_method': None, 'routing_method': None, 'approximation_degree': None, 'optimization_settings': {'level': 1}, 'coupling_map': None, 'basis_gates': None}, 'resilience': {'noise_amplifier': 'TwoQubitAmplifier', 'noise_factors': (1, 3, 5), 'extrapolator': 'LinearExtrapolator', 'level': 0}, 'execution': {'shots': 4000, 'init_qubits': True, 'noise_model': None, 'seed_simulator': None}, 'environment': {'log_level': 'WARNING', 'callback': None, 'job_tags': []}, 'simulator': {'noise_model': None, 'seed_simulator': None, 'coupling_map': None, 'basis_gates': None}}

The code seems to repeatedly execute the job submission without producing the expected output or completing the execution.

We've been following the structure documented in the Qiskit Optimization tutorials, and while we're attempting to use the Qiskit Runtime Sampler, the code doesn't seem to provide the expected output and remains in continuous execution on the simulator.

Could you please advise us on how to resolve this issue or provide guidance on properly integrating the Sampler from Qiskit Runtime within our code to work with IBM's backend service?

Thank you in advance for any help or suggestions! We're relatively new to this, so we appreciate your patience and guidance :)

woodsp-ibm commented 11 months ago

Creating and configuring the Runtime Sampler is covered by the docs I linked above i.e. here https://qiskit.org/ecosystem/ibm-runtime/

SamplingVQE is a variational algorithm where the minimum value of an objective function it defines is determined by the optimizer. The objective function will execute circuits to compute the function value at any given point the optimizer chooses - the point is used to set the parameters in the ansatz. The optimizer will have some strategy to locate minimum from the initial_point, which is random by default though can be set. This will end up sending a job for each execution and each point until the optimizer reaches a minimum value or hits its maxiter. What you can also do is to setup the callback for SamplngVQE via which you can monitor the progress - this tutorial was done for VQE callback but it should be the same way for SamplingVQE.

You can also check the overall algorithm/code is working as expected, completely locally, by using qiskit.primitives Sampler, or the sampler from Aer qiskit_aer.primitives Sampler, instead of using the remote simulator.

One further thing I will note is that qiskit.algorithms has been deprecated in Qiskit and instead you are recommended to use qiskit_algorithms where the code was relocated to. And since it was deprecated a while ago it has already been deleted from Qiskit here ahead of the next release. You should simply be able to pip install qiskit-algorithms and then change the import so that instead of qiskit.algorithms its qiskit_algorithms i.e replace the . by _.