-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Assignment of parameters in pulse Schedule
/ScheduleBlock
doable through parameter name
#12088
Assignment of parameters in pulse Schedule
/ScheduleBlock
doable through parameter name
#12088
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 the following people are requested to review this:
|
59ae72f
to
cbaed51
Compare
@arthurostrauss Is there previous discussion of this? It is convenient, but it is not something that |
cbaed51
to
c9f51e7
Compare
The motivation behind such feature lies in the will to integrate in a closer form the primitives V2 to pulse level control, as the former do carry |
Pull Request Test Coverage Report for Build 8855032197Details
💛 - Coveralls |
By the way after checking, it is possible to use
|
c9f51e7
to
b01354e
Compare
@arthurostrauss That sounds nice! My main concern was adding something to pulse |
b01354e
to
6d76478
Compare
Hi @wshanks, any updates on this? |
6d76478
to
665f7a6
Compare
@arthurostrauss For my part, I had an initial concern about deviating from |
665f7a6
to
376b54b
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.
Although the type annotations do not advertise it, assign_parameters
already supports str
. What you are adding here is support for assigning a sequence to elements of a ParameterVector
using the name of the ParameterVector
like:
from qiskit import pulse
from qiskit.circuit import ParameterVector
vec = ParameterVector("test", 2)
with pulse.builder.build() as sched:
pulse.builder.play(pulse.Gaussian(100, vec[0], vec[1]), pulse.DriveChannel(0))
sched.assign_parameters({"theta": [1, 2]})
Since ParameterVector
support was only added recently in #12045, this PR should be viewed as a follow up to it.
Going back to my original concern about deviation from the behavior of QuantumCircuit
, I note that QuantumCircuit.assign_parameters
does not allow assigning to a parameter vector by name. We should see if there is interest in such support for that case as well.
releasenotes/notes/parameter_assignment_by_name_for_pulse_schedules-3a27bbbbf235fb9e.yaml
Show resolved
Hide resolved
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.
Sorry for the slightly redundant comments in the previous review. I was shifting comments around and hadn't meant to submit the review yet. Here were a couple other comments.
qiskit/pulse/parameter_manager.py
Outdated
if param.name == parameter: | ||
out[param] = value | ||
elif ( | ||
isinstance(param, ParameterVectorElement) and param.vector.name == parameter |
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 find this part to be getting a little complicated. Could we lift the string handling to before this loop? So in a preceding loop, loop over parameter_binds
and make a new copy that has all Parameter
and ParameterVector
keys by replacing any strings with the appropriately named matches?
376b54b
to
5406185
Compare
releasenotes/notes/parameter_assignment_by_name_for_pulse_schedules-3a27bbbbf235fb9e.yaml
Outdated
Show resolved
Hide resolved
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.
@arthurostrauss Could you address my comment about lifting up the string handling to earlier in the function and my minor suggestion about the release note? Otherwise, I think this looks good.
d0a5834
to
6a58f19
Compare
qiskit/pulse/parameter_manager.py
Outdated
@@ -410,21 +411,23 @@ def _unroll_param_dict( | |||
A dictionary from parameter to value. | |||
""" | |||
out = {} | |||
param_name_dict = {param.name: param for param in self.parameters} |
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 does not account for the fact that more than one parameter could have the same name, which pulse allows but circuit does not (it gives an error when trying to add the second parameter).
I think would be best to loop over self.parameters
and make one dict shaped like dict[str, list[Parameter | ParameterVector]]
mapping each name to the parameters with that name. Then make a new dict and loop over parameter_binds
copying in the key and values for when the key is a Parameter
or ParameterVector
and replacing the key when it is a string with an entry for each value in the first dict for that string (could be more than one parameter). Then loop over that dict like below but without accounting for string keys.
You may want to account for a name being used for a parameter and a parameter vector. There is validation for vectors but not for scalars. If you tried to assign to a list a string name used by both a ParameterVector
and a Parameter
, it would not catch the assignment of a list to a Parameter
here.
Maybe it is worth testing duplicated names?
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.
So just to clarify, the expected behaviour is that every Parameter
carrying the same name should be assigned to the same value? Or should it be that a list of values could be passed such that all parameters carrying the same name are taken into account? If it's the latter case, then how can we resolve this when using string parameter assignment?
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.
Every Parameter
carrying the same name should be assigned the same value when a string is assigned a value. Also, every ParameterVector
should be assigned the same list of values.
releasenotes/notes/parameter_assignment_by_name_for_pulse_schedules-3a27bbbbf235fb9e.yaml
Outdated
Show resolved
Hide resolved
a3ec205
to
6c53868
Compare
It is now possible to specify in the mapping the names of the parameters instead of the parameters themselves to assign parameters to pulse `Schedule`s and `ScheduleBlock`s. It is even possible to assign all the parameters of a ParameterVector by just specifying its name and setting as a value a list of parameter values.
…b9e.yaml Co-authored-by: Will Shanks <wshaos@posteo.net>
Corrected mistake in string assignment Corrected mistake in string assignment
…dules-3a27bbbbf235fb9e.yaml Co-authored-by: Will Shanks <wshaos@posteo.net>
The check is now based on the value type to infer if assignment should be done on Parameters or ParameterVectors Removed unnecessary import from utils Corrected string assignment Correction part 2 Corrected test The inplace=True argument was preventing the reuse of a parametrized waveform in the schedule, making the test fail
461aacc
to
054022e
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.
This looks good to me now!
Summary
This PR extends the previous usage of the method
assign_parameters
ofSchedule
andScheduleBlock
by enabling the specification of the parameter name instead of the parameter directly. This assignment by name also works when assigning values to aParameterVector
(the length of the list of parameter values should in this case match the length of the vector).Details and comments