def test_nested_assignment_partial_bind(self): """Test nested schedule with call instruction. Inline the schedule and partially bind parameters.""" context = AlignEquispaced(duration=self.context_dur) subroutine = pulse.ScheduleBlock(alignment_context=context) subroutine += pulse.Play(self.parametric_waveform1, self.d1) nested_block = pulse.ScheduleBlock() nested_block += pulse.Call(subroutine=subroutine) test_obj = pulse.ScheduleBlock() test_obj += nested_block test_obj = inline_subroutines(test_obj) value_dict = {self.context_dur: 1000, self.dur1: 200, self.ch1: 1} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_obj) ref_context = AlignEquispaced(duration=1000) ref_subroutine = pulse.ScheduleBlock(alignment_context=ref_context) ref_subroutine += pulse.Play( pulse.Gaussian(200, self.amp1_1 + self.amp1_2, 50), pulse.DriveChannel(1)) ref_nested_block = pulse.ScheduleBlock() ref_nested_block += ref_subroutine ref_obj = pulse.ScheduleBlock() ref_obj += ref_nested_block self.assertEqual(assigned, ref_obj)
def test_complex_parameter_expression(self): """Test assignment of complex-valued parameter expression to parameter.""" amp = Parameter("amp") mag = Parameter("A") phi = Parameter("phi") test_obj = pulse.Constant(duration=160, amp=amp) # generate parameter expression value_dict = {amp: mag * np.exp(1j * phi)} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_obj) # generate complex value value_dict = {mag: 0.1, phi: 0.5} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(assigned) # evaluated parameter expression: 0.0877582561890373 + 0.0479425538604203*I value_dict = {amp: 0.1 * np.exp(0.5j)} visitor = ParameterSetter(param_map=value_dict) ref_obj = visitor.visit(test_obj) self.assertEqual(assigned, ref_obj)
def test_set_parameter_to_alignment_context(self): """Test get parameters from alignment context.""" test_obj = AlignEquispaced(duration=self.context_dur + self.dur1) value_dict = {self.context_dur: 1000, self.dur1: 100} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_obj) ref_obj = AlignEquispaced(duration=1100) self.assertEqual(assigned, ref_obj)
def test_set_parameter_to_pulse(self): """Test get parameters from pulse instruction.""" test_obj = self.parametric_waveform1 value_dict = {self.amp1_1: 0.1, self.amp1_2: 0.2, self.dur1: 160} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_obj) ref_obj = pulse.Gaussian(duration=160, amp=0.3, sigma=40) self.assertEqual(assigned, ref_obj)
def test_set_parameter_to_channel(self): """Test get parameters from channel.""" test_obj = pulse.DriveChannel(self.ch1 + self.ch2) value_dict = {self.ch1: 1, self.ch2: 2} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_obj) ref_obj = pulse.DriveChannel(3) self.assertEqual(assigned, ref_obj)
def test_set_parameter_to_inst(self): """Test get parameters from instruction.""" test_obj = pulse.ShiftPhase(self.phi1 + self.phi2, pulse.DriveChannel(0)) value_dict = {self.phi1: 0.123, self.phi2: 0.456} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_obj) ref_obj = pulse.ShiftPhase(0.579, pulse.DriveChannel(0)) self.assertEqual(assigned, ref_obj)
def test_complex_value_to_parameter(self): """Test complex value can be assigned to parameter object.""" amp = Parameter("amp") test_obj = pulse.Constant(duration=160, amp=amp) value_dict = {amp: 0.1j} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_obj) ref_obj = pulse.Constant(duration=160, amp=1j * 0.1) self.assertEqual(assigned, ref_obj)
def test_set_parameter_to_complex_schedule(self): """Test get parameters from complicated schedule.""" test_block = deepcopy(self.test_sched) value_dict = { self.amp1_1: 0.1, self.amp1_2: 0.2, self.amp2: 0.3, self.amp3: 0.4, self.dur1: 100, self.dur2: 125, self.dur3: 150, self.ch1: 0, self.ch2: 2, self.ch3: 4, self.phi1: 1.0, self.phi2: 2.0, self.phi3: 3.0, self.meas_dur: 300, self.mem1: 3, self.reg1: 0, self.context_dur: 1000, } visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_block) # create ref schedule subroutine = pulse.ScheduleBlock(alignment_context=AlignLeft()) subroutine += pulse.ShiftPhase(1.0, pulse.DriveChannel(0)) subroutine += pulse.Play(pulse.Gaussian(100, 0.3, 25), pulse.DriveChannel(0)) sched = pulse.Schedule() sched += pulse.ShiftPhase(3.0, pulse.DriveChannel(4)) ref_obj = pulse.ScheduleBlock(alignment_context=AlignEquispaced(1000), name="long_schedule") ref_obj += subroutine ref_obj += pulse.ShiftPhase(2.0, pulse.DriveChannel(2)) ref_obj += pulse.Play(pulse.Gaussian(125, 0.3, 25), pulse.DriveChannel(2)) ref_obj += pulse.Call(sched) ref_obj += pulse.Play(pulse.Gaussian(150, 0.4, 25), pulse.DriveChannel(4)) ref_obj += pulse.Acquire(300, pulse.AcquireChannel(0), pulse.MemorySlot(3), pulse.RegisterSlot(0)) self.assertEqual(assigned, ref_obj)
def test_set_parameter_to_acquire(self): """Test set parameters to acquire instruction.""" test_obj = pulse.Acquire(16000, pulse.AcquireChannel(self.ch1), pulse.MemorySlot(self.ch1)) value_dict = {self.ch1: 2} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_obj) ref_obj = pulse.Acquire(16000, pulse.AcquireChannel(2), pulse.MemorySlot(2)) self.assertEqual(assigned, ref_obj)
def test_with_function(self): """Test ParameterExpressions formed trivially in a function.""" def get_shift(variable): return variable - 1 test_obj = pulse.ShiftPhase(get_shift(self.phi1), self.d1) value_dict = {self.phi1: 2.0, self.ch1: 2} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_obj) ref_obj = pulse.ShiftPhase(1.0, pulse.DriveChannel(2)) self.assertEqual(assigned, ref_obj)
def test_set_parameter_to_call(self): """Test get parameters from instruction.""" sched = pulse.Schedule() sched += pulse.ShiftPhase(self.phi1, self.d1) test_obj = pulse.Call(subroutine=sched) value_dict = {self.phi1: 1.57, self.ch1: 2} visitor = ParameterSetter(param_map=value_dict) assigned = visitor.visit(test_obj) ref_sched = pulse.Schedule() ref_sched += pulse.ShiftPhase(1.57, pulse.DriveChannel(2)) ref_obj = pulse.Call(subroutine=ref_sched) self.assertEqual(assigned, ref_obj)