Beispiel #1
0
def _qobj_to_circuit_cals(qobj, pulse_lib, param_pulses):
    """Return circuit calibrations dictionary from qobj/exp config calibrations."""
    qobj_cals = qobj.config.calibrations.to_dict()["gates"]
    converter = QobjToInstructionConverter(pulse_lib)

    qc_cals = {}
    for gate in qobj_cals:
        config = (tuple(gate["qubits"]), tuple(gate["params"]))
        cal = {
            config: pulse.Schedule(
                name="%s %s %s" % (gate["name"], str(gate["params"]), str(gate["qubits"]))
            )
        }
        for instruction in gate["instructions"]:
            schedule = (
                converter.convert_parametric(PulseQobjInstruction.from_dict(instruction))
                if "pulse_shape" in instruction and instruction["pulse_shape"] in param_pulses
                else converter(PulseQobjInstruction.from_dict(instruction))
            )
            cal[config] = cal[config].insert(schedule.ch_start_time(), schedule)
        if gate["name"] in qc_cals:
            qc_cals[gate["name"]].update(cal)
        else:
            qc_cals[gate["name"]] = cal

    return qc_cals
Beispiel #2
0
    def setUp(self):
        self.linear = SamplePulse(np.arange(0, 0.01), name='linear')
        self.pulse_library = [PulseLibraryItem(name=self.linear.name,
                                               samples=self.linear.samples.tolist())]

        self.converter = QobjToInstructionConverter(self.pulse_library, buffer=0)
        self.num_qubits = 2
Beispiel #3
0
    def test_parameterized_schedule(self):
        """Test building parameterized schedule."""
        cmd_def = CmdDef()
        converter = QobjToInstructionConverter([])
        qobj = PulseQobjInstruction(name='pv',
                                    ch='u1',
                                    t0=10,
                                    val='P2*cos(np.pi*P1)')
        converted_instruction = converter(qobj)

        cmd_def.add('pv_test', 0, converted_instruction)
        self.assertEqual(cmd_def.get_parameters('pv_test', 0), ('P1', 'P2'))

        sched = cmd_def.get('pv_test', 0, 0, P2=-1)
        self.assertEqual(sched.instructions[0][-1].command.value, -1)

        with self.assertRaises(PulseError):
            cmd_def.get('pv_test', 0, 0, P1=-1)

        with self.assertRaises(PulseError):
            cmd_def.get('pv_test', 0, P1=1, P2=2, P3=3)

        sched = cmd_def.pop('pv_test', 0, 0, P2=-1)
        self.assertEqual(sched.instructions[0][-1].command.value, -1)

        self.assertFalse(cmd_def.has('pv_test', 0))
    def __init__(self, qubit_freq_est: List[float], meas_freq_est: List[float],
                 buffer: int, pulse_library: List[PulseLibraryItem],
                 cmd_def: List[Command], **kwargs: Dict[str, Any]):
        """
        Validate and reformat transport layer inputs to initialize.

        Args:
            qubit_freq_est: Estimated qubit frequencies in GHz.
            meas_freq_est: Estimated measurement cavity frequencies in GHz.
            buffer: Default buffer time (in units of dt) between pulses.
            pulse_library: Pulse name and sample definitions.
            cmd_def: Operation name and definition in terms of Commands.
            **kwargs: Other attributes for the super class.
        """
        super().__init__(**kwargs)

        self.buffer = buffer
        self.qubit_freq_est = [freq * 1e9 for freq in qubit_freq_est]
        """Qubit frequencies in Hertz."""
        self.meas_freq_est = [freq * 1e9 for freq in meas_freq_est]
        """Measurement frequencies in Hertz."""
        self.pulse_library = pulse_library
        self.cmd_def = cmd_def
        self.instruction_schedule_map = InstructionScheduleMap()

        self.converter = QobjToInstructionConverter(pulse_library)
        for inst in cmd_def:
            pulse_insts = [self.converter(inst) for inst in inst.sequence]
            schedule = ParameterizedSchedule(*pulse_insts, name=inst.name)
            self.instruction_schedule_map.add(inst.name, inst.qubits, schedule)
