def test_multiple_instructions_with_different_parameters(self): """Test adding many instruction with different parameter binding.""" backend = FakeAthens() backend.defaults().instruction_schedule_map.add( "my_gate", (0,), self.my_gate_q0, arguments=["P0"] ) qc = circuit.QuantumCircuit(1) qc.append(circuit.Gate("my_gate", 1, [1.0]), [0]) qc.append(circuit.Gate("my_gate", 1, [2.0]), [0]) qc.append(circuit.Gate("my_gate", 1, [3.0]), [0]) transpiled_qc = transpile(qc, backend, basis_gates=["my_gate"], initial_layout=[0]) my_gate_q0_1_0 = self.my_gate_q0.assign_parameters({self.sched_param: 1.0}, inplace=False) my_gate_q0_2_0 = self.my_gate_q0.assign_parameters({self.sched_param: 2.0}, inplace=False) my_gate_q0_3_0 = self.my_gate_q0.assign_parameters({self.sched_param: 3.0}, inplace=False) ref_calibration = { "my_gate": { ((0,), (1.0,)): my_gate_q0_1_0, ((0,), (2.0,)): my_gate_q0_2_0, ((0,), (3.0,)): my_gate_q0_3_0, } } self.assertDictEqual(transpiled_qc.calibrations, ref_calibration)
def test_transpile_with_instmap(self): """Test providing instruction schedule map.""" instmap = FakeAthens().defaults().instruction_schedule_map instmap.add("sx", (0,), self.custom_sx_q0) instmap.add("sx", (1,), self.custom_sx_q1) # Inst map is renewed backend = FakeAthens() qc = circuit.QuantumCircuit(2) qc.sx(0) qc.x(0) qc.rz(0, 0) qc.sx(1) qc.measure_all() transpiled_qc = transpile(qc, backend, inst_map=instmap, initial_layout=[0, 1]) ref_calibration = { "sx": { ((0,), ()): self.custom_sx_q0, ((1,), ()): self.custom_sx_q1, } } self.assertDictEqual(transpiled_qc.calibrations, ref_calibration)
def test_transpile_with_different_qubit(self): """Test transpile with qubit without custom gate.""" backend = FakeAthens() backend.defaults().instruction_schedule_map.add("sx", (0,), self.custom_sx_q0) qc = circuit.QuantumCircuit(1) qc.sx(0) qc.measure_all() transpiled_qc = transpile(qc, backend, initial_layout=[3]) self.assertDictEqual(transpiled_qc.calibrations, {})
def test_fake_backends_get_kwargs(self): """Fake backends honor kwargs passed.""" backend = FakeAthens() qc = QuantumCircuit(2) qc.x(range(0, 2)) qc.measure_all() trans_qc = transpile(qc, backend) raw_counts = backend.run(trans_qc, shots=1000).result().get_counts() self.assertEqual(sum(raw_counts.values()), 1000)
class TestCalibrationBuilder(QiskitTestCase): """Test the Calibration Builder.""" def setUp(self): super().setUp() self.backend = FakeAthens() self.inst_map = self.backend.defaults().instruction_schedule_map
def test_frequency(self): """Test calibrations update from spectroscopy.""" qubit = 1 peak_offset = 5.0e6 backend = SpectroscopyBackend(line_width=2e6, freq_offset=peak_offset) freq01 = backend.defaults().qubit_freq_est[qubit] frequencies = np.linspace(freq01 - 10.0e6, freq01 + 10.0e6, 21) spec = QubitSpectroscopy(qubit, frequencies) spec.set_run_options(meas_level=MeasLevel.CLASSIFIED) exp_data = spec.run(backend) self.assertExperimentDone(exp_data) result = exp_data.analysis_results(1) value = result.value.n self.assertTrue( freq01 + peak_offset - 2e6 < value < freq01 + peak_offset + 2e6) self.assertEqual(result.quality, "good") # Test the integration with the Calibrations cals = Calibrations.from_backend(FakeAthens()) self.assertNotEqual( cals.get_parameter_value(cals.__drive_freq_parameter__, qubit), value) Frequency.update(cals, exp_data) self.assertEqual( cals.get_parameter_value(cals.__drive_freq_parameter__, qubit), value)
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_frequency(self): """Test calibrations update from spectroscopy.""" qubit = 1 peak_offset = 5.0e6 backend = SpectroscopyBackend(line_width=2e6, freq_offset=peak_offset) freq01 = backend.defaults().qubit_freq_est[qubit] frequencies = np.linspace(freq01 - 10.0e6, freq01 + 10.0e6, 21) / 1e6 spec = QubitSpectroscopy(qubit, frequencies, unit="MHz") spec.set_run_options(meas_level=MeasLevel.CLASSIFIED) exp_data = spec.run(backend) exp_data.block_for_results() result = exp_data.analysis_results(1) value = result.value.value self.assertTrue( freq01 + peak_offset - 2e6 < value < freq01 + peak_offset + 2e6) self.assertEqual(result.quality, "good") # Test the integration with the BackendCalibrations cals = BackendCalibrations(FakeAthens()) self.assertNotEqual(cals.get_qubit_frequencies()[qubit], value) Frequency.update(cals, exp_data) self.assertEqual(cals.get_qubit_frequencies()[qubit], value)
def test_instmap_picklable(self): """Test if instmap can be pickled.""" instmap = FakeAthens().defaults().instruction_schedule_map ser_obj = pickle.dumps(instmap) deser_instmap = pickle.loads(ser_obj) self.assertEqual(instmap, deser_instmap)
def test_two_instmaps_different(self): """Test eq method when two instmaps are not identical.""" instmap1 = FakeAthens().defaults().instruction_schedule_map instmap2 = copy.deepcopy(instmap1) # override one of instruction instmap2.add("sx", (0, ), Schedule()) self.assertNotEqual(instmap1, instmap2)
def test_transpile_with_custom_gate(self): """Test providing non-basis gate.""" backend = FakeAthens() backend.defaults().instruction_schedule_map.add( "my_gate", (0,), self.my_gate_q0, arguments=["P0"] ) backend.defaults().instruction_schedule_map.add( "my_gate", (1,), self.my_gate_q1, arguments=["P0"] ) qc = circuit.QuantumCircuit(2) qc.append(circuit.Gate("my_gate", 1, [1.0]), [0]) qc.append(circuit.Gate("my_gate", 1, [2.0]), [1]) transpiled_qc = transpile(qc, backend, basis_gates=["my_gate"], initial_layout=[0, 1]) my_gate_q0_1_0 = self.my_gate_q0.assign_parameters({self.sched_param: 1.0}, inplace=False) my_gate_q1_2_0 = self.my_gate_q1.assign_parameters({self.sched_param: 2.0}, inplace=False) ref_calibration = { "my_gate": { ((0,), (1.0,)): my_gate_q0_1_0, ((1,), (2.0,)): my_gate_q1_2_0, } } self.assertDictEqual(transpiled_qc.calibrations, ref_calibration)
def test_transpile_with_custom_basis_gate(self): """Test transpile with custom calibrations.""" backend = FakeAthens() backend.defaults().instruction_schedule_map.add("sx", (0,), self.custom_sx_q0) backend.defaults().instruction_schedule_map.add("sx", (1,), self.custom_sx_q1) qc = circuit.QuantumCircuit(2) qc.sx(0) qc.x(0) qc.rz(0, 0) qc.sx(1) qc.measure_all() transpiled_qc = transpile(qc, backend, initial_layout=[0, 1]) ref_calibration = { "sx": { ((0,), ()): self.custom_sx_q0, ((1,), ()): self.custom_sx_q1, } } self.assertDictEqual(transpiled_qc.calibrations, ref_calibration)
def test_transpile_with_multiple_circuits(self): """Test transpile with multiple circuits with custom gate.""" backend = FakeAthens() backend.defaults().instruction_schedule_map.add( "my_gate", (0,), self.my_gate_q0, arguments=["P0"] ) params = [0.0, 1.0, 2.0, 3.0] circs = [] for param in params: qc = circuit.QuantumCircuit(1) qc.append(circuit.Gate("my_gate", 1, [param]), [0]) circs.append(qc) transpiled_qcs = transpile(circs, backend, basis_gates=["my_gate"], initial_layout=[0]) for param, transpiled_qc in zip(params, transpiled_qcs): my_gate_q0_x = self.my_gate_q0.assign_parameters( {self.sched_param: param}, inplace=False ) ref_calibration = {"my_gate": {((0,), (param,)): my_gate_q0_x}} self.assertDictEqual(transpiled_qc.calibrations, ref_calibration)
def test_transpile_with_bare_backend(self): """Test transpile without custom calibrations.""" backend = FakeAthens() qc = circuit.QuantumCircuit(2) qc.sx(0) qc.x(0) qc.rz(0, 0) qc.sx(1) qc.measure_all() transpiled_qc = transpile(qc, backend, initial_layout=[0, 1]) ref_calibration = {} self.assertDictEqual(transpiled_qc.calibrations, ref_calibration)
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_skip_target_basis_equivalences_1(self): """Test that BasisTranslator skips gates in the target_basis - #6085""" qstr = 'OPENQASM 2.0; \ include "qelib1.inc"; \ qreg q[5]; \ cu1(4.1564508) q[2],q[3]; \ ccx q[0],q[1],q[4]; \ rzz(0.48622471) q[0],q[2]; \ ccx q[3],q[4],q[1]; \ ch q[2],q[0]; \ y q[3]; \ cu3(2.5787688,1.3265772,3.1398664) q[1],q[4]; \ id q[0]; \ y q[1]; \ t q[2]; \ t q[3]; \ tdg q[4]; \ cz q[0],q[1]; \ ccx q[2],q[4],q[3]; \ rx(5.976651) q[1]; \ u3(4.8520889,3.6025647,5.7475542) q[2]; \ cswap q[0],q[3],q[4]; \ rz(2.1885681) q[2]; \ cu3(2.7675189,4.5725211,2.9940136) q[0],q[3]; \ cx q[1],q[4];' circ = QuantumCircuit() circ = circ.from_qasm_str(qstr) circ_transpiled = transpile( circ, backend=FakeAthens(), basis_gates=["id", "rz", "sx", "x", "cx"], routing_method="sabre", seed_transpiler=42, ) self.assertEqual(circ_transpiled.count_ops(), { "cx": 91, "rz": 66, "sx": 22 })
def test_frequency(self): """Test calibrations update from spectroscopy.""" qubit = 1 peak_offset = 5.0e6 backend = MockIQBackend( experiment_helper=SpectroscopyHelper(freq_offset=peak_offset), 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} freq01 = backend.defaults().qubit_freq_est[qubit] frequencies = np.linspace(freq01 - 10.0e6, freq01 + 10.0e6, 21) spec = QubitSpectroscopy(qubit, frequencies) spec.set_run_options(meas_level=MeasLevel.CLASSIFIED) exp_data = spec.run(backend) self.assertExperimentDone(exp_data) result = exp_data.analysis_results(1) value = result.value.n self.assertTrue( freq01 + peak_offset - 2e6 < value < freq01 + peak_offset + 2e6) self.assertEqual(result.quality, "good") # Test the integration with the Calibrations cals = Calibrations.from_backend(FakeAthens()) self.assertNotEqual( cals.get_parameter_value(cals.__drive_freq_parameter__, qubit), value) Frequency.update(cals, exp_data) self.assertEqual( cals.get_parameter_value(cals.__drive_freq_parameter__, qubit), value)
from qiskit import IBMQ, execute from qiskit import QuantumCircuit from qiskit.providers.aer import QasmSimulator from qiskit.tools.visualization import plot_histogram from qiskit.test.mock import FakeSantiago from qiskit.test.mock import FakeAthens if __name__ == '__main__': #device_backend = FakeVigo() device_backend = FakeAthens() vigo_simulator = QasmSimulator.from_backend(device_backend) print(isinstance(1, int)) print(tuple([1])) print(device_backend.properties().gate_error(gate="cx", qubits=(0, 1))) print(device_backend.properties().gate_error(gate="cx", qubits=(4, 3))) print(device_backend.properties().gate_error(gate="cx", qubits=(1, 2))) print(device_backend.properties().gate_error(gate="cx", qubits=(2, 3))) print(device_backend.properties().gate_error(gate="u2", qubits=2)) print(device_backend.properties().gate_error(gate="u2", qubits=1)) print(device_backend.properties().gate_error(gate="u2", qubits=0)) print(device_backend.properties().gate_error(gate="u2", qubits=3))
def setUp(self): super().setUp() self.backend = FakeAthens() self.inst_map = self.backend.defaults().instruction_schedule_map
def test_two_instmaps_equal(self): """Test eq method when two instmaps are identical.""" instmap1 = FakeAthens().defaults().instruction_schedule_map instmap2 = copy.deepcopy(instmap1) self.assertEqual(instmap1, instmap2)