def test_update_calibrations(self): """Test that the calibration version of the experiment updates the cals.""" tol = 1e4 # 10 kHz resolution freq_name = self.cals.__drive_freq_parameter__ # Check qubit frequency before running the cal f01 = self.cals.get_parameter_value(freq_name, 0) self.assertTrue( len(self.cals.parameters_table(parameters=[freq_name])["data"]), 1) self.assertEqual(f01, FakeArmonk().defaults().qubit_freq_est[0]) freq_shift = 4e6 osc_shift = 2e6 # oscillation with 6 MHz backend = MockIQBackend( RamseyXYHelper(freq_shift=freq_shift + osc_shift)) expdata = FrequencyCal(0, self.cals, backend, osc_freq=osc_shift).run() self.assertExperimentDone(expdata) # Check that qubit frequency after running the cal is shifted by freq_shift, i.e. 4 MHz. f01 = self.cals.get_parameter_value(freq_name, 0) self.assertTrue( len(self.cals.parameters_table(parameters=[freq_name])["data"]), 2) self.assertTrue( abs(f01 - (freq_shift + FakeArmonk().defaults().qubit_freq_est[0])) < tol)
def test_update_calibrations(self): """Test that we can properly update an instance of Calibrations.""" freq01 = FakeArmonk().defaults().qubit_freq_est[0] backend = MockIQBackend( experiment_helper=SpectroscopyHelper(freq_offset=5e6, line_width=2e6), iq_cluster_centers=[((-1.0, -1.0), (1.0, 1.0))], iq_cluster_width=[0.2], ) backend._configuration.basis_gates = ["x"] backend._configuration.timing_constraints = {"granularity": 16} backend.defaults().qubit_freq_est = [freq01, freq01] library = FixedFrequencyTransmon(basis_gates=["x", "sx"]) cals = Calibrations.from_backend(FakeArmonk(), libraries=[library]) prev_freq = cals.get_parameter_value(cals.__drive_freq_parameter__, (0,)) self.assertEqual(prev_freq, freq01) frequencies = np.linspace(freq01 - 10.0e6, freq01 + 10.0e6, 21) expdata = RoughFrequencyCal(0, cals, frequencies).run(backend) self.assertExperimentDone(expdata) # Check the updated frequency which should be shifted by 5MHz. post_freq = cals.get_parameter_value(cals.__drive_freq_parameter__, (0,)) self.assertTrue(abs(post_freq - freq01 - 5e6) < 1e6)
def test_qubit_lo_from_configurable_backend(self): """Test computation of qubit_lo_freq from configurable backend.""" backend = FakeArmonk() test_model = PulseSystemModel.from_backend(backend) qubit_lo_from_hamiltonian = test_model.hamiltonian.get_qubit_lo_from_drift() freqs = test_model.calculate_channel_frequencies(qubit_lo_from_hamiltonian) expected = getattr(backend.configuration(), 'hamiltonian')['vars']['wq0'] / (2 * np. pi) self.assertAlmostEqual(freqs['D0'], expected, places=4)
def test_execute_block(self): """Test executing a ScheduleBlock on a Pulse backend""" with pulse.build(name="test_block") as sched_block: pulse.play(pulse.Constant(160, 1.0), pulse.DriveChannel(0)) pulse.acquire(50, pulse.MeasureChannel(0), pulse.MemorySlot(0)) backend = FakeArmonk() test_result = backend.run(sched_block).result() self.assertDictEqual(test_result.get_counts(), {"0": 1024})
def setUp(self): """Initialize some cals.""" super().setUp() library = FixedFrequencyTransmon() self.cals = Calibrations.from_backend(FakeArmonk(), libraries=[library])
def setUp(self): """Setup the tests.""" super().setUp() library = FixedFrequencyTransmon() self.backend = FakeArmonk() self.cals = Calibrations.from_backend(self.backend, library)
def test_experiment_config(self): """Test converting to and from config works""" cals = Calibrations.from_backend(FakeArmonk()) frequencies = [1, 2, 3] exp = RoughFrequencyCal(0, cals, frequencies) loaded_exp = RoughFrequencyCal.from_config(exp.config()) self.assertNotEqual(exp, loaded_exp) self.assertTrue(self.json_equiv(exp, loaded_exp))
def test_qubit_lo_from_configurable_backend(self): backend = FakeArmonk() test_model = PulseSystemModel.from_backend(backend) qubit_lo_from_hamiltonian = test_model.hamiltonian.get_qubit_lo_from_drift( ) freqs = test_model.calculate_channel_frequencies( qubit_lo_from_hamiltonian) self.assertAlmostEqual(freqs['D0'], 4.974286046328553)
def test_circuits(self): """Test the circuits of the experiment.""" drag = FineDrag(0) drag.set_experiment_options(schedule=self.schedule) for circuit in drag.circuits(FakeArmonk())[1:]: for idx, name in enumerate(["Drag", "rz", "Drag", "rz"]): self.assertEqual(circuit.data[idx][0].name, name)
def test_circuits(self): """Test the circuits of the experiment.""" drag = FineDrag(0, Gate("Drag", num_qubits=1, params=[])) drag.set_experiment_options(schedule=self.schedule) drag.backend = FakeArmonk() for circuit in drag.circuits()[1:]: for idx, name in enumerate(["Drag", "rz", "Drag", "rz"]): self.assertEqual(circuit.data[idx][0].name, name)
def setUp(self): """Setup for the test.""" super().setUp() self.inst_map = pulse.InstructionScheduleMap() self.sx_duration = 160 with pulse.build(name="sx") as sx_sched: pulse.play(pulse.Gaussian(self.sx_duration, 0.5, 40), pulse.DriveChannel(0)) self.inst_map.add("sx", 0, sx_sched) self.cals = Calibrations.from_backend(FakeArmonk(), libraries=[FixedFrequencyTransmon()])
def test_inst_map_transpilation(self): """Test that we can use the inst_map to inject the cals into the circuit.""" cals = BackendCalibrations( FakeArmonk(), library=FixedFrequencyTransmon(basis_gates=["x"]), ) param = Parameter("amp") cals.inst_map_add("Rabi", (0,), "x", assign_params={"amp": param}) circ = QuantumCircuit(1) circ.x(0) circ.append(Gate("Rabi", num_qubits=1, params=[param]), (0,)) circs, amps = [], [0.12, 0.25] for amp in amps: new_circ = circ.assign_parameters({param: amp}, inplace=False) circs.append(new_circ) # Check that calibrations are absent for circ in circs: self.assertEqual(len(circ.calibrations), 0) # Transpile to inject the cals. # TODO Enable this test once terra 0.19.0 is live. circs = transpile(circs) # TODO add: inst_map=cals.instruction_schedule_map # Check that we have the expected schedules. with pulse.build() as x_expected: pulse.play(pulse.Drag(160, 0.5, 40, 0), pulse.DriveChannel(0)) for idx, circ in enumerate(circs): amp = amps[idx] with pulse.build() as rabi_expected: pulse.play(pulse.Drag(160, amp, 40, 0), pulse.DriveChannel(0)) self.assertEqual(circ.calibrations["x"][((0,), ())], x_expected) circ_rabi = next(iter(circ.calibrations["Rabi"].values())) self.assertEqual(circ_rabi, rabi_expected) # Test the removal of the Rabi instruction self.assertTrue(cals.default_inst_map.has("Rabi", (0,))) cals.default_inst_map.remove("Rabi", (0,)) self.assertFalse(cals.default_inst_map.has("Rabi", (0,)))
def test_update_calibrations(self): """Test that we can properly update an instance of Calibrations.""" freq01 = FakeArmonk().defaults().qubit_freq_est[0] backend = SpectroscopyBackend(freq_offset=5e6, line_width=2e6) backend.defaults().qubit_freq_est = [freq01, freq01] library = FixedFrequencyTransmon(basis_gates=["x", "sx"]) cals = Calibrations.from_backend(FakeArmonk(), library=library) prev_freq = cals.get_parameter_value(cals.__drive_freq_parameter__, (0, )) self.assertEqual(prev_freq, freq01) frequencies = np.linspace(freq01 - 10.0e6, freq01 + 10.0e6, 21) RoughFrequencyCal(0, cals, frequencies).run(backend).block_for_results() # Check the updated frequency which should be shifted by 5MHz. post_freq = cals.get_parameter_value(cals.__drive_freq_parameter__, (0, )) self.assertTrue(abs(post_freq - freq01 - 5e6) < 1e6)
def test_init(self): """Test that initialization.""" qubit = 1 cals = Calibrations.from_backend(FakeArmonk()) frequencies = [1000, 2000, 3000] auto_update = False absolute = False freq = RoughFrequencyCal( qubit, cals, frequencies, auto_update=auto_update, absolute=absolute ) self.assertEqual(freq.physical_qubits, (qubit,)) self.assertEqual(freq._frequencies, frequencies) self.assertEqual(freq._absolute, False) self.assertEqual(freq.auto_update, False)
def get_qiskit_backend(backend_name): try: from qiskit.test.mock import FakeArmonk, FakeAlmaden, FakeAthens except ImportError: pass '''TODO: might be able to do this in a better way without having to do conditional checks for each backend name.''' if backend_name == 'Armonk': backend = FakeArmonk() elif backend_name == 'Almaden': backend = FakeAlmaden() elif backend_name == 'Athens': backend = FakeAthens() else: '''TODO: There is no FakeCasablanca, so using FakeAlmaden for it right now''' backend = FakeAlmaden() return backend
def test_init(self): """Test that initialization.""" qubit = 1 cals = BackendCalibrations(FakeArmonk()) frequencies = [1, 2, 3] unit = "kHz" auto_update = False absolute = False freq = RoughFrequencyCal(qubit, cals, frequencies, unit, auto_update, absolute) self.assertEqual(freq.physical_qubits, (qubit, )) self.assertEqual(freq._frequencies, [1000, 2000, 3000]) self.assertEqual(freq._absolute, False) self.assertEqual(freq.auto_update, False)
def test_calibration_version(self): """Test the calibration version of the experiment.""" freq_shift = 0.1e6 backend = MockFineFreq(freq_shift, sx_duration=self.sx_duration) fine_freq = FineFrequencyCal(0, self.cals, backend) armonk_freq = FakeArmonk().defaults().qubit_freq_est[0] freq_before = self.cals.get_parameter_value( self.cals.__drive_freq_parameter__, 0) self.assertAlmostEqual(freq_before, armonk_freq) fine_freq.run().block_for_results() freq_after = self.cals.get_parameter_value( self.cals.__drive_freq_parameter__, 0) # Test equality up to 10kHz on a 100 kHz shift self.assertAlmostEqual(freq_after, armonk_freq - freq_shift, delta=1e4)
def test_setup_withLibrary(self): """Test that we can setup with a library.""" cals = BackendCalibrations( FakeArmonk(), library=FixedFrequencyTransmon( basis_gates=["x", "sx"], default_values={"duration": 320} ), ) # Check the x gate with pulse.build(name="x") as expected: pulse.play(pulse.Drag(duration=320, amp=0.5, sigma=80, beta=0), pulse.DriveChannel(0)) self.assertEqual(cals.get_schedule("x", (0,)), expected) # Check the sx gate with pulse.build(name="sx") as expected: pulse.play(pulse.Drag(duration=320, amp=0.25, sigma=80, beta=0), pulse.DriveChannel(0)) self.assertEqual(cals.get_schedule("sx", (0,)), expected)
def test_calibration_version(self): """Test the calibration version of the experiment.""" exp_helper = FineFreqHelper(sx_duration=self.sx_duration, freq_shift=0.1e6) backend = MockIQBackend(exp_helper) exp_helper.dt = backend.configuration().dt fine_freq = FineFrequencyCal(0, self.cals, backend) armonk_freq = FakeArmonk().defaults().qubit_freq_est[0] freq_before = self.cals.get_parameter_value(self.cals.__drive_freq_parameter__, 0) self.assertAlmostEqual(freq_before, armonk_freq) expdata = fine_freq.run() self.assertExperimentDone(expdata) freq_after = self.cals.get_parameter_value(self.cals.__drive_freq_parameter__, 0) # Test equality up to 10kHz on a 100 kHz shift self.assertAlmostEqual(freq_after, armonk_freq + exp_helper.freq_shift, delta=1e4)
def setUp(self): """Setup the tests""" super().setUp() library = FixedFrequencyTransmon() self.backend = FakeArmonk() self.cals = Calibrations.from_backend(self.backend, library) # Add some pulses on the 1-2 transition. d0 = pulse.DriveChannel(0) with pulse.build(name="x12") as x12: with pulse.frequency_offset(-300e6, d0): pulse.play(pulse.Drag(160, Parameter("amp"), 40, 0.0), d0) with pulse.build(name="sx12") as sx12: with pulse.frequency_offset(-300e6, d0): pulse.play(pulse.Drag(160, Parameter("amp"), 40, 0.0), d0) self.cals.add_schedule(x12, 0) self.cals.add_schedule(sx12, 0) self.cals.add_parameter_value(0.4, "amp", 0, "x12") self.cals.add_parameter_value(0.2, "amp", 0, "sx12")
def test_run_options(self): """Test that we can get run options.""" cals = BackendCalibrations(FakeArmonk()) self.assertEqual(cals.get_meas_frequencies(), [6993370669.000001]) self.assertEqual(cals.get_qubit_frequencies(), [4971852852.405576])