Exemplo n.º 1
0
    def __or__(self, qubits):
        """
        Applies permutation to qubits (and synthesizes circuit).

        Args:
            qubits (tuple<Qureg>): Qubits to which the permutation is being
                                   applied.
        """
        try:
            import revkit
        except ImportError:  # pragma: no cover
            raise RuntimeError(
                "The RevKit Python library needs to be installed and in the "
                "PYTHONPATH in order to call this function")

        # convert qubits to flattened list
        qs = BasicGate.make_tuple_of_qureg(qubits)
        qs = sum(qs, [])

        # permutation must have 2*q elements, where q is the number of qubits
        if 2**(len(qs)) != len(self.permutation):
            raise AttributeError(
                "Number of qubits does not fit to the size of the permutation")

        # create reversible truth table from permutation
        revkit.perm(permutation=" ".join(map(str, self.permutation)))

        # create reversible circuit from reversible truth table
        self.kwargs.get("synth", revkit.tbs)()

        # convert reversible circuit to ProjectQ code and execute it
        _exec(revkit.write_projectq(log=True)["contents"], qs)
 def __or__(self, quregs):
     from projectq.meta import Control
     quregs = BasicGate.make_tuple_of_qureg(quregs)
     if sum(len(reg) for reg in quregs) == 0:
         return
     eng = [q for reg in quregs for q in reg][0].engine
     with Control(eng, self._controls):
         self._gate | quregs
Exemplo n.º 3
0
    def __or__(self, qubits):
        """
        Apply control function to qubits (and synthesizes circuit).

        Args:
            qubits (tuple<Qureg>): Qubits to which the control function is
                                   being applied. The first `n` qubits are for
                                   the controls, the last qubit is for the
                                   target qubit.
        """
        try:
            import revkit  # pylint: disable=import-outside-toplevel
        except ImportError as err:  # pragma: no cover
            raise RuntimeError(
                "The RevKit Python library needs to be installed and in the "
                "PYTHONPATH in order to call this function") from err
        # pylint: disable=invalid-name

        # convert qubits to tuple
        qs = []
        for item in BasicGate.make_tuple_of_qureg(qubits):
            qs += item if isinstance(item, list) else [item]

        # function truth table cannot be larger than number of control qubits
        # allow
        if 2**(2**(len(qs) - 1)) <= self.function:
            raise AttributeError(
                "Function truth table exceeds number of control qubits")

        # create truth table from function integer
        hex_length = max(2**(len(qs) - 1) // 4, 1)
        revkit.tt(table="{0:#0{1}x}".format(self.function, hex_length))

        # create reversible circuit from truth table
        self.kwargs.get("synth", revkit.esopbs)()

        # check whether circuit has correct signature
        if revkit.ps(mct=True, silent=True)['qubits'] != len(qs):
            raise RuntimeError(
                "Generated circuit lines does not match provided qubits")

        # convert reversible circuit to ProjectQ code and execute it
        _exec(revkit.to_projectq(mct=True), qs)