Skip to content
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

Make InstructionScheduleMap support ParameterExpressions #4940

Merged
merged 6 commits into from Sep 15, 2020

Conversation

menehune23
Copy link
Contributor

@menehune23 menehune23 commented Aug 18, 2020

Summary

@menehune23 menehune23 force-pushed the feature/3923-param-expressions branch 3 times, most recently from b22852f to 4563c30 Compare August 18, 2020 06:11
@taalexander taalexander added this to To do in Pulse via automation Aug 21, 2020
@taalexander taalexander self-assigned this Aug 21, 2020
@taalexander taalexander added the type: enhancement It's working, but needs polishing label Aug 21, 2020
Copy link
Contributor

@taalexander taalexander left a comment

Choose a reason for hiding this comment

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

Looks good, just a couple minor changes recommended.

qiskit/pulse/instruction_schedule_map.py Outdated Show resolved Hide resolved
qiskit/pulse/instruction_schedule_map.py Outdated Show resolved Hide resolved
qiskit/pulse/instruction_schedule_map.py Outdated Show resolved Hide resolved
@menehune23 menehune23 force-pushed the feature/3923-param-expressions branch 2 times, most recently from c744dd1 to e2b89bc Compare August 24, 2020 04:16
Copy link
Contributor

@taalexander taalexander left a comment

Choose a reason for hiding this comment

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

Were we going to cast a ParameterExpression to a float in the ParameterizedSchedule if supplied?

qiskit/pulse/instruction_schedule_map.py Outdated Show resolved Hide resolved
@nkanazawa1989
Copy link
Contributor

nkanazawa1989 commented Aug 25, 2020

@menehune23 Thanks for the update. This is really great and I've been waiting this for long time.

  • How do we bind actual parameters to the ParameterExpression?, i.e. synchronization with circuit parameter.

Pulse scheduler always pass *args. This cannot generate **kwargs due to its implementation.
https://github.com/Qiskit/qiskit-terra/blob/43c649018124de507d876cf3bfedc02abacbcce8/qiskit/scheduler/lowering.py#L87-L88

For example, is it possible to create an entry of Rx(theta) gate as shown in this paper?
https://arxiv.org/abs/2004.11205

We may want to add rx to basis gates:
https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/circuit/library/standard_gates/rx.py#L23

Then scheduler pass theta to the instmap and it convert theta to amp of Gaussian. In this case theta may be also ParameterExpression with a bound float value.

  • parameter ordering issue.

In ParameterizedSchedule the order of parameters is force sorted by key and thus function signature of generator is not remained.
https://github.com/Qiskit/qiskit-terra/blob/43c649018124de507d876cf3bfedc02abacbcce8/qiskit/pulse/schedule.py#L776

@menehune23
Copy link
Contributor Author

Were we going to cast a ParameterExpression to a float in the ParameterizedSchedule if supplied?

@taalexander Per the discussion in #3923, my understanding was that for this issue, we'd cast to float just before passing to any callable schedule (i.e. functions and ParameterizedSchedules). Let me know if that's not what you had in mind.

