From 5a726684298bc41740e81252a13d066112c55d85 Mon Sep 17 00:00:00 2001 From: Jiahao Yuan Date: Fri, 5 Jul 2024 14:41:52 +0800 Subject: [PATCH] feat: measure element duration (#183) --- bosing.pyi | 1 + docs/quickstart.rst | 7 +++++++ example/flexible_length.py | 22 ++++++++++++++++++++++ src/lib.rs | 7 ++++++- 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 example/flexible_length.py diff --git a/bosing.pyi b/bosing.pyi index 2f64d76..6bcd0f8 100644 --- a/bosing.pyi +++ b/bosing.pyi @@ -82,6 +82,7 @@ class Element: def max_duration(self) -> float: ... @property def min_duration(self) -> float: ... + def measure(self) -> float: ... @final class Play(Element): diff --git a/docs/quickstart.rst b/docs/quickstart.rst index c4bf0ea..4cebcc6 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -27,3 +27,10 @@ .. plot:: ../example/flexible.py :include-source: + + +获取 `Element` 长度 +------------------- + +.. plot:: ../example/flexible_length.py + :include-source: diff --git a/example/flexible_length.py b/example/flexible_length.py new file mode 100644 index 0000000..7b865ae --- /dev/null +++ b/example/flexible_length.py @@ -0,0 +1,22 @@ +import matplotlib.pyplot as plt + +from bosing import Channel, Hann, Play, Stack, generate_waveforms + +sample_rate = 2e9 +schedule = Stack( + Play( + channel_id="xy", + shape_id="hann", + amplitude=0.3, + width=100e-9, + plateau=200e-9, + ), + margin=10e-9, +) +channels = {"xy": Channel(30e6, sample_rate, int(sample_rate * schedule.measure()))} +shapes = {"hann": Hann()} +result = generate_waveforms(channels, shapes, schedule) +w = result["xy"] +plt.plot(w[0], label="I") +plt.plot(w[1], label="Q") +plt.legend() diff --git a/src/lib.rs b/src/lib.rs index 20b0b62..cd5d813 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ use crate::{ Sampler, }, quant::{Amplitude, ChannelId, Frequency, Phase, ShapeId, Time}, - schedule::{ElementCommonBuilder, ElementRef}, + schedule::{ElementCommonBuilder, ElementRef, Measure}, }; /// Channel configuration. @@ -452,6 +452,11 @@ impl Element { fn min_duration(&self) -> Time { self.0.common.min_duration() } + + /// Measure the minimum duration of the element. + fn measure(&self) -> Time { + self.0.measure() + } } trait ElementSubclass: Sized + DerefToPyAny