def convert_persistent_value(self, instruction): """Return converted `PersistentValueInstruction`. Args: instruction (PulseQobjInstruction): persistent value qobj Returns: Schedule: Converted and scheduled Instruction """ t0 = instruction.t0 channel = self.get_channel(instruction.ch) val = instruction.val # This is parameterized if isinstance(val, str): val_expr = parse_string_expr(val, partial_binding=False) def gen_pv_sched(*args, **kwargs): val = complex(val_expr(*args, **kwargs)) return commands.PersistentValue(val)(channel) << t0 return ParameterizedSchedule(gen_pv_sched, parameters=val_expr.params) return commands.PersistentValue(val)(channel) << t0
def convert_shift_frequency(self, instruction): """Return converted `ShiftFrequency`. Args: instruction (PulseQobjInstruction): Shift frequency qobj instruction. Returns: Schedule: Converted and scheduled Instruction """ t0 = instruction.t0 channel = self.get_channel(instruction.ch) frequency = instruction.frequency * 1e9 if isinstance(frequency, str): frequency_expr = parse_string_expr(frequency, partial_binding=False) def gen_sf_schedule(*args, **kwargs): _frequency = frequency_expr(*args, **kwargs) return instructions.ShiftFrequency(_frequency, channel) << t0 return ParameterizedSchedule(gen_sf_schedule, parameters=frequency_expr.params) return instructions.ShiftFrequency(frequency, channel) << t0
def test_partial_binding(self): """Test partial binding of parameters.""" expr = "P1 * P2 + P3 / P4 - P5" parsed_expr = parse_string_expr(expr, partial_binding=True) self.assertEqual(parsed_expr.params, ["P1", "P2", "P3", "P4", "P5"]) bound_three = parsed_expr(P1=1, P2=2, P3=3) self.assertEqual(bound_three.params, ["P4", "P5"]) self.assertEqual(bound_three(P4=4, P5=5), -2.25) self.assertEqual(bound_three(4, 5), -2.25) bound_four = bound_three(P4=4) self.assertEqual(bound_four.params, ["P5"]) self.assertEqual(bound_four(P5=5), -2.25) self.assertEqual(bound_four(5), -2.25) bound_four_new = bound_three(P4=40) self.assertEqual(bound_four_new.params, ["P5"]) self.assertEqual(bound_four_new(P5=5), -2.925) self.assertEqual(bound_four_new(5), -2.925)
def convert_frame_change(self, instruction): """Return converted `FrameChangeInstruction`. Args: instruction (PulseQobjInstruction): frame change qobj Returns: Schedule: Converted and scheduled Instruction """ t0 = instruction.t0 channel = self.get_channel(instruction.ch) phase = instruction.phase # This is parameterized if isinstance(phase, str): phase_expr = parse_string_expr(phase, partial_binding=False) def gen_fc_sched(*args, **kwargs): phase = abs(phase_expr(*args, **kwargs)) return commands.FrameChange(phase)(channel) << t0 return ParameterizedSchedule(gen_fc_sched, parameters=phase_expr.params) return commands.FrameChange(phase)(channel) << t0
def convert_shift_phase(self, instruction): """Return converted `ShiftPhase`. Args: instruction (PulseQobjInstruction): phase shift qobj instruction Returns: Schedule: Converted and scheduled Instruction """ t0 = instruction.t0 channel = self.get_channel(instruction.ch) phase = instruction.phase # This is parameterized if isinstance(phase, str): phase_expr = parse_string_expr(phase, partial_binding=False) def gen_fc_sched(*args, **kwargs): # this should be real value _phase = phase_expr(*args, **kwargs) return instructions.ShiftPhase(_phase, channel) << t0 return ParameterizedSchedule(gen_fc_sched, parameters=phase_expr.params) return instructions.ShiftPhase(phase, channel) << t0