def test_pulse_to_signals(self): """Generic test.""" sched = Schedule(name="Schedule") sched += Play(Drag(duration=20, amp=0.5, sigma=4, beta=0.5), DriveChannel(0)) sched += ShiftPhase(1.0, DriveChannel(0)) sched += Play(Drag(duration=20, amp=0.5, sigma=4, beta=0.5), DriveChannel(0)) sched += ShiftFrequency(0.5, DriveChannel(0)) sched += Play( GaussianSquare(duration=200, amp=0.3, sigma=4, width=150), DriveChannel(0)) test_gaussian = GaussianSquare(duration=200, amp=0.3, sigma=4, width=150) sched = sched.insert(0, Play(test_gaussian, DriveChannel(1))) converter = InstructionToSignals(dt=1, carriers=None) signals = converter.get_signals(sched) self.assertEqual(len(signals), 2) self.assertTrue(isinstance(signals[0], PiecewiseConstant)) self.assertTrue(isinstance(signals[0], PiecewiseConstant)) samples = test_gaussian.get_waveform().samples self.assertTrue( np.allclose(signals[1].samples[0:len(samples)], samples))
def test_parametric_pulses_with_no_duplicates(self): """Test parametric pulses with no duplicates.""" schedule = Schedule() drive_channel = DriveChannel(0) schedule += Play(Gaussian(duration=25, sigma=4, amp=0.5j), drive_channel) schedule += Play(Gaussian(duration=25, sigma=4, amp=0.49j), drive_channel) schedule += Play( GaussianSquare(duration=150, amp=0.2, sigma=8, width=140), drive_channel) schedule += Play( GaussianSquare(duration=150, amp=0.19, sigma=8, width=140), drive_channel) schedule += Play(Constant(duration=150, amp=0.1 + 0.4j), drive_channel) schedule += Play(Constant(duration=150, amp=0.1 + 0.41j), drive_channel) schedule += Play(Drag(duration=25, amp=0.2 + 0.3j, sigma=7.8, beta=4), drive_channel) schedule += Play(Drag(duration=25, amp=0.2 + 0.31j, sigma=7.8, beta=4), drive_channel) compressed_schedule = transforms.compress_pulses([schedule]) original_pulse_ids = get_pulse_ids([schedule]) compressed_pulse_ids = get_pulse_ids(compressed_schedule) self.assertEqual(len(original_pulse_ids), len(compressed_pulse_ids))
def rescale_amp(instruction: Play, theta: float) -> Union[Play, None]: """ Rescale the amplitude of a sample pulse. The samples are scaled linearly so that theta = np.pi/2 has no effect. Args: instruction: The instruction from which to create a new scaled instruction. theta: The angle that controls the scaling. """ scale = theta / (np.pi / 2.) if isinstance(instruction.pulse, Drag): drag = instruction.pulse return Play( Drag(duration=drag.duration, amp=drag.amp * scale, sigma=drag.sigma, beta=drag.beta), instruction.channel) if isinstance(instruction.pulse, Gaussian): gaus = instruction.pulse return Play( Drag(duration=gaus.duration, amp=gaus.amp * scale, sigma=gaus.sigma, beta=gaus.beta), instruction.channel)
def test_param_validation(self): """Test that parametric pulse parameters are validated when initialized.""" with self.assertRaises(PulseError): Gaussian(duration=25, sigma=0, amp=0.5j) with self.assertRaises(PulseError): GaussianSquare(duration=150, amp=0.2, sigma=8, width=160) with self.assertRaises(PulseError): ConstantPulse(duration=150, amp=0.9 + 0.8j) with self.assertRaises(PulseError): Drag(duration=25, amp=0.2 + 0.3j, sigma=-7.8, beta=4) with self.assertRaises(PulseError): Drag(duration=25, amp=0.2 + 0.3j, sigma=7.8, beta=4j)
def setUp(self): """Setup some schedules.""" super().setUp() with pulse.build(name="xp") as xp: pulse.play(Drag(duration=160, amp=0.208519, sigma=40, beta=0.0), DriveChannel(0)) with pulse.build(name="x90p") as x90p: pulse.play(Drag(duration=160, amp=0.208519, sigma=40, beta=0.0), DriveChannel(0)) self.x_plus = xp self.x_90_plus = x90p
def test_drag_samples(self): """Test that the drag samples match the formula.""" duration = 25 sigma = 4 amp = 0.5j beta = 1 # formulaic times = np.array(range(25), dtype=np.complex_) times = times - (25 / 2) + 0.5 gauss = amp * np.exp(-(times / sigma)**2 / 2) gauss_deriv = -(times / sigma**2) * gauss drag = gauss + 1j * beta * gauss_deriv # command command = Drag(duration=duration, sigma=sigma, amp=amp, beta=beta) samples = command.get_sample_pulse().samples np.testing.assert_almost_equal(samples, drag)
def test_parameters(self): """Test that the parameters can be extracted as a dict through the `parameters` attribute.""" drag = Drag(duration=25, amp=0.2 + 0.3j, sigma=7.8, beta=4) self.assertEqual(set(drag.parameters.keys()), {'duration', 'amp', 'sigma', 'beta'}) const = ConstantPulse(duration=150, amp=1) self.assertEqual(set(const.parameters.keys()), {'duration', 'amp'})
def setUp(self): """Setup some schedules.""" super().setUp() beta = Parameter("β") with pulse.build(name="xp") as xp: pulse.play(Drag(duration=160, amp=0.208519, sigma=40, beta=beta), DriveChannel(0)) self.x_plus = xp self.test_tol = 0.05
def test_raise_multiple_parameter(self): """Check that the experiment raises with unassigned parameters.""" beta = Parameter("β") amp = Parameter("amp") with pulse.build(name="xp") as xp: pulse.play(Drag(duration=160, amp=amp, sigma=40, beta=beta), DriveChannel(0)) with self.assertRaises(QiskitError): RoughDrag(1, xp, betas=np.linspace(-3, 3, 21))
def test_parametric_commands_in_sched(self): """Test that schedules can be built with parametric commands.""" sched = Schedule(name='test_parametric') sched += Play(Gaussian(duration=25, sigma=4, amp=0.5j), DriveChannel(0)) sched += Play(Drag(duration=25, amp=0.2+0.3j, sigma=7.8, beta=4), DriveChannel(1)) sched += Play(Constant(duration=25, amp=1), DriveChannel(2)) sched_duration = sched.duration sched += Play(GaussianSquare(duration=1500, amp=0.2, sigma=8, width=140), MeasureChannel(0)) << sched_duration self.assertEqual(sched.duration, 1525) self.assertTrue('sigma' in sched.instructions[0][1].pulse.parameters)
def stretch_sub_sched(sim_pulse_array, factor): ''' Input: A set of pulses happening at the same time (sim = simultaneous) and factor to be stretched by Output: A schedule consisting of the stretched pulses ''' sub_sched = qiskit.pulse.Schedule() for instruc in sim_pulse_array: #anything except shift phase if (isinstance(instruc, Play)): if (isinstance(instruc.pulse, Drag)): drag = instruc.pulse ## param = {"duration": self.duration, "amp": self.amp, "sigma": self.sigma, "width": self.width} param = drag.parameters duration = int(factor * param['duration']) sigma = (factor * param['sigma']) #stretching the drag pulse s_pulse = Drag(duration, param['amp'] / factor, sigma, param['beta']) channel = instruc.channels[0] sub_sched = sub_sched.append(pulse.Play(s_pulse, channel)) elif (isinstance(instruc.pulse, GaussianSquare)): gauss = instruc.pulse ## param = {"duration": self.duration, "amp": self.amp, "sigma": self.sigma, "width": self.width} param = gauss.parameters #print('------Old Param') #print(param) #stretching the drag pulse duration = get_closest_multiple_of_16(factor * param['duration']) sigma = (factor * param['sigma']) width = get_closest_multiple_of_16(factor * param['width']) s_pulse = GaussianSquare(duration, param['amp'] / factor, sigma, width) #print('------new Param') #print('Duration') #print(str(duration) + ' '+ str(param['amp']/factor) + ' '+ str(sigma) + ' '+ str(width)) #print(param) channel = instruc.channels[0] sub_sched = sub_sched.append(pulse.Play(s_pulse, channel)) #if not acquire elif (not isinstance(instruc, Acquire)): sub_sched += instruc else: sub_sched += instruc return sub_sched
def test_repr(self): """Test the repr methods for parametric pulses.""" gaussian = Gaussian(duration=25, amp=0.7, sigma=4) self.assertEqual(repr(gaussian), 'Gaussian(duration=25, amp=(0.7+0j), sigma=4)') gaus_square = GaussianSquare(duration=20, sigma=30, amp=1.0, width=3) self.assertEqual( repr(gaus_square), 'GaussianSquare(duration=20, amp=(1+0j), sigma=30, width=3)') drag = Drag(duration=5, amp=0.5, sigma=7, beta=1) self.assertEqual(repr(drag), 'Drag(duration=5, amp=(0.5+0j), sigma=7, beta=1)') const = ConstantPulse(duration=150, amp=0.1 + 0.4j) self.assertEqual(repr(const), 'ConstantPulse(duration=150, amp=(0.1+0.4j))')
def test_drag_validation(self): """Test drag parameter validation, specifically the beta validation.""" duration = 25 sigma = 4 amp = 0.5j beta = 1 command = Drag(duration=duration, sigma=sigma, amp=amp, beta=beta) samples = command.get_sample_pulse().samples self.assertTrue(max(np.abs(samples)) <= 1) beta = sigma**2 with self.assertRaises(PulseError): command = Drag(duration=duration, sigma=sigma, amp=amp, beta=beta) # If sigma is high enough, side peaks fall out of range and norm restriction is met sigma = 100 command = Drag(duration=duration, sigma=sigma, amp=amp, beta=beta)
def test_raise_multiple_parameter(self): """Check that the experiment raises with unassigned parameters.""" beta = Parameter("β") amp = Parameter("amp") with pulse.build(name="xp") as xp: pulse.play(Drag(duration=160, amp=amp, sigma=40, beta=beta), DriveChannel(0)) backend = DragBackend(error=0.05, gate_name="xp") drag = DragCal(1) drag.set_experiment_options(betas=np.linspace(-3, 3, 21)) drag.set_experiment_options(schedule=xp) with self.assertRaises(CalibrationError): drag.run(backend).analysis_results(0)
def stretch_sub_sched(sim_pulse_array, factor): ''' Input: A set of pulses happening at the same time (sim = simultaneous) and factor to be stretched by Output: A schedule consisting of the stretched pulses ''' sub_sched = qiskit.pulse.Schedule() for instruc in sim_pulse_array: #anything except shift phase if (isinstance(instruc, Play)): if (isinstance(instruc.pulse, Drag)): drag = instruc.pulse ## param = {"duration": self.duration, "amp": self.amp, "sigma": self.sigma, "width": self.width} param = drag.parameters #stretching the drag pulse s_pulse = Drag(int(factor * param['duration']), param['amp'] / factor, factor * param['sigma'], param['beta']) channel = instruc.channels[0] sub_sched = sub_sched.append(pulse.Play(s_pulse, channel)) # elif (isinstance(instruc.pulse, GaussianSquare)): # gauss = instruc.pulse # ## param = {"duration": self.duration, "amp": self.amp, "sigma": self.sigma, "width": self.width} # param = gauss.parameters # #stretching the drag pulse # s_pulse = GaussianSquare(int(factor*param['duration']), param['amp']/factor, factor*param['sigma'],factor*param['width']) # channel = instruc.channels[0] # sub_sched = sub_sched.append(pulse.Play(s_pulse, channel)) #if not acquire elif (not isinstance(instruc, Acquire)): sub_sched += instruc else: sub_sched += instruc return sub_sched
def test_construction(self): """Test that parametric pulses can be constructed without error.""" Gaussian(duration=25, sigma=4, amp=0.5j) GaussianSquare(duration=150, amp=0.2, sigma=8, width=140) ConstantPulse(duration=150, amp=0.1 + 0.4j) Drag(duration=25, amp=0.2 + 0.3j, sigma=7.8, beta=4)
def check_drag(duration, sigma, amp, beta): command = Drag(duration=duration, sigma=sigma, amp=amp, beta=beta) samples = command.get_sample_pulse().samples self.assertTrue(max(np.abs(samples)) <= 1)