def stretcher(circ, machine, factor): ''' Input: A circ (without measurement) which is to be converted to a schedule, machine to be transpiled upon, and the factor to be stretched by Output: The stretched schedule with measurement ''' num_qubits = circ.num_qubits circ_device = transpile(circ, machine) sched = schedule(circ_device, machine) instrucs = sched.instructions big_array = instruc_sorter(instrucs) stretch_sched = None j = 0 for element in big_array: sub_sched = stretch_sub_sched(element, factor) #first element if (j == 0): stretch_sched = sub_sched #print(stretch_sched.instructions) j = 1 else: stretch_sched = stretch_sched.insert(stretch_sched.duration, sub_sched) #adding measurement qubits = [i for i in range(num_qubits)] stretch_sched += measure( qubits, machine) << stretch_sched.duration #([0], backend) return stretch_sched
def test_measure_with_custom_inst_map(self): """Test measure with custom inst_map, meas_map with measure_name.""" q0_sched = Play(GaussianSquare(1200, 1, 0.4, 1150), MeasureChannel(0)) q0_sched += Acquire(1200, AcquireChannel(0), MemorySlot(0)) inst_map = InstructionScheduleMap() inst_map.add('my_sched', 0, q0_sched) sched = measure(qubits=[0], measure_name='my_sched', inst_map=inst_map, meas_map=[[0]]) self.assertEqual(sched.instructions, q0_sched.instructions) with self.assertRaises(PulseError): measure(qubits=[0], measure_name="name", inst_map=inst_map, meas_map=[[0]])
def test_measure(self): """Test utility function - measure.""" sched = measure(qubits=[0], backend=self.backend) expected = Schedule( self.inst_map.get('measure', [0, 1]).filter(channels=[MeasureChannel(0)]), Acquire(10, AcquireChannel(0), MemorySlot(0)), Acquire(10, AcquireChannel(1), MemorySlot(1))) self.assertEqual(sched.instructions, expected.instructions)
def rabi_schedules(amp_list, qubits, pulse_width, pulse_sigma=None, width_sigma_ratio=4, drives=None, inst_map=None, meas_map=None): """ Generates schedules for a rabi experiment using a Gaussian pulse Args: amp_list (list of floats): List of amplitudes for the Gaussian pulse [-1,1] qubits (list of integers): indices of the qubits to perform a rabi pulse_width: width of gaussian (in dt units) pulse_sigma: sigma of gaussian width_sigma_ratio: set sigma to a certain ratio of the width (use if pulse_sigma is None) drives: list of DriveChannel objects inst_map: InstructionScheduleMap object to use meas_map: meas_map to use Returns: A list of QuantumSchedules xdata: a list of amps Raises: QiskitError: when necessary variables are not supplied. """ xdata = amp_list # copy the instruction to schedule mapping inst_map = copy.deepcopy(inst_map) # Following variables should not be optional. # To keep function interface constant, errors are inserted here. # TODO: redesign this function in next release if inst_map is None: QiskitError('Instruction schedule map is not provided. ', 'Run `backend.defaults().instruction_schedule_map` to get inst_map.') if meas_map is None: QiskitError('Measurement map is not provided. ', 'Run `backend.configuration().meas_map` to get meas_map.') if pulse_sigma is None: pulse_sigma = pulse_width / width_sigma_ratio # Construct the schedules rabi_scheds = [] for index, g_amp in enumerate(amp_list): rabi_pulse = pulse_lib.gaussian(duration=pulse_width, amp=g_amp, sigma=pulse_sigma, name='rabi_pulse_%d' % index) sched = pulse.Schedule(name='rabisched_%d_0' % index) for qubit in qubits: sched += pulse.Play(rabi_pulse, drives[qubit]) sched += measure(qubits, inst_map=inst_map, meas_map=meas_map).shift(pulse_width) rabi_scheds.append(sched) return rabi_scheds, xdata
def test_measure_sched_with_meas_map(self): """Test measure with custom meas_map as list and dict.""" sched_with_meas_map_list = measure(qubits=[0], backend=self.backend, meas_map=[[0, 1]]) sched_with_meas_map_dict = measure(qubits=[0], backend=self.backend, meas_map={ 0: [0, 1], 1: [0, 1] }) expected = Schedule( self.inst_map.get('measure', [0, 1]).filter(channels=[MeasureChannel(0)]), Acquire(10, AcquireChannel(0), MemorySlot(0)), Acquire(10, AcquireChannel(1), MemorySlot(1))) self.assertEqual(sched_with_meas_map_list.instructions, expected.instructions) self.assertEqual(sched_with_meas_map_dict.instructions, expected.instructions)
def test_measure_sched_with_qubit_mem_slots(self): """Test measure with custom qubit_mem_slots.""" sched = measure(qubits=[0], backend=self.backend, qubit_mem_slots={0: 1}) expected = Schedule( self.inst_map.get('measure', [0, 1]).filter(channels=[MeasureChannel(0)]), Acquire(10, AcquireChannel(0), MemorySlot(1)), Acquire(10, AcquireChannel(1), MemorySlot(0))) self.assertEqual(sched.instructions, expected.instructions)
def test_pulse_name_conflicts_in_other_schedule(self): """Test two pulses with the same name in different schedule can be resolved.""" backend = FakeAlmaden() schedules = [] ch_d0 = pulse.DriveChannel(0) for amp in (0.1, 0.2): sched = Schedule() sched += gaussian(duration=100, amp=amp, sigma=30, name='my_pulse')(ch_d0) sched += measure(qubits=[0], backend=backend) << 100 schedules.append(sched) qobj = assemble(schedules, backend) # two user pulses and one measurement pulse should be contained self.assertEqual(len(qobj.config.pulse_library), 3)
def drag_schedules(beta_list, qubits, pulse_amp, pulse_width, pulse_sigma=None, width_sigma_ratio=4, drives=None, inst_map=None, meas_map=None): """ Generates schedules for a drag experiment doing a pulse then the - pulse Args: beta_list (list of floats): List of relative amplitudes for the derivative pulse qubits (list of integers): indices of the qubits to perform a rabi pulse_amp (list): amp of the gaussian (list of length qubits) pulse_width (float): width of gaussian (in dt units) pulse_sigma (float): sigma of gaussian width_sigma_ratio (int): set sigma to a certain ratio of the width (use if pulse_sigma is None) drives (list): list of :class:`~qiskit.pulse.DriveChannel` objects inst_map (InstructionScheduleMap): InstructionScheduleMap object to use meas_map (list): meas_map to use Returns: A list of QuantumSchedules xdata: a list of amps Raises: QiskitError: when necessary variables are not supplied. """ xdata = beta_list # copy the instruction to schedule mapping inst_map = copy.deepcopy(inst_map) # Following variables should not be optional. # To keep function interface constant, errors are inserted here. # TODO: redesign this function in next release if inst_map is None: QiskitError( 'Instruction schedule map is not provided. ', 'Run `backend.defaults().instruction_schedule_map` to get inst_map.' ) if meas_map is None: QiskitError('Measurement map is not provided. ', 'Run `backend.configuration().meas_map` to get meas_map.') if pulse_sigma is None: pulse_sigma = pulse_width / width_sigma_ratio # Construct the schedules drag_scheds = [] for index, b_amp in enumerate(beta_list): sched = pulse.Schedule(name='dragsched_%d_0' % index) for qind, qubit in enumerate(qubits): drag_pulse_p = pulse_lib.drag(duration=pulse_width, amp=pulse_amp[qind], beta=b_amp, sigma=pulse_sigma, name='drag_pulse_%d_%d' % (index, qubit)) drag_pulse_m = pulse_lib.drag(duration=pulse_width, amp=-pulse_amp[qind], beta=b_amp, sigma=pulse_sigma, name='drag_pulse_%d_%d' % (index, qubit)) sched += pulse.Play(drag_pulse_p, drives[qubit]) sched += pulse.Play(drag_pulse_m, drives[qubit]) sched += measure(qubits, inst_map=inst_map, meas_map=meas_map).shift(pulse_width) drag_scheds.append(sched) return drag_scheds, xdata
def test_fail_measure(self): """Test failing measure.""" with self.assertRaises(PulseError): measure(qubits=[0], meas_map=self.backend.configuration().meas_map) with self.assertRaises(PulseError): measure(qubits=[0], inst_map=self.inst_map)