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

Stop building decomposition dag at instance creation #1445

Closed
wants to merge 12 commits into from

Conversation

mtreinish
Copy link
Member

@mtreinish mtreinish commented Dec 6, 2018

Summary

This commit adjust how we define decompositions for gates. Previously we
ran a function which built a dag of the decompositions during __init__
for the gate class. This meant we'd build a new dag every time we
created a new gate, which for large circuits would start to add up.
However, the decompositions are static for most gates and we only need
to run it once. To accomplish that this commit makes the decompositions
a class defined parameter and removes it from __init__. This way we only
ever build the decomposition dags once unless explicitly needed.

Gates that are not touched: crz, cu1, cu3, rx, ry, rz, rzz, u1, u2, u3,
are left alone because the decomposition depends on run time variables
in the instance and this optimization provides no benefit.

Details and comments

Fixes #1729

@ajavadia
Copy link
Member

luciano sped things up in #1455 by making sure that _define_decompositions() is only called upon request (i.e. when the unroller needs to make an unrolling decision). But it is still per instance, not per class. So there's still merit to this PR to get further improvement from some classes (those that don't have any parameters).

@mtreinish
Copy link
Member Author

ugh, well that makes rebasing this fun :) I'll do that right now

This commit adjust how we define decompositions for gates. Previously we
ran a function which built a dag of the decompositions during __init__
for the gate class. This meant we'd build a new dag every time we
created a new gate, which for large circuits would start to add up.
However, the decompositions are static for most gates and we only need
to run it once. To accomplish that this commit makes the decompositions
a class defined parameter and removes it from __init__. This way we only
ever build the decomposition dags once unless explicitly needed.

Gates that are not touched: crz, cu1, cu3, rx, ry, rz, rzz, u1, u2, u3,
are left alone because the decomposition depends on run time variables
in the instance and this optimization provides no benefit.

Fixes Qiskit#1441
@1ucian0
Copy link
Member

1ucian0 commented Dec 12, 2018

Sorry for the overlapping, it was part of moving the unroller as a pass :(

@ajavadia ajavadia removed this from the 0.7 milestone Dec 15, 2018
@1ucian0
Copy link
Member

1ucian0 commented Jan 29, 2019

Since #1441 is closed, I'm closing this PR.

@1ucian0 1ucian0 closed this Jan 29, 2019
@ajavadia
Copy link
Member

I would still like to do what Matthew did in this PR. It provides further speedups to what was already achieved. However, this PR is probably already quite out of sync with master, so might make sense to open a new PR. I'll open an issue.

@mtreinish mtreinish reopened this Jan 30, 2019
When we have a conditional applied during decomposition we modify the
decomposition dag to contain the conditional. However, since in many
cases now we have a single global instance of the decomposition dag for
a gate doing this is a bad idea. This will lead to the conditional
persiting for all future decompositions of the gate whether there is one
or not. To avoid this issue this commit adds a deepcopy() call to make a
duplicate of the decomposition dag for when we have a conditional so we
do not modify the original global decomposition dag.
@mtreinish mtreinish requested a review from kdk as a code owner February 6, 2019 21:52
@mtreinish mtreinish changed the title [WIP] Stop building decomposition dag at instance creation Stop building decomposition dag at instance creation Feb 6, 2019
@mtreinish
Copy link
Member Author

Hmm, it looks like we're still modifying input_dag in the substitute_node_with_dag() even in the non-conditional case. This is causing the failures later when there is a conditional, so simply duplicating the input_dag when there is a conditional (like a00e3a1 ) is not sufficient because the global decomposition dag has already been modified by the time we call copy. We'll need to figure out why/where the input_dag gets modified and guard against that if we plan to use class level attributes for the decomposition_dags.

@ajavadia ajavadia added this to the 0.8 milestone Mar 19, 2019
@1ucian0
Copy link
Member

1ucian0 commented Apr 3, 2019

I'm closing this PR because it has conflicts with the current master. Feel free to open a new PR for closing #1729 !

@1ucian0 1ucian0 closed this Apr 3, 2019
@mtreinish
Copy link
Member Author

@1ucian0 It's unlikely to be feasible while we're still modifying the dag in place. If we move to shared instance for any dag (including the decomposition dags like this commit) this gets really messy since those in place modifications effect the global/shared instance. We have to stop doing that as a first step before this is really something we can consider, especially weighed against the modest performance benefits I was seeing locally (there might be some edge cases I'm missing though)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid redefining decompositions for non-parametric gates
3 participants