Skip to content

Add fast path to Parameter.assign #10549

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

Merged
merged 2 commits into from
Sep 15, 2023

Conversation

jakelishman
Copy link
Member

Summary

This specialises Parameter.assign to more efficiently handle parameter assignment via QuantumCircuit.assign_parameters. The assignment can either be of self, and therefore should just return the value lifted to a ParameterExpression, or it's not of self, in which case we need to raise an error to match the superclass implementation.

The overhead of all the checks that go on during the regular ParameterExpression.assign (delegating to subs or bind) are a sizeable chunk of the cost of assignment in PEC-style circuits.

Details and comments

For the example in #10548 (and building on that branch), this PR takes the runtime from 3.51(6)s down to 2.66(2)s, which is better than the looped private-method performance of Terra 0.24.2.

For the case of inplace binding the whole set of parameters in one go, given in array form, this takes the runtime down from 2.86(2)s down to 1.79(2)s.

cc: @chriseclectic (again).

This specialises `Parameter.assign` to more efficiently handle parameter
assignment via `QuantumCircuit.assign_parameters`.  The assignment can
either be of `self`, and therefore should just return the value lifted
to a `ParameterExpression`, or it's not of `self`, in which case we need
to raise an error to match the superclass implementation.

The overhead of all the checks that go on during the regular
`ParameterExpression.assign` (delegating to `subs` or `bind`) are a
sizeable chunk of the cost of assignment in PEC-style circuits.
@jakelishman jakelishman added performance Changelog: New Feature Include in the "Added" section of the changelog labels Aug 2, 2023
@jakelishman jakelishman requested a review from a team as a code owner August 2, 2023 17:45
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@coveralls
Copy link

Pull Request Test Coverage Report for Build 5742085677

  • 7 of 9 (77.78%) changed or added relevant lines in 1 file are covered.
  • 6 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.01%) to 85.91%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/parameter.py 7 9 77.78%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 6 91.14%
Totals Coverage Status
Change from base Build 5727764372: 0.01%
Covered Lines: 73084
Relevant Lines: 85070

💛 - Coveralls

Comment on lines +103 to +104
# This is the `super().bind` case, where we're required to return a `ParameterExpression`,
# so we need to lift the given value to a symbolic expression.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to lift the value to a symbolic expression? In the final circuit keeping only the float would be even better than having the parameter expression, but I expect there's some type requirement I'm missing...

Copy link
Member Author

Choose a reason for hiding this comment

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

In this PR at least, because that would be a different logical change. I think there's a few issues discussing your topic elsewhere on the Terra repo, though.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 6036367887

  • 7 of 9 (77.78%) changed or added relevant lines in 1 file are covered.
  • 24 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.02%) to 87.269%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/parameter.py 7 9 77.78%
Files with Coverage Reduction New Missed Lines %
qiskit/extensions/quantum_initializer/squ.py 2 80.0%
crates/qasm2/src/lex.rs 4 90.63%
crates/qasm2/src/parse.rs 18 96.67%
Totals Coverage Status
Change from base Build 6036244196: -0.02%
Covered Lines: 74392
Relevant Lines: 85244

💛 - Coveralls

@jakelishman jakelishman requested a review from Cryoris September 6, 2023 10:58
Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

LGTM

@Cryoris Cryoris added this pull request to the merge queue Sep 15, 2023
Merged via the queue into Qiskit:main with commit 67021e4 Sep 15, 2023
@jakelishman jakelishman deleted the optimise-parameter-assign branch September 15, 2023 09:21
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 performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants