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

Fix short circuit detection for basis translator #12899

Merged
merged 1 commit into from
Aug 8, 2024

Conversation

mtreinish
Copy link
Member

Summary

The BasisTranslator transpiler pass has a check at the very start that is designed to return fast if there is nothing to translate; in other words if the instructions in the circuit are already a subset of instructions supported by the target. This avoid doing a lot of unecessary work to determine this later during the operation of the pass. However, this check was not correctly constructed because of a type mismatch and would only ever get triggered if the input circuit was empty. The source basis is collected as a Set[Tuple[str, int]] where each tuple is the name and num of qubits for each operation in the circuit. While the target basis is just a Set[str] for the names supported on the target. This mismatch caused the subset check to never return True unless it was empty thereby bypassing the intent of the short circuit path. This commit fixes the logic by constructing a temporary set of just the source names to evaluate whether we should return early or not.

Details and comments

The BasisTranslator transpiler pass has a check at the very start that
is designed to return fast if there is nothing to translate; in
other words if the instructions in the circuit are already a subset of
instructions supported by the target. This avoid doing a lot of
unecessary work to determine this later during the operation of the
pass. However, this check was not correctly constructed because of a
type mismatch and would only ever get triggered if the input circuit was
empty. The source basis is collected as a `Set[Tuple[str, int]]` where
each tuple is the name and num of qubits for each operation in the
circuit. While the target basis is just a `Set[str]` for the names
supported on the target. This mismatch caused the subset check to never
return True unless it was empty thereby bypassing the intent of the
short circuit path. This commit fixes the logic by constructing a
temporary set of just the source names to evaluate whether we should
return early or not.
@mtreinish mtreinish added performance Changelog: None Do not include in changelog mod: transpiler Issues and PRs related to Transpiler labels Aug 4, 2024
@mtreinish mtreinish added this to the 1.2.0 milestone Aug 4, 2024
@mtreinish mtreinish requested a review from a team as a code owner August 4, 2024 13:47
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@mtreinish mtreinish added the stable backport potential The bug might be minimal and/or import enough to be port to stable label Aug 4, 2024
@coveralls
Copy link

Pull Request Test Coverage Report for Build 10236733779

Details

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 12 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.01%) to 89.741%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 6 91.73%
crates/qasm2/src/parse.rs 6 97.61%
Totals Coverage Status
Change from base Build 10222290210: -0.01%
Covered Lines: 67338
Relevant Lines: 75036

💛 - Coveralls

@mtreinish
Copy link
Member Author

I did a quick asv run and as expected this shows a performance improvement for cases where the circuit is already in the target gates, which is what happens in the large qasm benchmarks:

Benchmarks that have improved:

| Change   | Before [1fdd527f] <lint_incr_latest>   | After [01c17c2b] <fix-bt-short-circuit>   |   Ratio | Benchmark (Parameter)                                                                           |
|----------|----------------------------------------|-------------------------------------------|---------|-------------------------------------------------------------------------------------------------|
| -        | 122±0.6ms                              | 96.1±0.9ms                                |    0.79 | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm_backend_with_prop(1) |
| -        | 93.9±1ms                               | 67.6±0.3ms                                |    0.72 | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm(1)                   |
| -        | 83.2±0.9ms                             | 55.6±0.2ms                                |    0.67 | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm_backend_with_prop(0) |
| -        | 71.2±1ms                               | 45.4±0.9ms                                |    0.64 | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm(0)                   |

Benchmarks that have stayed the same:

