.. _experiment_settings:

*********************************
Workflow with Experiment settings
*********************************

Parameters, Settings, and SettingNodes
======================================

The quantum computer is controlled by many instruments which
typically have a large number of control knobs for different purposes.
The controls between different instrument models may differ slightly in usage,
but in the end provide the same functionality.

The Settings in EXA provide a way to group, change and view the huge amount of settings that need to be set to
operate the instruments.

For more information, see the documentation of :mod:`Parameters and Settings <exa.common.data.parameter>` and
:mod:`SettingNode <exa.common.data.setting_node>`.


Workflows with settings
=======================

There are two possible workflows for running individual experiments.
The first one relies on an assumption that a consistent set of observations has been loaded to update all settings.
In this case, the user does not need to change any settings.
The second workflow is getting a template for possible settings for the experiment, modifying the values in the
template, and submitting the modified settings when running the experiment.

The workflows are presented in `settings_workflow_figure`. The arrows with large heads show the direction of Settings.

.. _settings_workflow_figure:

.. figure:: figures/exa_settings_workflow.png
   :width: 100 %
   :alt: exa settings workflow diagram

   Settings workflow. The arrows with large heads show the direction of Settings.

.. note::

    It is important to understand that the settings returned by ``get_settings`` are only a template.
    Modifying it **does not** immediately change anything in the physical instruments.
    Moreover, it **does not** reflect the current status of the instruments.
    Instead, it represents the user's **intent**.
    The settings are only applied to the instruments when submitted to Station Control.



Using the defaults
==================

Most Experiments can be run without modifying the instrument settings, as shown by the black
arrows in `settings_workflow_figure`.
This requires that the station and the :doc:`configuration <configuration_file>`
have been set up with sensible default values, and we have set :ref:`observation <observations>` loading rules
that fetch good calibration values.

The workflow is roughly:

1. Define experiment-specific default values in the EXA configuration.
   Load the EXA configuration with :func:`~exa.core.provider.provider.init_experiment_configuration`.
2. Set loading rules with :func:`~exa.core.observation.observation_loading_rules.set_global_default_observation_loading_rules`.
3. Change experiment options, such as number of repetitions.
4. Run experiment without referencing settings in any way.

Note that steps 1-3 are optional.
In this workflow, the experiment merges the settings from Station Control, the configuration,
and observations, and sends them to Station Control behind the scenes.

.. _modifying_experiment_settings:

Modifying the settings manually
===============================

In a more experimental workflow, we might want to manually override the settings generated by the experiment.

After the optional steps 1-3 above, the workflow is roughly:

4. Get a template for settings:

   .. code-block:: python

       my_settings = experiment.get_settings(component_to_measure)

   The method returns a :class:`~exa.common.data.setting_node.SettingNode` object that contains
   all the suggested settings that are needed to run the experiment.
   The structure and values are specific to the experiment.

5. Make modifications to ``my_settings``, as explained in :mod:`exa.common.data.setting_node`.
   Typical use cases are changing an outdated value or just trying out new things.
   Note that this step is optional: the settings can be passed back to the experiment as is.

6. Run experiment as

   .. code-block:: python

       experiment.run(..., settings=my_settings)

   The modified settings are passed to the experiment and later to Station Control for execution.

   If the ``run`` is called without settings, step 4 is executed behind the scenes.


Branches of the setting tree
============================

The settings tree has the following main branches:

* **controllers**: These correspond to the station settings, grouped by component.
* **gates**: Gate calibration data.
* **gate_definitions**: Information about known gates and their implementations.
* **stages**: Stages of the Compiler pipeline that generates the final run definition.
* **characterization**: Other information needed by the experiment.

  * **gate_properties**: Auxiliary information about gates obtained during calibration.
  * **model**: Physical characteristics of the QPU.
  * **other**: Namespace reserved for user-defined settings.


Merging rules
=============

When the ``get_settings`` method of an Experiment is called, the settings are combined from many sources and merged
with certain priorities.
A source with a higher priority overrides the value from lower-priority sources.

For controller settings, the sources and their priorities are:

1. Station Control default settings
2. Defaults from EXA configuration, "settings" section

   a. Settings common for all components
   b. Settings specific to a component, any experiment
   c. Settings specific to experiment, any component
   d. Settings specific to a component, specific experiment

3. Settings defined by the experiment source code
4. Observations
5. Runtime overrides by user

For gate settings, they are:

1. Defaults from EXA configuration, "gates" section

   a. Settings common for all loci
   b. Settings specific to a locus

2. Settings defined by the experiment source code
3. Observations
4. Runtime overrides by user