I know that eventually, the ParameterizedSchedule will be replaced with a proper implementation that can take in ParameterExpressions directly (#3384). It was at that later time that I was planning to adjust the casting logic in the InstructionScheduleMap to only cast for functions, but leave them uncast (remaining as ParameterExpressions) for the new schedule type.

@menehune23 menehune23 force-pushed the feature/3923-param-expressions branch from d4a3afe to 7dc40ed Compare August 26, 2020 04:21
@menehune23
Copy link
Contributor Author

menehune23 commented Aug 26, 2020

@nkanazawa1989

  • How do we bind actual parameters to the ParameterExpression?, i.e. synchronization with circuit parameter.

If you take a look at the test changes, you'll see how to bind a value to a ParameterExpression. In your case, inst.params can contain one or more ParameterExpressions (they must be bound, so that they can be cast to floats by the InstructionScheduleMap, as seen in this PR). Let me know if I understood your question correctly.

In ParameterizedSchedule the order of parameters is force sorted by key and thus function signature of generator is not remained.

I understand the problem, and I agree that it seems like a bug. That said, it looks like a pre-existing bug that should be filed separately.

@menehune23 menehune23 force-pushed the feature/3923-param-expressions branch from c8127c1 to c054633 Compare August 26, 2020 06:24
@nkanazawa1989
Copy link
Contributor

nkanazawa1989 commented Aug 26, 2020

In your test case the parameter is bound before set to the generator function (this is the PR to add parameter object to schedule, so basically this test case is already enough). However we want to keep them as unbound parameter objects and bind a value before sending the program to the backend. Perhaps we can use parametric pulses because they can keep argument as parameters.
https://github.com/Qiskit/qiskit-terra/blob/04f20797eaa1a460e405adb8333508cd73bc448b/qiskit/pulse/library/parametric_pulses.py#L148-L149

I understand the problem, and I agree that it seems like a bug. That said, it looks like a pre-existing bug that should be filed separately.

This is not a bug, it's intentional. In the textualized CmdDef of U3 gate there are three parameters P1 P2 P3 associated with the shift phase instructions. However the order of instruction is not [P1, P2, P3] and we need to sort this to match with U3 gate definition to convert the gate into schedule without using **kwargs. We need to modify circuit parameter framework or write smart parser of CmdDef to address this.

I agree that those can be addressed in another PR.

@menehune23
Copy link
Contributor Author

@nkanazawa1989 My apologies. I misunderstood where the binding was expected to happen. I'll look into this further when I have some time outside of work.

@menehune23
Copy link
Contributor Author

@taalexander @nkanazawa1989 I'm realizing too that my original thoughts around this issue were based on replacing the param type with ParameterExpressions. Since we're actually just adding it as an additional type (adding to the Union type), I need to step backwards and rethink some of my original ideas. It is beginning to make more sense now though.

@menehune23 menehune23 force-pushed the feature/3923-param-expressions branch 3 times, most recently from b46c25b to 1c1f2a2 Compare September 1, 2020 05:04
@menehune23 menehune23 changed the title Make InstructionScheduleMap params ParameterExpressions Make InstructionScheduleMap support ParameterExpressions Sep 1, 2020
@menehune23
Copy link
Contributor Author

@taalexander @nkanazawa1989 Now that I've had a chance to revisit this with better understanding, please take another fresh look. I summarized the changes in the release note, so please let me know if my understanding of when binds should occur is correct. Thanks!


inst_map = InstructionScheduleMap()
inst_map.add('f', (0,), test_func)
self.assertEqual(inst_map.get('f', (0,), x_test), ref_sched)
self.assertEqual(inst_map.get('f', (0,), dur_expr), expected_sched)
Copy link
Contributor

Choose a reason for hiding this comment

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

In this testcase dur_expr is fixed by dur_val defined in outer scope of generator (so parameter is kind of static). I understand that we have lack of framework that manages unbound parameters in the instruction schedule map, but here you intend in future this will be supported and dur_expr will be able to be updated dynamically? i.e.

inst_map.get('f', (0,), t=10)
inst_map.get('f', (0,), t=20)
inst_map.get('f', (0,), t=30)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nkanazawa1989 Ah yes, that should be possible. In fact, I’ll update the test case to demonstrate this, when I get a chance. Great thought! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nkanazawa1989 Updated. Let me know your thoughts.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps I misunderstood the purpose of this PR (in my example above it doesn't need to use ParameterExpression and we can just feed the parameter t into generator). Now I understand the intention of your test case. Anyways the code looks good now.

qiskit/pulse/schedule.py Show resolved Hide resolved
@mergify mergify bot merged commit d146305 into Qiskit:master Sep 15, 2020
Pulse automation moved this from To do to Done Sep 15, 2020
@lcapelluto lcapelluto added the Changelog: New Feature Include in the "Added" section of the changelog label Oct 14, 2020
@1ucian0 1ucian0 added the mod: pulse Related to the Pulse module label Jan 15, 2021
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 mod: pulse Related to the Pulse module type: enhancement It's working, but needs polishing
Projects
No open projects
Pulse
  
Done
Development

Successfully merging this pull request may close these issues.

InstructionScheduleMap parameters should be ParameterExpressions
5 participants