Ejemplo n.º 1
0
    def from_qubit_list(qubit_list, *qregs):
        """
        Populates a Layout from a list containing virtual
        qubits, Qubit or None.

        Args:
            qubit_list (list):
                e.g.: [qr[0], None, qr[2], qr[3]]
            *qregs (QuantumRegisters): The quantum registers to apply
                the layout to.
        Returns:
            Layout: the corresponding Layout object
        Raises:
            LayoutError: If the elements are not Qubit or None
        """
        out = Layout()
        for physical, virtual in enumerate(qubit_list):
            if virtual is None:
                continue
            if isinstance(virtual, Qubit):
                if virtual in out._v2p:
                    raise LayoutError(
                        "Duplicate values not permitted; Layout is bijective.")
                out[virtual] = physical
            else:
                raise LayoutError(
                    "The list should contain elements of the Bits or NoneTypes"
                )
        for qreg in qregs:
            out.add_register(qreg)
        return out
Ejemplo n.º 2
0
    def from_qubit_list(qubit_list):
        """
        Populates a Layout from a list containing virtual
        qubits, Qubit or None.

        Args:
            qubit_list (list):
                e.g.: [qr[0], None, qr[2], qr[3]]
        Returns:
            Layout: the corresponding Layout object
        Raises:
            LayoutError: If the elements are not Qubit or None
        """
        out = Layout()
        for physical, virtual in enumerate(qubit_list):
            if virtual is None:
                continue
            elif isinstance(virtual, Qubit):
                if virtual in out._v2p:
                    raise LayoutError(
                        'Duplicate values not permitted; Layout is bijective.')
                out[virtual] = physical
            else:
                raise LayoutError(
                    "The list should contain elements of the Bits or NoneTypes"
                )
        return out
Ejemplo n.º 3
0
    def from_intlist(int_list, *qregs):
        """Converts a list of integers to a Layout
        mapping virtual qubits (index of the list) to
        physical qubits (the list values).

        Args:
            int_list (list): A list of integers.
            *qregs (QuantumRegisters): The quantum registers to apply
                the layout to.
        Returns:
            Layout: The corresponding Layout object.
        Raises:
            LayoutError: Invalid input layout.
        """
        if not all(isinstanceint(i) for i in int_list):
            raise LayoutError('Expected a list of ints')
        if len(int_list) != len(set(int_list)):
            raise LayoutError(
                'Duplicate values not permitted; Layout is bijective.')
        n_qubits = sum(reg.size for reg in qregs)
        # Check if list is too short to cover all qubits
        if len(int_list) < n_qubits:
            err_msg = 'Integer list length must equal number of qubits in circuit.'
            raise LayoutError(err_msg)
        out = Layout()
        main_idx = 0
        for qreg in qregs:
            for idx in range(qreg.size):
                out[qreg[idx]] = int_list[main_idx]
                main_idx += 1
        if main_idx != len(int_list):
            for int_item in int_list[main_idx:]:
                out[int_item] = None
        return out
Ejemplo n.º 4
0
    def from_tuplelist(tuple_list):
        """
        Populates a Layout from a list containing virtual
        qubits---(QuantumRegister, int) tuples---, or None.

        Args:
            tuple_list (list):
                e.g.: [qr[0], None, qr[2], qr[3]]
        Returns:
            Layout: the corresponding Layout object
        Raises:
            LayoutError: If the elements are not (Register, integer) or None
        """
        out = Layout()
        for physical, virtual in enumerate(tuple_list):
            if virtual is None:
                continue
            elif Layout.is_virtual(virtual):
                if virtual in out._v2p:
                    raise LayoutError(
                        'Duplicate values not permitted; Layout is bijective.')
                out[virtual] = physical
            else:
                raise LayoutError(
                    "The list should contain elements of the form"
                    " (Register, integer) or None")
        return out
