def __init__(self,
                 bitmaps: Union[str, List[str]],
                 optimization: bool = False,
                 mct_mode: str = 'basic'):
        """
        Constructor for Truth Table-based Oracle

        Args:
            bitmaps: A single binary string or a list of binary strings
                representing the desired single- and multi-value truth table.
            optimization: Boolean flag for attempting circuit optimization.
                When set, the Quine-McCluskey algorithm is used to compute the prime
                implicants of the truth table,
                and then its exact cover is computed to try to reduce the circuit.
            mct_mode: The mode to use when constructing multiple-control Toffoli.
        Raises:
            AquaError: invalid input
        """
        if isinstance(bitmaps, str):
            bitmaps = [bitmaps]

        validate_in_set(
            'mct_mode', mct_mode,
            {'basic', 'basic-dirty-ancilla', 'advanced', 'noancilla'})
        super().__init__()

        self._mct_mode = mct_mode.strip().lower()
        self._optimization = optimization

        self._bitmaps = bitmaps

        # check that the input bitmaps length is a power of 2
        if not is_power_of_2(len(bitmaps[0])):
            raise AquaError('Length of any bitmap must be a power of 2.')
        for bitmap in bitmaps[1:]:
            if not len(bitmap) == len(bitmaps[0]):
                raise AquaError('Length of all bitmaps must be the same.')
        self._nbits = int(math.log(len(bitmaps[0]), 2))
        self._num_outputs = len(bitmaps)

        self._lit_to_var = None
        self._var_to_lit = None

        esop_exprs = []
        for bitmap in bitmaps:
            esop_expr = self._get_esop_ast(bitmap)
            esop_exprs.append(esop_expr)

        self._esops = [
            ESOP(esop_expr, num_vars=self._nbits) for esop_expr in esop_exprs
        ] if esop_exprs else None

        self.construct_circuit()
Exemple #2
0
    def __init__(self, bitmaps, optimization='off', mct_mode='basic'):
        """
        Constructor for Truth Table-based Oracle

        Args:
            bitmaps (str or [str]): A single binary string or a list of binary strings representing the desired
                single- and multi-value truth table.
            optimization (str): Optimization mode to use for minimizing the circuit.
                Currently, besides no optimization ('off'), Aqua also supports a 'qm-dlx' mode,
                which uses the Quine-McCluskey algorithm to compute the prime implicants of the truth table,
                and then compute an exact cover to try to reduce the circuit.
            mct_mode (str): The mode to use when constructing multiple-control Toffoli.
        """
        if isinstance(bitmaps, str):
            bitmaps = [bitmaps]

        self.validate(locals())
        super().__init__()

        self._mct_mode = mct_mode
        self._optimization = optimization

        self._bitmaps = bitmaps

        # check that the input bitmaps length is a power of 2
        if not is_power_of_2(len(bitmaps[0])):
            raise AquaError('Length of any bitmap must be a power of 2.')
        for bitmap in bitmaps[1:]:
            if not len(bitmap) == len(bitmaps[0]):
                raise AquaError('Length of all bitmaps must be the same.')
        self._nbits = int(math.log(len(bitmaps[0]), 2))
        self._num_outputs = len(bitmaps)

        esop_exprs = []
        for bitmap in bitmaps:
            esop_expr = self._get_esop_ast(bitmap)
            esop_exprs.append(esop_expr)

        self._esops = [
            ESOP(esop_expr, num_vars=self._nbits) for esop_expr in esop_exprs
        ] if esop_exprs else None

        self.construct_circuit()