Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1381 +/- ##
==========================================
- Coverage 39.10% 39.03% -0.07%
==========================================
Files 114 114
Lines 5674 5684 +10
==========================================
Hits 2219 2219
- Misses 3455 3465 +10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
alecandido
left a comment
There was a problem hiding this comment.
Just a comment on the type hint.
As mentioned, the np.unique function can be used to simplify the implementation.
We could also add another small improvement, just in terms of performances, deduplicating at the level of Pulse objects, rather than evaluated waveforms. This will just save the repeated evaluation
| amplitude_swept: set[PulseId], | ||
| duration_swept: dict[PulseLike, Sweeper], | ||
| ) -> dict[ComponentId, WaveformSpec]: | ||
| ) -> Union[dict[WaveformInd, WaveformSpec], WaveformIndices]: |
There was a problem hiding this comment.
| ) -> Union[dict[WaveformInd, WaveformSpec], WaveformIndices]: | |
| ) -> tuple[dict[WaveformInd, WaveformSpec], WaveformIndices]: |
alecandido
left a comment
There was a problem hiding this comment.
The current review is meant as "adiabatic". But I believe we can even do it without the cache and np.unique() on the waveforms. Let me investigate
|
|
||
| class Q1Sequence(Model): | ||
| waveforms: Waveforms | ||
| waveforms: dict[WaveformInd, Waveform] |
There was a problem hiding this comment.
You can rename this Waveforms, given that is also used nowhere else
|
|
||
| ComponentId = tuple[UUID4, int] | ||
| WaveformIndices = dict[ComponentId, tuple[int, int]] | ||
| WaveformInd = int |
There was a problem hiding this comment.
We can afford two more characters (on top of the eleven...)
| WaveformInd = int | |
| WaveformIndex = int |
| duration=int(duration_), | ||
| ) | ||
|
|
||
| # we do this since pulse is not hashable |
There was a problem hiding this comment.
In [1]: from qibolab import Pulse, Rectangular, Readout, Acquisition
In [2]: p = Pulse(amplitude=0.3, duration=40, envelope=Rectangular())
In [3]: r = Readout(acquisition=Acquisition(duration=30), probe=p)
In [4]: {p: 3, r: 4}
Out[4]:
{Pulse(id_=UUID('e5069a81-3a6e-4fee-a0f6-eaab0da119e2'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Rectangular(kind='rectangular'), relative_phase=0.0): 3,
Readout(id_=UUID('768f4f08-ca1e-4cc4-8839-cd48b93cccaf'), kind='readout', acquisition=Acquisition(id_=UUID('e6f84555-878a-4f31-b718-83839065378e'), kind='acquisition', duration=30.0), probe=Pulse(id_=UUID('e5069a81-3a6e-4fee-a0f6-eaab0da119e2'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Rectangular(kind='rectangular'), relative_phase=0.0)): 4}
In [6]: hash(p)
Out[6]: -853604456920802955
In [7]: hash(r)
Out[7]: 7280396492324559753?
| # we do this since pulse is not hashable | ||
| cache = {} | ||
|
|
||
| def waveform_cached(pulse, component, duration=None): |
There was a problem hiding this comment.
| def waveform_cached(pulse, component, duration=None): | |
| def waveform_cached(pulse: Pulse, component: str, duration: Optional[float] = None) -> WaveformSpec: |
| waveform_specs = { | ||
| idx: waveforms[np.where(inverse_idx == idx)[0][0]] for idx in unique_idx | ||
| } |
There was a problem hiding this comment.
The inverse_idx is indexing the unique values, while idx in unique_idx is indexing the original array. This np.where() seems actually wrong
In [30]: a
Out[30]: array(['c', 'a', 'a', 'a', 'c', 'a', 'a', 'c', 'c', 'b'], dtype='<U1')
In [31]: _, uni, inv = np.unique(a, return_inverse=True, return_index=True)
In [32]: np.where(inv == uni[1])
Out[32]: (array([], dtype=int64),)There was a problem hiding this comment.
The correct one should be directly
| waveform_specs = { | |
| idx: waveforms[np.where(inverse_idx == idx)[0][0]] for idx in unique_idx | |
| } | |
| waveform_specs = {idx: waveforms[idx] for idx in unique_idx} |
since unique_idx is meant to be the index where to unique values in bytes_ are taken from, and they should correspond to the same index in waveforms.
There was a problem hiding this comment.
Actually, maybe even
| waveform_specs = { | |
| idx: waveforms[np.where(inverse_idx == idx)[0][0]] for idx in unique_idx | |
| } | |
| waveform_specs = {inverse_idx[idx]: waveforms[idx] for idx in unique_idx} |
since the indices included in waveform_indices are actually the inverse ones
|
Ok, working with In [42]: ps
Out[42]:
[Pulse(id_=UUID('e5069a81-3a6e-4fee-a0f6-eaab0da119e2'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('aaca1878-3b5a-4f7a-bb47-f8070476f1f2'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0),
Pulse(id_=UUID('c700876d-8bdf-4de5-a786-1dabb5778a16'), kind='pulse', duration=40.0, amplitude=1e-07, envelope=Rectangular(kind='rectangular'), relative_phase=0.0)]
In [43]: ps_ = np.random.choice(ps, 10)
In [44]: ps_
Out[44]:
array([Pulse(id_=UUID('c700876d-8bdf-4de5-a786-1dabb5778a16'), kind='pulse', duration=40.0, amplitude=1e-07, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('aaca1878-3b5a-4f7a-bb47-f8070476f1f2'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0),
Pulse(id_=UUID('c700876d-8bdf-4de5-a786-1dabb5778a16'), kind='pulse', duration=40.0, amplitude=1e-07, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('e5069a81-3a6e-4fee-a0f6-eaab0da119e2'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('c700876d-8bdf-4de5-a786-1dabb5778a16'), kind='pulse', duration=40.0, amplitude=1e-07, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('aaca1878-3b5a-4f7a-bb47-f8070476f1f2'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0),
Pulse(id_=UUID('aaca1878-3b5a-4f7a-bb47-f8070476f1f2'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0),
Pulse(id_=UUID('aaca1878-3b5a-4f7a-bb47-f8070476f1f2'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0),
Pulse(id_=UUID('c700876d-8bdf-4de5-a786-1dabb5778a16'), kind='pulse', duration=40.0, amplitude=1e-07, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('aaca1878-3b5a-4f7a-bb47-f8070476f1f2'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0)],
dtype=object)
In [52]: ps__ = [Pulse.model_validate(p.model_dump()) for p in ps_]
In [53]: ps__
Out[53]:
[Pulse(id_=UUID('fa913bc5-8b30-4bf5-8ec7-7a58b875253e'), kind='pulse', duration=40.0, amplitude=1e-07, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('f58f1d30-978c-43cf-ac26-1868a3b04f5b'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0),
Pulse(id_=UUID('32a3df43-d53b-4a5d-81e4-449f2929f665'), kind='pulse', duration=40.0, amplitude=1e-07, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('5a568694-3d10-49bc-9cf6-57078fedcf2a'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('e052dd88-5c03-49cd-a683-9ed43151bd46'), kind='pulse', duration=40.0, amplitude=1e-07, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('ce15f263-8484-4ce1-85be-f79848dca8b7'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0),
Pulse(id_=UUID('f2ee585d-6e18-4c55-94f6-c871212cba71'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0),
Pulse(id_=UUID('19fa6f9f-8f09-4b9d-ad60-f8863ecbdf18'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0),
Pulse(id_=UUID('b59d159f-4923-4d99-99c8-51d76db9de09'), kind='pulse', duration=40.0, amplitude=1e-07, envelope=Rectangular(kind='rectangular'), relative_phase=0.0),
Pulse(id_=UUID('8ce16341-4dfa-4429-afde-b078cfe2ea3e'), kind='pulse', duration=40.0, amplitude=0.3, envelope=Gaussian(kind='gaussian', rel_sigma=0.1), relative_phase=0.0)]
In [55]: [hash(p) for p in ps__]
Out[55]:
[2182729606208933173,
-5036063928699656048,
2182729606208933173,
-853604456920802955,
2182729606208933173,
-5036063928699656048,
-5036063928699656048,
-5036063928699656048,
2182729606208933173,
-5036063928699656048]
In [56]: np.unique([hash(p) for p in ps__])
Out[56]: array([-5036063928699656048, -853604456920802955, 2182729606208933173]) |
66d5eff to
9d20486
Compare
9d20486 to
d0f9852
Compare
closes #1376