Ejemplo n.º 5
0
    def combine_into_edge_map(self, another_layout):
        """Combines self and another_layout into an "edge map".

        For example::

              self       another_layout  resulting edge map
           qr_1 -> 0        0 <- q_2         qr_1 -> q_2
           qr_2 -> 2        2 <- q_1         qr_2 -> q_1
           qr_3 -> 3        3 <- q_0         qr_3 -> q_0

        The edge map is used to compose dags via, for example, compose_back.

        Args:
            another_layout (Layout): The other layout to combine.
        Returns:
            dict: A "edge map".
        Raises:
            LayoutError: another_layout can be bigger than self, but not smaller. Otherwise, raises.
        """
        edge_map = dict()

        for virtual, physical in self.get_virtual_bits().items():
            if physical not in another_layout._p2v:
                raise LayoutError(
                    'The wire_map_from_layouts() method does not support when the'
                    ' other layout (another_layout) is smaller.')
            edge_map[virtual] = another_layout[physical]

        return edge_map
Ejemplo n.º 6
0
 def __init__(self, input_dict=None):
     """construct a Layout from a bijective dictionary, mapping
     virtual qubits to physical qubits"""
     self._p2v = {}
     self._v2p = {}
     if input_dict is not None:
         if not isinstance(input_dict, dict):
             raise LayoutError("Layout constructor takes a dict")
         self.from_dict(input_dict)
Ejemplo n.º 7
0
 def __delitem__(self, key):
     if isinstance(key, int):
         del self._p2v[key]
         del self._v2p[self._p2v[key]]
     elif isinstance(key, Qubit):
         del self._v2p[key]
         del self._p2v[self._v2p[key]]
     else:
         raise LayoutError('The key to remove should be of the form'
                           ' Qubit or integer) and %s was provided' % (type(key),))
Ejemplo n.º 8
0
 def order_based_on_type(value1, value2):
     """decides which one is physical/virtual based on the type. Returns (virtual, physical)"""
     if isinstance(value1, int) and isinstance(value2, (Qubit, type(None))):
         physical = value1
         virtual = value2
     elif isinstance(value2, int) and isinstance(value1, (Qubit, type(None))):
         physical = value2
         virtual = value1
     else:
         raise LayoutError('The map (%s -> %s) has to be a (Bit -> integer)'
                           ' or the other way around.' % (type(value1), type(value2)))
     return virtual, physical
Ejemplo n.º 9
0
 def swap(self, left, right):
     """Swaps the map between left and right.
     Args:
         left (tuple or int): Item to swap with right.
         right (tuple or int): Item to swap with left.
     Raises:
         LayoutError: If left and right have not the same type.
     """
     if type(left) is not type(right):
         raise LayoutError('The method swap only works with elements of the same type.')
     temp = self[left]
     self[left] = self[right]
     self[right] = temp
Ejemplo n.º 10
0
 def __delitem__(self, key):
     if isinstance(key, int):
         del self._p2v[key]
         del self._v2p[self._p2v[key]]
     elif isinstance(key, tuple) and \
             len(key) == 2 and \
             isinstance(key[0], Register) and isinstance(key[1], int):
         del self._v2p[key]
         del self._p2v[self._v2p[key]]
     else:
         raise LayoutError(
             'The key to remove should be of the form'
             ' (Register, integer) or integer) and %s was provided' %
             (type(key), ))
Ejemplo n.º 11
0
    def combine_into_edge_map(self, another_layout):
        """Combines self and another_layout into an "edge map".

        For example::

              self       another_layout  resulting edge map
           qr_1 -> 0        0 <- q_2         qr_1 -> q_2
           qr_2 -> 2        2 <- q_1         qr_2 -> q_1
           qr_3 -> 3        3 <- q_0         qr_3 -> q_0

        The edge map is used to compose dags via, for example, compose.

        Args:
            another_layout (Layout): The other layout to combine.
        Returns:
            dict: A "edge map".
        Raises:
            LayoutError: another_layout can be bigger than self, but not smaller.
                Otherwise, raises.
        """
        warnings.warn(
            'combine_into_edge_map is deprecated as of 0.14.0 and '
            'will be removed in a future release. Instead '
            'reorder_bits() should be used',
            DeprecationWarning,
            stacklevel=2)
        edge_map = dict()

        for virtual, physical in self.get_virtual_bits().items():
            if physical not in another_layout._p2v:
                raise LayoutError(
                    'The wire_map_from_layouts() method does not support when the'
                    ' other layout (another_layout) is smaller.')
            edge_map[virtual] = another_layout[physical]

        return edge_map