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

Registration decorator for parametric pulse shapes #3688

Open
lcapelluto opened this issue Jan 6, 2020 · 10 comments
Open

Registration decorator for parametric pulse shapes #3688

lcapelluto opened this issue Jan 6, 2020 · 10 comments
Assignees
Labels
mod: pulse Related to the Pulse module priority: low type: feature request New feature or request

Comments

@lcapelluto
Copy link
Contributor

lcapelluto commented Jan 6, 2020

What is the expected behavior?

From @eggerdj on PR #3463 which introduces Parametric Pulses:

Is there some way around this structure? I'm thinking of the following scenario:
External party, named ABC, wants to use Qiskit and provide their own pulse shapes. It would be a lot nicer if, a user of ABC, in his application, could do

from ABC.parametric_pulses import ABCPulse

where ABC.parametric_pulses.py contains the parametric pulses defined by ABC, without having to manually register the parametric pulse names in this Enum class. Is something like that possible?

The goal of this feature is to replace manually entering new parametric pulses into the mapper in assemble.

@lcapelluto lcapelluto added the type: feature request New feature or request label Jan 6, 2020
@lcapelluto lcapelluto added this to the 0.12 milestone Jan 6, 2020
@lcapelluto lcapelluto assigned lcapelluto and eggerdj and unassigned lcapelluto Jan 6, 2020
@taalexander taalexander mentioned this issue Jan 6, 2020
1 task
@ajavadia ajavadia modified the milestones: 0.12, 0.13 Jan 28, 2020
@kdk kdk removed this from the 0.13 milestone Mar 17, 2020
@drewrisinger
Copy link
Contributor

As an end-user looking at doing exactly this, I'd be interested in this functionality. If there is no registration decorator, where would we need to plug into/overwrite this functionality? qiskit.qobj.converters.pulse_instruction.ParametricPulseShapes, AFAICT? https://github.com/Qiskit/qiskit-terra/blob/6dbf9dd06b52e4530747bae0af69d728f011f431/qiskit/qobj/converters/pulse_instruction.py#L31-L40

@taalexander
Copy link
Contributor

Yes, I think you have the right idea. This would likely need to be modified into a registry, that registers standard pulses.

@drewrisinger
Copy link
Contributor

Hacky and haven't fully tested it, but looks like the following also temporarily does the job (not as good as registering)

import qiskit.qobj.converters.pulse_instruction as pulse_converter

class NewParamPulse(ParametricPulse):
    pass

pulse_converter.ParametricPulseShapes.NewParamPulse = NewParamPulse

@taalexander
Copy link
Contributor

taalexander commented May 16, 2020

This does seem like a good solution 👍.

@drewrisinger
Copy link
Contributor

drewrisinger commented May 19, 2020

follow-up. The above ^^ method doesn't work b/c of the enum lookup in qiskit.assembler.assemble_schedules::_assemble_instructions(): https://github.com/Qiskit/qiskit-terra/blob/ff5fede2dadf338e7ba36a3efcbf4df6b0e651aa/qiskit/assembler/assemble_schedules.py#L179-L180
Per https://docs.python.org/3/library/enum.html#programmatic-access-to-enumeration-members-and-their-attributes, looking up an enum this way (by passing the instruction type i.e. value) is only available for native enum members. So the remedy is to create a new replacement enum with your custom value, and then overwrite the ParametricPulseShapes above. However, due to the assemble_schedules() method being compiled at import time due to pervasive use of from ... import ... syntax, a much more hacky temporary solution is required:

class NewParamPulseShapes(enum.Enum):
    gaussian = ...
    new_param_pulse = ...

import qiskit.assembler
qiskit.assembler.assemble_schedules.__globals__["ParametricPulseShapes"] = NewParamPulseShapes
# NOTE: tried this syntax, didn't work
# pulse_converter.ParametricPulseShapes = NewParamPulseshapes

# Verify
print(qiskit.assembler.assemble_schedules.__globals__["ParametricPulseShapes"])
qiskit.assemble(schedule_with_new_param_pulse) # errors if above global replacement doesn't succeed

Hope this helps people in future!

@drewrisinger
Copy link
Contributor

Found a better solution than above. Unfortunately it involves an extra python library, but it's a one-line solution, so...

It uses https://pypi.org/project/aenum/

import aenum
import qiskit.qobj.converters.pulse_instruction as pulse_converter

aenum.extend_enum(pulse_converter.ParametricPulseShapes, "new_pulse_shape", NewParamPulse)

@1ucian0 1ucian0 added the mod: pulse Related to the Pulse module label Jan 15, 2021
@jwoehr
Copy link
Contributor

jwoehr commented Dec 17, 2021

Did this requirement get subsumed in Pulse Builder?

@drewrisinger
Copy link
Contributor

@jwoehr, I don't think so. Pulse Builder still doesn't cover e.g. a linear ramp parametric pulse defined as e.g. y = m*t + b. I've been using my workaround to add this functionality.

@jwoehr
Copy link
Contributor

jwoehr commented Dec 17, 2021

@jwoehr, I don't think so. Pulse Builder still doesn't cover e.g. a linear ramp parametric pulse defined as e.g. y = m*t + b. I've been using my workaround to add this functionality.

Thank you, noting that, @drewrisinger

@jakelishman
Copy link
Member

I believe this to have been obsoleted by #7821 and follow-on work. @nkanazawa1989, could you check, and close this issue if so?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mod: pulse Related to the Pulse module priority: low type: feature request New feature or request
Projects
None yet
Development

No branches or pull requests

9 participants