def test_constant_limit_amplitude_per_instance(self): """Test that the check for amplitude per instance.""" with self.assertRaises(PulseError): Constant(duration=100, amp=1.1 + 0.8j) waveform = Constant(duration=100, amp=1.1 + 0.8j, limit_amplitude=False) self.assertGreater(np.abs(waveform.amp), 1.0)
def test_constant_limit_amplitude(self): """Test that the check for amplitude less than or equal to 1 can be disabled.""" with self.assertRaises(PulseError): Constant(duration=100, amp=1.1 + 0.8j) with patch("qiskit.pulse.library.pulse.Pulse.limit_amplitude", new=False): waveform = Constant(duration=100, amp=1.1 + 0.8j) self.assertGreater(np.abs(waveform.amp), 1.0)
def test_constant_limit_amplitude(self): """Test that the check for amplitude less than or equal to 1 can be disabled.""" waveform = Constant(duration=100, amp=1.1 + 0.8j, limit_amplitude=False) self.assertGreater(np.abs(waveform.amp), 1.0) with self.assertRaises(PulseError): Constant(duration=100, amp=1.1 + 0.8j, limit_amplitude=True)
def test_constant_samples(self): """Test the constant pulse and its sampled construction.""" const = Constant(duration=150, amp=0.1 + 0.4j) self.assertEqual(const.get_waveform().samples[0], 0.1 + 0.4j) self.assertEqual(len(const.get_waveform().samples), 150) with self.assertRaises(PulseError): const = Constant(duration=150, amp=1.1 + 0.4j) with patch("qiskit.pulse.library.parametric_pulses.Pulse.limit_amplitude", new=False): const = qiskit.pulse.library.parametric_pulses.Constant(duration=150, amp=0.1 + 0.4j)
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_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_multiple_calibrations(self): """Test for circuit with multiple pulse gates.""" amp1 = Parameter("amp1") amp2 = Parameter("amp2") mygate = Gate("mygate", 1, [amp2]) with builder.build() as caldef1: builder.play(Constant(100, amp1), DriveChannel(0)) with builder.build() as caldef2: builder.play(Constant(100, amp2), DriveChannel(1)) qc = QuantumCircuit(2) qc.rx(amp1, 0) qc.append(mygate, [1]) qc.add_calibration("rx", (0, ), caldef1, [amp1]) qc.add_calibration(mygate, (1, ), caldef2) self.assert_roundtrip_equal(qc)
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_override(self): """Test for overriding standard gate with pulse gate.""" amp = Parameter("amp") with builder.build() as caldef: builder.play(Constant(100, amp), ControlChannel(0)) qc = QuantumCircuit(2) qc.rx(amp, 0) qc.add_calibration("rx", (0, ), caldef, [amp]) self.assert_roundtrip_equal(qc)
def test_constant_pulse_instruction(self): """Test that parametric pulses are correctly converted to PulseQobjInstructions.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) instruction = Play(Constant(duration=25, amp=1), ControlChannel(2)) valid_qobj = PulseQobjInstruction( name='parametric_pulse', pulse_shape='constant', ch='u2', t0=20, parameters={'duration': 25, 'amp': 1}) self.assertEqual(converter(20, instruction), valid_qobj)
def test_2q_gate(self): """Test for two qubit pulse gate.""" mygate = Gate("mygate", 2, []) with builder.build() as caldef: builder.play(Constant(100, 0.1), ControlChannel(0)) qc = QuantumCircuit(2) qc.append(mygate, [0, 1]) qc.add_calibration(mygate, (0, 1), caldef) self.assert_roundtrip_equal(qc)
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_parameterized_gate(self): """Test for parameterized pulse gate.""" amp = Parameter("amp") angle = Parameter("angle") mygate = Gate("mygate", 2, [amp, angle]) with builder.build() as caldef: builder.play(Constant(100, amp * np.exp(1j * angle)), ControlChannel(0)) qc = QuantumCircuit(2) qc.append(mygate, [0, 1]) qc.add_calibration(mygate, (0, 1), caldef) self.assert_roundtrip_equal(qc)
def test_constant_pulse_instruction(self): """Test that parametric pulses are correctly converted to PulseQobjInstructions.""" converter = InstructionToQobjConverter(PulseQobjInstruction, meas_level=2) instruction = Play(Constant(duration=25, amp=1), ControlChannel(2)) valid_qobj = PulseQobjInstruction( name="parametric_pulse", pulse_shape="constant", ch="u2", t0=20, parameters={ "duration": 25, "amp": 1 }, ) self.assertEqual(converter(20, instruction), valid_qobj)
def test_complex_param_is_complex(self): """Check that complex param 'amp' is cast to complex.""" const = Constant(duration=150, amp=1) self.assertIsInstance(const.amp, complex)
def test_constant_samples(self): """Test the constant pulse and its sampled construction.""" const = Constant(duration=150, amp=0.1 + 0.4j) self.assertEqual(const.get_waveform().samples[0], 0.1 + 0.4j) self.assertEqual(len(const.get_waveform().samples), 150)