def test_set_phase_rwa(self): """Test SetPhase command using an RWA approximate solution.""" omega_0 = 5.123 r = 0.01 system_model = self._system_model_1Q(omega_0, r) sched = Schedule() sched += SetPhase(np.pi / 2, DriveChannel(0)) sched += Play(Waveform(np.ones(100)), DriveChannel(0)) sched |= Acquire(1, AcquireChannel(0), MemorySlot(0)) << sched.duration y0 = np.array([1., 1.]) / np.sqrt(2) pulse_sim = PulseSimulator(system_model=system_model, initial_state=y0) qobj = assemble([sched], backend=pulse_sim, meas_level=2, meas_return='single', meas_map=[[0]], qubit_lo_freq=[omega_0], memory_slots=2, shots=1) results = pulse_sim.run(qobj).result() pulse_sim_yf = results.get_statevector() #run independent simulation phases = np.exp( (-1j * 2 * np.pi * omega_0 * np.array([1, -1]) / 2) * 100) approx_yf = phases * (expm(-1j * (np.pi / 2) * self.Y) @ y0) self.assertGreaterEqual(state_fidelity(pulse_sim_yf, approx_yf), 0.99)
def test_filter_inst_types(self): """Test filtering on instruction types.""" lp0 = self.linear(duration=3, slope=0.2, intercept=0.1) sched = Schedule(name="fake_experiment") sched = sched.insert(0, Play(lp0, self.config.drive(0))) sched = sched.insert(10, Play(lp0, self.config.drive(1))) sched = sched.insert(30, ShiftPhase(-1.57, self.config.drive(0))) sched = sched.insert(40, SetFrequency(8.0, self.config.drive(0))) sched = sched.insert(50, ShiftFrequency(4.0e6, self.config.drive(0))) sched = sched.insert(55, SetPhase(3.14, self.config.drive(0))) for i in range(2): sched = sched.insert( 60, Acquire(5, self.config.acquire(i), MemorySlot(i))) sched = sched.insert(90, Play(lp0, self.config.drive(0))) # test on Acquire only_acquire, no_acquire = self._filter_and_test_consistency( sched, instruction_types=[Acquire]) for _, inst in only_acquire.instructions: self.assertIsInstance(inst, Acquire) for _, inst in no_acquire.instructions: self.assertFalse(isinstance(inst, Acquire)) # test two instruction types only_pulse_and_fc, no_pulse_and_fc = self._filter_and_test_consistency( sched, instruction_types=[Play, ShiftPhase]) for _, inst in only_pulse_and_fc.instructions: self.assertIsInstance(inst, (Play, ShiftPhase)) for _, inst in no_pulse_and_fc.instructions: self.assertFalse(isinstance(inst, (Play, ShiftPhase))) self.assertEqual(len(only_pulse_and_fc.instructions), 4) self.assertEqual(len(no_pulse_and_fc.instructions), 5) # test on ShiftPhase only_fc, no_fc = self._filter_and_test_consistency( sched, instruction_types={ShiftPhase}) self.assertEqual(len(only_fc.instructions), 1) self.assertEqual(len(no_fc.instructions), 8) # test on SetPhase only_setp, no_setp = self._filter_and_test_consistency( sched, instruction_types={SetPhase}) self.assertEqual(len(only_setp.instructions), 1) self.assertEqual(len(no_setp.instructions), 8) # test on SetFrequency only_setf, no_setf = self._filter_and_test_consistency( sched, instruction_types=[SetFrequency]) for _, inst in only_setf.instructions: self.assertTrue(isinstance(inst, SetFrequency)) self.assertEqual(len(only_setf.instructions), 1) self.assertEqual(len(no_setf.instructions), 8) # test on ShiftFrequency only_shiftf, no_shiftf = self._filter_and_test_consistency( sched, instruction_types=[ShiftFrequency]) for _, inst in only_shiftf.instructions: self.assertTrue(isinstance(inst, ShiftFrequency)) self.assertEqual(len(only_shiftf.instructions), 1) self.assertEqual(len(no_shiftf.instructions), 8)
def test_set_phase(self): """Test SetPhase command. Similar to the ShiftPhase test but includes a mixing of ShiftPhase and SetPhase instructions to test relative vs absolute changes""" omega_0 = 1.3981 r = 1. system_model = self._system_model_1Q(omega_0, r) # intermix shift and set phase instructions to verify absolute v.s. relative changes sched = Schedule() amp1 = 0.12 sched += Play(Waveform([amp1]), DriveChannel(0)) phi1 = 0.12374 * np.pi sched += ShiftPhase(phi1, DriveChannel(0)) amp2 = 0.492 sched += Play(Waveform([amp2]), DriveChannel(0)) phi2 = 0.5839 * np.pi sched += SetPhase(phi2, DriveChannel(0)) amp3 = 0.12 + 0.21 * 1j sched += Play(Waveform([amp3]), DriveChannel(0)) phi3 = 0.1 * np.pi sched += ShiftPhase(phi3, DriveChannel(0)) amp4 = 0.2 + 0.3 * 1j sched += Play(Waveform([amp4]), DriveChannel(0)) sched |= Acquire(1, AcquireChannel(0), MemorySlot(0)) << sched.duration y0 = np.array([1., 0.]) pulse_sim = PulseSimulator(system_model=system_model, initial_state=y0) qobj = assemble([sched], backend=pulse_sim, meas_level=2, meas_return='single', meas_map=[[0]], qubit_lo_freq=[omega_0], memory_slots=2, shots=1) results = pulse_sim.run(qobj).result() pulse_sim_yf = results.get_statevector() #run independent simulation samples = np.array([[amp1], [amp2 * np.exp(1j * phi1)], [amp3 * np.exp(1j * phi2)], [amp4 * np.exp(1j * (phi2 + phi3))]]) indep_yf = simulate_1q_model(y0, omega_0, r, np.array([omega_0]), samples, 1.) self.assertGreaterEqual(state_fidelity(pulse_sim_yf, indep_yf), 1 - (10**-5))