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 ConstrainedReschedule pass #10077

Merged
merged 6 commits into from
May 9, 2023

Conversation

alexanderivrii
Copy link
Contributor

@alexanderivrii alexanderivrii commented May 4, 2023

Summary

This PR reimplements the ConstrainedReschedule transpiler pass to run in linear time, by going over the nodes once and pushing back the start times in case of misalignments and overlaps. This should solve the performance problem described in #10067.

Details and comments

The older commit 6c5eab6 contains both the original and the modified version of ConstrainedReschedule pass (with the newer code residing in transpiler/passes/scheduling/alignments/reschedule_improved.py), allowing to run both versions and compare the output circuits. On my laptop, for the random_circuit(50, 5, measure=True, seed=202342), the new code runs in 0.43 seconds compared to the old code running in 444.25 seconds.

I have tried to keep the code logic as close as possible to the original (in order to avoid introducing bugs), there are definitely places that could be rewritten in a more clear and shorter fashion.

@mtreinish has further suggestions to improve runtimes based on bd3cbb2.

@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

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

  • @Qiskit/terra-core

@coveralls
Copy link

coveralls commented May 4, 2023

Pull Request Test Coverage Report for Build 4906024179

  • 50 of 51 (98.04%) changed or added relevant lines in 1 file are covered.
  • 241 unchanged lines in 20 files lost coverage.
  • Overall coverage decreased (-0.02%) to 85.845%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/transpiler/passes/scheduling/alignments/reschedule.py 50 51 98.04%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/scheduling/alignments/reschedule.py 1 96.15%
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 1 90.25%
qiskit/utils/multiprocessing.py 1 90.91%
crates/qasm2/src/lex.rs 2 91.65%
qiskit/extensions/quantum_initializer/squ.py 2 80.0%
qiskit/qasm2/init.py 2 93.33%
crates/accelerate/src/sabre_swap/layer.rs 4 97.32%
qiskit/algorithms/gradients/finite_diff_estimator_gradient.py 4 94.52%
qiskit/algorithms/gradients/finite_diff_sampler_gradient.py 4 95.51%
qiskit/transpiler/passes/basis/basis_translator.py 5 98.11%
Totals Coverage Status
Change from base Build 4876894924: -0.02%
Covered Lines: 70792
Relevant Lines: 82465

💛 - Coveralls

@alexanderivrii alexanderivrii changed the title [WIP] Improve performance of ConstrainedReschedule pass Improve performance of ConstrainedReschedule pass May 4, 2023
@alexanderivrii alexanderivrii marked this pull request as ready for review May 4, 2023 13:47
@alexanderivrii alexanderivrii requested a review from a team as a code owner May 4, 2023 13:47
@kdk kdk linked an issue May 4, 2023 that may be closed by this pull request
@mtreinish mtreinish added this to the 0.24.1 milestone May 4, 2023
@kdk
Copy link
Member

kdk commented May 4, 2023

For posterity, performance from example in #10067 looks much better.
Before #10077:
image
After:
image

Copy link
Member

@kdk kdk left a comment

Choose a reason for hiding this comment

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

Looks good so far. One comment on documentation and a potential test case.

@kdk kdk added stable backport potential The bug might be minimal and/or import enough to be port to stable performance labels May 4, 2023
Comment on lines 226 to 232
try:
misalignment = node_start_time[node] % alignment
if misalignment == 0:
continue
shift = max(0, alignment - misalignment)
start_time = node_start_time[node]
except KeyError as ex:
raise TranspilerError(
f"Start time of {repr(node)} is not found. This node is likely added after "
"this circuit is scheduled. Run scheduler again."
) from ex
Copy link
Member

Choose a reason for hiding this comment

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

I think this pattern is much cleaner.

start_time = node_start_time.get(node)
if start_time is None:
        raise TranspilerError(....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, done in 6d21886.

Comment on lines 117 to 118
else:
# Directive or delay. These can start at arbitrary time.
Copy link
Member

@1ucian0 1ucian0 May 5, 2023

Choose a reason for hiding this comment

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

I would be explicit about a isinstance(node.op, [Directive, Delay]) branch and leave the else branch for raising on an unknown op type, except that really means that all the non-gate/non-measure can start at arbitrary time.

Copy link
Contributor Author

@alexanderivrii alexanderivrii May 7, 2023

Choose a reason for hiding this comment

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

Done in 6d21886.

Except that for checking whether node.op is a directive we probably need to check getattr(node.op, "_directive", False) and not isinstance(node.op, Directive), the Directive class seems to be something relevant for pulses.

Copy link
Member

@kdk kdk 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 the updates, this LGTM.

@kdk kdk added this pull request to the merge queue May 9, 2023
Merged via the queue into Qiskit:main with commit c3d7d5a May 9, 2023
mergify bot pushed a commit that referenced this pull request May 9, 2023
* experiments

* deleting unused file

* cleanup

* removing duplicate implementation

* clarifying docstring

* applying suggestions from code review

(cherry picked from commit c3d7d5a)
mtreinish pushed a commit that referenced this pull request May 10, 2023
* experiments

* deleting unused file

* cleanup

* removing duplicate implementation

* clarifying docstring

* applying suggestions from code review

(cherry picked from commit c3d7d5a)

Co-authored-by: Alexander Ivrii <[email protected]>
king-p3nguin pushed a commit to king-p3nguin/qiskit-terra that referenced this pull request May 22, 2023
* experiments

* deleting unused file

* cleanup

* removing duplicate implementation

* clarifying docstring

* applying suggestions from code review
@alexanderivrii alexanderivrii deleted the improve-reschedule branch October 23, 2023 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance priority: high stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate performance of ConstrainedReschedule pass
6 participants