Skip to content

Commit

Permalink
Merge branch 'develop' into improve-indices
Browse files Browse the repository at this point in the history
  • Loading branch information
CdeTerra authored Apr 13, 2022
2 parents 50cf251 + 4f684f3 commit ad381ce
Show file tree
Hide file tree
Showing 14 changed files with 625 additions and 265 deletions.
7 changes: 6 additions & 1 deletion pulser/register/mappable_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
if TYPE_CHECKING: # pragma: no cover
from pulser.register.base_register import BaseRegister, QubitId
from pulser.register.register_layout import RegisterLayout
from typing import Sequence as abcSequence


class MappableRegister:
Expand Down Expand Up @@ -67,8 +68,12 @@ def build_register(self, qubits: Mapping[QubitId, int]) -> BaseRegister:
raise ValueError(
"All qubits must be labeled with pre-declared qubit IDs."
)
register_ordered_qubits = {
id: qubits[id] for id in self._qubit_ids if id in chosen_ids
}
return self._layout.define_register(
*tuple(qubits.values()), qubit_ids=chosen_ids
*tuple(register_ordered_qubits.values()),
qubit_ids=tuple(register_ordered_qubits.keys()),
)

def find_indices(
Expand Down
2 changes: 1 addition & 1 deletion pulser/sampler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 Pulser Development Team
# Copyright 2022 Pulser Development Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
65 changes: 65 additions & 0 deletions pulser/sampler/noise_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2022 Pulser Development Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Defines a NoiseModel and how to apply it to the samples."""
from __future__ import annotations

import functools
from typing import Callable

from pulser.sampler.samples import QubitSamples

NoiseModel = Callable[[QubitSamples], QubitSamples]
"""A function that apply some noise on a list of QubitSamples.
A NoiseModel corresponds to a source of noises present in a device which is
relevant when sampling the input pulses. Physical effects contributing to
modifications of the shined amplitude, detuning and phase felt by qubits of the
register are susceptible to be implemented by a NoiseModel.
"""


def compose_local_noises(*functions: NoiseModel) -> NoiseModel:
"""Helper to compose multiple NoiseModel.
Args:
*functions: a list of functions
Returns:
The mathematical composition of *functions. The last element is applied
first. If *functions is [f, g, h], it returns f∘g∘h.
"""
return functools.reduce(
lambda f, g: lambda x: f(g(x)), functions, lambda x: x
)


def apply_noises(
samples: list[QubitSamples], noises: list[NoiseModel]
) -> list[QubitSamples]:
"""Apply a list of NoiseModel on a list of QubitSamples.
The noises are composed using the compose_local_noises function, such that
the last element is applied first.
Args:
samples (list[QubitSamples]): A list of QubitSamples.
noises (list[NoiseModel]): A list of NoiseModel.
Return:
A list of QubitSamples on which each element of noises has been
applied.
"""
tot_noise = compose_local_noises(*noises)

return [tot_noise(s) for s in samples]
21 changes: 17 additions & 4 deletions pulser/sampler/sampler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Copyright 2022 Pulser Development Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Exposes the sample() functions.
It contains many helpers.
Expand All @@ -10,18 +23,18 @@

import numpy as np

import pulser.simulation.noises as noises
from pulser.channels import Channel
from pulser.pulse import Pulse
from pulser.sampler.noise_model import NoiseModel, apply_noises
from pulser.sampler.samples import QubitSamples
from pulser.sequence import Sequence, _TimeSlot


def sample(
seq: Sequence,
modulation: bool = False,
common_noises: Optional[list[noises.NoiseModel]] = None,
global_noises: Optional[list[noises.NoiseModel]] = None,
common_noises: Optional[list[NoiseModel]] = None,
global_noises: Optional[list[NoiseModel]] = None,
) -> dict:
"""Samples the given Sequence and returns a nested dictionary.
Expand Down Expand Up @@ -80,7 +93,7 @@ def sample(
if modulation:
s = _modulate(ch, s)

s = noises.apply(s, ch_noises)
s = apply_noises(s, ch_noises)

if slm_on: # Update the samples of masked qubits during SLM on times
for i, _ in enumerate(s):
Expand Down
13 changes: 13 additions & 0 deletions pulser/sampler/samples.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Copyright 2022 Pulser Development Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Defines samples dataclasses."""
from __future__ import annotations

Expand Down
Loading

0 comments on commit ad381ce

Please sign in to comment.