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

Sampler Batch pattern works via QiskitRuntimeService but errors on a fake backend in local mode #1723

Closed
garrison opened this issue Jun 1, 2024 · 0 comments · Fixed by #1795
Labels
bug Something isn't working
Milestone

Comments

@garrison
Copy link
Member

garrison commented Jun 1, 2024

Describe the bug

The pattern to use Batch mode

with Batch(backend=backend):
    sampler = SamplerV2()

works properly via QiskitRuntimeService but fails with an error on a fake backend, which I discovered in CI at Qiskit/qiskit-addon-cutting#616.

Note that currently one can work around this by using an alternative pattern:

with Batch(backend=backend) as batch:
    sampler = SamplerV2(session=batch)

Steps to reproduce

from qiskit import QuantumCircuit
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import SamplerV2, Batch
from qiskit_ibm_runtime.fake_provider import FakeManilaV2

qc0 = QuantumCircuit(2, 2)
qc0.h(0)
qc0.cx(0, 1)
qc0.measure(range(2), range(2))

qc1 = QuantumCircuit(2, 2)
qc1.h(1)
qc1.cx(1, 0)
qc0.measure(range(2), range(2))

pass_manager = generate_preset_pass_manager(optimization_level=1, backend=backend)
qc0 = pass_manager.run(qc0)
qc1 = pass_manager.run(qc1)

backend = FakeManilaV2()
with Batch(backend=backend):
    sampler = SamplerV2()
    job0 = sampler.run([qc0])
    job1 = sampler.run([qc1])

print(job0.result()[0].data.c.array)
print(job1.result()[0].data.c.array)

raises the error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[11], line 22
     20 backend = FakeManilaV2()
     21 with Batch(backend=backend):
---> 22     sampler = SamplerV2()
     23     job0 = sampler.run([qc0])
     24     job1 = sampler.run([qc1])

File ~/Qiskit/qiskit-ibm-runtime/qiskit_ibm_runtime/sampler.py:127, in SamplerV2.__init__(self, mode, backend, session, options)
    125 if mode is None:
    126     mode = session if backend and session else backend if backend else session
--> 127 BasePrimitiveV2.__init__(self, mode=mode, options=options)

File ~/Qiskit/qiskit-ibm-runtime/qiskit_ibm_runtime/base_primitive.py:106, in BasePrimitiveV2.__init__(self, mode, options)
    104     self._mode = get_cm_session()
    105     self._service = self._mode.service
--> 106     self._backend = self._service.backend(  # type: ignore
    107         name=self._mode.backend(), instance=self._mode._instance
    108     )
    109 else:
    110     self._service = (
    111         QiskitRuntimeService()
    112         if QiskitRuntimeService.global_service is None
    113         else QiskitRuntimeService.global_service
    114     )

AttributeError: 'QiskitRuntimeLocalService' object has no attribute 'backend'

Expected behavior

It should work.

Suggested solutions

Additional Information

  • qiskit-ibm-runtime version: bug exists on qiskit-ibm-runtime 0.23.0 and on current main
  • Python version:
  • Operating system: Linux
@garrison garrison added the bug Something isn't working label Jun 1, 2024
garrison added a commit to Qiskit/qiskit-addon-cutting that referenced this issue Jun 1, 2024
garrison added a commit to Qiskit/qiskit-addon-cutting that referenced this issue Jun 3, 2024
* Use batch mode correctly in the tutorials

* Work around a qiskit-ibm-runtime bug

Qiskit/qiskit-ibm-runtime#1723
mergify bot pushed a commit to Qiskit/qiskit-addon-cutting that referenced this issue Jun 3, 2024
* Use batch mode correctly in the tutorials

* Work around a qiskit-ibm-runtime bug

Qiskit/qiskit-ibm-runtime#1723
(cherry picked from commit 3162cfd)
garrison added a commit to Qiskit/qiskit-addon-cutting that referenced this issue Jun 3, 2024
* Use batch mode correctly in the tutorials

* Work around a qiskit-ibm-runtime bug

Qiskit/qiskit-ibm-runtime#1723
(cherry picked from commit 3162cfd)

Co-authored-by: Jim Garrison <garrison@ibm.com>
@kt474 kt474 added this to the 0.26.0 milestone Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants