def test_bell_schedule(self): """Test complex schedule to create a Bell state.""" with builder.build() as test_sched: with builder.align_sequential(): # H builder.shift_phase(-1.57, DriveChannel(0)) builder.play(Drag(160, 0.05, 40, 1.3), DriveChannel(0)) builder.shift_phase(-1.57, DriveChannel(0)) # ECR with builder.align_left(): builder.play(GaussianSquare(800, 0.05, 64, 544), DriveChannel(1)) builder.play(GaussianSquare(800, 0.1 - 0.2j, 64, 544), ControlChannel(0)) builder.play(Drag(160, 0.1, 40, 1.5), DriveChannel(0)) with builder.align_left(): builder.play(GaussianSquare(800, -0.05, 64, 544), DriveChannel(1)) builder.play(GaussianSquare(800, -0.1 + 0.2j, 64, 544), ControlChannel(0)) builder.play(Drag(160, 0.1, 40, 1.5), DriveChannel(0)) # Measure with builder.align_left(): builder.play(GaussianSquare(8000, 0.2, 64, 7744), MeasureChannel(0)) builder.acquire(8000, AcquireChannel(0), MemorySlot(0)) self.assert_roundtrip_equal(test_sched)
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) with self.assertRaises(PulseError): GaussianSquare(duration=150, amp=0.2, sigma=8, width=100, risefall_sigma_ratio=5) with self.assertRaises(PulseError): GaussianSquare(duration=150, amp=0.2, sigma=8, width=160) with self.assertRaises(PulseError): GaussianSquare(duration=150, amp=0.2, sigma=8, risefall_sigma_ratio=10) with self.assertRaises(PulseError): Constant(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 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) GaussianSquare(duration=150, amp=0.2, sigma=8, risefall_sigma_ratio=2.5) Constant(duration=150, amp=0.1 + 0.4j) Drag(duration=25, amp=0.2 + 0.3j, sigma=7.8, beta=4)
def test_gaussian_square_pulse(self): """Test that GaussianSquare sample pulse matches the pulse library.""" gauss_sq = GaussianSquare(duration=125, sigma=4, amp=0.5j, width=100) sample_pulse = gauss_sq.get_waveform() self.assertIsInstance(sample_pulse, Waveform) pulse_lib_gauss_sq = gaussian_square(duration=125, sigma=4, amp=0.5j, width=100, zero_ends=True).samples np.testing.assert_almost_equal(sample_pulse.samples, pulse_lib_gauss_sq)
def test_gaussian_square_limit_amplitude_per_instance(self): """Test that the check for amplitude per instance.""" with self.assertRaises(PulseError): GaussianSquare(duration=100, sigma=1.0, amp=1.1 + 0.8j, width=10) waveform = GaussianSquare(duration=100, sigma=1.0, amp=1.1 + 0.8j, width=10, limit_amplitude=False) self.assertGreater(np.abs(waveform.amp), 1.0)
def test_gaussian_square_limit_amplitude(self): """Test that the check for amplitude less than or equal to 1 can be disabled.""" with self.assertRaises(PulseError): GaussianSquare(duration=100, sigma=1.0, amp=1.1 + 0.8j, width=10) with patch("qiskit.pulse.library.pulse.Pulse.limit_amplitude", new=False): waveform = GaussianSquare(duration=100, sigma=1.0, amp=1.1 + 0.8j, width=10) self.assertGreater(np.abs(waveform.amp), 1.0)
def test_gauss_square_samples(self): """Test that the gaussian square samples match the formula.""" duration = 125 sigma = 4 amp = 0.5j # formulaic times = np.array(range(25), dtype=np.complex_) times = times - (25 / 2) + 0.5 gauss = amp * np.exp(-(times / sigma)**2 / 2) # wf wf = GaussianSquare(duration=duration, sigma=sigma, amp=amp, width=100) samples = wf.get_waveform().samples np.testing.assert_almost_equal(samples[50], amp) np.testing.assert_almost_equal(samples[100], amp) np.testing.assert_almost_equal(samples[:10], gauss[:10])
def test_repr(self): """Test the repr methods for parametric pulses.""" gaus = Gaussian(duration=25, amp=0.7, sigma=4) self.assertEqual(repr(gaus), "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)" ) gaus_square = GaussianSquare(duration=20, sigma=30, amp=1.0, risefall_sigma_ratio=0.1) self.assertEqual( repr(gaus_square), "GaussianSquare(duration=20, amp=(1+0j), sigma=30, width=14.0)" ) 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 = Constant(duration=150, amp=0.1 + 0.4j) self.assertEqual(repr(const), "Constant(duration=150, amp=(0.1+0.4j))")
def test_hash_generation(self): """Test if pulse generate unique hash.""" test_hash = [hash(GaussianSquare(duration=688, amp=0.1+0.1j, sigma=64, width=432)) for _ in range(10)] ref_hash = [test_hash[0] for _ in range(10)] self.assertListEqual(test_hash, ref_hash)
def test_gaussian_square_pulse_instruction(self): """Test that parametric pulses are correctly converted to PulseQobjInstructions.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) instruction = Play(GaussianSquare(duration=1500, sigma=15, amp=-0.5 + 0.2j, width=1300), MeasureChannel(1)) valid_qobj = PulseQobjInstruction( name='parametric_pulse', pulse_shape='gaussian_square', ch='m1', t0=10, parameters={'duration': 1500, 'sigma': 15, 'amp': -0.5 + 0.2j, 'width': 1300}) self.assertEqual(converter(10, instruction), valid_qobj)
def test_gauss_square_extremes(self): """Test that the gaussian square pulse can build a gaussian.""" duration = 125 sigma = 4 amp = 0.5j gaus_square = GaussianSquare(duration=duration, sigma=sigma, amp=amp, width=0) gaus = Gaussian(duration=duration, sigma=sigma, amp=amp) np.testing.assert_almost_equal(gaus_square.get_waveform().samples, gaus.get_waveform().samples) gaus_square = GaussianSquare(duration=duration, sigma=sigma, amp=amp, width=121) const = Constant(duration=duration, amp=amp) np.testing.assert_almost_equal(gaus_square.get_waveform().samples[2:-2], const.get_waveform().samples[2:-2])
def test_gaussian_square_pulse_instruction(self): """Test that parametric pulses are correctly converted to PulseQobjInstructions.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) instruction = Play( GaussianSquare(duration=1500, sigma=15, amp=-0.5 + 0.2j, width=1300), MeasureChannel(1)) valid_qobj = PulseQobjInstruction( name="parametric_pulse", pulse_shape="gaussian_square", ch="m1", t0=10, parameters={ "duration": 1500, "sigma": 15, "amp": -0.5 + 0.2j, "width": 1300 }, ) self.assertEqual(converter(10, instruction), valid_qobj)
def test_gauss_square_passes_validation_after_construction(self): """Test that parameter validation is consistent before and after construction. This previously used to raise an exception: see gh-7882.""" pulse = GaussianSquare(duration=125, sigma=4, amp=0.5j, width=100) pulse.validate_parameters()