Beispiel #5
0
    def from_defaults(cls,
                      flat_cmd_def: List[PulseQobjInstruction],
                      pulse_library: Dict[str, SamplePulse],
                      buffer: int = 0) -> 'CmdDef':
        """Create command definition from backend defaults output.

        Args:
            flat_cmd_def: Command definition list returned by backend
            pulse_library: Dictionary of `SamplePulse`s
            buffer: Buffer between instructions on channel
        """
        if buffer:
            warnings.warn(
                "Buffers are no longer supported. Please use an explicit Delay."
            )
        converter = QobjToInstructionConverter(pulse_library)
        cmd_def = cls()

        for cmd in flat_cmd_def:
            qubits = cmd.qubits
            name = cmd.name
            instructions = []
            for instr in cmd.sequence:
                instructions.append(converter(instr))

            cmd_def.add(name, qubits,
                        ParameterizedSchedule(*instructions, name=name))

        return cmd_def
Beispiel #6
0
    def test_sequenced_parameterized_schedule(self):
        """Test parametrized schedule consist of multiple instruction. """
        cmd_def = CmdDef()
        converter = QobjToInstructionConverter([])
        qobjs = [PulseQobjInstruction(name='fc', ch='d0', t0=10, phase='P1'),
                 PulseQobjInstruction(name='fc', ch='d0', t0=20, phase='P2'),
                 PulseQobjInstruction(name='fc', ch='d0', t0=30, phase='P3')]
        converted_instruction = [converter(qobj) for qobj in qobjs]

        cmd_def.add('inst_seq', 0, ParameterizedSchedule(*converted_instruction, name='inst_seq'))

        with self.assertRaises(PulseError):
            cmd_def.get('inst_seq', 0, P1=1, P2=2, P3=3, P4=4, P5=5)

        with self.assertRaises(PulseError):
            cmd_def.get('inst_seq', 0, P1=1)

        with self.assertRaises(PulseError):
            cmd_def.get('inst_seq', 0, 1, 2, 3, P1=1)

        sched = cmd_def.get('inst_seq', 0, 1, 2, 3)
        self.assertEqual(sched.instructions[0][-1].command.phase, 1)
        self.assertEqual(sched.instructions[1][-1].command.phase, 2)
        self.assertEqual(sched.instructions[2][-1].command.phase, 3)

        sched = cmd_def.get('inst_seq', 0, P1=1, P2=2, P3=3)
        self.assertEqual(sched.instructions[0][-1].command.phase, 1)
        self.assertEqual(sched.instructions[1][-1].command.phase, 2)
        self.assertEqual(sched.instructions[2][-1].command.phase, 3)

        sched = cmd_def.get('inst_seq', 0, 1, 2, P3=3)
        self.assertEqual(sched.instructions[0][-1].command.phase, 1)
        self.assertEqual(sched.instructions[1][-1].command.phase, 2)
        self.assertEqual(sched.instructions[2][-1].command.phase, 3)
Beispiel #7
0
def _experiments_to_schedules(qobj) -> List[pulse.Schedule]:
    """Return a list of :class:`qiskit.pulse.Schedule` object(s) from a qobj.

    Args:
        qobj (Qobj): The Qobj object to convert to pulse schedules.

    Returns:
        A list of :class:`qiskit.pulse.Schedule` objects from the qobj

    Raises:
        pulse.PulseError: If a parameterized instruction is supplied.
    """
    converter = QobjToInstructionConverter(qobj.config.pulse_library)

    schedules = []
    for program in qobj.experiments:
        insts = []
        for inst in program.instructions:
            pulse_inst = converter(inst)
            if isinstance(pulse_inst, ParameterizedSchedule):
                raise pulse.PulseError('Parameterized instructions are not '
                                       'yet supported in the pulse schedule.')
            insts.append(pulse_inst)

        schedule = pulse.Schedule(*insts)
        schedules.append(schedule)
    return schedules
Beispiel #8
0
    def setUp(self):
        self.linear = SamplePulse(np.arange(0, 0.01), name='linear')
        self.pulse_library = [PulseLibraryItem(name=self.linear.name,
                                               samples=self.linear.samples.tolist())]

        self.converter = QobjToInstructionConverter(self.pulse_library, buffer=0)

        self.device = PulseChannelSpec(n_qubits=2, n_control=0, n_registers=2)
    def test_parameterized_schedule(self):
        """Test building parameterized schedule."""
        cmd_def = CmdDef()
        converter = QobjToInstructionConverter([], buffer=0)
        qobj = PulseQobjInstruction(name='pv', ch='u1', t0=10, val='P2*cos(np.pi*P1)')
        converted_instruction = converter(qobj)

        cmd_def.add('pv_test', 0, converted_instruction)
        self.assertEqual(cmd_def.get_parameters('pv_test', 0), ('P1', 'P2'))

        sched = cmd_def.get('pv_test', 0, P1='0', P2=-1)
        self.assertEqual(sched.instructions[0][-1].command.value, -1)
