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

Reuse parameterless gates in Optimize1qGatesDecomposition #10986

Closed
wants to merge 3 commits into from

Conversation

mtreinish
Copy link
Member

Summary

This commit is a small optimization inside the Optimize1qGatesDecomposition pass. The last stage of the pass is taking a circuit sequence and using it to construct an equivalent 1q dag. To do this the pass iterates over the returned circuit sequence from the 1q synthesis routine and looks up the gate name in a mapping to gate classes, and creates a new object of that class with any angles provided. However, for XGate and SXGate there are no angles, and since #10314 merged there is extra overhead with the repeated construction of these gate classes (see #10867 for more details). Since these gates are now singletons since #10314 it is safe to just reuse the same instance because calling XGate() will return that instance anyway. This commit updates the DAGCircuit construction to just reuse the same instance if the gate in circuit sequence is for x or sx.

Details and comments

This commit is a small optimization inside the
Optimize1qGatesDecomposition pass. The last stage of the pass is taking
a circuit sequence and using it to construct an equivalent 1q dag. To do
this the pass iterates over the returned circuit sequence from the 1q
synthesis routine and looks up the gate name in a mapping to gate
classes, and creates a new object of that class with any angles
provided. However, for XGate and SXGate there are no angles, and
since Qiskit#10314 merged there is extra overhead with the repeated
construction of these gate classes (see Qiskit#10867 for more details). Since
these gates are now singletons since Qiskit#10314 it is safe to just reuse the
same instance because calling XGate() will return that instance anyway.
This commit updates the DAGCircuit construction to just reuse the same
instance if the gate in circuit sequence is for x or sx.
@mtreinish mtreinish added performance Changelog: None Do not include in changelog labels Oct 6, 2023
@mtreinish mtreinish added this to the 0.45.0 milestone Oct 6, 2023
@mtreinish mtreinish requested a review from a team as a code owner October 6, 2023 12:55
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@mtreinish
Copy link
Member Author

Asv isn't showing any performance improvement here, but I'm starting to doubt that we're catching anything useful with the asv benchmarks in their current form (also the ripple adder benchmarks are broken because they do circuit += circuit:

Benchmarks that have stayed the same:

       before           after         ratio
     [cecb7889]       [0d8d4b4c]
     <lint_incr_latest>       <reuse-x-sx-1q-synth>
           failed           failed      n/a  ripple_adder.RippleAdderTranspile.time_transpile_square_grid_ripple_adder(10, 0)
           failed           failed      n/a  ripple_adder.RippleAdderTranspile.time_transpile_square_grid_ripple_adder(10, 1)
           failed           failed      n/a  ripple_adder.RippleAdderTranspile.time_transpile_square_grid_ripple_adder(10, 2)
           failed           failed      n/a  ripple_adder.RippleAdderTranspile.time_transpile_square_grid_ripple_adder(10, 3)
           failed           failed      n/a  ripple_adder.RippleAdderTranspile.time_transpile_square_grid_ripple_adder(20, 0)
           failed           failed      n/a  ripple_adder.RippleAdderTranspile.time_transpile_square_grid_ripple_adder(20, 1)
           failed           failed      n/a  ripple_adder.RippleAdderTranspile.time_transpile_square_grid_ripple_adder(20, 2)
           failed           failed      n/a  ripple_adder.RippleAdderTranspile.time_transpile_square_grid_ripple_adder(20, 3)
          232±3ms          244±3ms     1.05  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(0, 'stochastic', 'sabre')
          131±4ms          137±4ms     1.05  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(2, 'stochastic', 'dense')
          233±7ms         244±10ms     1.05  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(0, 'stochastic', 'dense')
          244±5ms          255±3ms     1.05  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(1, 'stochastic', 'dense')
          185±3ms          193±3ms     1.04  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(1, 'stochastic', 'sabre')
       14.9±0.2ms       15.5±0.7ms     1.04  queko.QUEKOTranspilerBench.time_transpile_bigd(0, 'sabre')
          125±2ms          129±3ms     1.04  queko.QUEKOTranspilerBench.time_transpile_bntf(0, 'sabre')
        167±0.9ms          173±3ms     1.03  passes.MultipleBasisPassBenchmarks.time_optimize_1q_decompose(20, 1024, ['u', 'cx', 'id'])
          328±4ms          339±5ms     1.03  transpiler_levels.TranspilerLevelBenchmarks.time_schedule_qv_14_x_14(0)
          205±4ms          212±4ms     1.03  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(1, 'stochastic', 'noise_adaptive')
          459±3ms          472±5ms     1.03  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(3, 'sabre', 'sabre')
          146±4ms          150±3ms     1.03  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(2, 'sabre', 'sabre')
          141±2ms          145±6ms     1.03  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm(1)
          289±1ms          296±2ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(2, 'stochastic', 'noise_adaptive')
       57.2±0.4ms         58.6±2ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(1, 'sabre', 'sabre')
          242±7ms          248±5ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(1, 'stochastic', 'noise_adaptive')
          293±8ms          299±2ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(2, 'stochastic', 'noise_adaptive')
          105±1ms          108±2ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(0, 'stochastic', 'noise_adaptive')
       73.3±0.3ms         74.8±3ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(2, 'sabre', 'dense')
          304±3ms          310±3ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(2, 'stochastic', 'dense')
          174±2ms          177±3ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(0, 'stochastic', 'sabre')
       1.06±0.01s       1.08±0.01s     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(3, 'stochastic', 'dense')
          165±1ms          168±2ms     1.02  passes.MultipleBasisPassBenchmarks.time_optimize_1q_decompose(20, 1024, ['rx', 'ry', 'rz', 'r', 'rxx', 'id'])
          111±3ms          113±5ms     1.02  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm_backend_with_prop(3)
          221±1ms          225±4ms     1.02  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm(2)
          108±2ms          110±2ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(1, 'stochastic', 'noise_adaptive')
          169±3ms          172±2ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(0, 'stochastic', 'dense')
          471±3ms          479±6ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(3, 'sabre', 'dense')
          279±1ms          283±3ms     1.02  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(2, 'stochastic', 'dense')
       16.9±0.2ms       17.2±0.4ms     1.01  queko.QUEKOTranspilerBench.time_transpile_bigd(1, 'sabre')
       66.0±0.7ms       66.9±0.2ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(0, 'sabre', 'dense')
        151±0.9ms          153±1ms     1.01  passes.MultipleBasisPassBenchmarks.time_optimize_1q_decompose(14, 1024, ['rz', 'x', 'sx', 'cx', 'id'])
          229±3ms          232±5ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(3, 'sabre', 'noise_adaptive')
       69.8±0.4ms       70.7±0.5ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(0, 'sabre', 'dense')
         977±20ms          990±7ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(3, 'stochastic', 'dense')
        126±0.9ms        127±0.6ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(2, 'stochastic', 'sabre')
       58.1±0.3ms       58.8±0.3ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(1, 'sabre', 'noise_adaptive')
          456±3ms          461±9ms     1.01  queko.QUEKOTranspilerBench.time_transpile_bntf(2, 'sabre')
       78.0±0.3ms         78.8±2ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(1, 'sabre', 'noise_adaptive')
         997±10ms       1.01±0.01s     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(3, 'stochastic', 'sabre')
          563±3ms          569±5ms     1.01  queko.QUEKOTranspilerBench.time_transpile_bss(2, 'sabre')
          197±3ms          198±5ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(0, 'stochastic', 'noise_adaptive')
          623±3ms          629±8ms     1.01  queko.QUEKOTranspilerBench.time_transpile_bntf(3, None)
          264±8ms        266±0.9ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(1, 'stochastic', 'sabre')
          526±4ms          531±4ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(3, 'sabre', 'noise_adaptive')
        122±0.6ms        123±0.5ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(2, 'sabre', 'noise_adaptive')
       1.01±0.01s       1.02±0.01s     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(3, 'stochastic', 'sabre')
          612±4ms          617±4ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(3, 'sabre', 'dense')
         67.1±2ms         67.6±2ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(0, 'sabre', 'sabre')
          916±4ms          923±5ms     1.01  transpiler_levels.TranspilerLevelBenchmarks.time_quantum_volume_transpile_50_x_20(1)
          2.04±0s       2.05±0.01s     1.01  transpiler_levels.TranspilerLevelBenchmarks.time_quantum_volume_transpile_50_x_20(2)
          229±2ms          230±2ms     1.01  queko.QUEKOTranspilerBench.time_transpile_bss(1, 'sabre')
          140±2ms          141±2ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(2, 'sabre', 'dense')
          316±3ms          318±4ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(3, 'stochastic', 'sabre')
        143±0.8ms        144±0.9ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(2, 'sabre', 'sabre')
          392±7ms          394±4ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(3, 'stochastic', 'noise_adaptive')
          204±4ms          205±4ms     1.01  queko.QUEKOTranspilerBench.time_transpile_bntf(1, 'sabre')
          253±2ms          254±4ms     1.01  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(2, 'stochastic', 'sabre')
       71.1±0.2ms       71.4±0.5ms     1.00  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm(0)
       78.6±0.7ms         79.0±1ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(1, 'sabre', 'sabre')
       1.51±0.01s       1.52±0.01s     1.00  transpiler_levels.TranspilerLevelBenchmarks.time_quantum_volume_transpile_50_x_20(0)
          808±3ms          812±2ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(3, 'stochastic', 'noise_adaptive')
       51.8±0.9ms       52.0±0.6ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(0, 'sabre', 'sabre')
          272±1ms          273±2ms     1.00  transpiler_levels.TranspilerLevelBenchmarks.time_schedule_qv_14_x_14(1)
       70.4±0.7ms         70.7±3ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(0, 'sabre', 'sabre')
        132±0.5ms        132±0.4ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(2, 'sabre', 'noise_adaptive')
          248±7ms          249±2ms     1.00  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm_backend_with_prop(1)
          406±5ms        407±0.4ms     1.00  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_qv_14_x_14(2)
         33.6±1ms       33.6±0.7ms     1.00  queko.QUEKOTranspilerBench.time_transpile_bigd(0, None)
        240±0.8ms        240±0.4ms     1.00  queko.QUEKOTranspilerBench.time_transpile_bss(2, None)
          672±3ms          672±7ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(3, 'sabre', 'sabre')
        123±0.6ms          123±2ms     1.00  passes.MultipleBasisPassBenchmarks.time_optimize_1q_decompose(14, 1024, ['rx', 'ry', 'rz', 'r', 'rxx', 'id'])
       84.1±0.6ms       84.2±0.6ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(1, 'sabre', 'sabre')
       14.0±0.2ms       14.0±0.1ms     1.00  queko.QUEKOTranspilerBench.time_transpile_bigd(1, None)
          206±2ms        206±0.9ms     1.00  passes.MultipleBasisPassBenchmarks.time_optimize_1q_decompose(20, 1024, ['rz', 'x', 'sx', 'cx', 'id'])
       53.8±0.7ms       53.8±0.9ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(0, 'sabre', 'noise_adaptive')
       55.5±0.2ms       55.4±0.1ms     1.00  passes.MultipleBasisPassBenchmarks.time_optimize_1q_decompose(5, 1024, ['rx', 'ry', 'rz', 'r', 'rxx', 'id'])
       81.0±0.9ms       80.9±0.2ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(1, 'sabre', 'dense')
         314±10ms         314±10ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(2, 'stochastic', 'sabre')
          490±6ms          489±5ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(3, 'sabre', 'noise_adaptive')
        126±0.5ms        126±0.5ms     1.00  queko.QUEKOTranspilerBench.time_transpile_bntf(2, None)
       24.3±0.4ms       24.2±0.1ms     1.00  queko.QUEKOTranspilerBench.time_transpile_bigd(2, None)
          226±3ms         225±10ms     1.00  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm_backend_with_prop(2)
       69.8±0.3ms       69.5±0.3ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(0, 'sabre', 'noise_adaptive')
          156±1ms        155±0.7ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(2, 'sabre', 'dense')
       75.6±0.5ms       75.3±0.6ms     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(1, 'sabre', 'dense')
          730±6ms          728±9ms     1.00  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_qv_14_x_14(3)
       1.02±0.01s       1.01±0.01s     1.00  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(3, 'stochastic', 'noise_adaptive')
       68.3±0.4ms       68.0±0.5ms     1.00  passes.MultipleBasisPassBenchmarks.time_optimize_1q_decompose(5, 1024, ['rz', 'x', 'sx', 'cx', 'id'])
       56.2±0.3ms       56.0±0.1ms     0.99  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(1, 'sabre', 'dense')
          165±2ms          164±1ms     0.99  queko.QUEKOTranspilerBench.time_transpile_bss(0, 'sabre')
         76.7±1ms         76.2±1ms     0.99  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(2, 'sabre', 'noise_adaptive')
          130±1ms          129±4ms     0.99  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(2, 'stochastic', 'noise_adaptive')
          242±3ms        241±0.7ms     0.99  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_qv_14_x_14(1)
         85.0±1ms       84.3±0.2ms     0.99  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(1, 'sabre', 'noise_adaptive')
         75.4±3ms         74.7±2ms     0.99  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(2, 'sabre', 'sabre')
          102±6ms          101±2ms     0.99  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(0, 'stochastic', 'dense')
         926±10ms         916±10ms     0.99  queko.QUEKOTranspilerBench.time_transpile_bss(3, None)
         75.7±1ms       74.8±0.5ms     0.99  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(0, 'sabre', 'noise_adaptive')
          268±7ms          265±4ms     0.99  queko.QUEKOTranspilerBench.time_transpile_bntf(0, None)
       51.9±0.5ms       51.3±0.1ms     0.99  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(0, 'sabre', 'dense')
        125±0.9ms        123±0.7ms     0.99  passes.MultipleBasisPassBenchmarks.time_optimize_1q_decompose(14, 1024, ['u', 'cx', 'id'])
          109±2ms          108±2ms     0.99  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm(3)
       1.12±0.01s       1.10±0.01s     0.99  queko.QUEKOTranspilerBench.time_transpile_bss(3, 'sabre')
       1.49±0.01s       1.47±0.01s     0.99  queko.QUEKOTranspilerBench.time_transpile_bntf(3, 'sabre')
       3.21±0.04s       3.16±0.01s     0.99  transpiler_levels.TranspilerLevelBenchmarks.time_quantum_volume_transpile_50_x_20(3)
       1.58±0.01s          1.56±0s     0.99  queko.QUEKOTranspilerBench.time_transpile_bss(0, None)
          107±2ms          106±1ms     0.98  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(0, 'stochastic', 'sabre')
         99.6±2ms         98.1±2ms     0.98  queko.QUEKOTranspilerBench.time_transpile_bigd(3, 'sabre')
          242±2ms        238±0.8ms     0.98  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(3, 'sabre', 'sabre')
          235±5ms          231±4ms     0.98  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_180(0, 'stochastic', 'noise_adaptive')
       32.4±0.6ms      31.8±0.06ms     0.98  passes.MultipleBasisPassBenchmarks.time_optimize_1q_decompose(5, 1024, ['u', 'cx', 'id'])
         95.2±4ms       93.2±0.4ms     0.98  queko.QUEKOTranspilerBench.time_transpile_bigd(3, None)
          170±3ms          167±5ms     0.98  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_from_large_qasm_backend_with_prop(0)
          314±9ms          307±7ms     0.97  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(3, 'stochastic', 'dense')
          142±3ms        138±0.7ms     0.97  queko.QUEKOTranspilerBench.time_transpile_bss(1, None)
          188±4ms          183±3ms     0.97  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_qft_16(1, 'stochastic', 'dense')
          284±4ms          275±2ms     0.97  transpiler_levels.TranspilerLevelBenchmarks.time_transpile_qv_14_x_14(0)
          227±5ms          220±1ms     0.97  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(3, 'sabre', 'dense')
       26.7±0.3ms       25.7±0.2ms     0.96  queko.QUEKOTranspilerBench.time_transpile_bigd(2, 'sabre')
         78.1±3ms       75.3±0.3ms     0.96  queko.QUEKOTranspilerBench.time_transpile_bntf(1, None)
          112±3ms          107±3ms     0.96  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(1, 'stochastic', 'dense')
          116±3ms          110±1ms     0.95  transpiler_qualitative.TranspilerQualitativeBench.time_transpile_time_cnt3_5_179(1, 'stochastic', 'sabre')

BENCHMARKS NOT SIGNIFICANTLY CHANGED.

@mtreinish mtreinish changed the title Reuse paramterless gates in Optimize1qGatesDecomposition Reuse parameterless gates in Optimize1qGatesDecomposition Oct 12, 2023
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 performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants