def setUp(self): super().setUp() self.amp0 = circuit.Parameter("amp0") self.amp1 = circuit.Parameter("amp1") self.dur0 = circuit.Parameter("dur0") self.dur1 = circuit.Parameter("dur1") self.test_par_waveform0 = pulse.Constant(self.dur0, self.amp0) self.test_par_waveform1 = pulse.Constant(self.dur1, self.amp1)
def setUp(self): super().setUp() with pulse.build() as _subroutine: pulse.delay(10, pulse.DriveChannel(0)) self.subroutine = _subroutine self.param1 = circuit.Parameter('amp1') self.param2 = circuit.Parameter('amp2') with pulse.build() as _function: pulse.play(pulse.Gaussian(160, self.param1, 40), pulse.DriveChannel(0)) pulse.play(pulse.Gaussian(160, self.param2, 40), pulse.DriveChannel(0)) pulse.play(pulse.Gaussian(160, self.param1, 40), pulse.DriveChannel(0)) self.function = _function
def test_parametrized_frame_change(self): """Test generating waveforms that are parameterized. Parametrized phase should be ignored when calculating waveform frame. This is due to phase modulated representation of waveforms, i.e. we cannot calculate the phase factor of waveform if the phase is unbound. """ param = circuit.Parameter('phase') test_fc1 = pulse.ShiftPhase(param, pulse.DriveChannel(0)) test_fc2 = pulse.ShiftPhase(1.57, pulse.DriveChannel(0)) test_waveform = pulse.Play(pulse.Constant(10, 0.1), pulse.DriveChannel(0)) ch_events = events.ChannelEvents(waveforms={0: test_waveform}, frames={0: [test_fc1, test_fc2]}, channel=pulse.DriveChannel(0)) # waveform frame pulse_inst = list(ch_events.get_waveforms())[0] self.assertFalse(pulse_inst.is_opaque) self.assertEqual(pulse_inst.frame.phase, 1.57) # framechange pulse_inst = list(ch_events.get_frame_changes())[0] self.assertTrue(pulse_inst.is_opaque) self.assertEqual(pulse_inst.frame.phase, param + 1.57)
def setUp(self): super().setUp() with pulse.build(name="sx_q0") as custom_sx_q0: pulse.play(pulse.Constant(100, 0.1), pulse.DriveChannel(0)) self.custom_sx_q0 = custom_sx_q0 with pulse.build(name="sx_q1") as custom_sx_q1: pulse.play(pulse.Constant(100, 0.2), pulse.DriveChannel(1)) self.custom_sx_q1 = custom_sx_q1 self.sched_param = circuit.Parameter("P0") with pulse.build(name="my_gate_q0") as my_gate_q0: pulse.shift_phase(self.sched_param, pulse.DriveChannel(0)) pulse.play(pulse.Constant(120, 0.1), pulse.DriveChannel(0)) self.my_gate_q0 = my_gate_q0 with pulse.build(name="my_gate_q1") as my_gate_q1: pulse.shift_phase(self.sched_param, pulse.DriveChannel(1)) pulse.play(pulse.Constant(120, 0.2), pulse.DriveChannel(1)) self.my_gate_q1 = my_gate_q1
def test_parametrized_context(self): """Test parametrize context parameter.""" duration = circuit.Parameter('dur') param_context = transforms.AlignEquispaced(duration=duration) block = pulse.ScheduleBlock(alignment_context=param_context) block += pulse.Delay(10, self.d0) block += pulse.Delay(10, self.d0) block += pulse.Delay(10, self.d0) block += pulse.Delay(10, self.d0) self.assertTrue(block.is_parameterized()) self.assertFalse(block.is_schedulable()) block.assign_parameters({duration: 100}, inplace=True) self.assertFalse(block.is_parameterized()) self.assertTrue(block.is_schedulable()) ref_sched = pulse.Schedule() ref_sched = ref_sched.insert(0, pulse.Delay(10, self.d0)) ref_sched = ref_sched.insert(10, pulse.Delay(20, self.d0)) ref_sched = ref_sched.insert(30, pulse.Delay(10, self.d0)) ref_sched = ref_sched.insert(40, pulse.Delay(20, self.d0)) ref_sched = ref_sched.insert(60, pulse.Delay(10, self.d0)) ref_sched = ref_sched.insert(70, pulse.Delay(20, self.d0)) ref_sched = ref_sched.insert(90, pulse.Delay(10, self.d0)) self.assertScheduleEqual(block, ref_sched)
def test_gen_filled_waveform_stepwise_opaque(self): """Test generating waveform with unbound parameter.""" amp = circuit.Parameter('amp') my_pulse = pulse.Gaussian(10, amp, 3, name='my_pulse') play = pulse.Play(my_pulse, pulse.DriveChannel(0)) inst_data = create_instruction(play, np.pi / 2, 5e9, 5, 0.1, True) objs = waveform.gen_filled_waveform_stepwise(inst_data, formatter=self.formatter, device=self.device) self.assertEqual(len(objs), 2) # type check self.assertEqual(type(objs[0]), drawings.BoxData) self.assertEqual(type(objs[1]), drawings.TextData) x_ref = np.array([5, 15]) y_ref = np.array([ -0.5 * self.formatter['box_height.opaque_shape'], 0.5 * self.formatter['box_height.opaque_shape'] ]) # data check np.testing.assert_array_equal(objs[0].xvals, x_ref) np.testing.assert_array_equal(objs[0].yvals, y_ref) # meta data check ref_meta = { 'duration (cycle time)': 10, 'duration (sec)': 1.0, 't0 (cycle time)': 5, 't0 (sec)': 0.5, 'waveform shape': 'Gaussian', 'amp': 'amp', 'sigma': 3, 'phase': np.pi / 2, 'frequency': 5e9, 'qubit': 0, 'name': 'my_pulse' } self.assertDictEqual(objs[0].meta, ref_meta) # style check ref_style = { 'alpha': self.formatter['alpha.opaque_shape'], 'zorder': self.formatter['layer.fill_waveform'], 'linewidth': self.formatter['line_width.opaque_shape'], 'linestyle': self.formatter['line_style.opaque_shape'], 'facecolor': self.formatter['color.opaque_shape'][0], 'edgecolor': self.formatter['color.opaque_shape'][1] } self.assertDictEqual(objs[0].styles, ref_style) # test label self.assertEqual(objs[1].text, 'Gaussian(amp)')
def test_gen_filled_waveform_stepwise_opaque(self): """Test generating waveform with unbound parameter.""" amp = circuit.Parameter("amp") my_pulse = pulse.Gaussian(10, amp, 3, name="my_pulse") play = pulse.Play(my_pulse, pulse.DriveChannel(0)) inst_data = create_instruction(play, np.pi / 2, 5e9, 5, 0.1, True) objs = waveform.gen_filled_waveform_stepwise(inst_data, formatter=self.formatter, device=self.device) self.assertEqual(len(objs), 2) # type check self.assertEqual(type(objs[0]), drawings.BoxData) self.assertEqual(type(objs[1]), drawings.TextData) x_ref = np.array([5, 15]) y_ref = np.array([ -0.5 * self.formatter["box_height.opaque_shape"], 0.5 * self.formatter["box_height.opaque_shape"], ]) # data check np.testing.assert_array_equal(objs[0].xvals, x_ref) np.testing.assert_array_equal(objs[0].yvals, y_ref) # meta data check ref_meta = { "duration (cycle time)": 10, "duration (sec)": 1.0, "t0 (cycle time)": 5, "t0 (sec)": 0.5, "waveform shape": "Gaussian", "amp": "amp", "sigma": 3, "phase": np.pi / 2, "frequency": 5e9, "qubit": 0, "name": "my_pulse", } self.assertDictEqual(objs[0].meta, ref_meta) # style check ref_style = { "alpha": self.formatter["alpha.opaque_shape"], "zorder": self.formatter["layer.fill_waveform"], "linewidth": self.formatter["line_width.opaque_shape"], "linestyle": self.formatter["line_style.opaque_shape"], "facecolor": self.formatter["color.opaque_shape"][0], "edgecolor": self.formatter["color.opaque_shape"][1], } self.assertDictEqual(objs[0].styles, ref_style) # test label self.assertEqual(objs[1].text, "Gaussian(amp)")
def test_call_with_not_existing_parameter(self): """Test call subroutine with parameter not defined.""" amp = circuit.Parameter('amp1') with pulse.build() as subroutine: pulse.play(pulse.Gaussian(160, amp, 40), pulse.DriveChannel(0)) with self.assertRaises(exceptions.PulseError): with pulse.build(): pulse.call(subroutine, amp=0.1)
def test_call_subroutine_with_parametrized_duration(self): """Test call subroutine containing a parametrized duration.""" dur = circuit.Parameter("dur") with pulse.build() as subroutine: pulse.play(pulse.Constant(dur, 0.1), pulse.DriveChannel(0)) pulse.play(pulse.Constant(dur, 0.2), pulse.DriveChannel(0)) with pulse.build() as main: pulse.call(subroutine) self.assertEqual(len(main.blocks), 1)
def test_call_with_parameter_name_collision(self): """Test call subroutine with duplicated parameter names.""" amp1 = circuit.Parameter('amp') amp2 = circuit.Parameter('amp') sigma = circuit.Parameter('sigma') with pulse.build() as subroutine: pulse.play(pulse.Gaussian(160, amp1, sigma), pulse.DriveChannel(0)) pulse.play(pulse.Gaussian(160, amp2, sigma), pulse.DriveChannel(0)) with pulse.build() as main_prog: pulse.call(subroutine, value_dict={amp1: 0.1, amp2: 0.2}, sigma=40) assigned_sched = inline_subroutines(main_prog) play_0 = assigned_sched.instructions[0][1] play_1 = assigned_sched.instructions[1][1] self.assertEqual(play_0.pulse.amp, 0.1) self.assertEqual(play_0.pulse.sigma, 40) self.assertEqual(play_1.pulse.amp, 0.2) self.assertEqual(play_1.pulse.sigma, 40)
def test_parameterized_parametric_pulse(self): """Test generating waveforms that are parameterized.""" param = circuit.Parameter('amp') test_waveform = pulse.Play(pulse.Constant(10, param), pulse.DriveChannel(0)) ch_events = events.ChannelEvents(waveforms={0: test_waveform}, frames={}, channel=pulse.DriveChannel(0)) pulse_inst = list(ch_events.get_waveforms())[0] self.assertTrue(pulse_inst.is_opaque) self.assertEqual(pulse_inst.inst, test_waveform)
def test_assigned_amplitude_is_complex(self): """Test pulse amp parameter is always complex valued. Note that IBM backend treats "amp" as a special parameter, and this should be complex value otherwise IBM backends raise 8042 error. "Pulse parameter "amp" must be specified as a list of the form [real, imag]" """ amp = circuit.Parameter("amp") block = pulse.ScheduleBlock() block += pulse.Play(pulse.Constant(100, amp), pulse.DriveChannel(0)) assigned_block = block.assign_parameters({amp: 0.1}, inplace=True) assigned_amp = assigned_block.blocks[0].pulse.amp self.assertIsInstance(assigned_amp, complex)
def test_pulse_amp_typecasted(self): """Test if scaled pulse amplitude is complex type.""" fake_play = Play( GaussianSquare(duration=800, amp=0.1, sigma=64, risefall_sigma_ratio=2), ControlChannel(0), ) fake_theta = circuit.Parameter("theta") assigned_theta = fake_theta.assign(fake_theta, 0.01) scaled = RZXCalibrationBuilderNoEcho.rescale_cr_inst( instruction=fake_play, theta=assigned_theta) scaled_pulse = scaled.pulse self.assertIsInstance(scaled_pulse.amp, complex)
def test_equality_of_parametrized_channels(self): """Test check equality of blocks involving parametrized channels.""" par_ch = circuit.Parameter("ch") block1 = pulse.ScheduleBlock(alignment_context=self.left_context) block1 += pulse.Play(self.test_waveform0, pulse.DriveChannel(par_ch)) block1 += pulse.Play(self.test_par_waveform0, self.d0) block2 = pulse.ScheduleBlock(alignment_context=self.left_context) block2 += pulse.Play(self.test_par_waveform0, self.d0) block2 += pulse.Play(self.test_waveform0, pulse.DriveChannel(par_ch)) self.assertEqual(block1, block2) block1_assigned = block1.assign_parameters({par_ch: 1}) block2_assigned = block2.assign_parameters({par_ch: 1}) self.assertEqual(block1_assigned, block2_assigned)
def test_call_with_common_parameter(self): """Test call subroutine with parameter that is defined multiple times.""" amp = circuit.Parameter('amp') with pulse.build() as subroutine: pulse.play(pulse.Gaussian(160, amp, 40), pulse.DriveChannel(0)) pulse.play(pulse.Gaussian(320, amp, 80), pulse.DriveChannel(0)) with pulse.build() as main_prog: pulse.call(subroutine, amp=0.1) assigned_sched = inline_subroutines(main_prog) play_0 = assigned_sched.instructions[0][1] play_1 = assigned_sched.instructions[1][1] self.assertEqual(play_0.pulse.amp, 0.1) self.assertEqual(play_1.pulse.amp, 0.1)
def gen_frame_symbol_with_parameters(self): """Test gen_frame_symbol with parameterized frame.""" theta = -1.0 * circuit.Parameter("P0") fcs = [pulse.ShiftPhase(theta, pulse.DriveChannel(0))] inst_data = create_instruction(fcs, np.pi / 2, 1e6, 5, 0.1) obj = frame.gen_frame_symbol(inst_data, formatter=self.formatter, device=self.device)[0] # metadata check ref_meta = { "total phase change": np.pi / 2, "total frequency change": 1e6, "program": ["ShiftPhase(-1.0*P0)"], "t0 (cycle time)": 5, "t0 (sec)": 0.5, } self.assertDictEqual(obj.meta, ref_meta)
def test_call_with_parameters(self): """Test call subroutine with parameters.""" amp = circuit.Parameter('amp') with pulse.build() as subroutine: pulse.play(pulse.Gaussian(160, amp, 40), pulse.DriveChannel(0)) with pulse.build() as main_prog: pulse.call(subroutine, amp=0.1) pulse.call(subroutine, amp=0.3) self.assertEqual(main_prog.is_parameterized(), False) assigned_sched = inline_subroutines(main_prog) play_0 = assigned_sched.instructions[0][1] play_1 = assigned_sched.instructions[1][1] self.assertEqual(play_0.pulse.amp, 0.1) self.assertEqual(play_1.pulse.amp, 0.3)
def test_call_partly_with_parameters(self): """Test multiple calls partly with parameters then assign.""" amp = circuit.Parameter("amp") with pulse.build() as subroutine: pulse.play(pulse.Gaussian(160, amp, 40), pulse.DriveChannel(0)) with pulse.build() as main_prog: pulse.call(subroutine, amp=0.1) pulse.call(subroutine) self.assertEqual(main_prog.is_parameterized(), True) main_prog.assign_parameters({amp: 0.5}) self.assertEqual(main_prog.is_parameterized(), False) assigned_sched = target_qobj_transform(main_prog) play_0 = assigned_sched.instructions[0][1] play_1 = assigned_sched.instructions[1][1] self.assertEqual(play_0.pulse.amp, 0.1) self.assertEqual(play_1.pulse.amp, 0.5)