Beispiel #10
0
    def test_negative_phases(self):
        """Test bind parameters with negative values."""
        cmd_def = CmdDef()
        converter = QobjToInstructionConverter([])
        qobjs = [PulseQobjInstruction(name='fc', ch='d0', t0=10, phase='P1'),
                 PulseQobjInstruction(name='fc', ch='d0', t0=20, phase='-(P2)')]
        converted_instruction = [converter(qobj) for qobj in qobjs]

        cmd_def.add('inst_seq', 0, ParameterizedSchedule(*converted_instruction, name='inst_seq'))

        sched = cmd_def.get('inst_seq', 0, -1, 2)

        self.assertEqual(sched.instructions[0][-1].command.phase, -1)
        self.assertEqual(sched.instructions[1][-1].command.phase, -2)
    def setUp(self):
        self.linear = SamplePulse(np.arange(0, 0.01), name='linear')
        self.pulse_library = [
            PulseLibraryItem(name=self.linear.name,
                             samples=self.linear.samples.tolist())
        ]

        self.converter = QobjToInstructionConverter(self.pulse_library,
                                                    buffer=0)

        self.device = DeviceSpecification(qubits=[
            Qubit(0, DriveChannel(0), MeasureChannel(0), AcquireChannel(0))
        ],
                                          registers=[RegisterSlot(0)],
                                          mem_slots=[MemorySlot(0)])
Beispiel #12
0
    def __init__(
        self,
        qubit_freq_est: List[float],
        meas_freq_est: List[float],
        buffer: int,
        pulse_library: List[PulseLibraryItem],
        cmd_def: List[Command],
        meas_kernel: MeasurementKernel = None,
        discriminator: Discriminator = None,
        **kwargs: Dict[str, Any],
    ):
        """
        Validate and reformat transport layer inputs to initialize.
        Args:
            qubit_freq_est: Estimated qubit frequencies in GHz.
            meas_freq_est: Estimated measurement cavity frequencies in GHz.
            buffer: Default buffer time (in units of dt) between pulses.
            pulse_library: Pulse name and sample definitions.
            cmd_def: Operation name and definition in terms of Commands.
            meas_kernel: The measurement kernels
            discriminator: The discriminators
            **kwargs: Other attributes for the super class.
        """
        self._data = {}
        self.buffer = buffer
        self.qubit_freq_est = [freq * 1e9 for freq in qubit_freq_est]
        """Qubit frequencies in Hertz."""
        self.meas_freq_est = [freq * 1e9 for freq in meas_freq_est]
        """Measurement frequencies in Hertz."""
        self.pulse_library = pulse_library
        self.cmd_def = cmd_def
        self.instruction_schedule_map = InstructionScheduleMap()

        self.converter = QobjToInstructionConverter(pulse_library)
        for inst in cmd_def:
            pulse_insts = [self.converter(inst) for inst in inst.sequence]
            schedule = Schedule(*pulse_insts, name=inst.name)
            schedule.metadata[
                "publisher"] = CalibrationPublisher.BACKEND_PROVIDER
            self.instruction_schedule_map.add(inst.name, inst.qubits, schedule)

        if meas_kernel is not None:
            self.meas_kernel = meas_kernel
        if discriminator is not None:
            self.discriminator = discriminator

        self._data.update(kwargs)
Beispiel #13
0
    def test_parameterized_schedule(self):
        """Test adding parameterized schedule."""
        converter = QobjToInstructionConverter([], buffer=0)
        qobj = PulseQobjInstruction(name='pv', ch='u1', t0=10, val='P2*cos(np.pi*P1)')
        converted_instruction = converter(qobj)

        inst_map = InstructionScheduleMap()

        inst_map.add('pv_test', 0, converted_instruction)
        self.assertEqual(inst_map.get_parameters('pv_test', 0), ('P1', 'P2'))

        sched = inst_map.get('pv_test', 0, P1=0, P2=-1)
        self.assertEqual(sched.instructions[0][-1].command.value, -1)
        with self.assertRaises(PulseError):
            inst_map.get('pv_test', 0, 0, P1=-1)
        with self.assertRaises(PulseError):
            inst_map.get('pv_test', 0, P1=1, P2=2, P3=3)
