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

Improve performance of BackendSamplerV2 and EstimatorV2 #12291

Merged
merged 9 commits into from
Apr 25, 2024

Conversation

t-imamichi
Copy link
Member

@t-imamichi t-imamichi commented Apr 22, 2024

Summary

Fixes #12290
Addresses Qiskit/qiskit-ibm-runtime#1631 (need to port this PR to qiskit-ibm-runtime to close it)

TODO

  • BackendSamplerV2
  • BackendEstimatorV2
  • reno

Details and comments

Sampler

from timeit import timeit

from qiskit_aer import AerSimulator
from qiskit_ibm_runtime.fake_provider import FakeSherbrooke

from qiskit import QuantumCircuit
from qiskit.primitives import BackendSampler, BackendSamplerV2
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager

backend = AerSimulator.from_backend(FakeSherbrooke())
shots = 10000
num_copies = 20


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_v1(qc: QuantumCircuit):
    print("\nBackendSamplerV1")
    sampler = BackendSampler(backend)
    print(f"{timeit(lambda: sampler.run([qc] * num_copies, shots=shots).result(), number=1)} sec")


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")


qc = gen_circuit(5, 5)
pm = generate_preset_pass_manager(optimization_level=2, backend=backend)
qc2 = pm.run(qc)
bench_sampler_v1(qc2)
bench_sampler_v2(qc2)
# main
BackendSamplerV1
0.714542875000916 sec

BackendSamplerV2
9.41767649999747 sec
# this PR
BackendSamplerV1
0.7391576250010985 sec

BackendSamplerV2
0.7517977079987759 sec

Estimator

from math import sqrt
from timeit import timeit

from qiskit_aer import AerSimulator
from qiskit_ibm_runtime.fake_provider import FakeSherbrooke

from qiskit import QuantumCircuit
from qiskit.primitives import BackendEstimator, BackendEstimatorV2
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.quantum_info import random_pauli_list, SparsePauliOp

backend = AerSimulator.from_backend(FakeSherbrooke())
shots = 8192
precision = sqrt(1 / shots)
num_qubits = 5
num_copies = 10
seed = 123


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)
    return qc


def bench_estimator_v1(qc: QuantumCircuit, obs: SparsePauliOp):
    print("\nBackendEstimatorV1")
    estimator = BackendEstimator(backend)
    print(f"{timeit(lambda: estimator.run([qc] * num_copies, [obs] * num_copies, shots=shots).result(), number=1)} sec")


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


qc = gen_circuit(num_qubits, 5)
pm = generate_preset_pass_manager(optimization_level=2, backend=backend)
qc2 = pm.run(qc)
obs = SparsePauliOp(random_pauli_list(num_qubits, 5, phase=False)).apply_layout(qc2.layout)
bench_estimator_v1(qc2, obs)
bench_estimator_v2(qc2, obs)
# main
BackendEstimatorV1
2.3618681670050137 sec

BackendEstimatorV2
6.282726375000493 sec
# this PR
BackendEstimatorV1
2.3475185420029447 sec

BackendEstimatorV2
2.151061958000355 sec

@coveralls
Copy link

coveralls commented Apr 22, 2024

Pull Request Test Coverage Report for Build 8802706774

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 88 of 88 (100.0%) changed or added relevant lines in 2 files are covered.
  • 27 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.02%) to 89.285%

Files with Coverage Reduction New Missed Lines %
qiskit/primitives/containers/data_bin.py 1 97.92%
crates/qasm2/src/lex.rs 3 92.11%
qiskit/transpiler/preset_passmanagers/builtin_plugins.py 11 96.47%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 8783263005: 0.02%
Covered Lines: 60428
Relevant Lines: 67680

💛 - Coveralls

@t-imamichi t-imamichi changed the title Improve the performance of Backend{SamplerV2, EstimatorV2} (WIP) Improve the performance of Backend{SamplerV2, EstimatorV2} Apr 22, 2024
@t-imamichi t-imamichi added performance mod: primitives Related to the Primitives module labels Apr 22, 2024
@t-imamichi t-imamichi requested a review from ihincks April 22, 2024 10:40
@t-imamichi t-imamichi changed the title (WIP) Improve the performance of Backend{SamplerV2, EstimatorV2} Improve performance of BackendSamplerV2 and EstimatorV2 Apr 22, 2024
@t-imamichi t-imamichi marked this pull request as ready for review April 22, 2024 13:36
@t-imamichi t-imamichi requested review from a team as code owners April 22, 2024 13:36
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core
  • @ajavadia
  • @levbishop
  • @t-imamichi

Copy link
Contributor

@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 for this quick work. I've left a few comments, which I mostly made as I read through the file figuring out what was going on. It all makes sense to me, so LGTM!

qiskit/primitives/backend_estimator_v2.py Show resolved Hide resolved
qiskit/primitives/backend_estimator_v2.py Outdated Show resolved Hide resolved
qiskit/primitives/backend_estimator_v2.py Outdated Show resolved Hide resolved
qiskit/primitives/backend_estimator_v2.py Show resolved Hide resolved
qiskit/primitives/backend_estimator_v2.py Outdated Show resolved Hide resolved
qiskit/primitives/backend_estimator_v2.py Outdated Show resolved Hide resolved
qiskit/primitives/backend_estimator_v2.py Outdated Show resolved Hide resolved
qiskit/primitives/backend_sampler_v2.py Show resolved Hide resolved
Co-authored-by: Ian Hincks <ian.hincks@gmail.com>
@t-imamichi
Copy link
Member Author

I refactored BackendEstimatorV2 by introducing an internal data class _PreprocessedData.

Copy link
Contributor

@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.

LGTM

@t-imamichi t-imamichi added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Apr 25, 2024
@t-imamichi t-imamichi added this pull request to the merge queue Apr 25, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Apr 25, 2024
@t-imamichi t-imamichi added this pull request to the merge queue Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog mod: primitives Related to the Primitives module performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Performance regression of BackendSamplerV2 and BackendEstimatorV2
4 participants