-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Upgrading bind_parameters: allow in place and mixed floats + parameters #4037
Conversation
* can now bind/substitute using the in_place kwarg * can bind floats and Params in the same dict
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, few comments in line. I wonder if it would make sense to rename the new method to sub_parameters
or similar since binding implies a degree of finality. i.e. once bound, a circuit is ready for execution; you can only bind once; you can't pass a Parameter
to execute(parameter_binds=...)
;...
Concerning the naming we could also use |
mainly rename in_place to inplace
…erra into num-parameters-property
still keeping bind_parameters in some places in the test
…erra into num-parameters-property
releasenotes/notes/bind-parameters-mixed-values-and-in-place-48a68c882a03070b.yaml
Outdated
Show resolved
Hide resolved
releasenotes/notes/bind-parameters-mixed-values-and-in-place-48a68c882a03070b.yaml
Outdated
Show resolved
Hide resolved
…8a68c882a03070b.yaml Co-Authored-By: Kevin Krsulich <[email protected]>
Co-Authored-By: Kevin Krsulich <[email protected]>
Co-Authored-By: Kevin Krsulich <[email protected]>
…erra into num-parameters-property
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, two small comments and then this is good to go.
…erra into num-parameters-property
…rs (Qiskit#4037) * add num parameters keyword * upgrade bind_parameters: in_place and mixed params * can now bind/substitute using the in_place kwarg * can bind floats and Params in the same dict * add comments, better variable naming * add reno, use bind_params instead of _subst_params * add test for num_parameters property * apply suggested changes mainly rename in_place to inplace * rename bind to assign_parameters still keeping bind_parameters in some places in the test * Update releasenotes/notes/bind-parameters-mixed-values-and-in-place-48a68c882a03070b.yaml Co-Authored-By: Kevin Krsulich <[email protected]> * Update qiskit/circuit/quantumcircuit.py Co-Authored-By: Kevin Krsulich <[email protected]> * Update qiskit/circuit/quantumcircuit.py Co-Authored-By: Kevin Krsulich <[email protected]> * remove _subst_param from reno * delete _partition_dict * throw error upon ParameterExpr in bind_parameters * revert changes on bind_, test both assign+bind * assertions must be in the loop * complete the docstring Co-authored-by: Kevin Krsulich <[email protected]> Co-authored-by: Kevin Krsulich <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Summary
In the current situation we need to use
bind_parameters
to bind float values to a circuit (which returns a copy of the original) and_substitute_parameters
to replace the existing parameters with newParameter
objects (which modifies the circuit in place).With this PR the
bind_parameter
method gets an upgrade such that it can bind both numeric values andParameter
objects and can do so in-place or on a copy.The signature is (type hints not actually included, just for explaining reasons here)
This is much needed functionality for circuits to become native objects in Aqua, where parameters are replaced a lot and it is cumbersome to always have to distinct between numeric values and
Parameter
s.Also a
num_parameters
attribute is added to the circuit for the same integration reason.Details and comments
This allows to do things such as