Beispiel #14
0
    def test_sequenced_parameterized_schedule(self):
        """Test parametrized schedule consists of multiple instruction. """

        converter = QobjToInstructionConverter([], buffer=0)
        qobjs = [
            PulseQobjInstruction(name='fc', ch='d0', t0=10, phase='P1'),
            PulseQobjInstruction(name='fc', ch='d0', t0=20, phase='P2'),
            PulseQobjInstruction(name='fc', ch='d0', t0=30, phase='P3')
        ]
        converted_instruction = [converter(qobj) for qobj in qobjs]

        inst_map = InstructionScheduleMap()

        inst_map.add(
            'inst_seq', 0,
            ParameterizedSchedule(*converted_instruction, name='inst_seq'))

        with self.assertRaises(PulseError):
            inst_map.get('inst_seq', 0, P1=1, P2=2, P3=3, P4=4, P5=5)

        with self.assertRaises(PulseError):
            inst_map.get('inst_seq', 0, P1=1)

        with self.assertRaises(PulseError):
            inst_map.get('inst_seq', 0, 1, 2, 3, P1=1)

        p3_expr = Parameter('p3')
        p3_expr = p3_expr.bind({p3_expr: 3})

        sched = inst_map.get('inst_seq', 0, 1, 2, p3_expr)
        self.assertEqual(sched.instructions[0][-1].phase, 1)
        self.assertEqual(sched.instructions[1][-1].phase, 2)
        self.assertEqual(sched.instructions[2][-1].phase, 3)

        sched = inst_map.get('inst_seq', 0, P1=1, P2=2, P3=p3_expr)
        self.assertEqual(sched.instructions[0][-1].phase, 1)
        self.assertEqual(sched.instructions[1][-1].phase, 2)
        self.assertEqual(sched.instructions[2][-1].phase, 3)

        sched = inst_map.get('inst_seq', 0, 1, 2, P3=p3_expr)
        self.assertEqual(sched.instructions[0][-1].phase, 1)
        self.assertEqual(sched.instructions[1][-1].phase, 2)
        self.assertEqual(sched.instructions[2][-1].phase, 3)
Beispiel #15
0
    def from_defaults(cls, flat_cmd_def: List[PulseQobjInstruction],
                      pulse_library: Dict[str, SamplePulse]) -> 'CmdDef':
        """Create command definition from backend defaults output.
        Args:
            flat_cmd_def: Command definition list returned by backend
            pulse_library: Dictionary of `SamplePulse`s
        """
        converter = QobjToInstructionConverter(pulse_library, buffer=0)
        cmd_def = cls()

        for cmd in flat_cmd_def:
            qubits = cmd.qubits
            name = cmd.name
            instructions = []
            for instr in cmd.sequence:
                instructions.append(converter(instr))

            cmd_def.add(name, qubits, ParameterizedSchedule(*instructions, name=name))

        return cmd_def
    def test_sequenced_parameterized_schedule(self):
        """Test parameterized schedule consists of multiple instruction."""

        converter = QobjToInstructionConverter([], buffer=0)
        qobjs = [
            PulseQobjInstruction(name="fc", ch="d0", t0=10, phase="P1"),
            PulseQobjInstruction(name="fc", ch="d0", t0=20, phase="P2"),
            PulseQobjInstruction(name="fc", ch="d0", t0=30, phase="P3"),
        ]
        converted_instruction = [converter(qobj) for qobj in qobjs]

        inst_map = InstructionScheduleMap()

        inst_map.add("inst_seq", 0,
                     Schedule(*converted_instruction, name="inst_seq"))

        with self.assertRaises(PulseError):
            inst_map.get("inst_seq", 0, P1=1, P2=2, P3=3, P4=4, P5=5)

        with self.assertRaises(PulseError):
            inst_map.get("inst_seq", 0, 1, 2, 3, 4, 5, 6, 7, 8)

        p3_expr = Parameter("p3")
        p3_expr = p3_expr.bind({p3_expr: 3})

        sched = inst_map.get("inst_seq", 0, 1, 2, p3_expr)
        self.assertEqual(sched.instructions[0][-1].phase, 1)
        self.assertEqual(sched.instructions[1][-1].phase, 2)
        self.assertEqual(sched.instructions[2][-1].phase, 3)

        sched = inst_map.get("inst_seq", 0, P1=1, P2=2, P3=p3_expr)
        self.assertEqual(sched.instructions[0][-1].phase, 1)
        self.assertEqual(sched.instructions[1][-1].phase, 2)
        self.assertEqual(sched.instructions[2][-1].phase, 3)

        sched = inst_map.get("inst_seq", 0, 1, 2, P3=p3_expr)
        self.assertEqual(sched.instructions[0][-1].phase, 1)
        self.assertEqual(sched.instructions[1][-1].phase, 2)
        self.assertEqual(sched.instructions[2][-1].phase, 3)