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

Add check for the size during transpiler optimization #7542

Merged
merged 11 commits into from
Mar 19, 2022

Conversation

TheGupta2012
Copy link
Contributor

@TheGupta2012 TheGupta2012 commented Jan 19, 2022

Summary

Add check for the size during transpiler optimization fixing #7386

Details and comments

  • This PR introduces the size check on a dag during transpiler optimization
  • In qiskit/transpiler/preset_passmanagers/level2.py, a size check loop is introduced.
  • A new list of transpiler passes is introduced to the PassManager which are iterated till the size is not constant across consecutive iterations.

Code for Results

from qiskit import QuantumCircuit, transpile

qc = QuantumCircuit.from_qasm_str(
    """
                    OPENQASM 2.0;
                    include "qelib1.inc";
                    qreg q[8];
                    cx q[1],q[2];
                    cx q[2],q[3];
                    cx q[5],q[4];
                    cx q[6],q[5];
                    cx q[4],q[5];
                    cx q[3],q[4];
                    cx q[5],q[6];
                    cx q[5],q[4];
                    cx q[3],q[4];
                    cx q[2],q[3];
                    cx q[1],q[2];
                    cx q[6],q[7];
                    cx q[6],q[5];
                    cx q[5],q[4];
                    cx q[7],q[6];
                    cx q[6],q[7];
                """
)

print("Original :", qc.draw())

print(
    f"Circuit transpiled with opt level 2 :", transpile(qc, optimization_level=2).draw()
)

Resultant Output

Original :                                              
q_0: ────────────────────────────────────────
                                             
q_1: ──■──────────────────────────────────■──
     ┌─┴─┐                              ┌─┴─┐
q_2: ┤ X ├──■────────────────────────■──┤ X ├
     └───┘┌─┴─┐                    ┌─┴─┐└───┘
q_3: ─────┤ X ├───────■─────────■──┤ X ├─────
     ┌───┐└───┘     ┌─┴─┐┌───┐┌─┴─┐├───┤     
q_4: ┤ X ├───────■──┤ X ├┤ X ├┤ X ├┤ X ├─────
     └─┬─┘┌───┐┌─┴─┐└───┘└─┬─┘├───┤└─┬─┘     
q_5: ──■──┤ X ├┤ X ├──■────■──┤ X ├──■───────
          └─┬─┘└───┘┌─┴─┐     └─┬─┘┌───┐     
q_6: ───────■───────┤ X ├──■────■──┤ X ├──■──
                    └───┘┌─┴─┐     └─┬─┘┌─┴─┐
q_7: ────────────────────┤ X ├───────■──┤ X ├
                         └───┘          └───┘
Circuit transpiled with opt level 2 :                                              
q_0: ────────────────────────────────────────
                                             
q_1: ────────────────────────────────────────
                                             
q_2: ────────────────────────────────────────
                                             
q_3: ────────────────────────────────────────
     ┌───┐               ┌───┐     ┌───┐     
q_4: ┤ X ├───────■───────┤ X ├─────┤ X ├─────
     └─┬─┘┌───┐┌─┴─┐     └─┬─┘┌───┐└─┬─┘     
q_5: ──■──┤ X ├┤ X ├──■────■──┤ X ├──■───────
          └─┬─┘└───┘┌─┴─┐     └─┬─┘┌───┐     
q_6: ───────■───────┤ X ├──■────■──┤ X ├──■──
                    └───┘┌─┴─┐     └─┬─┘┌─┴─┐
q_7: ────────────────────┤ X ├───────■──┤ X ├
                         └───┘          └───┘


@TheGupta2012 TheGupta2012 requested a review from a team as a code owner January 19, 2022 13:20
@coveralls
Copy link

coveralls commented Jan 19, 2022

Pull Request Test Coverage Report for Build 2007200649

  • 14 of 14 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.01%) to 83.504%

Totals Coverage Status
Change from base Build 2007186808: 0.01%
Covered Lines: 52465
Relevant Lines: 62829

💛 - Coveralls

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Is there a reason to limit this to level 2? The same iteration happens in levels 1 and 3 as well.

The tests of the preset pass managers are in test/python/transpiler/test_preset_passmanagers.py, but this would be fairly tricky to write a targetted test for, I think. In principle, we could construct one by making a circuit with two disjoint sets of qubits, where the first set has an operation that cancels down to a size of 0, but only after 3 loops, while the second collapses down in only 1 loop to a depth of more than first set has after the first loop. Finding those operations would be tricky, though; it would be easy for unrelated improvements in other transpiler passes to stymie the test by immediately reducing both paths to their smallest size.

At the very least, we could add a regression test of the case in #7386 to ensure that two subsequent calls to transpile don't change the size or depth.

qiskit/transpiler/preset_passmanagers/level2.py Outdated Show resolved Hide resolved
@TheGupta2012 TheGupta2012 marked this pull request as draft January 19, 2022 16:54
@TheGupta2012 TheGupta2012 marked this pull request as ready for review January 20, 2022 13:27
@TheGupta2012 TheGupta2012 requested a review from kdk January 25, 2022 16:59
@TheGupta2012
Copy link
Contributor Author

@jakelishman would this PR require any more changes? I had updated the issue with some benchmarks which caught my eye.

@jakelishman
Copy link
Member

I put some comments in #7386 about the benchmarking we'd want to do in advance, and the possibility of new benchmarks, but I'll have to pass final review to @kdk (or to whomever he delegates) because I'm going to be away from work for a while. It looks sensible to me on a quick glance, though.

@TheGupta2012 TheGupta2012 requested a review from kdk February 15, 2022 18:52
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 here @TheGupta2012 , this LGTM!

@kdk kdk added automerge Changelog: New Feature Include in the "Added" section of the changelog labels Mar 18, 2022
@kdk kdk added this to the 0.20 milestone Mar 18, 2022
@kdk kdk linked an issue Mar 18, 2022 that may be closed by this pull request
@mergify mergify bot merged commit 37de124 into Qiskit:main Mar 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

better convergence criteria in preset passmanagers
4 participants