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 analytic pulse function samplers #2042

Merged
merged 50 commits into from
Apr 16, 2019
Merged

Conversation

taalexander
Copy link
Contributor

@taalexander taalexander commented Mar 28, 2019

Summary

Add analytic pulse sampler which convert an analytic pulse of signature:
def f(times: np.ndarray, *args, **kwargs) -> np.ndarray
to a discrete pulse:
def g(duration: int, *args, **kwargs) -> SamplePulse

These will be used to implement the discrete pulse library from analytic pulse functions.

Closes #1935.

Details and comments

Currently implemented samplers:

  • left
  • right
  • midpoint

These are decorators that may be used like:

@pulse.samplers.left
def linear(x: np.ndarray, m: float, b: float) -> np.ndarray:
    """Linear test function
    Args:
        x: Input times.
        m: Slope.
        b: Intercept
    Returns:
        np.ndarray
    """
    return m*x+b

@taalexander taalexander changed the title Add analytic pulse function samplers [WIP] Add analytic pulse function samplers Mar 29, 2019
@taalexander taalexander force-pushed the issue-1935-samplers branch from b63167b to 936ff28 Compare March 29, 2019 03:10
@taalexander taalexander changed the title [WIP] Add analytic pulse function samplers Add analytic pulse function samplers Mar 29, 2019
@nkanazawa1989
Copy link
Contributor

Can you add name to sampler? just to have the same interface with functional_pulse.

@taalexander
Copy link
Contributor Author

Since the sampler just returns a functional_pulse it already has the same interface as functional_pulse.

They operate on a function with the signature:
`def f(times: np.ndarray, *args, **kwargs) -> np.ndarray`
Where `times` is a numpy array of floats with length `n_times` and the output array
is a complex numpy array with length `n_times`. The output of the decorator is an
Copy link
Member

Choose a reason for hiding this comment

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

update documentation (n_times not defined anywhere)

return generate_sampler


def left_sample_fun(continuous_pulse: Callable, duration: int, *args, **kwargs) -> np.ndarray:
Copy link
Member

Choose a reason for hiding this comment

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

seems like this function should be deleted.

@ajavadia
Copy link
Member

ajavadia commented Apr 10, 2019

can you help me understand this:

left_linear_pulse_fun = samplers.left(linear)
pulse = left_linear_pulse_fun(duration, m=m, b=b)

So linear is just a function (not a pulse yet). Why does it need to have the times argument, which is characteristic of a discrete pulse?

And then in the above, do we get an instance of a continuous pulse somewhere?

I may be a bit lost here but what benefit do decorators have over just something simple like pulse objects having methods

a_sample_pulse = a_continuous_pulse.sample(strategy='left')

@taalexander
Copy link
Contributor Author

taalexander commented Apr 10, 2019

All samplers concern themselves with doing is taking a function whose input is a list of continuous times and outputting a function whose first input is a duration, and then wrapping with a FunctionalPulse. The times are characteristic of a continuous pulse and are an array of times to evaluate the continuous function with. Discrete pulses accept an integer duration. By sampling we now have an object that may be evaluated to return decorators.

The approach you suggest means having another type of Instruction class to handle. It makes plotting harder as well. I would like to go in this direction, in the long run, to support analytic pulses which have a duration and an underlying set of parameters (same parameters as parameterized circuits) that may then be sampled in the way you suggest to output a SamplePulse, or passed as an analytic pulse directly if supported by the backend.

WIth the current implementations samplers are used like

@pulse.sample.left
def linear(times, m, b):
    return m*times + b

duration = 10
m= 0.01
b = 0.0

sched += linear(duration, m, b)

@ajavadia
Copy link
Member

Ok i'm fine moving forward. Tagged @kdk in case he has comments on the design, but I think we can also revisit later.

@kdk kdk merged commit d2bc1f6 into Qiskit:master Apr 16, 2019
lia-approves pushed a commit to edasgupta/qiskit-terra that referenced this pull request Jul 30, 2019
* Update remote simulator test for backend IBMQ/Aer changes

* Added test module and case for samplers.

* Added base sampling decorator along with left, right, and midpoint samplers

* Added tests to samplers.

* change FunctionalPulse to decorator

* update error function and imports

* update unittest

* add functools.wraps

* remove getter, setter from sample pulse

* fix lint

* Added dynamic updating of docstrings for decorated sampled functions.

* Added explicit documentation for standard library samplers.

* Added extensive sampler module docstring for developers.

* Update changelog with sampler.

* Separate standard library and external libraries

* Updated changelog for functional_pulse.

* update sampler for functional_pulse decorator. Add additional tests.

* Changed all instances of 'analytic' to 'continuous'

* Update sampler module structure to separate sampling functions and decorators.

* Remove sampler prefix to sampler module's modules.

* Update small bug from rename

* Change dtype casting to np.complex_

* remove accidental remote simulator file.

* Removed required default parameters.

* Update changelog. Update defaults model validation to add nonetypes.

* Update changelog.

* Readd parameters to be required.

* Update docstring and delete extra function following review.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Standard sampling library
4 participants