Example #1
0
    def test_from_dict_per_class(self):
        """Test Qobj and its subclass representations given a dictionary."""
        test_parameters = {
            PulseQobj: (self.valid_qobj, self.valid_dict),
            PulseQobjConfig: (
                PulseQobjConfig(meas_level=1,
                                memory_slot_size=8192,
                                meas_return='avg',
                                pulse_library=[
                                    QobjPulseLibrary(name='pulse0',
                                                     samples=[0.1 + 0.0j])
                                ],
                                qubit_lo_freq=[4.9],
                                meas_lo_freq=[6.9],
                                rep_time=1000),
                {
                    'meas_level': 1,
                    'memory_slot_size': 8192,
                    'meas_return': 'avg',
                    'pulse_library': [{
                        'name': 'pulse0',
                        'samples': [[0.1, 0.0]]
                    }],
                    'qubit_lo_freq': [4.9],
                    'meas_lo_freq': [6.9],
                    'rep_time': 1000
                },
            ),
            QobjPulseLibrary: (QobjPulseLibrary(name='pulse0',
                                                samples=[0.1 + 0.0j]), {
                                                    'name': 'pulse0',
                                                    'samples': [[0.1, 0.0]]
                                                }),
            PulseQobjExperiment: (PulseQobjExperiment(instructions=[
                PulseQobjInstruction(name='pulse0', t0=0, ch='d0')
            ]), {
                'instructions': [{
                    'name': 'pulse0',
                    't0': 0,
                    'ch': 'd0'
                }]
            }),
            PulseQobjInstruction: (PulseQobjInstruction(name='pulse0',
                                                        t0=0,
                                                        ch='d0'), {
                                                            'name': 'pulse0',
                                                            't0': 0,
                                                            'ch': 'd0'
                                                        })
        }

        for qobj_class, (qobj_item, expected_dict) in test_parameters.items():
            with self.subTest(msg=str(qobj_class)):
                self.assertEqual(qobj_item,
                                 qobj_class.from_dict(expected_dict))
def assemble_schedules(schedules,
                       qobj_id=None,
                       qobj_header=None,
                       run_config=None):
    """Assembles a list of schedules into a qobj which can be run on the backend.
    Args:
        schedules (list[Schedule]): schedules to assemble
        qobj_id (int): identifier for the generated qobj
        qobj_header (QobjHeader): header to pass to the results
        run_config (RunConfig): configuration of the runtime environment
    Returns:
        PulseQobj: the Qobj to be run on the backends
    Raises:
        QiskitError: when invalid schedules or configs are provided
    """
    qobj_config = QasmQobjConfig()
    if run_config:
        qobj_config = QasmQobjConfig(**run_config.to_dict())

    # Get appropriate convertors
    instruction_converter = PulseQobjConverter
    instruction_converter = instruction_converter(PulseQobjInstruction,
                                                  **run_config.to_dict())
    lo_converter = LoConfigConverter(PulseQobjExperimentConfig,
                                     run_config.qubit_lo_freq,
                                     run_config.meas_lo_freq,
                                     **run_config.to_dict())

    # Pack everything into the Qobj
    qobj_schedules = []
    user_pulselib = set()
    for idx, schedule in enumerate(schedules):
        # instructions
        qobj_instructions = []
        # Instructions are returned as tuple of shifted time and instruction
        for shift, instruction in list(schedule.flatten()):
            # TODO: support conditional gate
            qobj_instructions.append(instruction_converter(shift, instruction))
            if isinstance(instruction, PulseInstruction):
                # add samples to pulse library
                user_pulselib.add(instruction.command)
        # experiment header
        qobj_experiment_header = QobjExperimentHeader(
            name=schedule.name or 'Experiment-%d' % idx)

        qobj_schedules.append({
            'header': qobj_experiment_header,
            'instructions': qobj_instructions
        })

    # setup pulse_library
    run_config.pulse_library = [
        QobjPulseLibrary(name=pulse.name, samples=pulse.samples)
        for pulse in user_pulselib
    ]

    # create qob experiment field
    experiments = []
    if len(run_config.schedule_los) == 1:
        lo_dict = run_config.schedule_los.pop()
        # update global config
        q_los = lo_converter.get_qubit_los(lo_dict)
        if q_los:
            run_config.qubit_lo_freq = q_los
        m_los = lo_converter.get_meas_los(lo_dict)
        if m_los:
            run_config.meas_lo_freq = m_los

    if run_config.schedule_los:
        # multiple frequency setups
        if len(qobj_schedules) == 1:
            # frequency sweep
            for lo_dict in run_config.schedule_los:
                experiments.append(
                    PulseQobjExperiment(
                        instructions=qobj_schedules[0]['instructions'],
                        experimentheader=qobj_schedules[0]['header'],
                        experimentconfig=lo_converter(lo_dict)))
        elif len(qobj_schedules) == len(run_config.schedule_los):
            # n:n setup
            for lo_dict, schedule in zip(run_config.schedule_los,
                                         qobj_schedules):
                experiments.append(
                    PulseQobjExperiment(
                        instructions=schedule['instructions'],
                        experimentheader=schedule['header'],
                        experimentconfig=lo_converter(lo_dict)))
        else:
            raise QiskitError(
                'Invalid LO setting is specified. '
                'The LO should be configured for each schedule, or '
                'single setup for all schedules (unique), or '
                'multiple setups for a single schedule (frequency sweep),'
                'or no LO configured at all.')
    else:
        # unique frequency setup
        for schedule in qobj_schedules:
            experiments.append(
                PulseQobjExperiment(
                    instructions=schedule['instructions'],
                    experimentheader=schedule['header'],
                ))

    qobj_config = PulseQobjConfig(**run_config.to_dict())

    return PulseQobj(qobj_id=qobj_id,
                     config=qobj_config,
                     experiments=experiments,
                     header=qobj_header)
