Qiskit / qiskit-ibm-runtime

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

Sampler fails when submitting large numbers of circuits #264

Closed nonhermitian closed 8 months ago

nonhermitian commented 2 years ago

Describe the bug

N = 12
qc = QuantumCircuit(N)
qc.x(range(N))
qc.h(range(N))
qc.ch(range(N//2,0,-1), range(N//2-1,-1,-1))
qc.ch(range(N//2, N-1), range(N//2+1,N))
qc.measure_all()

with ibm_sampler(circuits=[qc]*300, skip_transpilation=False) as sampler:
    st = time.time()
    result = sampler(
        circuit_indices=[list(range(300))],
        shots=1e5,
    )

gives

Traceback (most recent call last):
  Input In [22] in <cell line: 10>
    result = sampler(
  File /opt/miniconda3/envs/qiskit/lib/python3.10/site-packages/qiskit_ibm_runtime/sampler.py:99 in __call__
    quasi_dists=raw_result["quasi_dists"],
TypeError: string indices must be integers

It also gives this error extremely fast. Namely the above should take ~2 hours to compute.

Steps to reproduce

Expected behavior

Suggested solutions

Additional Information

rathishcholarajan commented 2 years ago

@ikkoham @t-imamichi Could you please check if this is an issue in the sampler primitive?

t-imamichi commented 2 years ago

I made a script based on the description. It returns an error in 12 seconds.

I think circuit_indices=[list(range(K))] needs to be replaced with circuit_indices=list(range(K)). The interface is defined as circuit_indices: Sequence[int]. https://github.com/Qiskit/qiskit-terra/blob/60fdd33b264e0a9cb538189f796ef578daae182d/qiskit/primitives/base_sampler.py#L165

Environment OS: macOS 12.3.1 Python: 3.9.12 qiskit-ibm-runtime: 0.3.0 qiskit-terra: 0.20.0

from timeit import default_timer
import numpy as np
from qiskit import Aer, QuantumCircuit
from qiskit_ibm_runtime import IBMRuntimeService, IBMSampler, RuntimeJobFailureError

service = IBMRuntimeService()
ibm_sampler = IBMSampler(service=service, backend="ibmq_qasm_simulator")

N = 12
qc = QuantumCircuit(N)
qc.x(range(N))
qc.h(range(N))
qc.ch(range(N//2,0,-1), range(N//2-1,-1,-1))
qc.ch(range(N//2, N-1), range(N//2+1,N))
qc.measure_all()

K = 300
with ibm_sampler(circuits=[qc]*K, skip_transpilation=False) as sampler:
    start = default_timer()
    try:
        result = sampler(
            circuit_indices=[list(range(K))],
            # circuit_indices=list(range(K)),  # should be OK
            shots=1e5,
        )
    except RuntimeJobFailureError as ex:
        print(ex.message)

print(default_timer() - start, 'sec')

output

2022-04-07T00:46:49.221307318-05:00 04/07/2022 05:46:49 AM - qiskit.quantum_info.synthesis.two_qubit_decompose - DEBUG Requested fidelity: None calculated fidelity: 1.0 actual fidelity 1.0000000000000004
2022-04-07T00:46:50.237597248-05:00 /code/expval_venv/lib/python3.8/site-packages/qiskit/circuit/qpy_serialization.py:1767: UserWarning: The qiskit version used to generate the provided QPY file, 0.20.0, is newer than the current qiskit version 0.19.2. This may result in an error if the QPY file uses instructions not present in this current qiskit version
2022-04-07T00:46:50.237597248-05:00   warnings.warn(
2022-04-07T00:46:50.237597248-05:00 04/07/2022 05:46:50 AM - jaeger_tracing - INFO Initializing Jaeger Tracer with UDP reporter
2022-04-07T00:46:50.240131756-05:00 04/07/2022 05:46:50 AM - asyncio - DEBUG Using selector: EpollSelector
2022-04-07T00:46:50.244054595-05:00 04/07/2022 05:46:50 AM - jaeger_tracing - INFO Using sampler ConstSampler(True)
2022-04-07T00:46:50.245473273-05:00 04/07/2022 05:46:50 AM - jaeger_tracing - INFO opentracing.tracer initialized to <jaeger_client.tracer.Tracer object at 0x7f4cfdce13d0>[app_name=ntc-job]
2022-04-07T00:46:50.347696593-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.16356 (ms)
2022-04-07T00:46:50.349101967-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.08798 (ms)
2022-04-07T00:46:50.349504608-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.06175 (ms)
2022-04-07T00:46:50.349920672-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05937 (ms)
2022-04-07T00:46:50.350248803-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05412 (ms)
2022-04-07T00:46:50.350589205-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05460 (ms)
2022-04-07T00:46:50.350938866-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05579 (ms)
2022-04-07T00:46:50.351288672-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05436 (ms)
2022-04-07T00:46:50.351845898-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.07915 (ms)
2022-04-07T00:46:50.352190990-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05674 (ms)
2022-04-07T00:46:50.352826022-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.10061 (ms)
2022-04-07T00:46:50.353444659-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.06437 (ms)
2022-04-07T00:46:50.353853454-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.07939 (ms)
2022-04-07T00:46:50.354205356-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05555 (ms)
2022-04-07T00:46:50.354484921-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05293 (ms)
2022-04-07T00:46:50.354763851-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05102 (ms)
2022-04-07T00:46:50.355094946-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05341 (ms)
2022-04-07T00:46:50.355368238-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05221 (ms)
2022-04-07T00:46:50.355628908-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05031 (ms)
2022-04-07T00:46:50.355964623-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05245 (ms)
2022-04-07T00:46:50.356326716-05:00 04/07/2022 05:46:50 AM - qiskit.compiler.assembler - INFO Total Assembly Time - 0.05937 (ms)
2022-04-07T00:46:50.356720132-05:00 04/07/2022 05:46:50 AM - programruntime.runtime_provider - DEBUG QuantumProgramProvider initialization completed. Program ID=sampler, execution ID=c977lh53umar17a8pg70, hub=, group=, project=.
2022-04-07T00:46:50.359716636-05:00 Traceback (most recent call last):
2022-04-07T00:46:50.359716636-05:00   File "/code/program_starter.py", line 57, in <module>
2022-04-07T00:46:50.359716636-05:00     starter.run(user_params, main)
2022-04-07T00:46:50.359716636-05:00   File "/provider/programruntime/program_starter_wrapper.py", line 89, in run
2022-04-07T00:46:50.359716636-05:00     final_result = main(backend, messenger, **user_params)
2022-04-07T00:46:50.359716636-05:00   File "/code/program.py", line 36, in main
2022-04-07T00:46:50.359716636-05:00     raw_result = sampler(
2022-04-07T00:46:50.359716636-05:00   File "/code/expval_venv/lib/python3.8/site-packages/qiskit_primitives/sampler/sampler.py", line 178, in __call__
2022-04-07T00:46:50.359716636-05:00     if len(value) != len(self.parameters[i]):
2022-04-07T00:46:50.359716636-05:00 TypeError: tuple indices must be integers or slices, not list

11.106608441999999 sec
nonhermitian commented 2 years ago

Yes, this looks to be my mistake. However, there should really be some validation for this.

However, the underlying reason for my mistake is that I need to change the circuit_indices when submitting different numbers of circuits. The standard use case will always be the case where the number of circuits I send to the sampler is the number I want to evaluate later. Thus having to set the indices is just plan annoying. I think it is fine if calling the primitive directly as a raw call (Even though it is still silly), but the interface provided by Qiskit can easily take None as a default, and have that internally set the indices to be over all the circuits.

t-imamichi commented 2 years ago

We will remove the behavior of circuit_indices of the reference implementation (Qiskit terra) that takes None as a default and set all circuit indices internally. See Lev's comment https://github.com/Qiskit/qiskit-terra/issues/7836#issuecomment-1085939623 We can remove the behavior from the runtime version of sampler and estimator too.

drew-distefano commented 8 months ago

Closing since circuit_indices was removed a few months ago