def test_unlinked_parameters(self): """Test the we get schedules with unlinked parameters.""" library = FixedFrequencyTransmon(link_parameters=False) sched_x = library["x"] sched_y = library["y"] sched_sx = library["sx"] sched_sy = library["sy"] # Test the number of parameters. self.assertEqual(len(sched_x.parameters & sched_y.parameters), 2) self.assertEqual(len(sched_sx.parameters & sched_sy.parameters), 2) expected = [ (0.5, "amp", (), "x"), (0.0, "β", (), "x"), (160, "duration", (), "x"), (40, "σ", (), "x"), (160, "duration", (), "sx"), (0.0, "β", (), "sx"), (0.25, "amp", (), "sx"), (40, "σ", (), "sx"), (0.5j, "amp", (), "y"), (0.0, "β", (), "y"), (160, "duration", (), "y"), (40, "σ", (), "y"), (160, "duration", (), "sy"), (0.0, "β", (), "sy"), (0.25j, "amp", (), "sy"), (40, "σ", (), "sy"), ] self.assertSetEqual(set(library.default_values()), set(expected))
def test_turn_off_drag(self): """Test the use_drag parameter.""" library = FixedFrequencyTransmon(use_drag=False) self.assertTrue( isinstance(library["x"].blocks[0].pulse, pulse.Gaussian)) library = FixedFrequencyTransmon() self.assertTrue(isinstance(library["x"].blocks[0].pulse, pulse.Drag))
def test_setup_partial_gates(self): """Check that we do not setup all gates if not required.""" library = FixedFrequencyTransmon(basis_gates=["x", "sy"]) self.assertTrue("x" in library) self.assertTrue("sy" in library) self.assertTrue("y" not in library) self.assertTrue("sx" not in library) with self.assertRaises(CalibrationError): FixedFrequencyTransmon(basis_gates=["x", "bswap"])
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 setUp(self): """Initialize some cals.""" super().setUp() library = FixedFrequencyTransmon() self.cals = Calibrations.from_backend(FakeArmonk(), libraries=[library])
def setUp(self): """Setup the test.""" super().setUp() library = FixedFrequencyTransmon() self.backend = MockIQBackend(FineDragHelper()) self.cals = Calibrations.from_backend(self.backend, 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_instruction_schedule_map_export(self): """Test that exporting the inst map works as planned.""" backend = FakeBelem() cals = BackendCalibrations( backend, library=FixedFrequencyTransmon(basis_gates=["sx"]), ) u_chan = pulse.ControlChannel(Parameter("ch0.1")) with pulse.build(name="cr") as cr: pulse.play(pulse.GaussianSquare(640, 0.5, 64, 384), u_chan) cals.add_schedule(cr, num_qubits=2) cals.update_inst_map({"cr"}) for qubit in range(backend.configuration().num_qubits): self.assertTrue(cals.default_inst_map.has("sx", (qubit,))) # based on coupling map of Belem to keep the test robust. expected_pairs = [(0, 1), (1, 0), (1, 2), (2, 1), (1, 3), (3, 1), (3, 4), (4, 3)] coupling_map = set(tuple(pair) for pair in backend.configuration().coupling_map) for pair in expected_pairs: self.assertTrue(pair in coupling_map) self.assertTrue(cals.default_inst_map.has("cr", pair), pair)
def setUp(self): """Setup the tests""" super().setUp() library = FixedFrequencyTransmon() self.backend = MockFineAmp(-np.pi * 0.07, np.pi, "xp") self.cals = Calibrations.from_backend(self.backend, library)
def setUp(self): """Setup the tests""" super().setUp() library = FixedFrequencyTransmon() self.backend = DragBackend(gate_name="Drag(x)") self.cals = Calibrations.from_backend(self.backend, library) self.test_tol = 0.05
def setUp(self): """Setup the tests""" super().setUp() library = FixedFrequencyTransmon() self.backend = MockIQBackend(DragHelper(gate_name="Drag(x)")) self.cals = Calibrations.from_backend(self.backend, libraries=[library]) self.test_tol = 0.05
def test_standard_single_qubit_gates(self): """Test the setup of single-qubit gates.""" library = FixedFrequencyTransmon(default_values={"duration": 320}) for gate in ["x", "sx"]: sched = library[gate] self.assertTrue(isinstance(sched, pulse.ScheduleBlock)) self.assertEqual(len(sched.parameters), 5) sched_x = library["x"] sched_y = library["y"] sched_sx = library["sx"] sched_sy = library["sy"] self.assertEqual(sched_x.blocks[0].pulse.duration, sched_sx.blocks[0].pulse.duration) self.assertEqual(sched_x.blocks[0].pulse.sigma, sched_sx.blocks[0].pulse.sigma) self.assertEqual(len(sched_x.parameters & sched_y.parameters), 4) self.assertEqual(len(sched_sx.parameters & sched_sy.parameters), 4) expected = [ (0.5, "amp", (), "x"), (0.0, "β", (), "x"), (320, "duration", (), "x"), (80, "σ", (), "x"), (320, "duration", (), "sx"), (0.0, "β", (), "sx"), (0.25, "amp", (), "sx"), (80, "σ", (), "sx"), ] for param_conf in library.default_values(): self.assertTrue(param_conf in expected) # Check that an error gets raise if the gate is not in the library. with self.assertRaises(CalibrationError): print(library["bswap"]) # Test the basis gates of the library. self.assertListEqual(library.basis_gates, ["x", "y", "sx", "sy"])
def setUp(self): """Setup the tests""" super().setUp() library = FixedFrequencyTransmon() self.backend = MockIQBackend(FineAmpHelper(-np.pi * 0.07, np.pi, "xp")) self.backend.configuration().basis_gates.append("sx") self.backend.configuration().basis_gates.append("x") self.cals = Calibrations.from_backend(self.backend, libraries=[library])
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_serialization(self): """Test the serialization of the object.""" lib1 = FixedFrequencyTransmon( basis_gates=["x", "sy"], default_values={"duration": 320}, link_parameters=False, ) lib2 = FixedFrequencyTransmon.from_config(lib1.config()) self.assertEqual(lib2.basis_gates, lib1.basis_gates) # Note: we convert to string since the parameters prevent a direct comparison. self.assertTrue(self._test_library_equivalence(lib1, lib2)) # Test that the extra args are properly accounted for. lib3 = FixedFrequencyTransmon( basis_gates=["x", "sy"], default_values={"duration": 320}, link_parameters=True, ) self.assertFalse(self._test_library_equivalence(lib1, lib3))
def test_json_serialization(self): """Test that the library can be serialized using JSon.""" lib1 = FixedFrequencyTransmon( basis_gates=["x", "sy"], default_values={"duration": 320}, link_parameters=False, ) # Test that serialization fails without the right encoder with self.assertRaises(TypeError): json.dumps(lib1) # Test that serialization works with the proper library lib_data = json.dumps(lib1, cls=ExperimentEncoder) lib2 = json.loads(lib_data, cls=ExperimentDecoder) self.assertTrue(self._test_library_equivalence(lib1, lib2))
def test_inst_map_updates(self): """Test that updating a parameter will force an inst map update.""" cals = BackendCalibrations( FakeBelem(), library=FixedFrequencyTransmon(basis_gates=["sx", "x"]), ) # Test the schedules before the update. for qubit in range(5): for gate, amp in [("x", 0.5), ("sx", 0.25)]: with pulse.build() as expected: pulse.play(pulse.Drag(160, amp, 40, 0), pulse.DriveChannel(qubit)) self.assertEqual(cals.default_inst_map.get(gate, qubit), expected) # Update the duration, this should impact all gates. cals.add_parameter_value(200, "duration", schedule="sx") # Test that all schedules now have an updated duration in the inst_map for qubit in range(5): for gate, amp in [("x", 0.5), ("sx", 0.25)]: with pulse.build() as expected: pulse.play(pulse.Drag(200, amp, 40, 0), pulse.DriveChannel(qubit)) self.assertEqual(cals.default_inst_map.get(gate, qubit), expected) # Update the amp on a single qubit, this should only update one gate in the inst_map cals.add_parameter_value(0.8, "amp", qubits=(4,), schedule="sx") # Test that all schedules now have an updated duration in the inst_map for qubit in range(5): for gate, amp in [("x", 0.5), ("sx", 0.25)]: if gate == "sx" and qubit == 4: amp = 0.8 with pulse.build() as expected: pulse.play(pulse.Drag(200, amp, 40, 0), pulse.DriveChannel(qubit)) self.assertEqual(cals.default_inst_map.get(gate, qubit), expected)
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 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_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)