Example #3
0
 def setUp(self):
     self.valid_qobj = PulseQobj(
         qobj_id='12345',
         header=QobjHeader(),
         config=PulseQobjConfig(shots=1024,
                                memory_slots=2,
                                max_credits=10,
                                meas_level=1,
                                memory_slot_size=8192,
                                meas_return='avg',
                                pulse_library=[
                                    QobjPulseLibrary(name='pulse0',
                                                     samples=[
                                                         0.0 + 0.0j,
                                                         0.5 + 0.0j,
                                                         0.0 + 0.0j
                                                     ])
                                ],
                                qubit_lo_freq=[4.9],
                                meas_lo_freq=[6.9],
                                rep_time=1000),
         experiments=[
             PulseQobjExperiment(instructions=[
                 PulseQobjInstruction(name='pulse0', t0=0, ch='d0'),
                 PulseQobjInstruction(name='fc', t0=5, ch='d0', phase=1.57),
                 PulseQobjInstruction(
                     name='pv', t0=10, ch='d0', val=0.1 + 0.0j),
                 PulseQobjInstruction(name='acquire',
                                      t0=15,
                                      duration=5,
                                      qubits=[0],
                                      memory_slot=[0],
                                      kernels=[
                                          QobjMeasurementOption(
                                              name='boxcar',
                                              params={
                                                  "start_window": 0,
                                                  "stop_window": 5
                                              })
                                      ])
             ])
         ])
     self.valid_dict = {
         'qobj_id':
         '12345',
         'type':
         'PULSE',
         'schema_version':
         '1.1.0',
         'header': {},
         'config': {
             'max_credits':
             10,
             'memory_slots':
             2,
             'shots':
             1024,
             'meas_level':
             1,
             'memory_slot_size':
             8192,
             'meas_return':
             'avg',
             'pulse_library': [{
                 'name':
                 'pulse0',
                 'samples': [[0.0, 0.0], [0.5, 0.0], [0.0, 0.0]]
             }],
             'qubit_lo_freq': [4.9],
             'meas_lo_freq': [6.9],
             'rep_time':
             1000
         },
         'experiments': [{
             'instructions': [{
                 'name': 'pulse0',
                 't0': 0,
                 'ch': 'd0'
             }, {
                 'name': 'fc',
                 't0': 5,
                 'ch': 'd0',
                 'phase': 1.57
             }, {
                 'name': 'pv',
                 't0': 10,
                 'ch': 'd0',
                 'val': [0.1, 0.0]
             }, {
                 'name':
                 'acquire',
                 't0':
                 15,
                 'duration':
                 5,
                 'qubits': [0],
                 'memory_slot': [0],
                 'kernels': [{
                     'name': 'boxcar',
                     'params': {
                         'start_window': 0,
                         'stop_window': 5
                     }
                 }]
             }]
         }]
     }