| Change   | Before [1fdd527f] <lint_incr_latest>   | After [01c17c2b] <fix-bt-short-circuit>   | Ratio   | Benchmark (Parameter)                                                                                  |
|----------|----------------------------------------|-------------------------------------------|---------|--------------------------------------------------------------------------------------------------------|
|          | 0                                      | 0                                         | n/a     | utility_scale.UtilityScaleBenchmarks.track_bvlike_depth('cx')                                          |
|          | 0                                      | 0                                         | n/a     | utility_scale.UtilityScaleBenchmarks.track_bvlike_depth('cz')                                          |
|          | 0                                      | 0                                         | n/a     | utility_scale.UtilityScaleBenchmarks.track_bvlike_depth('ecr')                                         |
|          | 3.77±0.07s                             | 3.95±0s                                   | 1.05    | utility_scale.UtilityScaleBenchmarks.time_circSU2('cz')                                                |
|          | 3.66±0.07s                             | 3.82±0.01s                                | 1.04    | utility_scale.UtilityScaleBenchmarks.time_circSU2('cx')                                                |
|          | 3.74±0.08s                             | 3.85±0.02s                                | 1.03    | utility_scale.UtilityScaleBenchmarks.time_circSU2('ecr')                                               |
|          | 8.62±0.02s                             | 8.75±0.03s                                | 1.02    | utility_scale.UtilityScaleBenchmarks.time_qv('cz')                                                     |
|          | 587±3ms                                | 592±5ms                                   | 1.01    | transpiler_levels.TranspilerLevelBenchmarks.time_quantum_volume_transpile_50_x_20(2)                   |
|          | 300±0.7ms                              | 301±10ms                                  | 1.01    | utility_scale.UtilityScaleBenchmarks.time_bv_100('cz')                                                 |
|          | 94.4±0.8ms                             | 94.9±0.2ms                                | 1.01    | utility_scale.UtilityScaleBenchmarks.time_bvlike('cz')                                                 |
|          | 110±1ms                                | 111±0.6ms                                 | 1.01    | utility_scale.UtilityScaleBenchmarks.time_parse_qft_n100('cx')                                         |
|          | 1.65±0.01s                             | 1.67±0.01s                                | 1.01    | utility_scale.UtilityScaleBenchmarks.time_qaoa('ecr')                                                  |
|          | 935±3ms                                | 941±6ms                                   | 1.01    | utility_scale.UtilityScaleBenchmarks.time_square_heisenberg('cx')                                      |
|          | 215±0.5ms                              | 215±2ms                                   | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.time_quantum_volume_transpile_50_x_20(0)                   |
|          | 801±7ms                                | 798±8ms                                   | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.time_quantum_volume_transpile_50_x_20(3)                   |
|          | 72.8±0.9ms                             | 73.0±0.5ms                                | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.time_schedule_qv_14_x_14(0)                                |
|          | 79.4±1ms                               | 79.7±0.3ms                                | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.time_schedule_qv_14_x_14(1)                                |
|          | 63.7±1ms                               | 63.6±0.4ms                                | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm(2)                          |
|          | 1488                                   | 1488                                      | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_quantum_volume_transpile_50_x_20(0)            |
|          | 1391                                   | 1391                                      | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_quantum_volume_transpile_50_x_20(1)            |
|          | 1258                                   | 1258                                      | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_quantum_volume_transpile_50_x_20(2)            |
|          | 1314                                   | 1314                                      | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_quantum_volume_transpile_50_x_20(3)            |
|          | 2705                                   | 2705                                      | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_from_large_qasm(0)                   |
|          | 2005                                   | 2005                                      | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_from_large_qasm(1)                   |
|          | 7                                      | 7                                         | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_from_large_qasm(2)                   |
|          | 7                                      | 7                                         | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_from_large_qasm(3)                   |
|          | 2705                                   | 2705                                      | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_from_large_qasm_backend_with_prop(0) |
|          | 2005                                   | 2005                                      | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_from_large_qasm_backend_with_prop(1) |
|          | 7                                      | 7                                         | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_from_large_qasm_backend_with_prop(2) |
|          | 7                                      | 7                                         | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_from_large_qasm_backend_with_prop(3) |
|          | 416                                    | 416                                       | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_qv_14_x_14(0)                        |
|          | 304                                    | 304                                       | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_qv_14_x_14(1)                        |
|          | 307                                    | 307                                       | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_qv_14_x_14(2)                        |
|          | 268                                    | 268                                       | 1.00    | transpiler_levels.TranspilerLevelBenchmarks.track_depth_transpile_qv_14_x_14(3)                        |
|          | 283±0.9ms                              | 282±3ms                                   | 1.00    | utility_scale.UtilityScaleBenchmarks.time_bv_100('ecr')                                                |
|          | 94.9±0.2ms                             | 94.6±0.4ms                                | 1.00    | utility_scale.UtilityScaleBenchmarks.time_bvlike('cx')                                                 |
|          | 96.0±1ms                               | 96.4±0.8ms                                | 1.00    | utility_scale.UtilityScaleBenchmarks.time_bvlike('ecr')                                                |
|          | 111±0.6ms                              | 111±0.2ms                                 | 1.00    | utility_scale.UtilityScaleBenchmarks.time_parse_qft_n100('cz')                                         |
|          | 111±0.7ms                              | 111±0.3ms                                 | 1.00    | utility_scale.UtilityScaleBenchmarks.time_parse_qft_n100('ecr')                                        |
|          | 35.4±0.2ms                             | 35.5±0.1ms                                | 1.00    | utility_scale.UtilityScaleBenchmarks.time_parse_square_heisenberg_n100('cx')                           |
|          | 2.24±0.01s                             | 2.25±0s                                   | 1.00    | utility_scale.UtilityScaleBenchmarks.time_qaoa('cz')                                                   |
|          | 20.6±0.03s                             | 20.6±0.03s                                | 1.00    | utility_scale.UtilityScaleBenchmarks.time_qft('cz')                                                    |
|          | 19.9±0.1s                              | 19.9±0.03s                                | 1.00    | utility_scale.UtilityScaleBenchmarks.time_qft('ecr')                                                   |
|          | 5.01±0.01s                             | 5.03±0.02s                                | 1.00    | utility_scale.UtilityScaleBenchmarks.time_qv('cx')                                                     |
|          | 1.36±0s                                | 1.37±0.01s                                | 1.00    | utility_scale.UtilityScaleBenchmarks.time_square_heisenberg('cz')                                      |
|          | 395                                    | 395                                       | 1.00    | utility_scale.UtilityScaleBenchmarks.track_bv_100_depth('cx')                                          |
|          | 397                                    | 397                                       | 1.00    | utility_scale.UtilityScaleBenchmarks.track_bv_100_depth('cz')                                          |
|          | 397                                    | 397                                       | 1.00    | utility_scale.UtilityScaleBenchmarks.track_bv_100_depth('ecr')                                         |
|          | 300                                    | 300                                       | 1.00    | utility_scale.UtilityScaleBenchmarks.track_circSU2_depth('cx')                                         |
|          | 300                                    | 300                                       | 1.00    | utility_scale.UtilityScaleBenchmarks.track_circSU2_depth('cz')                                         |
|          | 300                                    | 300                                       | 1.00    | utility_scale.UtilityScaleBenchmarks.track_circSU2_depth('ecr')                                        |
|          | 1483                                   | 1483                                      | 1.00    | utility_scale.UtilityScaleBenchmarks.track_qaoa_depth('cx')                                            |
|          | 1488                                   | 1488                                      | 1.00    | utility_scale.UtilityScaleBenchmarks.track_qaoa_depth('cz')                                            |
|          | 1488                                   | 1488                                      | 1.00    | utility_scale.UtilityScaleBenchmarks.track_qaoa_depth('ecr')                                           |
|          | 1954                                   | 1954                                      | 1.00    | utility_scale.UtilityScaleBenchmarks.track_qft_depth('cx')                                             |
|          | 1954                                   | 1954                                      | 1.00    | utility_scale.UtilityScaleBenchmarks.track_qft_depth('cz')                                             |
|          | 1954                                   | 1954                                      | 1.00    | utility_scale.UtilityScaleBenchmarks.track_qft_depth('ecr')                                            |
|          | 2538                                   | 2538                                      | 1.00    | utility_scale.UtilityScaleBenchmarks.track_qv_depth('cx')                                              |
|          | 2538                                   | 2538                                      | 1.00    | utility_scale.UtilityScaleBenchmarks.track_qv_depth('cz')                                              |
|          | 2538                                   | 2538                                      | 1.00    | utility_scale.UtilityScaleBenchmarks.track_qv_depth('ecr')                                             |
|          | 435                                    | 435                                       | 1.00    | utility_scale.UtilityScaleBenchmarks.track_square_heisenberg_depth('cx')                               |
|          | 435                                    | 435                                       | 1.00    | utility_scale.UtilityScaleBenchmarks.track_square_heisenberg_depth('cz')                               |
|          | 435                                    | 435                                       | 1.00    | utility_scale.UtilityScaleBenchmarks.track_square_heisenberg_depth('ecr')                              |
|          | 283±2ms                                | 279±1ms                                   | 0.99    | transpiler_levels.TranspilerLevelBenchmarks.time_quantum_volume_transpile_50_x_20(1)                   |
|          | 66.0±0.2ms                             | 65.3±0.5ms                                | 0.99    | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm(3)                          |
|          | 69.3±1ms                               | 68.5±0.5ms                                | 0.99    | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm_backend_with_prop(2)        |
|          | 50.8±1ms                               | 50.4±0.2ms                                | 0.99    | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_qv_14_x_14(0)                               |
|          | 283±1ms                                | 281±5ms                                   | 0.99    | utility_scale.UtilityScaleBenchmarks.time_bv_100('cx')                                                 |
|          | 10.4±0.08ms                            | 10.2±0.03ms                               | 0.99    | utility_scale.UtilityScaleBenchmarks.time_parse_qaoa_n100('cx')                                        |
|          | 10.3±0.1ms                             | 10.3±0.05ms                               | 0.99    | utility_scale.UtilityScaleBenchmarks.time_parse_qaoa_n100('cz')                                        |
|          | 10.4±0.1ms                             | 10.3±0.07ms                               | 0.99    | utility_scale.UtilityScaleBenchmarks.time_parse_qaoa_n100('ecr')                                       |
|          | 35.7±0.2ms                             | 35.5±0.08ms                               | 0.99    | utility_scale.UtilityScaleBenchmarks.time_parse_square_heisenberg_n100('cz')                           |
|          | 35.8±0.3ms                             | 35.5±0.2ms                                | 0.99    | utility_scale.UtilityScaleBenchmarks.time_parse_square_heisenberg_n100('ecr')                          |
|          | 987±7ms                                | 980±9ms                                   | 0.99    | utility_scale.UtilityScaleBenchmarks.time_qaoa('cx')                                                   |
|          | 18.9±0.02s                             | 18.8±0.03s                                | 0.99    | utility_scale.UtilityScaleBenchmarks.time_qft('cx')                                                    |
|          | 4.35±0.03s                             | 4.32±0.04s                                | 0.99    | utility_scale.UtilityScaleBenchmarks.time_qv('ecr')                                                    |
|          | 1.18±0.01s                             | 1.17±0.01s                                | 0.99    | utility_scale.UtilityScaleBenchmarks.time_square_heisenberg('ecr')                                     |
|          | 131±2ms                                | 129±0.4ms                                 | 0.98    | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_qv_14_x_14(3)                               |
|          | 73.6±0.9ms                             | 71.7±0.5ms                                | 0.97    | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_qv_14_x_14(1)                               |
|          | 110±2ms                                | 107±2ms                                   | 0.97    | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_qv_14_x_14(2)                               |
|          | 71.5±1ms                               | 69.0±0.2ms                                | 0.96    | transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm_backend_with_prop(3)        |

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.

