Tutorial: Compiling to Hardware

See also

The complete source code of this tutorial can be found in

Compiling to Hardware.ipynb

Compilation allows converting the schedules introduced in Tutorial: Schedules and Pulses into a set of instructions that can be executed on the control hardware.

In this notebook, we will define an example schedule, demonstrate how to compile it, and run it on a virtual hardware setup.

Schedule definition

We start by defining an example schedule.

from quantify_scheduler import Schedule
from quantify_scheduler.operations.pulse_library import SquarePulse
from quantify_scheduler.resources import ClockResource

sched = Schedule("Simple schedule")
sched.add(SquarePulse(amp=0.2, duration=8e-9, port="q0:res", clock="q0.ro"))
sched.add(SquarePulse(amp=0.1, duration=12e-9, port="q0:res", clock="q0.ro"))

readout_clock = ClockResource(name="q0.ro", freq=7e9)
sched.add_resource(readout_clock)

sched
Schedule "Simple schedule" containing (2) 2  (unique) operations.

Hardware configuration

In our example setup, we will use a Qblox Cluster containing an RF control module (QCM-RF). To compile the schedule, we will need to provide the compiler with a dictionary detailing the hardware configuration.

Please check the documentation on how to properly create such a configuration for the supported backends:

Compilation

Now we are ready to proceed to the compilation stage. For each of the control stack’s instruments, the compilation generates:

  • The schedule’s absolute timing. During the schedule’s definition, we didn’t assign absolute times to the operations. Instead, only the duration was defined. For the instruments to know how to execute the schedule, the absolute timing of the operations is calculated.

  • A set of parameters that are used to properly configure each instrument for the execution of the schedule. These parameters typically don’t change during the execution of the schedule.

  • A compiled program that contains instructions on what the instrument must do in order for the schedule to be executed.

We perform the compilation via compile().

We start by setting the directory where the compiled schedule files will be stored, via set_datadir.

from quantify_core.data import handling as dh

dh.set_datadir(dh.default_datadir()) 
Data will be saved in:
/home/rsoko/quantify-data

Next, we create a device configuration that contains all knowledge of the physical device under test (DUT). To generate it we use the QuantumDevice class.

The schedule defined at the beginning of this tutorial consists of 2 pulse operations. As such, the hardware configuration must contain the necessary information to execute the schedule. We add the hardware configuration to the QuantumDevice object and compile the schedule using this information.

from quantify_scheduler.backends.graph_compilation import SerialCompiler
from quantify_scheduler.device_under_test.quantum_device import QuantumDevice

quantum_device = QuantumDevice("DUT")

quantum_device.hardware_config(hardware_cfg)
compiler = SerialCompiler(name="compiler")
compiled_sched = compiler.compile(
    schedule=sched, config=quantum_device.generate_compilation_config()
)

compiled_sched.plot_pulse_diagram(plot_backend='plotly')

The cell above compiles the schedule, returning a CompiledSchedule object. This class differs from Schedule in that it is immutable and contains the compiled_instructions attribute. We inspect these instructions below.

