def test_call_gate_and_circuit(self): """Test calling circuit with gates.""" h_control = circuit.QuantumCircuit(2) h_control.h(0) with pulse.build(self.backend) as schedule: with pulse.align_sequential(): # this is circuit, a subroutine stored as Call instruction pulse.call(h_control) # this is instruction, not subroutine pulse.cx(0, 1) # this is macro, not subroutine pulse.measure([0, 1]) # subroutine h_reference = compiler.schedule(compiler.transpile(h_control, self.backend), self.backend) # gate cx_circ = circuit.QuantumCircuit(2) cx_circ.cx(0, 1) cx_reference = compiler.schedule(compiler.transpile(cx_circ, self.backend), self.backend) # measurement measure_reference = macros.measure( qubits=[0, 1], inst_map=self.inst_map, meas_map=self.configuration.meas_map ) reference = pulse.Schedule() reference += pulse.instructions.Call(h_reference) reference += cx_reference reference += measure_reference << reference.duration self.assertScheduleEqual(schedule, reference)
def test_schedule_supplied(self): """Test that schedule is used if it is supplied to the builder.""" d0 = pulse.DriveChannel(0) with pulse.build(name='reference') as reference: with pulse.align_sequential(): pulse.delay(10, d0) with pulse.build(schedule=reference) as schedule: pass self.assertEqual(schedule, reference) self.assertEqual(schedule.name, 'reference')
def test_measure(self): """Test pulse measurement macro against circuit measurement and ensure agreement.""" with pulse.build(self.backend) as schedule: with pulse.align_sequential(): pulse.x(0) pulse.measure(0) reference_qc = circuit.QuantumCircuit(1, 1) reference_qc.x(0) reference_qc.measure(0, 0) reference_qc = compiler.transpile(reference_qc, self.backend) reference = compiler.schedule(reference_qc, self.backend) self.assertEqual(schedule, reference)
def test_default_alignment_sequential(self): """Test default sequential alignment setting.""" d0 = pulse.DriveChannel(0) d1 = pulse.DriveChannel(0) with pulse.build(default_alignment="sequential") as schedule: pulse.delay(10, d0) pulse.delay(20, d1) with pulse.build() as reference: with pulse.align_sequential(): pulse.delay(10, d0) pulse.delay(20, d1) self.assertScheduleEqual(schedule, reference)
def test_align_sequential(self): """Test the sequential alignment context.""" d0 = pulse.DriveChannel(0) d1 = pulse.DriveChannel(1) with pulse.build() as schedule: with pulse.align_sequential(): pulse.delay(3, d0) pulse.delay(5, d1) pulse.delay(7, d0) reference = pulse.Schedule() # d0 reference.insert(0, instructions.Delay(3, d0), inplace=True) reference.insert(8, instructions.Delay(7, d0), inplace=True) # d1 reference.insert(3, instructions.Delay(5, d1), inplace=True) self.assertEqual(schedule, reference)
def test_used_in_calls(self): """Test that we can identify schedules by name when calls are present.""" with pulse.build(name="xp") as xp: pulse.play(pulse.Gaussian(160, 0.5, 40), pulse.DriveChannel(1)) with pulse.build(name="xp2") as xp2: pulse.play(pulse.Gaussian(160, 0.5, 40), pulse.DriveChannel(1)) with pulse.build(name="call_xp") as xp_call: pulse.call(xp) with pulse.build(name="call_call_xp") as xp_call_call: pulse.play(pulse.Drag(160, 0.5, 40, 0.2), pulse.DriveChannel(1)) pulse.call(xp_call) self.assertSetEqual(used_in_calls("xp", [xp_call]), {"call_xp"}) self.assertSetEqual(used_in_calls("xp", [xp2]), set()) self.assertSetEqual(used_in_calls("xp", [xp_call, xp_call_call]), {"call_xp", "call_call_xp"}) with pulse.build(name="xp") as xp: pulse.play(pulse.Gaussian(160, 0.5, 40), pulse.DriveChannel(2)) cr_tone_p = pulse.GaussianSquare(640, 0.2, 64, 500) rotary_p = pulse.GaussianSquare(640, 0.1, 64, 500) cr_tone_m = pulse.GaussianSquare(640, -0.2, 64, 500) rotary_m = pulse.GaussianSquare(640, -0.1, 64, 500) with pulse.build(name="cr") as cr: with pulse.align_sequential(): with pulse.align_left(): pulse.play(rotary_p, pulse.DriveChannel(3)) # Rotary tone pulse.play(cr_tone_p, pulse.ControlChannel(2)) # CR tone. pulse.call(xp) with pulse.align_left(): pulse.play(rotary_m, pulse.DriveChannel(3)) pulse.play(cr_tone_m, pulse.ControlChannel(2)) pulse.call(xp) self.assertSetEqual(used_in_calls("xp", [cr]), {"cr"})
def test_complex_build(self): """Test a general program build with nested contexts, circuits and macros.""" d0 = pulse.DriveChannel(0) d1 = pulse.DriveChannel(1) d2 = pulse.DriveChannel(2) delay_dur = 19 short_dur = 31 long_dur = 101 with pulse.build(self.backend) as schedule: with pulse.align_sequential(): pulse.delay(delay_dur, d0) pulse.u2(0, pi/2, 1) with pulse.align_right(): pulse.play(library.Constant(short_dur, 0.1), d1) pulse.play(library.Constant(long_dur, 0.1), d2) pulse.u2(0, pi/2, 1) with pulse.align_left(): pulse.u2(0, pi/2, 0) pulse.u2(0, pi/2, 1) pulse.u2(0, pi/2, 0) pulse.measure(0) # prepare and schedule circuits that will be used. single_u2_qc = circuit.QuantumCircuit(2) single_u2_qc.u2(0, pi/2, 1) single_u2_qc = compiler.transpile(single_u2_qc, self.backend) single_u2_sched = compiler.schedule(single_u2_qc, self.backend) # sequential context sequential_reference = pulse.Schedule() sequential_reference += instructions.Delay(delay_dur, d0) sequential_reference.insert(delay_dur, single_u2_sched, inplace=True) # align right align_right_reference = pulse.Schedule() align_right_reference += pulse.Play( library.Constant(long_dur, 0.1), d2) align_right_reference.insert(long_dur-single_u2_sched.duration, single_u2_sched, inplace=True) align_right_reference.insert( long_dur-single_u2_sched.duration-short_dur, pulse.Play(library.Constant(short_dur, 0.1), d1), inplace=True) # align left triple_u2_qc = circuit.QuantumCircuit(2) triple_u2_qc.u2(0, pi/2, 0) triple_u2_qc.u2(0, pi/2, 1) triple_u2_qc.u2(0, pi/2, 0) triple_u2_qc = compiler.transpile(triple_u2_qc, self.backend) align_left_reference = compiler.schedule( triple_u2_qc, self.backend, method='alap') # measurement measure_reference = macros.measure(qubits=[0], inst_map=self.inst_map, meas_map=self.configuration.meas_map) reference = pulse.Schedule() reference += sequential_reference # Insert so that the long pulse on d2 occurs as early as possible # without an overval on d1. insert_time = (reference.ch_stop_time(d1) - align_right_reference.ch_start_time(d1)) reference.insert(insert_time, align_right_reference, inplace=True) reference.insert(reference.ch_stop_time(d0, d1), align_left_reference, inplace=True) reference += measure_reference self.assertEqual(schedule, reference)