def test_virtual_keyerror(self): """When asking for an unexistant virtual qubit, KeyError""" layout = Layout() layout[(self.qr, 0)] = 1 with self.assertRaises(KeyError): _ = layout[(self.qr, 1)]
def test_layout_avoid_dangling_virtual(self): """ No dangling pointers for virtual qubits.""" layout = Layout({(self.qr, 0): 0}) self.assertEqual(layout[0], (self.qr, 0)) layout[0] = (self.qr, 1) with self.assertRaises(KeyError): print(layout[(self.qr, 0)])
def test_layout_from_intlist(self): """Create a layout from a list of integers. virtual physical q1_0 -> 4 q2_0 -> 5 q2_1 -> 6 q3_0 -> 8 q3_1 -> 9 q3_2 -> 10 """ qr1 = QuantumRegister(1, 'qr1') qr2 = QuantumRegister(2, 'qr2') qr3 = QuantumRegister(3, 'qr3') intlist_layout = [4, 5, 6, 8, 9, 10] layout = Layout.from_intlist(intlist_layout, qr1, qr2, qr3) expected = Layout({ 4: qr1[0], 5: qr2[0], 6: qr2[1], 8: qr3[0], 9: qr3[1], 10: qr3[2] }) self.assertDictEqual(layout._p2v, expected._p2v) self.assertDictEqual(layout._v2p, expected._v2p)
def test_layout_from_intlist_short(self): """If the intlist is longer that your quantum register, map them to None. virtual physical q1_0 -> 4 q2_0 -> 5 q2_1 -> 6 None -> 8 None -> 9 None -> 10 """ qr1 = QuantumRegister(1, 'qr1') qr2 = QuantumRegister(2, 'qr2') intlist_layout = [4, 5, 6, 8, 9, 10] layout = Layout.from_intlist(intlist_layout, qr1, qr2) expected = Layout({ 4: qr1[0], 5: qr2[0], 6: qr2[1], 8: None, 9: None, 10: None }) self.assertDictEqual(layout._p2v, expected._p2v) self.assertDictEqual(layout._v2p, expected._v2p)
def test_copy(self): """Test copy methods return equivalent layouts.""" layout = Layout() layout.add((self.qr, 0)) layout.add((self.qr, 1)) layout_dict_copy = layout.copy() self.assertTrue(isinstance(layout_dict_copy, Layout)) self.assertDictEqual(layout.get_physical_bits(), layout_dict_copy.get_physical_bits()) self.assertDictEqual(layout.get_virtual_bits(), layout_dict_copy.get_virtual_bits()) layout_copy_copy = copy.copy(layout) self.assertTrue(isinstance(layout_copy_copy, Layout)) self.assertDictEqual(layout.get_physical_bits(), layout_dict_copy.get_physical_bits()) self.assertDictEqual(layout.get_virtual_bits(), layout_dict_copy.get_virtual_bits()) layout_copy_deepcopy = copy.deepcopy(layout) self.assertTrue(isinstance(layout_copy_deepcopy, Layout)) self.assertDictEqual(layout.get_physical_bits(), layout_dict_copy.get_physical_bits()) self.assertDictEqual(layout.get_virtual_bits(), layout_dict_copy.get_virtual_bits())
def test_layout_swap_error(self): """swap() method error""" layout = Layout() layout.add((self.qr, 0)) layout.add((self.qr, 1)) with self.assertRaises(LayoutError): layout.swap(0, (self.qr, 0))
def test_layout_avoid_dangling_physical(self): """ No dangling pointers for physical qubits.""" layout = Layout({(self.qr, 0): 0}) self.assertEqual(layout[0], (self.qr, 0)) layout[(self.qr, 0)] = 1 with self.assertRaises(KeyError): _ = layout[0]
def transpile(circuits, backend=None, basis_gates=None, coupling_map=None, initial_layout=None, seed_mapper=None, pass_manager=None): """transpile one or more circuits. Args: circuits (QuantumCircuit or list[QuantumCircuit]): circuits to compile backend (BaseBackend): a backend to compile for basis_gates (list[str]): list of basis gate names supported by the target. Default: ['u1','u2','u3','cx','id'] coupling_map (list): coupling map (perhaps custom) to target in mapping initial_layout (list): initial layout of qubits in mapping seed_mapper (int): random seed for the swap_mapper pass_manager (PassManager): a pass_manager for the transpiler stages Returns: QuantumCircuit or list[QuantumCircuit]: transpiled circuit(s). Raises: TranspilerError: if args are not complete for the transpiler to function """ return_form_is_single = False if isinstance(circuits, QuantumCircuit): circuits = [circuits] return_form_is_single = True # Check for valid parameters for the experiments. basis_gates = basis_gates or backend.configuration().basis_gates if coupling_map: coupling_map = coupling_map elif backend: # This needs to be removed once Aer 0.2 is out coupling_map = getattr(backend.configuration(), 'coupling_map', None) else: coupling_map = None if not basis_gates: raise TranspilerError('no basis_gates or backend to compile to') # Convert integer list format to Layout if isinstance(initial_layout, list) and \ all(isinstance(elem, int) for elem in initial_layout): if isinstance(circuits, list): circ = circuits[0] else: circ = circuits initial_layout = Layout.generate_from_intlist(initial_layout, *circ.qregs) if initial_layout is not None and not isinstance(initial_layout, Layout): initial_layout = Layout(initial_layout) circuits = parallel_map(_transpilation, circuits, task_kwargs={'basis_gates': basis_gates, 'coupling_map': coupling_map, 'initial_layout': initial_layout, 'seed_mapper': seed_mapper, 'pass_manager': pass_manager}) if return_form_is_single: return circuits[0] return circuits
def test_layout_repr_with_holes(self): """A non-bijective Layout repr reproduces layout""" qr = QuantumRegister(5, 'qr') layout = Layout({qr[0]: 0, qr[1]: 3, qr[2]: 4, qr[3]: 5, qr[4]: 6}) repr_layout = eval(layout.__repr__()) # pylint: disable=eval-used self.assertDictEqual(layout._p2v, repr_layout._p2v) self.assertDictEqual(layout._v2p, repr_layout._v2p)
def test_layout_len_with_idle(self): """Length of the layout is the amount of physical bits""" layout = Layout() self.assertEqual(len(layout), 0) layout.add((self.qr, 2)) self.assertEqual(len(layout), 1) layout.add((self.qr, 1), 3) self.assertEqual(len(layout), 2)
def test_layout_add(self): """add() method""" layout = Layout() layout[(self.qr, 0)] = 0 layout.add((self.qr, 1)) self.assertEqual(layout[(self.qr, 1)], 1) self.assertEqual(layout[1], (self.qr, 1))
def test_layout_get_physical_bits(self): """Get the map from the physical bits view""" layout = Layout({(self.qr, 0): 0, (self.qr, 1): 1, (self.qr, 2): 2}) self.assertDictEqual(layout.get_physical_bits(), { 0: (self.qr, 0), 1: (self.qr, 1), 2: (self.qr, 2) })
def test_layout_repr_with_holes(self): """A non-bijective Layout repr reproduces layout""" qr = QuantumRegister(5, 'qr') layout = Layout([(qr, 0), None, None, (qr, 1), (qr, 2), (qr, 3), (qr, 4), None]) repr_layout = eval(layout.__repr__()) # pylint: disable=eval-used self.assertDictEqual(layout._p2v, repr_layout._p2v) self.assertDictEqual(layout._v2p, repr_layout._v2p)
def test_layout_error_when_same_type(self): """Layout does not work when key and value are the same type""" layout = Layout() with self.assertRaises(LayoutError): layout[(self.qr, 0)] = (self.qr, 1) with self.assertRaises(LayoutError): layout[0] = 1
def test_layout_error_str_key(self): """Layout does not work with strings""" layout = Layout() with self.assertRaises(LayoutError): layout['a_string'] = 3 with self.assertRaises(LayoutError): layout[2] = 'a_string'
def test_layout_add_register(self): """add_register() method""" layout = Layout() layout.add_register(QuantumRegister(2, 'q0')) layout.add_register(QuantumRegister(1, 'qr1')) self.assertEqual(layout[(QuantumRegister(2, 'q0'), 0)], 0) self.assertEqual(layout[(QuantumRegister(2, 'q0'), 1)], 1) self.assertEqual(layout[(QuantumRegister(1, 'qr1'), 0)], 2)
def test_layout_from_dict(self): """Constructor from a dict""" layout = Layout({(self.qr, 0): 0, (self.qr, 1): 1, (self.qr, 2): 2}) self.assertEqual(layout[(self.qr, 0)], 0) self.assertEqual(layout[(self.qr, 1)], 1) self.assertEqual(layout[(self.qr, 2)], 2) self.assertEqual(layout[0], (self.qr, 0)) self.assertEqual(layout[1], (self.qr, 1)) self.assertEqual(layout[2], (self.qr, 2))
def test_set_virtual_without_physical(self): """When adding a virtual without care in which physical is going""" layout = Layout() layout.add((self.qr, 1), 2) layout.add((self.qr, 0)) self.assertDictEqual(layout.get_virtual_bits(), { (self.qr, 0): 1, (self.qr, 1): 2 })
def test_layout_swap(self): """swap() method""" layout = Layout() layout.add((self.qr, 0)) layout.add((self.qr, 1)) layout.swap(0, 1) self.assertDictEqual(layout.get_virtual_bits(), { (self.qr, 0): 1, (self.qr, 1): 0 })
def test_layout_repr(self): """Layout repr reproduces layout""" qr = QuantumRegister(5, 'qr') layout = Layout({ (qr, 0): 2, (qr, 1): 4, (qr, 2): 3, (qr, 3): 0, (qr, 4): 1, }) repr_layout = eval(layout.__repr__()) # pylint: disable=eval-used self.assertDictEqual(layout._p2v, repr_layout._p2v) self.assertDictEqual(layout._v2p, repr_layout._v2p)
def test_layout_from_dict_hole(self): """Constructor from a dict with a hole""" qr0 = QuantumRegister(2) qr1 = QuantumRegister(2) layout = Layout({qr0[0]: 0, qr1[0]: 1, qr1[1]: 3, qr0[1]: 4}) self.assertEqual(layout[qr0[0]], 0) self.assertEqual(layout[qr1[0]], 1) with self.assertRaises(KeyError): _ = layout[None] self.assertEqual(layout[qr1[1]], 3) self.assertEqual(layout[qr0[1]], 4) self.assertEqual(layout[0], qr0[0]) self.assertEqual(layout[1], qr1[0]) self.assertEqual(layout[3], qr1[1]) self.assertEqual(layout[4], qr0[1])
def test_layout_from_list(self): """Constructor from a list""" layout = Layout([(QuantumRegister(2, 'q0'), 0), (QuantumRegister(2, 'qr1'), 0), None, (QuantumRegister(2, 'qr1'), 1), (QuantumRegister(2, 'q0'), 1)]) self.assertEqual(layout[(QuantumRegister(2, 'q0'), 0)], 0) self.assertEqual(layout[(QuantumRegister(2, 'qr1'), 0)], 1) with self.assertRaises(KeyError): _ = layout[None] self.assertEqual(layout[(QuantumRegister(2, 'qr1'), 1)], 3) self.assertEqual(layout[(QuantumRegister(2, 'q0'), 1)], 4) self.assertEqual(layout[0], (QuantumRegister(2, 'q0'), 0)) self.assertEqual(layout[1], (QuantumRegister(2, 'qr1'), 0)) self.assertEqual(layout[2], None) self.assertEqual(layout[3], (QuantumRegister(2, 'qr1'), 1)) self.assertEqual(layout[4], (QuantumRegister(2, 'q0'), 1))
def test_layout_from_tuplelist(self): """Create a Layout from list of tuples virtual physical q1_0 -> 3 q2_0 -> 5 q2_1 -> 7 """ qr1 = QuantumRegister(1, 'qr1') qr2 = QuantumRegister(2, 'qr2') tuplelist_layout = [ None, None, None, qr1[0], None, qr2[0], None, qr2[1] ] layout = Layout.from_tuplelist(tuplelist_layout) expected = Layout({ 3: qr1[0], 5: qr2[0], 7: qr2[1], }) self.assertDictEqual(layout._p2v, expected._p2v) self.assertDictEqual(layout._v2p, expected._v2p)
def test_layout_set(self): """Setter""" layout = Layout() layout[(self.qr, 0)] = 0 self.assertEqual(layout[(self.qr, 0)], 0) self.assertEqual(layout[0], (self.qr, 0))
def test_layout_get_bits(self): """Get the map from the (qu)bits view""" layout_dict = {(self.qr, 0): 0, (self.qr, 1): 1, (self.qr, 2): 2} layout = Layout(layout_dict) self.assertDictEqual(layout_dict, layout.get_virtual_bits())