def test_instructions_hash(self): """Test hashing for acquire instruction.""" kernel_opts = { 'start_window': 0, 'stop_window': 10 } kernel = configuration.Kernel(name='boxcar', **kernel_opts) discriminator_opts = { 'neighborhoods': [{'qubits': 1, 'channels': 1}], 'cal': 'coloring', 'resample': False } discriminator = configuration.Discriminator( name='linear_discriminator', **discriminator_opts) acq_1 = instructions.Acquire(10, channels.AcquireChannel(0), channels.MemorySlot(0), kernel=kernel, discriminator=discriminator, name='acquire') acq_2 = instructions.Acquire(10, channels.AcquireChannel(0), channels.MemorySlot(0), kernel=kernel, discriminator=discriminator, name='acquire') hash_1 = hash(acq_1) hash_2 = hash(acq_2) self.assertEqual(hash_1, hash_2)
def test_can_construct_valid_acquire_command(self): """Test if valid acquire command can be constructed.""" kernel_opts = {"start_window": 0, "stop_window": 10} kernel = configuration.Kernel(name="boxcar", **kernel_opts) discriminator_opts = { "neighborhoods": [{ "qubits": 1, "channels": 1 }], "cal": "coloring", "resample": False, } discriminator = configuration.Discriminator( name="linear_discriminator", **discriminator_opts) acq = instructions.Acquire( 10, channels.AcquireChannel(0), channels.MemorySlot(0), kernel=kernel, discriminator=discriminator, name="acquire", ) self.assertEqual(acq.duration, 10) self.assertEqual(acq.discriminator.name, "linear_discriminator") self.assertEqual(acq.discriminator.params, discriminator_opts) self.assertEqual(acq.kernel.name, "boxcar") self.assertEqual(acq.kernel.params, kernel_opts) self.assertIsInstance(acq.id, int) self.assertEqual(acq.name, "acquire") self.assertEqual( acq.operands, (10, channels.AcquireChannel(0), channels.MemorySlot(0), None))
def test_can_construct_valid_acquire_command(self): """Test if valid acquire command can be constructed.""" kernel_opts = { 'start_window': 0, 'stop_window': 10 } kernel = configuration.Kernel(name='boxcar', **kernel_opts) discriminator_opts = { 'neighborhoods': [{'qubits': 1, 'channels': 1}], 'cal': 'coloring', 'resample': False } discriminator = configuration.Discriminator(name='linear_discriminator', **discriminator_opts) acq = instructions.Acquire(10, channels.AcquireChannel(0), channels.MemorySlot(0), kernel=kernel, discriminator=discriminator, name='acquire') self.assertEqual(acq.duration, 10) self.assertEqual(acq.discriminator.name, 'linear_discriminator') self.assertEqual(acq.discriminator.params, discriminator_opts) self.assertEqual(acq.kernel.name, 'boxcar') self.assertEqual(acq.kernel.params, kernel_opts) self.assertIsInstance(acq.id, int) self.assertEqual(acq.name, 'acquire') self.assertEqual(acq.operands, (10, channels.AcquireChannel(0), channels.MemorySlot(0), None))
def convert_acquire(self, instruction): """Return converted `Acquire`. Args: instruction (PulseQobjInstruction): acquire qobj Returns: Schedule: Converted and scheduled Instruction """ t0 = instruction.t0 duration = instruction.duration qubits = instruction.qubits acquire_channels = [channels.AcquireChannel(qubit) for qubit in qubits] mem_slots = [channels.MemorySlot(instruction.memory_slot[i]) for i in range(len(qubits))] if hasattr(instruction, 'register_slot'): register_slots = [channels.RegisterSlot(instruction.register_slot[i]) for i in range(len(qubits))] else: register_slots = [None] * len(qubits) discriminators = (instruction.discriminators if hasattr(instruction, 'discriminators') else None) if not isinstance(discriminators, list): discriminators = [discriminators] if any(discriminators[i] != discriminators[0] for i in range(len(discriminators))): warnings.warn("Can currently only support one discriminator per acquire. Defaulting " "to first discriminator entry.") discriminator = discriminators[0] if discriminator: discriminator = Discriminator(name=discriminators[0].name, **discriminators[0].params) kernels = (instruction.kernels if hasattr(instruction, 'kernels') else None) if not isinstance(kernels, list): kernels = [kernels] if any(kernels[0] != kernels[i] for i in range(len(kernels))): warnings.warn("Can currently only support one kernel per acquire. Defaulting to first " "kernel entry.") kernel = kernels[0] if kernel: kernel = Kernel(name=kernels[0].name, **kernels[0].params) schedule = Schedule() for acquire_channel, mem_slot, reg_slot in zip(acquire_channels, mem_slots, register_slots): schedule |= instructions.Acquire(duration, acquire_channel, mem_slot=mem_slot, reg_slot=reg_slot, kernel=kernel, discriminator=discriminator) << t0 return schedule
def add_implicit_acquires(schedule: ScheduleComponent, meas_map: List[List[int]]) -> Schedule: """Return a new schedule with implicit acquires from the measurement mapping replaced by explicit ones. .. warning:: Since new acquires are being added, Memory Slots will be set to match the qubit index. This may overwrite your specification. Args: schedule: Schedule to be aligned. meas_map: List of lists of qubits that are measured together. Returns: A ``Schedule`` with the additional acquisition instructions. """ new_schedule = Schedule.initialize_from(schedule) acquire_map = {} for time, inst in schedule.instructions: if isinstance(inst, instructions.Acquire): if inst.mem_slot and inst.mem_slot.index != inst.channel.index: warnings.warn( "One of your acquires was mapped to a memory slot which didn't match" " the qubit index. I'm relabeling them to match.") # Get the label of all qubits that are measured with the qubit(s) in this instruction all_qubits = [] for sublist in meas_map: if inst.channel.index in sublist: all_qubits.extend(sublist) # Replace the old acquire instruction by a new one explicitly acquiring all qubits in # the measurement group. for i in all_qubits: explicit_inst = instructions.Acquire( inst.duration, chans.AcquireChannel(i), mem_slot=chans.MemorySlot(i), kernel=inst.kernel, discriminator=inst.discriminator, ) if time not in acquire_map: new_schedule.insert(time, explicit_inst, inplace=True) acquire_map = {time: {i}} elif i not in acquire_map[time]: new_schedule.insert(time, explicit_inst, inplace=True) acquire_map[time].add(i) else: new_schedule.insert(time, inst, inplace=True) return new_schedule
def test_instructions_hash(self): """Test hashing for acquire instruction.""" kernel_opts = {"start_window": 0, "stop_window": 10} kernel = configuration.Kernel(name="boxcar", **kernel_opts) discriminator_opts = { "neighborhoods": [{ "qubits": 1, "channels": 1 }], "cal": "coloring", "resample": False, } discriminator = configuration.Discriminator( name="linear_discriminator", **discriminator_opts) acq_1 = instructions.Acquire( 10, channels.AcquireChannel(0), channels.MemorySlot(0), kernel=kernel, discriminator=discriminator, name="acquire", ) acq_2 = instructions.Acquire( 10, channels.AcquireChannel(0), channels.MemorySlot(0), kernel=kernel, discriminator=discriminator, name="acquire", ) hash_1 = hash(acq_1) hash_2 = hash(acq_2) self.assertEqual(hash_1, hash_2)
def measure(qubits: List[int], backend=None, inst_map: Optional[InstructionScheduleMap] = None, meas_map: Optional[Union[List[List[int]], Dict[int, List[int]]]] = None, qubit_mem_slots: Optional[Dict[int, int]] = None, measure_name: str = 'measure') -> Schedule: """Return a schedule which measures the requested qubits according to the given instruction mapping and measure map, or by using the defaults provided by the backend. By default, the measurement results for each qubit are trivially mapped to the qubit index. This behavior is overridden by qubit_mem_slots. For instance, to measure qubit 0 into MemorySlot(1), qubit_mem_slots can be provided as {0: 1}. Args: qubits: List of qubits to be measured. backend (Union[Backend, BaseBackend]): A backend instance, which contains hardware-specific data required for scheduling. inst_map: Mapping of circuit operations to pulse schedules. If None, defaults to the ``instruction_schedule_map`` of ``backend``. meas_map: List of sets of qubits that must be measured together. If None, defaults to the ``meas_map`` of ``backend``. qubit_mem_slots: Mapping of measured qubit index to classical bit index. measure_name: Name of the measurement schedule. Returns: A measurement schedule corresponding to the inputs provided. Raises: PulseError: If both ``inst_map`` or ``meas_map``, and ``backend`` is None. """ schedule = Schedule( name="Default measurement schedule for qubits {}".format(qubits)) try: inst_map = inst_map or backend.defaults().instruction_schedule_map meas_map = meas_map or backend.configuration().meas_map except AttributeError: raise exceptions.PulseError( 'inst_map or meas_map, and backend cannot be None simultaneously') if isinstance(meas_map, list): meas_map = utils.format_meas_map(meas_map) measure_groups = set() for qubit in qubits: measure_groups.add(tuple(meas_map[qubit])) for measure_group_qubits in measure_groups: if qubit_mem_slots is not None: unused_mem_slots = set(measure_group_qubits) - set( qubit_mem_slots.values()) try: default_sched = inst_map.get(measure_name, measure_group_qubits) except exceptions.PulseError: raise exceptions.PulseError( "We could not find a default measurement schedule called '{}'. " "Please provide another name using the 'measure_name' keyword " "argument. For assistance, the instructions which are defined are: " "{}".format(measure_name, inst_map.instructions)) for time, inst in default_sched.instructions: if qubit_mem_slots and isinstance(inst, instructions.Acquire): if inst.channel.index in qubit_mem_slots: mem_slot = channels.MemorySlot( qubit_mem_slots[inst.channel.index]) else: mem_slot = channels.MemorySlot(unused_mem_slots.pop()) schedule = schedule.insert( time, instructions.Acquire(inst.duration, inst.channel, mem_slot=mem_slot)) elif qubit_mem_slots is None and isinstance( inst, instructions.Acquire): schedule = schedule.insert(time, inst) # Measurement pulses should only be added if its qubit was measured by the user elif inst.channels[0].index in qubits: schedule = schedule.insert(time, inst) return schedule