compiled_sched.compiled_instructions
{'cluster0': {'settings': {'reference_source': 'internal'},
  'cluster0_module2': {'sequencers': {'seq0': {'nco_en': True,
     'sync_en': True,
     'connected_outputs': [0, 1],
     'connected_inputs': None,
     'init_offset_awg_path_0': 0.0,
     'init_offset_awg_path_1': 0.0,
     'init_gain_awg_path_0': 1.0,
     'init_gain_awg_path_1': 1.0,
     'modulation_freq': 50000000.0,
     'mixer_corr_phase_offset_degree': -4.1,
     'mixer_corr_gain_ratio': 0.9998,
     'integration_length_acq': None,
     'sequence': {'program': '          \n          set_mrk       1          # set markers to 1                    \n          wait_sync     4                                                \n          upd_param     4                                                \n          wait          4          # latency correction of 4 + 0 ns      \n          move          1,R0       # iterator for loop with label start  \n  start:                                                                 \n          reset_ph                                                       \n          upd_param     4                                                \n          set_awg_gain  6553,0     # setting gain for ModSquarePulse     \n          play          0,1,4      # play ModSquarePulse (8 ns)          \n          wait          4          # auto generated wait (4 ns)          \n          set_awg_gain  3276,0     # setting gain for ModSquarePulse     \n          play          2,3,4      # play ModSquarePulse (12 ns)         \n          wait          8          # auto generated wait (8 ns)          \n          loop          R0,@start                                        \n          stop                                                           \n',
      'waveforms': {'8471807001840742666': {'data': [1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0],
        'index': 0},
       '-7548636948366144465': {'data': [0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0],
        'index': 1},
       '6506832761720335688': {'data': [1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0,
         1.0],
        'index': 2},
       '-4179281123533662613': {'data': [0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0,
         0.0],
        'index': 3}}},
     'seq_fn': '/home/rsoko/quantify-data/schedules/20230926-173147-165-0427e9_q0res_q0.ro.json',
     'ttl_acq_input_select': None,
     'ttl_acq_threshold': None,
     'ttl_acq_auto_bin_incr_en': None}},
   'settings': {'offset_ch0_path0': -5.52,
    'offset_ch0_path1': -5.56,
    'offset_ch1_path0': None,
    'offset_ch1_path1': None,
    'in0_gain': None,
    'in1_gain': None,
    'lo0_freq': 6950000000.0,
    'lo1_freq': None,
    'out0_att': None,
    'out1_att': None,
    'in0_att': None},
   'repetitions': 1}}}

Execution on the hardware

In the compiled schedule, we have all the information necessary to execute the schedule. In this specific case, only sequencer seq0 of the RF control module (QCM-RF) is needed. The compiled schedule contains the file path where the sequencer’s program is stored, as well as the QCoDeS parameters that need to be set in the device.

Now that we have compiled the schedule, we are almost ready to execute it with our control setup.

We start by connecting to a dummy cluster device by passing a dummy_cfg argument when initializing a Cluster:

from qblox_instruments import Cluster, ClusterType

Cluster.close_all()  # Closes all registered instruments (not just Clusters)

cluster0 = Cluster("cluster0", dummy_cfg={"2": ClusterType.CLUSTER_QCM_RF})

Here, dummy_cfg={"2": ClusterType.CLUSTER_QCM_RF} initializes a dummy cluster instrument that contains an RF control module in slot 2, as specified by the example hardware config.

We attach these instruments to the InstrumentCoordinator via the appropriate InstrumentCoordinatorComponentBase component wrapper class. More information on the scheduler execution can be found in the User Guide.

from quantify_scheduler.instrument_coordinator import InstrumentCoordinator
from quantify_scheduler.instrument_coordinator.components.qblox import ClusterComponent

ic = InstrumentCoordinator("ic")
ic.add_component(ClusterComponent(cluster0))

The InstrumentCoordinator object is responsible for the smooth and in-concert operation of the different instruments, so that the provided schedule is correctly executed. Essentially, it “coordinates” the control stack instruments, giving the relevant commands to the different instruments of the control stack at each point in time.

The experiment can now be conducted using the methods of InstrumentCoordinator:

  1. We prepare the instruments with the appropriate settings and upload the schedule program by calling the prepare() method and passing the compiled schedule as an argument.

  2. We start the hardware execution by calling the start() method.

Additionally, the wait_done() method is useful to wait for the experiment to finish and assure the synchronicity of the python script.

# Set the qcodes parameters and upload the schedule program
ic.prepare(compiled_sched)

# Start the hardware execution
ic.start()

# Wait for the experiment to finish or for a timeout
ic.wait_done(timeout_sec=10)

The InstrumentCoordinator has two more methods that were not covered in this experiment:

  • retrieve_acquisition() - In case the schedule contained acquisitions, this method retrieves the acquired data.

  • stop() - Stops all running instruments.

Note that the schedule used in this tutorial was defined purely in terms of pulses. However, quantify-scheduler also supports the usage of quantum gates in schedules. Given that gates may require different pulses depending on the type of quantum system, an extra step of defining the quantum device configuration, i.e. the qubits, is necessary. This use case is covered in the Tutorial: Operations and Qubits tutorial.