def test_update_from_instruction_schedule_map_add_instruction(self): target = Target() inst_map = InstructionScheduleMap() inst_map.add("sx", 0, self.custom_sx_q0) inst_map.add("sx", 1, self.custom_sx_q1) target.update_from_instruction_schedule_map(inst_map, {"sx": SXGate()}) self.assertEqual(inst_map, target.instruction_schedule_map())
def test_update_from_instruction_schedule_map_new_qarg_raises(self): inst_map = InstructionScheduleMap() inst_map.add("sx", 0, self.custom_sx_q0) inst_map.add("sx", 1, self.custom_sx_q1) inst_map.add("sx", 2, self.custom_sx_q1) with self.assertRaises(KeyError): self.pulse_target.update_from_instruction_schedule_map(inst_map)
def test_update_from_instruction_schedule_map_new_instruction_no_name_map(self): target = Target() inst_map = InstructionScheduleMap() inst_map.add("sx", 0, self.custom_sx_q0) inst_map.add("sx", 1, self.custom_sx_q1) with self.assertRaises(ValueError): target.update_from_instruction_schedule_map(inst_map)
class PulseDefaults(BaseModel): """Description of default settings for Pulse systems. These are instructions or settings that may be good starting points for the Pulse user. The user may modify these defaults for custom scheduling. """ 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] self.meas_freq_est = [freq * 1e9 for freq in meas_freq_est] self.pulse_library = pulse_library self.cmd_def = cmd_def self.circuit_instruction_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.circuit_instruction_map.add(inst.name, inst.qubits, schedule) def __str__(self): qubit_freqs = [freq / 1e9 for freq in self.qubit_freq_est] meas_freqs = [freq / 1e9 for freq in self.meas_freq_est] qfreq = "Qubit Frequencies [GHz]\n{freqs}".format(freqs=qubit_freqs) mfreq = "Measurement Frequencies [GHz]\n{freqs} ".format( freqs=meas_freqs) return ("<{name}({insts}{qfreq}\n{mfreq})>" "".format(name=self.__class__.__name__, insts=str(self.circuit_instruction_map), qfreq=qfreq, mfreq=mfreq)) def build_cmd_def(self) -> InstructionScheduleMap: """ Return the InstructionScheduleMap built for this PulseDefaults instance. Returns: InstructionScheduleMap: Generated from defaults. """ warnings.warn( "This method is deprecated. Returning a InstructionScheduleMap instead. " "This can be accessed simply through the `circuit_instruction_map` attribute " "of this PulseDefaults instance.", DeprecationWarning) return self.circuit_instruction_map
def test_update_from_instruction_schedule_map_with_dt_set(self): inst_map = InstructionScheduleMap() with pulse.build(name="sx_q1") as custom_sx: pulse.play(pulse.Constant(1000, 0.2), pulse.DriveChannel(1)) inst_map.add("sx", 0, self.custom_sx_q0) inst_map.add("sx", 1, custom_sx) self.pulse_target.dt = 1.0 self.pulse_target.update_from_instruction_schedule_map(inst_map, {"sx": SXGate()}) self.assertEqual(inst_map, self.pulse_target.instruction_schedule_map()) self.assertEqual(self.pulse_target["sx"][(1,)].duration, 1000.0) self.assertIsNone(self.pulse_target["sx"][(1,)].error) self.assertIsNone(self.pulse_target["sx"][(0,)].error)
def instruction_schedule_map(self): """Return an :class:`~qiskit.pulse.InstructionScheduleMap` for the instructions in the target with a pulse schedule defined. Returns: InstructionScheduleMap: The instruction schedule map for the instructions in this target with a pulse schedule defined. """ if self._instruction_schedule_map is not None: return self._instruction_schedule_map out_inst_schedule_map = InstructionScheduleMap() for instruction, qargs in self._gate_map.items(): for qarg, properties in qargs.items(): if properties is not None and properties.calibration is not None: out_inst_schedule_map.add(instruction, qarg, properties.calibration) self._instruction_schedule_map = out_inst_schedule_map return out_inst_schedule_map
class PulseDefaults(SimpleNamespace): """Description of default settings for Pulse systems. These are instructions or settings that may be good starting points for the Pulse user. The user may modify these defaults for custom scheduling. """ 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.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) if meas_kernel is not None: self.meas_kernel = meas_kernel if discriminator is not None: self.discriminator = discriminator self.__dict__.update(kwargs) def to_dict(self): """Return a dictionary format representation of the PulseDefaults. Returns: dict: The dictionary form of the PulseDefaults. """ out_dict = { 'qubit_freq_est': self.qubit_freq_est, 'meas_freq_est': self.qubit_freq_est, 'buffer': self.buffer, 'pulse_library': [x.to_dict() for x in self.pulse_library], 'cmd_def': [x.to_dict() for x in self.cmd_def], } if hasattr(self, 'meas_kernel'): out_dict['meas_kernel'] = self.meas_kernel.to_dict() if hasattr(self, 'discriminator'): out_dict['discriminator'] = self.discriminator.to_dict() for key, value in self.__dict__.items(): if key not in [ 'qubit_freq_est', 'meas_freq_est', 'buffer', 'pulse_library', 'cmd_def', 'meas_kernel', 'discriminator', 'converter', 'instruction_schedule_map' ]: out_dict[key] = value return out_dict @classmethod def from_dict(cls, data): """Create a new PulseDefaults object from a dictionary. Args: data (dict): A dictionary representing the PulseDefaults to create. It will be in the same format as output by :meth:`to_dict`. Returns: PulseDefaults: The PulseDefaults from the input dictionary. """ in_data = copy.copy(data) in_data['pulse_library'] = [ PulseLibraryItem.from_dict(x) for x in in_data.pop('pulse_library') ] in_data['cmd_def'] = [ Command.from_dict(x) for x in in_data.pop('cmd_def') ] if 'meas_kernel' in in_data: in_data['meas_kernel'] = MeasurementKernel.from_dict( in_data.pop('meas_kernel')) if 'discriminator' in in_data: in_data['discriminator'] = Discriminator.from_dict( in_data.pop('discriminator')) return cls(**in_data) def __getstate__(self): return self.to_dict() def __setstate__(self, state): return self.from_dict(state) def __reduce__(self): return (self.__class__, (self.qubit_freq_est, self.meas_freq_est, self.buffer, self.pulse_library, self.cmd_def)) def __str__(self): qubit_freqs = [freq / 1e9 for freq in self.qubit_freq_est] meas_freqs = [freq / 1e9 for freq in self.meas_freq_est] qfreq = "Qubit Frequencies [GHz]\n{freqs}".format(freqs=qubit_freqs) mfreq = "Measurement Frequencies [GHz]\n{freqs} ".format( freqs=meas_freqs) return ("<{name}({insts}{qfreq}\n{mfreq})>" "".format(name=self.__class__.__name__, insts=str(self.instruction_schedule_map), qfreq=qfreq, mfreq=mfreq))
class PulseDefaults: """Description of default settings for Pulse systems. These are instructions or settings that may be good starting points for the Pulse user. The user may modify these defaults for custom scheduling. """ _data = {} 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) 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) def __getattr__(self, name): try: return self._data[name] except KeyError as ex: raise AttributeError(f"Attribute {name} is not defined") from ex def to_dict(self): """Return a dictionary format representation of the PulseDefaults. Returns: dict: The dictionary form of the PulseDefaults. """ out_dict = { "qubit_freq_est": self.qubit_freq_est, "meas_freq_est": self.qubit_freq_est, "buffer": self.buffer, "pulse_library": [x.to_dict() for x in self.pulse_library], "cmd_def": [x.to_dict() for x in self.cmd_def], } if hasattr(self, "meas_kernel"): out_dict["meas_kernel"] = self.meas_kernel.to_dict() if hasattr(self, "discriminator"): out_dict["discriminator"] = self.discriminator.to_dict() for key, value in self.__dict__.items(): if key not in [ "qubit_freq_est", "meas_freq_est", "buffer", "pulse_library", "cmd_def", "meas_kernel", "discriminator", "converter", "instruction_schedule_map", ]: out_dict[key] = value out_dict.update(self._data) out_dict["qubit_freq_est"] = [freq * 1e-9 for freq in self.qubit_freq_est] out_dict["meas_freq_est"] = [freq * 1e-9 for freq in self.meas_freq_est] return out_dict @classmethod def from_dict(cls, data): """Create a new PulseDefaults object from a dictionary. Args: data (dict): A dictionary representing the PulseDefaults to create. It will be in the same format as output by :meth:`to_dict`. Returns: PulseDefaults: The PulseDefaults from the input dictionary. """ in_data = copy.copy(data) in_data["pulse_library"] = [ PulseLibraryItem.from_dict(x) for x in in_data.pop("pulse_library") ] in_data["cmd_def"] = [Command.from_dict(x) for x in in_data.pop("cmd_def")] if "meas_kernel" in in_data: in_data["meas_kernel"] = MeasurementKernel.from_dict(in_data.pop("meas_kernel")) if "discriminator" in in_data: in_data["discriminator"] = Discriminator.from_dict(in_data.pop("discriminator")) return cls(**in_data) def __str__(self): qubit_freqs = [freq / 1e9 for freq in self.qubit_freq_est] meas_freqs = [freq / 1e9 for freq in self.meas_freq_est] qfreq = f"Qubit Frequencies [GHz]\n{qubit_freqs}" mfreq = f"Measurement Frequencies [GHz]\n{meas_freqs} " return "<{name}({insts}{qfreq}\n{mfreq})>".format( name=self.__class__.__name__, insts=str(self.instruction_schedule_map), qfreq=qfreq, mfreq=mfreq, )