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_pass_alive_with_dcx_ish(self): """Test if the pass is not terminated by error with direct CX input.""" cx_sched = Schedule() # Fake direct cr cx_sched.insert(0, Play(GaussianSquare(800, 0.2, 64, 544), ControlChannel(1)), inplace=True) # Fake direct compensation tone # Compensation tone doesn't have dedicated pulse class. # So it's reported as a waveform now. compensation_tone = Waveform(0.1 * np.ones(800, dtype=complex)) cx_sched.insert(0, Play(compensation_tone, DriveChannel(0)), inplace=True) inst_map = InstructionScheduleMap() inst_map.add("cx", (1, 0), schedule=cx_sched) theta = pi / 3 rzx_qc = circuit.QuantumCircuit(2) rzx_qc.rzx(theta, 1, 0) pass_ = RZXCalibrationBuilder(instruction_schedule_map=inst_map) with self.assertWarns(UserWarning): # User warning that says q0 q1 is invalid cal_qc = PassManager(pass_).run(rzx_qc) self.assertEqual(cal_qc, rzx_qc)
def test_circuits(self): """Test that transpiling works and that we can have a y gate with a calibration.""" qubit = 1 inst_map = InstructionScheduleMap() for inst in ["sx", "y"]: inst_map.add(inst, (qubit, ), pulse.Schedule(name=inst)) hac = HalfAngle(qubit) hac.set_transpile_options(inst_map=inst_map) # mimic what will happen in the experiment. transpile_opts = copy.copy(hac.transpile_options.__dict__) transpile_opts["initial_layout"] = list(hac._physical_qubits) circuits = transpile(hac.circuits(), FakeAthens(), **transpile_opts) for idx, circ in enumerate(circuits): self.assertEqual(circ.count_ops()["sx"], idx * 2 + 2) self.assertEqual(circ.calibrations["sx"][((qubit, ), ())], pulse.Schedule(name="sx")) if idx > 0: self.assertEqual(circ.count_ops()["y"], idx) self.assertEqual(circ.calibrations["y"][((qubit, ), ())], pulse.Schedule(name="y"))
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_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_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_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(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_remove(self): """Test removing a defined operation and removing an undefined operation.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add('tmp', 0, sched) inst_map.remove('tmp', 0) self.assertFalse(inst_map.has('tmp', 0)) with self.assertRaises(PulseError): inst_map.remove('not_there', (0, )) self.assertFalse('tmp' in inst_map.qubit_instructions(0))
def test_instructions(self): """Test `instructions`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add('u1', 1, sched) inst_map.add('u3', 0, sched) instructions = inst_map.instructions for inst in ['u1', 'u3']: self.assertTrue(inst in instructions)
def test_instructions(self): """Test `instructions`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add("u1", 1, sched) inst_map.add("u3", 0, sched) instructions = inst_map.instructions for inst in ["u1", "u3"]: self.assertTrue(inst in instructions)
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_qubits_with_instruction_gate(self): """Test `qubits_with_instruction`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add(U1Gate(0), (0, ), sched) inst_map.add(U1Gate(0), (1, ), sched) inst_map.add(CXGate(), [0, 1], sched) self.assertEqual(inst_map.qubits_with_instruction(U1Gate(0)), [0, 1]) self.assertEqual(inst_map.qubits_with_instruction(CXGate()), [(0, 1)]) self.assertEqual(inst_map.qubits_with_instruction('none'), [])
def test_qubits_with_instruction(self): """Test `qubits_with_instruction`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add("u1", (0, ), sched) inst_map.add("u1", (1, ), sched) inst_map.add("cx", [0, 1], sched) self.assertEqual(inst_map.qubits_with_instruction("u1"), [0, 1]) self.assertEqual(inst_map.qubits_with_instruction("cx"), [(0, 1)]) self.assertEqual(inst_map.qubits_with_instruction("none"), [])
def test_add_block(self): """Test add block, and that errors are raised when expected.""" sched = ScheduleBlock() sched.append(Play(Waveform(np.ones(5)), DriveChannel(0)), inplace=True) inst_map = InstructionScheduleMap() inst_map.add("u1", 1, sched) inst_map.add("u1", 0, sched) self.assertIn("u1", inst_map.instructions) self.assertEqual(inst_map.qubits_with_instruction("u1"), [0, 1]) self.assertTrue("u1" in inst_map.qubit_instructions(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_qubits_with_instruction(self): """Test `qubits_with_instruction`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add('u1', (0, ), sched) inst_map.add('u1', (1, ), sched) inst_map.add('cx', [0, 1], sched) self.assertEqual(inst_map.qubits_with_instruction('u1'), [0, 1]) self.assertEqual(inst_map.qubits_with_instruction('cx'), [(0, 1)]) self.assertEqual(inst_map.qubits_with_instruction('none'), [])
def test_pop(self): """Test pop with default.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add('tmp', 100, sched) self.assertEqual(inst_map.pop('tmp', 100), sched) self.assertFalse(inst_map.has('tmp', 100)) self.assertEqual(inst_map.qubit_instructions(100), []) self.assertEqual(inst_map.qubits_with_instruction('tmp'), []) with self.assertRaises(PulseError): inst_map.pop('not_there', (0, ))
def test_qubit_instructions_gate(self): """Test `qubit_instructions`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add(U1Gate(0), (0, ), sched) inst_map.add(U1Gate(0), (1, ), sched) inst_map.add(CXGate(), [0, 1], sched) self.assertEqual(inst_map.qubit_instructions(0), ["u1"]) self.assertEqual(inst_map.qubit_instructions(1), ["u1"]) self.assertEqual(inst_map.qubit_instructions((0, 1)), ["cx"]) self.assertEqual(inst_map.qubit_instructions(10), [])
def test_qubit_instructions(self): """Test `qubit_instructions`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add('u1', (0, ), sched) inst_map.add('u1', (1, ), sched) inst_map.add('cx', [0, 1], sched) self.assertEqual(inst_map.qubit_instructions(0), ['u1']) self.assertEqual(inst_map.qubit_instructions(1), ['u1']) self.assertEqual(inst_map.qubit_instructions((0, 1)), ['cx']) self.assertEqual(inst_map.qubit_instructions(10), [])
def test_has(self): """Test `has` and `assert_has`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add('u1', (0, ), sched) inst_map.add('cx', [0, 1], sched) self.assertTrue(inst_map.has('u1', [0])) self.assertTrue(inst_map.has('cx', (0, 1))) with self.assertRaises(PulseError): inst_map.assert_has('dne', [0]) with self.assertRaises(PulseError): inst_map.assert_has('cx', 100)
def test_has_gate(self): """Test `has` and `assert_has`.""" sched = Schedule() inst_map = InstructionScheduleMap() inst_map.add(U1Gate(0), (0, ), sched) inst_map.add(CXGate(), [0, 1], sched) self.assertTrue(inst_map.has(U1Gate(0), [0])) self.assertTrue(inst_map.has(CXGate(), (0, 1))) with self.assertRaises(PulseError): inst_map.assert_has("dne", [0]) with self.assertRaises(PulseError): inst_map.assert_has(CXGate(), 100)
def test_measure_with_custom_inst_map(self): """Test measure with custom inst_map, meas_map with measure_name.""" q0_sched = Play(GaussianSquare(1200, 1, 0.4, 1150), MeasureChannel(0)) q0_sched += Acquire(1200, AcquireChannel(0), MemorySlot(0)) inst_map = InstructionScheduleMap() inst_map.add('my_sched', 0, q0_sched) sched = macros.measure(qubits=[0], measure_name='my_sched', inst_map=inst_map, meas_map=[[0]]) self.assertEqual(sched.instructions, q0_sched.instructions) with self.assertRaises(PulseError): macros.measure(qubits=[0], measure_name="name", inst_map=inst_map, meas_map=[[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_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_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.""" 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_schedule_block_in_instmap(self): """Test schedule block in instmap can be scheduled.""" duration = Parameter("duration") with build() as pulse_prog: play(Gaussian(duration, 0.1, 10), DriveChannel(0)) instmap = InstructionScheduleMap() instmap.add("block_gate", (0, ), pulse_prog, ["duration"]) qc = QuantumCircuit(1) qc.append(Gate("block_gate", 1, [duration]), [0]) qc.assign_parameters({duration: 100}, inplace=True) sched = schedule(qc, self.backend, inst_map=instmap) ref_sched = Schedule() ref_sched += Play(Gaussian(100, 0.1, 10), DriveChannel(0)) self.assertEqual(sched, ref_sched)