Skip to content

Commit

Permalink
Merge pull request #536 from qiboteam/fix_fit_builer
Browse files Browse the repository at this point in the history
Fix update not working using `qq fit`
  • Loading branch information
andrea-pasquale authored Oct 4, 2023
2 parents 2a6d6c6 + d8bec37 commit 1eadcc9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 60 deletions.
89 changes: 35 additions & 54 deletions src/qibocal/auto/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ def __post_init__(self):
if len(self.qubits) == 0:
self.qubits = self.action.qubits

def _allocate_local_qubits(self, qubits, platform):
if len(self.qubits) > 0:
if platform is not None:
if any(isinstance(i, tuple) for i in self.qubits):
task_qubits = allocate_qubits_pairs(platform, self.qubits)
elif any(
isinstance(i, tuple) or isinstance(i, list) for i in self.qubits
):
task_qubits = allocate_single_qubits_lists(platform, self.qubits)
else:
task_qubits = allocate_single_qubits(platform, self.qubits)
else:
# None platform use just ids
task_qubits = self.qubits
else:
task_qubits = qubits
self.qubits = list(task_qubits)

return task_qubits

@property
def id(self) -> Id:
"""Task Id."""
Expand Down Expand Up @@ -112,69 +132,29 @@ def run(
folder: Path = None,
):
completed = Completed(self, Normal(), folder)
task_qubits = self._allocate_local_qubits(qubits, platform)

if mode.name in ["autocalibration", "acquire"]:
completed.data, completed.data_time = self._acquire(
platform=platform, qubits=qubits
)

if mode.name in ["autocalibration", "fit"]:
completed.results, completed.results_time = self._fit(completed.data)

return completed

def _acquire(self, platform: Platform, qubits: Union[Qubits, QubitsPairs]) -> Data:
"""Acquisition
Args:
platform (`Platform`): Qibolab's platform
qubits (`Union[Qubits, QubitsPairs]`): Qubit or QubitPairs dict.
Returns:
data (`Data`): data acquisition output
time (float): acquisition time
"""
try:
operation: Routine = self.operation
parameters = self.parameters
except RuntimeError:
operation = dummy_operation
parameters = DummyPars()
if operation.platform_dependent and operation.qubits_dependent:
if len(self.qubits) > 0:
if platform is not None:
if operation.two_qubit_gates:
qubits = allocate_qubits_pairs(platform, self.qubits)
elif any(
isinstance(i, tuple) or isinstance(i, list) for i in self.qubits
):
qubits = allocate_single_qubits_lists(platform, self.qubits)
else:
qubits = allocate_single_qubits(platform, self.qubits)
else:
qubits = self.qubits

data, time = operation.acquisition(
parameters, platform=platform, qubits=qubits
)
# after acquisition we update the qubit parameter
self.qubits = list(qubits)
else:
data, time = operation.acquisition(parameters, platform=platform)
return data, time

def _fit(self, data: Data) -> Results:
"""Fitting
if mode.name in ["autocalibration", "acquire"]:
if operation.platform_dependent and operation.qubits_dependent:
completed.data, completed.data_time = operation.acquisition(
parameters, platform=platform, qubits=task_qubits
)
else:
completed.data, completed.data_time = operation.acquisition(
parameters, platform=platform
)

Args:
data (`Data`): data acquisition
if mode.name in ["autocalibration", "fit"]:
completed.results, completed.results_time = operation.fit(completed.data)

Returns:
results (`Results`): fitting output
time (float): fitting time
"""
operation: Routine = self.operation
return operation.fit(data)
return completed


@dataclass
Expand Down Expand Up @@ -231,7 +211,8 @@ def results(self, results: Results):
@property
def data(self):
"""Access task's data."""
if not (self.datapath / DATAFILE).is_file():
# FIXME: temporary fix for coverage
if not (self.datapath / DATAFILE).is_file(): # pragma: no cover
return None
if self._data is None:
Data = self.task.operation.data_type
Expand Down
5 changes: 3 additions & 2 deletions src/qibocal/cli/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..auto.history import add_timings_to_meta
from ..auto.mode import ExecutionMode
from ..auto.runcard import Runcard
from .utils import META, RUNCARD, UPDATED_PLATFORM
from .utils import META, RUNCARD, UPDATED_PLATFORM, create_qubits_dict


def fit(path, update):
Expand All @@ -22,8 +22,9 @@ def fit(path, update):
# load path, meta, runcard and executor
meta = yaml.safe_load((path / META).read_text())
runcard = Runcard.load(yaml.safe_load((path / RUNCARD).read_text()))
qubits = create_qubits_dict(runcard)
executor = Executor.load(
runcard, path, update=update, platform=runcard.platform_obj
runcard, path, update=update, platform=runcard.platform_obj, qubits=qubits
)

# perform post-processing
Expand Down
6 changes: 4 additions & 2 deletions src/qibocal/cli/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from qibocal.auto.runcard import Runcard
from qibocal.auto.task import TaskId

from .utils import create_qubits_dict

META = "meta.json"
RUNCARD = "runcard.yml"
UPDATED_PLATFORM = "new_platform.yml"
Expand All @@ -27,9 +29,9 @@ def report(path):
# load path, meta and runcard
meta = yaml.safe_load((path / META).read_text())
runcard = Runcard.load(yaml.safe_load((path / RUNCARD).read_text()))

qubits = create_qubits_dict(runcard)
# load executor
executor = Executor.load(runcard, path)
executor = Executor.load(runcard, path, qubits=qubits)

# produce html
builder = ReportBuilder(path, runcard.qubits, executor, meta)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def test_execution(card: pathlib.Path):
"""
testcard = TestCard(**yaml.safe_load(card.read_text(encoding="utf-8")))
executor = Executor.load(testcard.runcard, output=pathlib.Path(tempfile.mkdtemp()))
executor = Executor.load(
testcard.runcard,
output=pathlib.Path(tempfile.mkdtemp()),
qubits=testcard.runcard.qubits,
)
list(executor.run(mode=ExecutionMode.acquire))

assert testcard.validation.result == [step[0] for step in executor.history]
4 changes: 3 additions & 1 deletion tests/test_task_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def test_qubits_argument(platform, local_qubits):
global_qubits = (
allocate_single_qubits(platform, QUBITS) if platform is not None else QUBITS
)
data, time = task._acquire(platform, global_qubits)
task._allocate_local_qubits(global_qubits, platform)

_, _ = task.operation.acquisition(task.parameters, platform, global_qubits)
if local_qubits:
assert task.qubits == local_qubits
else:
Expand Down

0 comments on commit 1eadcc9

Please sign in to comment.