Skip to content

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

Merged
merged 11 commits into from
Jun 6, 2025

Conversation

kaldag
Copy link
Contributor

@kaldag kaldag commented May 15, 2025

Summary

Allows for string values as arguments in assign_parameters with strict=False.
Fixes #13933

Details and comments

@kaldag kaldag requested a review from a team as a code owner May 15, 2025 05:26
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label May 15, 2025
@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 following people are relevant to this code:

  • @Qiskit/terra-core

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 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?

Comment on lines 4812 to 4813
elif isinstance(parameter, str) and not self.has_parameter(parameter):
out[Parameter(parameter)] = value
Copy link
Member

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.

Copy link
Member

@jakelishman jakelishman 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 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

Comment on lines 4810 to 4813
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
Copy link
Member

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.

Copy link
Contributor Author

@kaldag kaldag May 23, 2025

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.

Copy link
Contributor Author

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.

@coveralls
Copy link

coveralls commented May 23, 2025

Pull Request Test Coverage Report for Build 15473811754

Warning: 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

  • 4 of 5 (80.0%) changed or added relevant lines in 1 file are covered.
  • 20 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.004%) to 87.976%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 4 5 80.0%
Files with Coverage Reduction New Missed Lines %
crates/circuit/src/symbol_expr.rs 4 73.77%
crates/qasm2/src/lex.rs 4 92.23%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 15473244251: -0.004%
Covered Lines: 83021
Relevant Lines: 94368

💛 - Coveralls

@eliarbel eliarbel removed the Community PR PRs from contributors that are not 'members' of the Qiskit repo label May 29, 2025
@kaldag kaldag force-pushed the assign_params_strict branch from 7faa378 to 4e4d811 Compare June 3, 2025 08:24
Copy link
Member

@jakelishman jakelishman left a 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.

@jakelishman
Copy link
Member

@Mergifyio backport stable/1.4 stable/2.1

@jakelishman jakelishman enabled auto-merge June 6, 2025 14:11
@jakelishman jakelishman added Changelog: Bugfix Include in the "Fixed" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Jun 6, 2025
Copy link
Contributor

mergify bot commented Jun 6, 2025

backport stable/1.4 stable/2.1

✅ Backports have been created

@jakelishman jakelishman added this pull request to the merge queue Jun 6, 2025
Merged via the queue into Qiskit:main with commit 25dec6a Jun 6, 2025
27 checks passed
mergify bot pushed a commit that referenced this pull request Jun 6, 2025
* 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)
mergify bot pushed a commit that referenced this pull request Jun 6, 2025
* 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)
github-merge-queue bot pushed a commit that referenced this pull request Jun 6, 2025
…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>
github-merge-queue bot pushed a commit that referenced this pull request Jun 6, 2025
…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>
@kaldag kaldag deleted the assign_params_strict branch June 6, 2025 19:38
rahaman-quantum pushed a commit to rahaman-quantum/qiskit that referenced this pull request Jun 20, 2025
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Setting strict=False in QuantumCircuit.assign_parameters has no effect
6 participants