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_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_drag_limit_amplitude(self): """Test that the check for amplitude less than or equal to 1 can be disabled.""" with self.assertRaises(PulseError): Drag(duration=100, sigma=1.0, beta=1.0, amp=1.1 + 0.8j) with patch("qiskit.pulse.library.pulse.Pulse.limit_amplitude", new=False): waveform = Drag(duration=100, sigma=1.0, beta=1.0, amp=1.1 + 0.8j) self.assertGreater(np.abs(waveform.amp), 1.0)
def test_drag_limit_amplitude_per_instance(self): """Test that the check for amplitude per instance.""" with self.assertRaises(PulseError): Drag(duration=100, sigma=1.0, beta=1.0, amp=1.1 + 0.8j) waveform = Drag(duration=100, sigma=1.0, beta=1.0, amp=1.1 + 0.8j, limit_amplitude=False) self.assertGreater(np.abs(waveform.amp), 1.0)
def test_drag_validation(self): """Test drag parameter validation, specifically the beta validation.""" duration = 25 sigma = 4 amp = 0.5j beta = 1 wf = Drag(duration=duration, sigma=sigma, amp=amp, beta=beta) samples = wf.get_waveform().samples self.assertTrue(max(np.abs(samples)) <= 1) beta = sigma ** 2 with self.assertRaises(PulseError): wf = 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 wf = Drag(duration=duration, sigma=sigma, amp=amp, beta=beta)
def test_drag_limit_amplitude(self): """Test that the check for amplitude less than or equal to 1 can be disabled.""" waveform = Drag(duration=100, sigma=1.0, beta=1.0, amp=1.1 + 0.8j, limit_amplitude=False) self.assertGreater(np.abs(waveform.amp), 1.0) with self.assertRaises(PulseError): Drag(duration=100, sigma=1.0, beta=1.0, amp=1.1 + 0.8j, limit_amplitude=True)
def test_drag_pulse(self): """Test that the Drag sample pulse matches the pulse library.""" drag = Drag(duration=25, sigma=4, amp=0.5j, beta=1) sample_pulse = drag.get_waveform() self.assertIsInstance(sample_pulse, Waveform) pulse_lib_drag = pl_drag(duration=25, sigma=4, amp=0.5j, beta=1, zero_ends=True).samples np.testing.assert_almost_equal(sample_pulse.samples, pulse_lib_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 = Constant(duration=150, amp=1) self.assertEqual(set(const.parameters.keys()), {'duration', 'amp'})
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_get_parameters(self): """Test getting pulse parameters as attribute.""" drag_pulse = Drag(duration=100, amp=0.1, sigma=40, beta=3) self.assertEqual(drag_pulse.duration, 100) self.assertEqual(drag_pulse.amp, 0.1) self.assertEqual(drag_pulse.sigma, 40) self.assertEqual(drag_pulse.beta, 3) with self.assertRaises(AttributeError): # pylint: disable=pointless-statement drag_pulse.non_existing_parameter
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)') 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_fully_parametrized_pulse(self): """Test instantiating a pulse with parameters.""" amp = Parameter("amp") duration = Parameter("duration") sigma = Parameter("sigma") beta = Parameter("beta") # doesn't raise an error drag = Drag(duration=duration, amp=amp, sigma=sigma, beta=beta) with self.assertRaises(PulseError): drag.get_waveform()
def test_drag_pulse_instruction(self): """Test that parametric pulses are correctly converted to PulseQobjInstructions.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) instruction = Play(Drag(duration=25, sigma=15, amp=-0.5 + 0.2j, beta=0.5), DriveChannel(0)) valid_qobj = PulseQobjInstruction( name='parametric_pulse', pulse_shape='drag', ch='d0', t0=30, parameters={'duration': 25, 'sigma': 15, 'amp': -0.5 + 0.2j, 'beta': 0.5}) self.assertEqual(converter(30, instruction), valid_qobj)
def test_deepcopy(self): """Test deep copying instance.""" import copy drag = Drag(duration=100, amp=0.1, sigma=40, beta=3) drag_copied = copy.deepcopy(drag) self.assertNotEqual(id(drag), id(drag_copied)) orig_wf = drag.get_waveform() copied_wf = drag_copied.get_waveform() np.testing.assert_almost_equal(orig_wf.samples, copied_wf.samples)
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 # wf wf = Drag(duration=duration, sigma=sigma, amp=amp, beta=beta) samples = wf.get_waveform().samples np.testing.assert_almost_equal(samples, drag)
def test_drag_pulse_instruction(self): """Test that parametric pulses are correctly converted to PulseQobjInstructions.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) instruction = Play( Drag(duration=25, sigma=15, amp=-0.5 + 0.2j, beta=0.5), DriveChannel(0)) valid_qobj = PulseQobjInstruction( name="parametric_pulse", pulse_shape="drag", ch="d0", t0=30, parameters={ "duration": 25, "sigma": 15, "amp": -0.5 + 0.2j, "beta": 0.5 }, ) self.assertEqual(converter(30, instruction), valid_qobj)
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)
def test_constraints_cache(self): """Test speed up of instantiation with lambdify constraints cache.""" drag_instance1 = Drag(duration=100, amp=0.1, sigma=40, beta=3) drag_instance2 = Drag(duration=100, amp=0.1, sigma=40, beta=3) self.assertTrue( drag_instance1._constraints_lam is drag_instance2._constraints_lam)
def check_drag(duration, sigma, amp, beta): wf = Drag(duration=duration, sigma=sigma, amp=amp, beta=beta) samples = wf.get_waveform().samples self.assertTrue(max(np.abs(samples)) <= 1)