Tutorial 1. Controlling a basic experiment using MeasurementControl¶
The complete source code of this tutorial can be found in
Following this Tutorial requires familiarity with the core concepts of Quantify, we highly recommended to consult the (short) User guide before proceeding (see Quantify documentation). If you have some difficulties following the tutorial it might be worth reviewing the User guide !
It takes care of saving the data in a standardized format as well as live plotting of the data during the experiment. Quantify makes a distinction between Iterative measurements and Batched measurements.
In a Batched measurement, the
MeasurementControl processes setpoints in batches, for example triggering 10 samples and then reading those 10 outputs.
This is useful in resource constrained or overhead heavy situations.
Both measurement policies can be 1D, 2D or higher dimensional. Quantify also supports adaptive measurements in which the datapoints are determined during the measurement loop, which are explored in subsequent tutorials.
This tutorial is structured as follows. In the first section we use a 1D Iterative loop to explain the flow of a basic experiment. We start by setting up a noisy cosine model to serve as our mock setup and then use the meas_ctrl to measure this. We then execute an analysis on the data from this experiment.
Import modules and instantiate the MeasurementControl¶
%matplotlib inline import numpy as np
import quantify_core.visualization.pyqt_plotmon as pqm from quantify_core.analysis import base_analysis as ba from quantify_core.analysis import cosine_analysis as ca from quantify_core.data.handling import set_datadir from quantify_core.measurement import MeasurementControl from quantify_core.utilities.examples_support import ( default_datadir, mk_cosine_instrument, ) from quantify_core.utilities.experiment_helpers import create_plotmon_from_historical
from quantify_core.utilities.inspect_utils import display_source_code from quantify_core.visualization.instrument_monitor import InstrumentMonitor
Tip: What data directory should I use?
We highly recommended to settle for a single common data directory for all
notebooks/experiments within your measurement setup/PC (e.g.
The utilities to find/search/extract data only work if all the experiment containers
are located within the same directory.
set_datadir(default_datadir()) # change me!
Data will be saved in: /home/docs/quantify-data
meas_ctrl = MeasurementControl("meas_ctrl") # Create the live plotting intrument which handles the graphical interface # Two windows will be created, the main will feature 1D plots and any 2D plots will go to the secondary plotmon = pqm.PlotMonitor_pyqt("plotmon") # Connect the live plotting monitor to the measurement control meas_ctrl.instr_plotmon(plotmon.name) # The instrument monitor will give an overview of all parameters of all instruments insmon = InstrumentMonitor("Instruments Monitor") # By connecting to the meas_ctrl the parameters will be updated in real-time during an experiment. meas_ctrl.instrument_monitor(insmon.name)
Define a simple model¶
We start by defining a simple model to mock our experiment setup (i.e. emulate physical setup for demonstration purpose). We will be generating a cosine with some normally distributed noise added on top of it.
# We create an instrument to contain all the parameters of our model to ensure # we have proper data logging. display_source_code(mk_cosine_instrument)
def mk_cosine_instrument() -> Instrument: """A container of parameters (mock instrument) providing a cosine model.""" instr = Instrument("ParameterHolder") # ManualParameter's is a handy class that preserves the QCoDeS' Parameter # structure without necessarily having a connection to the physical world instr.add_parameter( "amp", initial_value=1, unit="V", label="Amplitude", parameter_class=ManualParameter, ) instr.add_parameter( "freq", initial_value=0.5, unit="Hz", label="Frequency", parameter_class=ManualParameter, ) instr.add_parameter( "t", initial_value=1, unit="s", label="Time", parameter_class=ManualParameter ) instr.add_parameter( "phi", initial_value=0, unit="Rad", label="Phase", parameter_class=ManualParameter, ) instr.add_parameter( "noise_level", initial_value=0.05, unit="V", label="Noise level", parameter_class=ManualParameter, ) instr.add_parameter( "acq_delay", initial_value=0.02, unit="s", parameter_class=ManualParameter ) def cosine_model(): sleep(instr.acq_delay()) # simulates the acquisition delay of an instrument return ( cos_func(instr.t(), instr.amp(), instr.freq(), phase=instr.phi(), offset=0) + np.random.randn() * instr.noise_level() ) # Wrap our function in a Parameter to be able to associate metadata to it, e.g. unit instr.add_parameter( name="sig", label="Signal level", unit="V", get_cmd=cosine_model ) return instr
pars = mk_cosine_instrument()
Many experiments involving physical instruments are much slower than the time it takes to simulate our cosine_model, that is why we added a sleep() controlled by the acq_delay.
This allows us to exemplify (later in the tutorial) some of the features of the meas_ctrl that would be imperceptible otherwise.
# by setting this to a non-zero value we can see the live plotting in action for a slower experiment pars.acq_delay(0.0)
A 1D Iterative loop¶
Running the 1D experiment¶
The complete experiment is defined in just 4 lines of code. We specify what parameter we want to set, time t in this case, what points to measure at, and what parameter to measure.
We then tell the MeasurementControl meas_ctrl to run which will return an
meas_ctrl.settables( pars.t ) # as a QCoDeS parameter, 't' obeys the JSON schema for a valid Settable and can be passed to the meas_ctrl directly. meas_ctrl.setpoints(np.linspace(0, 2, 50)) meas_ctrl.gettables( pars.sig ) # as a QCoDeS parameter, 'sig' obeys the JSON schema for a valid Gettable and can be passed to the meas_ctrl directly. dataset = meas_ctrl.run("Cosine test")
Starting iterative measurement... 2% completed | elapsed time: 0s | time left: 4s 2% completed | elapsed time: 0s | time left: 4s 100% completed | elapsed time: 0s | time left: 0s 100% completed | elapsed time: 0s | time left: 0s
# The dataset has a time-based unique identifier automatically assigned to it # The name of the experiment is stored as well dataset.attrs["tuid"], dataset.attrs["name"]
('20211208-141307-319-13d6b2', 'Cosine test')
As shown below, a Data variable is assigned to each dimension of the settables and the gettable(s), following a format in which the settable take the form x0, x1, etc. and the gettable(s) the form y0, y1, y2, etc.. You can click on the icons on the right to see the attributes of each variable and the values.
<xarray.Dataset> Dimensions: (dim_0: 50) Coordinates: x0 (dim_0) float64 0.0 0.04082 0.08163 0.1224 ... 1.918 1.959 2.0 Dimensions without coordinates: dim_0 Data variables: y0 (dim_0) float64 0.5367 0.468 0.5099 0.3701 ... 0.4297 0.48 0.51 Attributes: tuid: 20211208-141307-319-13d6b2 name: Cosine test grid_2d: False grid_2d_uniformly_spaced: False
- dim_0: 50
- x0(dim_0)float640.0 0.04082 0.08163 ... 1.959 2.0
- name :
- long_name :
- units :
- batched :
array([0. , 0.04081633, 0.08163265, 0.12244898, 0.16326531, 0.20408163, 0.24489796, 0.28571429, 0.32653061, 0.36734694, 0.40816327, 0.44897959, 0.48979592, 0.53061224, 0.57142857, 0.6122449 , 0.65306122, 0.69387755, 0.73469388, 0.7755102 , 0.81632653, 0.85714286, 0.89795918, 0.93877551, 0.97959184, 1.02040816, 1.06122449, 1.10204082, 1.14285714, 1.18367347, 1.2244898 , 1.26530612, 1.30612245, 1.34693878, 1.3877551 , 1.42857143, 1.46938776, 1.51020408, 1.55102041, 1.59183673, 1.63265306, 1.67346939, 1.71428571, 1.75510204, 1.79591837, 1.83673469, 1.87755102, 1.91836735, 1.95918367, 2. ])
- y0(dim_0)float640.5367 0.468 0.5099 ... 0.48 0.51
- name :
- long_name :
- Signal level
- units :
- batched :
array([ 0.53673561, 0.46796812, 0.50985668, 0.3701023 , 0.21565074, 0.13424155, 0.11030636, -0.15382444, -0.23683876, -0.33462527, -0.4163621 , -0.60903279, -0.65018945, -0.4011746 , -0.43623181, -0.3749326 , -0.25986156, -0.18252125, -0.03717678, 0.07007941, 0.18128912, 0.30914878, 0.39296684, 0.43339883, 0.45131383, 0.446591 , 0.58259465, 0.39924091, 0.30692563, 0.21932734, 0.01614244, -0.04009115, -0.18341005, -0.28755134, -0.42380416, -0.46198959, -0.48344647, -0.48695904, -0.44118425, -0.36548403, -0.29059918, -0.25487874, -0.13168564, 0.01209121, 0.16653239, 0.21892451, 0.35135912, 0.42972453, 0.47996584, 0.50997829])
- tuid :
- name :
- Cosine test
- grid_2d :
- grid_2d_uniformly_spaced :
We can play with some live plotting options to see how the meas_ctrl behaves when changing the update interval.
# By default the meas_ctrl updates the datafile and live plot every 0.1 seconds (and not faster) to reduce overhead. meas_ctrl.update_interval( 0.1 ) # Setting it even to 0.01 creates a dramatic slowdown, try it out!
In order to avoid an experiment being bottlenecked by the update_interval we recommend setting it between ~0.1-1.0 s for a comfortable refresh rate and good performance.
meas_ctrl.settables(pars.t) meas_ctrl.setpoints(np.linspace(0, 50, 1000)) meas_ctrl.gettables(pars.sig) dataset = meas_ctrl.run("Many points live plot test")
Starting iterative measurement... 0% completed | elapsed time: 0s | time left: 47s 0% completed | elapsed time: 0s | time left: 47s 31% completed | elapsed time: 0s | time left: 0s 31% completed | elapsed time: 0s | time left: 0s
55% completed | elapsed time: 0s | time left: 0s 55% completed | elapsed time: 0s | time left: 0s 95% completed | elapsed time: 0s | time left: 0s 95% completed | elapsed time: 0s | time left: 0s
100% completed | elapsed time: 0s | time left: 0s 100% completed | elapsed time: 0s | time left: 0s
pars.noise_level(0) # let's disable noise from here on to get prettier figures
Analyzing the experiment¶
Plotting the data and saving the plots for a simple 1D case can be achieve in a few lines using a standard analysis from the
quantify_core.analysis.base_analysis module. In the same module you can find several common analyses that might fit your needs. It also provides a base data-analysis class (
BaseAnalysis) – a flexible framework for building custom analyses, which we explore in detail in a dedicated tutorial.
Dataset generated by the meas_ctrl contains all the information required to perform basic analysis of the experiment. Running an analysis can be as simple as:
a_obj = ca.CosineAnalysis(label="Cosine test").run() a_obj.display_figs_mpl()
Here the analysis loads the latest dataset on disk matching a search based on the
BaseAnalysis for alternative dataset specification.
After loading the data, it executes the different steps of the analysis and saves the results into a directory within the experiment container.
The Data storage contains more details on the folder structure and
files contained in the data directory. The the
quantify_core.data.handling module provides
convenience data searching and handling utilities like
For guidance on creating custom analyses, e.g., fitting a model to the data, see Tutorial 3. Building custom analyses - the data analysis framework where we showcase the implementation of the analysis above.
A 2D Iterative loop¶
It is often desired to measure heatmaps (2D grids) of some parameter. This can be done by specifying two settables. The setpoints of the grid can be specified in two ways.
Method 1 - a quick grid¶
times = np.linspace(0, 5, 500) amps = np.linspace(-1, 0, 31) meas_ctrl.settables([pars.t, pars.amp]) # meas_ctrl takes care of creating a meshgrid meas_ctrl.setpoints_grid([times, amps]) meas_ctrl.gettables(pars.sig) dataset = meas_ctrl.run("2D Cosine test")
Starting iterative measurement... 0% completed | elapsed time: 0s | time left: 1537s 0% completed | elapsed time: 0s | time left: 1537s
16% completed | elapsed time: 2s | time left: 10s 16% completed | elapsed time: 2s | time left: 10s
36% completed | elapsed time: 4s | time left: 7s 36% completed | elapsed time: 4s | time left: 7s
56% completed | elapsed time: 6s | time left: 4s 56% completed | elapsed time: 6s | time left: 4s
79% completed | elapsed time: 8s | time left: 2s 79% completed | elapsed time: 8s | time left: 2s
100% completed | elapsed time: 10s | time left: 0s 100% completed | elapsed time: 10s | time left: 0s
a_obj = ba.Basic2DAnalysis(label="2D Cosine test").run() a_obj.display_figs_mpl()
Method 2 - custom tuples in 2D¶
N.B. it is also possible to do this for higher dimensional loops
r = np.linspace(0, 1.2, 200) dt = np.linspace(0, 0.5, 200) f = 3 theta = np.cos(2 * np.pi * f * dt) def polar_coords(r_, theta_): x_ = r_ * np.cos(2 * np.pi * theta_) y_ = r_ * np.sin(2 * np.pi * theta_) return x_, y_ x, y = polar_coords(r, theta) setpoints = np.column_stack([x, y]) setpoints[:5] # show a few points
array([[ 0.00000000e+00, -0.00000000e+00], [ 6.03000109e-03, -4.24843885e-05], [ 1.20555181e-02, -3.39642455e-04], [ 1.80542054e-02, -1.14460856e-03], [ 2.39683681e-02, -2.70570137e-03]])
meas_ctrl.settables([pars.t, pars.amp]) meas_ctrl.setpoints(setpoints) meas_ctrl.gettables(pars.sig) dataset = meas_ctrl.run("2D radial setpoints")
Starting iterative measurement... 0% completed | elapsed time: 0s | time left: 18s 0% completed | elapsed time: 0s | time left: 18s 100% completed | elapsed time: 0s | time left: 0s 100% completed | elapsed time: 0s | time left: 0s
In this case running a simple (non-interpolated) 2D analysis will not be meaningful. Nevertheless the dataset can be loaded back using the
plotmon_loaded = create_plotmon_from_historical(label="2D radial setpoints") plotmon_loaded.secondary_QtPlot