-
Notifications
You must be signed in to change notification settings - Fork 2.6k
allows string values in assign_parameters with strict=False #14365
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
Conversation
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 following people are relevant to this code:
|
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.
Thanks for tackling this. I left a comment inline, can you also add a release note and some unit tests to confirm the fix and prevent it from regressing in the future?
qiskit/circuit/quantumcircuit.py
Outdated
elif isinstance(parameter, str) and not self.has_parameter(parameter): | ||
out[Parameter(parameter)] = value |
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.
I assume you did this to keep the check in place for the error message when strict=True
in the calling function. It's just a bit odd looking here because I'd expect we'd just skip above. I'd be tempted to maybe move the extras logic into this function and that would simplify the logic a bit. So add strict
as an argument to this function and collect extras for an error message if it's set here.
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.
Thanks for looking at this Kalyan, sorry it took me awfully long to respond first time round. I left a comment on the implementation, and additionally:
- please can you add a test case to the suite as a regression test? I think the relevant file is
test/python/circuit/test_parameters.py
, but it's worth checking. - please can you add a release note about the bug fix? There's instructions on release notes here
qiskit/circuit/quantumcircuit.py
Outdated
elif isinstance(parameter, str): | ||
elif isinstance(parameter, str) and self.has_parameter(parameter): | ||
out[self.get_parameter(parameter)] = value | ||
elif isinstance(parameter, str) and not self.has_parameter(parameter): | ||
out[Parameter(parameter)] = value |
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.
If the QuantumCircuit
doesn't have the parameter, I don't think there's any benefit making a dummy Parameter
instance - it'll never bind to anything anyway. I think we can just not add it to the dictionary if it's not present and we're in strict=False
mode, but we still need to throw an error if we're in strict=True
. We might need to thread a strict
keyword argument down to this inner method.
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.
@jakelishman @mtreinish Thanks for the suggestions. However, even if we pass strict
to _unroll_param_dict
, the fact that a dictionary with string keys gets passed to target._data.assign_parameters_mapping(raw_mapping)
will remain a problem, unless we convert the keys to Parameter
type. If the parameters are already present in the circuit, it is not a problem because the method get_parameter
converts the keys from string type to Parameter
type. However, if the parameters are not present in the circuit, we will have to create a Parameter
instance out of the strings.
Once it is passed as a dictionary with Parameter keys to the assign_parameters
method, the rest of the logic falls in place. We don't really need to pass strict
to the _unroll_param_dict
method.
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.
Changed as suggested by @jakelishman. strict
argument passed to the inner method.
Pull Request Test Coverage Report for Build 15473811754Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
7faa378
to
4e4d811
Compare
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.
Thanks Kalyan - I pushed up a small commit to make the test case more rigorous (testing that the result was correct by comparison to a known-good call), but otherwise the changeset is good.
I'll backport this into all supported branches - it'll go out in 1.4.4 and (at least) 2.1.0 final.
@Mergifyio backport stable/1.4 stable/2.1 |
✅ Backports have been created
|
* allows string values in assign_parameters with strict=False * added release notes and a test case * formatting changes in unittest file * Changes with `strict` passed to an inner method. * formatting changes in release notes * corrections in inner method * Make test more rigorous --------- Co-authored-by: Jake Lishman <jake.lishman@ibm.com> (cherry picked from commit 25dec6a)
* allows string values in assign_parameters with strict=False * added release notes and a test case * formatting changes in unittest file * Changes with `strict` passed to an inner method. * formatting changes in release notes * corrections in inner method * Make test more rigorous --------- Co-authored-by: Jake Lishman <jake.lishman@ibm.com> (cherry picked from commit 25dec6a)
…14554) * allows string values in assign_parameters with strict=False * added release notes and a test case * formatting changes in unittest file * Changes with `strict` passed to an inner method. * formatting changes in release notes * corrections in inner method * Make test more rigorous --------- (cherry picked from commit 25dec6a) Co-authored-by: kaldag <kalyan.dasgupta@gmail.com> Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
…14553) * allows string values in assign_parameters with strict=False * added release notes and a test case * formatting changes in unittest file * Changes with `strict` passed to an inner method. * formatting changes in release notes * corrections in inner method * Make test more rigorous --------- (cherry picked from commit 25dec6a) Co-authored-by: kaldag <kalyan.dasgupta@gmail.com> Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
…4365) * allows string values in assign_parameters with strict=False * added release notes and a test case * formatting changes in unittest file * Changes with `strict` passed to an inner method. * formatting changes in release notes * corrections in inner method * Make test more rigorous --------- Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Summary
Allows for string values as arguments in
assign_parameters
withstrict=False
.Fixes #13933
Details and comments