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

Port BackendSampler and BackendEstimator from Qiskit 1.1 #1678

Closed
wants to merge 3 commits into from

Conversation

t-imamichi
Copy link
Member

@t-imamichi t-imamichi commented May 14, 2024

Summary

(Update May 22)

If #1690 is merged, we don't need this PR anymore. We can remove all files related to backend primitives.


Port Qiskit/qiskit#12291
I copied SamplerPubResult so that Qiskit 1.0 works.
If we will update the requirement with qiskit>=1.1 after Qiskit 1.1 release, we don't have to keep them.
We just need backend_sampler_v2.py and backend_estimator_v2.py.

Fixes #1631

If #1690

Details and comments

microbenchmarking

from timeit import timeit

from qiskit_aer import AerSimulator
from qiskit_ibm_runtime.fake_provider import FakeSherbrooke

from qiskit import QuantumCircuit
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager

from qiskit_ibm_runtime.qiskit.primitives import BackendSamplerV2, BackendEstimatorV2

backend = AerSimulator.from_backend(FakeSherbrooke())
shots = 10000
precision = 0.01
num_copies = 10


def gen_circuit(num_qubits: int, reps: int):
    qc = QuantumCircuit(num_qubits)
    for _ in range(reps):
        qc.h(range(num_qubits))
        for i in range(0, num_qubits - 1, 2):
            qc.cx(i, i + 1)
    qc.measure_all()
    return qc


def bench_sampler_v2(qc: QuantumCircuit):
    print("\nBackendSamplerV2")
    sampler = BackendSamplerV2(backend=backend)
    print(f"{timeit(lambda: sampler.run([qc] * num_copies, shots=shots).result(), number=1)} sec")


def bench_estimator_v2(qc: QuantumCircuit):
    print("\nBackendEstimatorV2")
    estimator = BackendEstimatorV2(backend=backend)
    op = "Z" + "I" * (qc.num_qubits - 1)
    print(
        f"{timeit(lambda: estimator.run([(qc, op)] * num_copies, precision=precision).result(), number=1)} sec"
    )


qc = gen_circuit(5, 5)
pm = generate_preset_pass_manager(optimization_level=2, backend=backend)
qc2 = pm.run(qc)
bench_sampler_v2(qc2)
bench_estimator_v2(qc2)

main branch

BackendSamplerV2
3.3306877909999457 sec

BackendEstimatorV2
3.359602416996495 sec

this PR

BackendSamplerV2
0.45033658400643617 sec

BackendEstimatorV2
0.4832707500027027 sec

@coveralls
Copy link

coveralls commented May 14, 2024

Pull Request Test Coverage Report for Build 9089237108

Details

  • 109 of 130 (83.85%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.06%) to 82.707%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit_ibm_runtime/qiskit/primitives/sampler_pub_result.py 9 30 30.0%
Totals Coverage Status
Change from base Build 9084268669: -0.06%
Covered Lines: 6313
Relevant Lines: 7633

💛 - Coveralls

Copy link
Collaborator

@ihincks ihincks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @t-imamichi the improvement to performance will be nice.

We just need backend_sampler_v2.py and backend_estimator_v2.py.

I'm a bit confused. If we wait for qiskit 1.1, can't we use BackendSamplerV2 and BackendEstimatorV2 from there, and not have any such implementations in this repo?

@t-imamichi
Copy link
Member Author

I think BackendSamplerV2 and BackendEstimatorV2 were copied from qiskit to qiskit-ibm-runtime because they are not in qiskit 1.0 release yet (but they will be part of qiskit 1.1 release).
They are used here.

from ..qiskit.primitives import BackendEstimatorV2, BackendSamplerV2 # type: ignore[attr-defined]

So, if we can update the requirement qiskit>=1.1.0, we then can remove them from qiskit-ibm-runtime.
If we need to keep qiskit>=1.0.0, we need this PR to fix the performance issue of the local mode.

qiskit>=1.0.0

Copy link
Collaborator

@ihincks ihincks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @t-imamichi. Let's then merge this PR and wait for #1690.

from qiskit.primitives.containers.pub_result import PubResult


class SamplerPubResult(PubResult):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not confident that including this is worth it: it may cause isinstance confusion when people have one in qiskit and here.

However, this is not a blocker for me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on how we control the compatibility with Qiskit 1.0.
If we still allow users to use Qiskit 1.0.*, we need SamplerPubResult here. Yes, it would be confusing to Qiskit 1.1 users.
But if #1690 is merged, we can simply remove all backend primitives as well as SamplerPubResult.
I'm curious about the status of #1690

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope that #1690 can be done this sprint, we'll see what happens in sprint planning, and that we can cut a new qiskit_ibm_runtime release with the change. What do you think, @kt474 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I can help @t-imamichi with that issue this sprint (based on what needs to be split out of #1700). Then we can cut a new release.

@t-imamichi
Copy link
Member Author

Superseded by #1700

@t-imamichi t-imamichi closed this May 22, 2024
@t-imamichi t-imamichi deleted the backend-primitives branch May 23, 2024 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Explore possible regression in local simulation performance
4 participants