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

Add pulse builder "macro" decorator #4960

Closed
taalexander opened this issue Aug 20, 2020 · 7 comments · Fixed by #5257
Closed

Add pulse builder "macro" decorator #4960

taalexander opened this issue Aug 20, 2020 · 7 comments · Fixed by #5257
Assignees
Labels
good first issue Good for newcomers mod: pulse Related to the Pulse module type: enhancement It's working, but needs polishing
Projects

Comments

@taalexander
Copy link
Contributor

taalexander commented Aug 20, 2020

What is the expected enhancement?

I have found a decorator of this form very useful for constructing pulse experiments from functions.

I suggest macro to represent a python function with an activated builder context that will be called and evaluated during program construction. I propose that we reserve function for support of pulse functions which will be analyzed and compiled as a function in the pulse IR with a signature, to then be loaded and evaluated in the hardware.

import functools
from qiskit.pulse import builder


# decorator for pulse builder function
def macro(func):
    @functools.wraps(func)
    def inst_map_wrapper(*args, **kwargs):
        builder_ = builder._active_builder()
        # activate the pulse builder before calling the function
        with pulse.build(builder_.backend) as built:
            output =  func(*args, **kwargs)
        
        builder_.call_schedule(built)
        return output
        

    return inst_map_wrapper


@macro
def nested(a):
    pulse.play(pulse.Gaussian(100, a, 20), pulse.drive_channel(0))
    return a*2

@macro
def test():
    pulse.play(pulse.Constant(100, 1.0), pulse.drive_channel(0))
    output = nested(0.5)
    print(output)

with pulse.build(backend=backend) as sched:
    print(builder._active_builder())
    test()

sched.draw()

We should add this to the standard pulse builder library, document, rename what needs to be to be clearer and test.

@taalexander taalexander added type: enhancement It's working, but needs polishing good first issue Good for newcomers labels Aug 20, 2020
@taalexander taalexander added this to To do in Pulse via automation Aug 20, 2020
@Aniket049
Copy link

how can i contribute to this?

@taalexander
Copy link
Contributor Author

Hello @Aniket049, to get started you could add the macro decorator to pulse.builder.py and add some documentation/tests. We could iterate from there. Would you like to me to assign this to you?

@Aniket049
Copy link

Aniket049 commented Aug 31, 2020 via email

@taalexander
Copy link
Contributor Author

Hi @Aniket049, have you made any progress here?

@Aniket049
Copy link

Aniket049 commented Sep 15, 2020 via email

@taalexander
Copy link
Contributor Author

Hi @Aniket049, any update here? 😄

@taalexander taalexander assigned taalexander and unassigned Aniket049 Oct 19, 2020
@taalexander
Copy link
Contributor Author

Hello @Aniket049, I've reassigned to myself as I needed this functionality. I hope this isn't too much of an inconvenience.

@1ucian0 1ucian0 added the mod: pulse Related to the Pulse module label Jan 15, 2021
@mergify mergify bot closed this as completed in #5257 Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers mod: pulse Related to the Pulse module type: enhancement It's working, but needs polishing
Projects
No open projects
Pulse
  
To do
Development

Successfully merging a pull request may close this issue.

3 participants