def test_partially_bound_callable(self): """Test register partial function.""" import functools def callable_schedule(par_b, par_a): sched = Schedule() sched.insert(10, Play(Constant(10, par_b), DriveChannel(0)), inplace=True) sched.insert(20, Play(Constant(10, par_a), DriveChannel(0)), inplace=True) return sched ref_sched = Schedule() ref_sched.insert(10, Play(Constant(10, 0.1), DriveChannel(0)), inplace=True) ref_sched.insert(20, Play(Constant(10, 0.2), DriveChannel(0)), inplace=True) inst_map = InstructionScheduleMap() def test_callable_sched1(par_b): return callable_schedule(par_b, 0.2) inst_map.add("my_gate1", (0,), test_callable_sched1, ["par_b"]) ret_sched = inst_map.get("my_gate1", (0,), par_b=0.1) self.assertEqual(ret_sched, ref_sched) # bind partially test_callable_sched2 = functools.partial(callable_schedule, par_a=0.2) inst_map.add("my_gate2", (0,), test_callable_sched2, ["par_b"]) ret_sched = inst_map.get("my_gate2", (0,), par_b=0.1) self.assertEqual(ret_sched, ref_sched)
def test_binding_unassigned_parameters(self): """Test getting schedule with unassigned parameter binding.""" param = Parameter("param") target_sched = Schedule() target_sched.insert(0, ShiftPhase(param, DriveChannel(0)), inplace=True) inst_map = InstructionScheduleMap() inst_map.add("target_sched", (0,), target_sched) with self.assertRaises(PulseError): inst_map.get("target_sched", (0,), P0=0)
def test_binding_too_many_parameters(self): """Test getting schedule with too many parameter binding.""" param = Parameter('param') target_sched = Schedule() target_sched.insert(0, ShiftPhase(param, DriveChannel(0)), inplace=True) inst_map = InstructionScheduleMap() inst_map.add('target_sched', (0,), target_sched) with self.assertRaises(PulseError): inst_map.get('target_sched', (0,), 0, 1, 2, 3)
def test_get_schedule_with_unbound_parameter(self): """Test get schedule with partial binding.""" param1 = Parameter('param1') param2 = Parameter('param2') target_sched = Schedule() target_sched.insert(0, ShiftPhase(param1, DriveChannel(0)), inplace=True) target_sched.insert(10, ShiftPhase(param2, DriveChannel(0)), inplace=True) inst_map = InstructionScheduleMap() inst_map.add('target_sched', (0, ), target_sched) ref_sched = Schedule() ref_sched.insert(0, ShiftPhase(param1, DriveChannel(0)), inplace=True) ref_sched.insert(10, ShiftPhase(1.23, DriveChannel(0)), inplace=True) test_sched = inst_map.get('target_sched', (0, ), param2=1.23) for test_inst, ref_inst in zip(test_sched.instructions, ref_sched.instructions): self.assertEqual(test_inst[0], ref_inst[0]) self.assertAlmostEqual(test_inst[1], ref_inst[1])
def test_schedule_with_multiple_parameters_under_same_name(self): """Test getting schedule with parameters that have the same name.""" param1 = Parameter('param') param2 = Parameter('param') param3 = Parameter('param') target_sched = Schedule() target_sched.insert(0, ShiftPhase(param1, DriveChannel(0)), inplace=True) target_sched.insert(10, ShiftPhase(param2, DriveChannel(0)), inplace=True) target_sched.insert(20, ShiftPhase(param3, DriveChannel(0)), inplace=True) inst_map = InstructionScheduleMap() inst_map.add('target_sched', (0, ), target_sched) ref_sched = Schedule() ref_sched.insert(0, ShiftPhase(1.23, DriveChannel(0)), inplace=True) ref_sched.insert(10, ShiftPhase(1.23, DriveChannel(0)), inplace=True) ref_sched.insert(20, ShiftPhase(1.23, DriveChannel(0)), inplace=True) test_sched = inst_map.get('target_sched', (0, ), param=1.23) for test_inst, ref_inst in zip(test_sched.instructions, ref_sched.instructions): self.assertEqual(test_inst[0], ref_inst[0]) self.assertAlmostEqual(test_inst[1], ref_inst[1])
def test_schedule_generator_supports_parameter_expressions(self): """Test expression-based schedule generator functionalty.""" t_param = Parameter('t') amp = 1.0 def test_func(dur: ParameterExpression, t_val: int): dur_bound = dur.bind({t_param: t_val}) sched = Schedule() sched += Play(library.constant(int(float(dur_bound)), amp), DriveChannel(0)) return sched expected_sched = Schedule() expected_sched += Play(library.constant(10, amp), DriveChannel(0)) inst_map = InstructionScheduleMap() inst_map.add('f', (0, ), test_func) self.assertEqual(inst_map.get('f', (0, ), dur=2 * t_param, t_val=5), expected_sched) self.assertEqual(inst_map.get_parameters('f', (0, )), ( 'dur', 't_val', ))
def test_schedule_with_non_alphanumeric_ordering(self): """Test adding and getting schedule with non obvious parameter ordering.""" theta = Parameter("theta") phi = Parameter("phi") lamb = Parameter("lambda") target_sched = Schedule() target_sched.insert(0, ShiftPhase(theta, DriveChannel(0)), inplace=True) target_sched.insert(10, ShiftPhase(phi, DriveChannel(0)), inplace=True) target_sched.insert(20, ShiftPhase(lamb, DriveChannel(0)), inplace=True) inst_map = InstructionScheduleMap() inst_map.add("target_sched", (0,), target_sched, arguments=["theta", "phi", "lambda"]) ref_sched = Schedule() ref_sched.insert(0, ShiftPhase(0, DriveChannel(0)), inplace=True) ref_sched.insert(10, ShiftPhase(1, DriveChannel(0)), inplace=True) ref_sched.insert(20, ShiftPhase(2, DriveChannel(0)), inplace=True) # if parameter is alphanumerical ordering this maps to # theta -> 2 # phi -> 1 # lamb -> 0 # however non alphanumerical ordering is specified in add method thus mapping should be # theta -> 0 # phi -> 1 # lamb -> 2 test_sched = inst_map.get("target_sched", (0,), 0, 1, 2) for test_inst, ref_inst in zip(test_sched.instructions, ref_sched.instructions): self.assertEqual(test_inst[0], ref_inst[0]) self.assertEqual(test_inst[1], ref_inst[1])
def test_get_gate(self): """Test `get`.""" sched = Schedule() sched.append(Play(Waveform(np.ones(5)), DriveChannel(0))) inst_map = InstructionScheduleMap() inst_map.add(XGate(), 0, sched) self.assertEqual(sched, inst_map.get(XGate(), (0, )))
def test_get_block(self): """Test `get` block.""" sched = ScheduleBlock() sched.append(Play(Waveform(np.ones(5)), DriveChannel(0)), inplace=True) inst_map = InstructionScheduleMap() inst_map.add("x", 0, sched) self.assertEqual(sched, inst_map.get("x", (0, )))
def test_parameterized_schedule(self): """Test adding parameterized schedule.""" converter = QobjToInstructionConverter([], buffer=0) qobj = PulseQobjInstruction(name='pv', ch='u1', t0=10, val='P2*cos(np.pi*P1)') converted_instruction = converter(qobj) inst_map = InstructionScheduleMap() inst_map.add('pv_test', 0, converted_instruction) self.assertEqual(inst_map.get_parameters('pv_test', 0), ('P1', 'P2')) sched = inst_map.get('pv_test', 0, P1=0, P2=-1) self.assertEqual(sched.instructions[0][-1].command.value, -1) with self.assertRaises(PulseError): inst_map.get('pv_test', 0, 0, P1=-1) with self.assertRaises(PulseError): inst_map.get('pv_test', 0, P1=1, P2=2, P3=3)
def test_get(self): """Test `get`.""" sched = Schedule() sched.append(SamplePulse(np.ones(5))(DriveChannel(0))) inst_map = InstructionScheduleMap() inst_map.add('u1', 0, sched) self.assertEqual(sched, inst_map.get('u1', (0, )))
def test_sequenced_parameterized_schedule(self): """Test parametrized schedule consists of multiple instruction. """ converter = QobjToInstructionConverter([], buffer=0) qobjs = [ PulseQobjInstruction(name='fc', ch='d0', t0=10, phase='P1'), PulseQobjInstruction(name='fc', ch='d0', t0=20, phase='P2'), PulseQobjInstruction(name='fc', ch='d0', t0=30, phase='P3') ] converted_instruction = [converter(qobj) for qobj in qobjs] inst_map = InstructionScheduleMap() inst_map.add( 'inst_seq', 0, ParameterizedSchedule(*converted_instruction, name='inst_seq')) with self.assertRaises(PulseError): inst_map.get('inst_seq', 0, P1=1, P2=2, P3=3, P4=4, P5=5) with self.assertRaises(PulseError): inst_map.get('inst_seq', 0, P1=1) with self.assertRaises(PulseError): inst_map.get('inst_seq', 0, 1, 2, 3, P1=1) p3_expr = Parameter('p3') p3_expr = p3_expr.bind({p3_expr: 3}) sched = inst_map.get('inst_seq', 0, 1, 2, p3_expr) self.assertEqual(sched.instructions[0][-1].phase, 1) self.assertEqual(sched.instructions[1][-1].phase, 2) self.assertEqual(sched.instructions[2][-1].phase, 3) sched = inst_map.get('inst_seq', 0, P1=1, P2=2, P3=p3_expr) self.assertEqual(sched.instructions[0][-1].phase, 1) self.assertEqual(sched.instructions[1][-1].phase, 2) self.assertEqual(sched.instructions[2][-1].phase, 3) sched = inst_map.get('inst_seq', 0, 1, 2, P3=p3_expr) self.assertEqual(sched.instructions[0][-1].phase, 1) self.assertEqual(sched.instructions[1][-1].phase, 2) self.assertEqual(sched.instructions[2][-1].phase, 3)
def test_schedule_generator(self): """Test schedule generator functionalty.""" x_test = 10 amp_test = 1.0 def test_func(x): sched = Schedule() sched += Play(library.constant(int(x), amp_test), DriveChannel(0)) return sched ref_sched = Schedule() ref_sched += Play(library.constant(x_test, amp_test), DriveChannel(0)) inst_map = InstructionScheduleMap() inst_map.add('f', (0,), test_func) self.assertEqual(inst_map.get('f', (0,), x_test), ref_sched) self.assertEqual(inst_map.get_parameters('f', (0,)), ('x',))
def test_schedule_generator(self): """Test schedule generator functionalty.""" dur_val = 10 amp = 1.0 def test_func(dur: int): sched = Schedule() sched += Play(library.constant(int(dur), amp), DriveChannel(0)) return sched expected_sched = Schedule() expected_sched += Play(library.constant(dur_val, amp), DriveChannel(0)) inst_map = InstructionScheduleMap() inst_map.add('f', (0, ), test_func) self.assertEqual(inst_map.get('f', (0, ), dur_val), expected_sched) self.assertEqual(inst_map.get_parameters('f', (0, )), ('dur', ))
def test_sequenced_parameterized_schedule(self): """Test parameterized schedule consists of multiple instruction.""" converter = QobjToInstructionConverter([], buffer=0) qobjs = [ PulseQobjInstruction(name="fc", ch="d0", t0=10, phase="P1"), PulseQobjInstruction(name="fc", ch="d0", t0=20, phase="P2"), PulseQobjInstruction(name="fc", ch="d0", t0=30, phase="P3"), ] converted_instruction = [converter(qobj) for qobj in qobjs] inst_map = InstructionScheduleMap() inst_map.add("inst_seq", 0, Schedule(*converted_instruction, name="inst_seq")) with self.assertRaises(PulseError): inst_map.get("inst_seq", 0, P1=1, P2=2, P3=3, P4=4, P5=5) with self.assertRaises(PulseError): inst_map.get("inst_seq", 0, 1, 2, 3, 4, 5, 6, 7, 8) p3_expr = Parameter("p3") p3_expr = p3_expr.bind({p3_expr: 3}) sched = inst_map.get("inst_seq", 0, 1, 2, p3_expr) self.assertEqual(sched.instructions[0][-1].phase, 1) self.assertEqual(sched.instructions[1][-1].phase, 2) self.assertEqual(sched.instructions[2][-1].phase, 3) sched = inst_map.get("inst_seq", 0, P1=1, P2=2, P3=p3_expr) self.assertEqual(sched.instructions[0][-1].phase, 1) self.assertEqual(sched.instructions[1][-1].phase, 2) self.assertEqual(sched.instructions[2][-1].phase, 3) sched = inst_map.get("inst_seq", 0, 1, 2, P3=p3_expr) self.assertEqual(sched.instructions[0][-1].phase, 1) self.assertEqual(sched.instructions[1][-1].phase, 2) self.assertEqual(sched.instructions[2][-1].phase, 3)