Skip to content

Commit

Permalink
Add save_results flag (#397)
Browse files Browse the repository at this point in the history
* fix: 🐛 Add save_results flag.

* Small change

* Small change

* style: 🎨 Remove unused imports.

* Disconnect all instruments after execution.

* Small fix

* Small fix

* style: 🎨 Improve execute function.
  • Loading branch information
AlbertMitjans authored May 29, 2023
1 parent 1b43106 commit a23b8d4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
24 changes: 5 additions & 19 deletions src/qililab/execute.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import os
from pathlib import Path

from qibo.models import Circuit

import qililab as ql


def execute(circuit: Circuit, runcard_name: str):
def execute(circuit: Circuit, runcard_name: str, nshots=1):
"""Execute a qibo with qililab and native gates
Args:
Expand Down Expand Up @@ -46,21 +43,10 @@ def execute(circuit: Circuit, runcard_name: str):
# create platform
platform = ql.build_platform(name=runcard_name)

settings = ql.ExperimentSettings(
hardware_average=1,
repetition_duration=0,
software_average=1,
)
options = ql.ExperimentOptions(
loops=[], # loops to run the experiment
settings=settings, # experiment settings
)
settings = ql.ExperimentSettings(hardware_average=1, repetition_duration=0, software_average=nshots)
options = ql.ExperimentOptions(settings=settings)

# create experiment with options
sample_experiment = ql.Experiment(
platform=platform, # platform to run the experiment
circuits=[circuit], # circuits to run the experiment
options=options, # experiment options
)
sample_experiment = ql.Experiment(platform=platform, circuits=[circuit], options=options)

return sample_experiment.execute()
return sample_experiment.execute(save_results=False)
42 changes: 25 additions & 17 deletions src/qililab/experiment/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from threading import Thread

import numpy as np
from qcodes.instrument import Instrument as QcodesInstrument
from qibo.models.circuit import Circuit
from tqdm.auto import tqdm

Expand All @@ -33,7 +34,7 @@ class Experiment:
# Specify the types of the attributes that are not defined during initialization
execution_manager: ExecutionManager
results: Results
results_path: Path
results_path: Path | None
_plot: LivePlot | None
_remote_id: int

Expand Down Expand Up @@ -66,7 +67,7 @@ def build_execution(self):
# Build ``ExecutionManager`` class
self.execution_manager = EXECUTION_BUILDER.build(platform=self.platform, pulse_schedules=self.pulse_schedules)

def run(self) -> Results:
def run(self, save_results=True) -> Results:
"""This method is responsible for:
* Creating the live plotting (if connection is provided).
* Preparing the `Results` class and the `results.yml` file.
Expand All @@ -90,13 +91,13 @@ def run(self) -> Results:
)
if not hasattr(self, "execution_manager"):
raise ValueError("Please build the execution_manager before running an experiment.")

# Prepares the results
self.results, self.results_path = self.prepare_results()
num_schedules = self.execution_manager.num_schedules
self.results, self.results_path = self.prepare_results(save_results=save_results)

data_queue: Queue = Queue() # queue used to store the experiment results
self._asynchronous_data_handling(queue=data_queue)

num_schedules = self.execution_manager.num_schedules
for idx, _ in itertools.product(
tqdm(range(num_schedules), desc="Sequences", leave=False, disable=num_schedules == 1),
range(self.software_average),
Expand Down Expand Up @@ -133,9 +134,11 @@ def _threaded_function():
q = np.array(acq["q"])
amplitude = 20 * np.log10(np.abs(i + 1j * q)).astype(np.float64)
self._plot.send_points(value=amplitude[0])
with open(file=self.results_path / "results.yml", mode="a", encoding="utf8") as data_file:
result_dict = result.to_dict()
yaml.safe_dump(data=[result_dict], stream=data_file, sort_keys=False)

if self.results_path is not None:
with open(file=self.results_path / "results.yml", mode="a", encoding="utf8") as data_file:
result_dict = result.to_dict()
yaml.safe_dump(data=[result_dict], stream=data_file, sort_keys=False)

thread = Thread(target=_threaded_function)
thread.start()
Expand Down Expand Up @@ -171,7 +174,7 @@ def disconnect(self):
"""Disconnects from the instruments and releases the device."""
self.platform.disconnect()

def execute(self) -> Results:
def execute(self, save_results=True) -> Results:
"""Runs the whole execution pipeline, which includes the following steps:
* Connect to the instruments.
Expand All @@ -191,9 +194,10 @@ def execute(self) -> Results:
self.initial_setup()
self.build_execution()
self.turn_on_instruments()
results = self.run()
results = self.run(save_results=save_results)
self.turn_off_instruments()
self.disconnect()
QcodesInstrument.close_all()
return results

def remote_save_experiment(self) -> None:
Expand Down Expand Up @@ -434,7 +438,7 @@ def repetition_duration(self):
"""
return self.options.settings.repetition_duration

def prepare_results(self) -> tuple[Results, Path]:
def prepare_results(self, save_results=True) -> tuple[Results, Path | None]:
"""Creates the ``Results`` class, creates the ``results.yml`` file where the results will be saved, and dumps
the experiment data into this file.
Expand All @@ -452,13 +456,17 @@ def prepare_results(self) -> tuple[Results, Path]:
num_schedules=self.execution_manager.num_schedules,
loops=self.options.loops,
)
# Create the folders & files needed to save the results locally
results_path = self._path_to_results_folder()
self._create_results_file(results_path)

# Dump the experiment data into the created file
with open(file=results_path / EXPERIMENT_FILENAME, mode="w", encoding="utf-8") as experiment_file:
yaml.dump(data=self.to_dict(), stream=experiment_file, sort_keys=False)
if save_results:
# Create the folders & files needed to save the results locally
results_path = self._path_to_results_folder()
self._create_results_file(results_path)

# Dump the experiment data into the created file
with open(file=results_path / EXPERIMENT_FILENAME, mode="w", encoding="utf-8") as experiment_file:
yaml.dump(data=self.to_dict(), stream=experiment_file, sort_keys=False)
else:
results_path = None

return results, results_path

Expand Down

0 comments on commit a23b8d4

Please sign in to comment.