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

Translate parameterized gates only in gradient calculations #9067

Merged
merged 19 commits into from
Jan 6, 2023

Conversation

Cryoris
Copy link
Contributor

@Cryoris Cryoris commented Nov 3, 2022

Summary

Closes #9054.

Details and comments

All gradient now preserve unparameterized operations instead of attempting to unroll them.
This

  • allows to evaluate gradients on custom, opaque gates that individual primitives can handle
  • keeps a higher level of abstraction for optimized synthesis and compilation after the gradient circuits have been constructed.

@Cryoris Cryoris added Changelog: New Feature Include in the "Added" section of the changelog mod: algorithms Related to the Algorithms module labels Nov 3, 2022
@Cryoris Cryoris added this to the 0.23.0 milestone Nov 3, 2022
@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:

@Cryoris
Copy link
Contributor Author

Cryoris commented Nov 3, 2022

Tagging @a-matsuo for review 🙂

@coveralls
Copy link

coveralls commented Nov 3, 2022

Pull Request Test Coverage Report for Build 3750990070

  • 61 of 63 (96.83%) changed or added relevant lines in 6 files are covered.
  • 36 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.1%) to 84.64%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/algorithms/gradients/utils.py 4 6 66.67%
Files with Coverage Reduction New Missed Lines %
qiskit/extensions/quantum_initializer/squ.py 2 79.78%
src/sabre_swap/layer.rs 2 98.95%
qiskit/opflow/converters/circuit_sampler.py 6 95.57%
qiskit/utils/quantum_instance.py 26 73.11%
Totals Coverage Status
Change from base Build 3747694715: 0.1%
Covered Lines: 64130
Relevant Lines: 75768

💛 - Coveralls

@a-matsuo
Copy link
Contributor

a-matsuo commented Nov 7, 2022

It looks good for me except which directory we should put the code as @t-imamichi mentioned.

@Cryoris
Copy link
Contributor Author

Cryoris commented Nov 14, 2022

FYI in d567074 I also removed trailing classical registers that weren't used in the LCU gradients (+ QFI) if the add_measurement is False. With the current estimators that's not really a problem but if someone develops their own primitives this could fail if they don't support classical registers.

@a-matsuo
Copy link
Contributor

Thank you @Cryoris LGTM!

woodsp-ibm
woodsp-ibm previously approved these changes Nov 15, 2022
t-imamichi
t-imamichi previously approved these changes Nov 16, 2022
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

I only looked at the transpiler pass, but I left a few comments inline. The biggest thing for me is we should add Target support from the start to this pass, especially because the BasisTranslator which is used internally already supports working with the Target.

qiskit/transpiler/passes/basis/translate_parameterized.py Outdated Show resolved Hide resolved
Comment on lines +31 to +35
Once a parameterized instruction is found that is not in the ``supported_gates`` list,
the instruction is decomposed one level and the parameterized sub-blocks are recursively
decomposed. The recursion is stopped once all parameterized gates are in ``supported_gates``,
or if a gate has no definition and a translation to the basis is attempted (this might happen
e.g. for the ``UGate`` if it's not in the specified gate list).
Copy link
Member

Choose a reason for hiding this comment

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

How is this different than the unroll* passes which effectively do this already? Looking at the code it's because this only unrolls parameterized gates, but if we adding another pass to do this we should probably call out why you'd want to use this instead of any of those.

The other related question is whether we want to add this functionality to an existing pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, it's unrolling plus translating if the unrolling didn't reach the target base. If it were only unrolling then we could add a criterion function or something to the Unroller which we could set to detect parameterized gates. Then we could afterwards do a translation pass on the gates which are not yet in the right basis. So this pass here would be something like

def is_parameterized(gate):
    # returns True if ``gate`` is parameterized, else False

this_pass = PassManager([
   Unroller(basis_gates, criterion=is_parameterized),
   BasisTranslator(...)
])

That would of course mean that we have to construct this pass manually, but since this is used often in gradient calculations I thought it might be generally useful -- but we can also do the above!

qiskit/transpiler/passes/basis/translate_parameterized.py Outdated Show resolved Hide resolved
Copy link
Member

@mtreinish mtreinish 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 adding the target support, just two small inline comments about it

qiskit/transpiler/passes/basis/translate_parameterized.py Outdated Show resolved Hide resolved
qiskit/transpiler/passes/basis/translate_parameterized.py Outdated Show resolved Hide resolved
mtreinish
mtreinish previously approved these changes Dec 9, 2022
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

I just left one question inline, but I had a question inline and I'm not sure how you want to proceed with regards to that. I'm fine either keeping the qubit restriction on the target lookup or dropping it depend on the expectations around the pass. So I'm karking this as approved, but if you want to change that line go ahead and feel free to merge this without waiting on a re-approval from me.

that's because this pass is run before qubit mapping
@mergify mergify bot merged commit 27da80d into Qiskit:main Jan 6, 2023
king-p3nguin pushed a commit to king-p3nguin/qiskit-terra that referenced this pull request Jan 11, 2023
)

* translate parameterized gates only

* TranslateParameterized to proper pass

* update reno w/ new trafo pass

* Only add cregs if required

* rm unused import

* directly construct DAG

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* target support

* qubit-wise support of unrolling

* globally check for supported gates in target

that's because this pass is run before qubit mapping

* lint!

* updates after merge from main

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Cryoris added a commit to Cryoris/qiskit-terra that referenced this pull request Jan 12, 2023
)

* translate parameterized gates only

* TranslateParameterized to proper pass

* update reno w/ new trafo pass

* Only add cregs if required

* rm unused import

* directly construct DAG

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* target support

* qubit-wise support of unrolling

* globally check for supported gates in target

that's because this pass is run before qubit mapping

* lint!

* updates after merge from main

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
ElePT pushed a commit to ElePT/qiskit that referenced this pull request Jun 27, 2023
)

* translate parameterized gates only

* TranslateParameterized to proper pass

* update reno w/ new trafo pass

* Only add cregs if required

* rm unused import

* directly construct DAG

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* target support

* qubit-wise support of unrolling

* globally check for supported gates in target

that's because this pass is run before qubit mapping

* lint!

* updates after merge from main

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
ElePT pushed a commit to ElePT/qiskit-algorithms-test that referenced this pull request Jul 17, 2023
…iskit#9067)

* translate parameterized gates only

* TranslateParameterized to proper pass

* update reno w/ new trafo pass

* Only add cregs if required

* rm unused import

* directly construct DAG

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* target support

* qubit-wise support of unrolling

* globally check for supported gates in target

that's because this pass is run before qubit mapping

* lint!

* updates after merge from main

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
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 mod: algorithms Related to the Algorithms module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support unparameterized opaque gates in the gradients
8 participants