@jakelishman jakelishman added this pull request to the merge queue Aug 8, 2024
Merged via the queue into Qiskit:main with commit fa5510c Aug 8, 2024
15 checks passed
mergify bot pushed a commit that referenced this pull request Aug 8, 2024
The BasisTranslator transpiler pass has a check at the very start that
is designed to return fast if there is nothing to translate; in
other words if the instructions in the circuit are already a subset of
instructions supported by the target. This avoid doing a lot of
unecessary work to determine this later during the operation of the
pass. However, this check was not correctly constructed because of a
type mismatch and would only ever get triggered if the input circuit was
empty. The source basis is collected as a `Set[Tuple[str, int]]` where
each tuple is the name and num of qubits for each operation in the
circuit. While the target basis is just a `Set[str]` for the names
supported on the target. This mismatch caused the subset check to never
return True unless it was empty thereby bypassing the intent of the
short circuit path. This commit fixes the logic by constructing a
temporary set of just the source names to evaluate whether we should
return early or not.

(cherry picked from commit fa5510c)
github-merge-queue bot pushed a commit that referenced this pull request Aug 8, 2024
The BasisTranslator transpiler pass has a check at the very start that
is designed to return fast if there is nothing to translate; in
other words if the instructions in the circuit are already a subset of
instructions supported by the target. This avoid doing a lot of
unecessary work to determine this later during the operation of the
pass. However, this check was not correctly constructed because of a
type mismatch and would only ever get triggered if the input circuit was
empty. The source basis is collected as a `Set[Tuple[str, int]]` where
each tuple is the name and num of qubits for each operation in the
circuit. While the target basis is just a `Set[str]` for the names
supported on the target. This mismatch caused the subset check to never
return True unless it was empty thereby bypassing the intent of the
short circuit path. This commit fixes the logic by constructing a
temporary set of just the source names to evaluate whether we should
return early or not.

(cherry picked from commit fa5510c)

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
eliarbel added a commit to eliarbel/qiskit that referenced this pull request Sep 18, 2024
This is a manual backport of the change introduced in PR Qiskit#12889. It
is done manually since in 0.46 we are using PyO3 version 0.19.2 which
does not yet have the `Bound` concept PR Qiskit#12889 relies on in the
`add_submodule` helper function. In this branch we also need to
handle just a subset of submodule functions compared to the ones
in Qiskit#12899.
github-merge-queue bot pushed a commit that referenced this pull request Sep 20, 2024
This is a manual backport of the change introduced in PR #12889. It
is done manually since in 0.46 we are using PyO3 version 0.19.2 which
does not yet have the `Bound` concept PR #12889 relies on in the
`add_submodule` helper function. In this branch we also need to
handle just a subset of submodule functions compared to the ones
in #12899.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog mod: transpiler Issues and PRs related to Transpiler performance 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.

4 participants