How to use the openfermion.ops.BinaryPolynomial function in openfermion

To help you get started, we’ve selected a few openfermion examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github quantumlib / OpenFermion / src / openfermion / transforms / _binary_codes.py View on Github external
def _decoder_checksum(modes, odd):
    """ Helper function for checksum_code that outputs the decoder.

    Args:
        modes (int):  number of modes
        odd (int or bool): 1 (True) or 0 (False), if odd,
            we encode all states with odd Hamming weight

    Returns (list): list of BinaryPolynomial
    """
    if odd:
        all_in = BinaryPolynomial('1')
    else:
        all_in = BinaryPolynomial()

    for mode in range(modes - 1):
        all_in += BinaryPolynomial('w' + str(mode))

    djw = linearize_decoder(numpy.identity(modes - 1, dtype=int))
    djw.append(all_in)
    return djw
github quantumlib / OpenFermion / src / openfermion / transforms / _binary_codes.py View on Github external
def _decoder_checksum(modes, odd):
    """ Helper function for checksum_code that outputs the decoder.

    Args:
        modes (int):  number of modes
        odd (int or bool): 1 (True) or 0 (False), if odd,
            we encode all states with odd Hamming weight

    Returns (list): list of BinaryPolynomial
    """
    if odd:
        all_in = BinaryPolynomial('1')
    else:
        all_in = BinaryPolynomial()

    for mode in range(modes - 1):
        all_in += BinaryPolynomial('w' + str(mode))

    djw = linearize_decoder(numpy.identity(modes - 1, dtype=int))
    djw.append(all_in)
    return djw
github quantumlib / OpenFermion / src / openfermion / ops / _binary_code.py View on Github external
self.encoder = scipy.sparse.csc_matrix(encoding)
        self.n_qubits, self.n_modes = numpy.shape(encoding)

        if self.n_modes != len(decoding):
            raise BinaryCodeError(
                'size mismatch, decoder and encoder should have the same'
                ' first dimension')

        decoder_qubits = set()
        self.decoder = []

        for symbolic_binary in decoding:
            if isinstance(symbolic_binary, (tuple, list, str, int,
                                            numpy.int32, numpy.int64)):
                symbolic_binary = BinaryPolynomial(symbolic_binary)
            if isinstance(symbolic_binary, BinaryPolynomial):
                self.decoder.append(symbolic_binary)
                decoder_qubits = decoder_qubits | set(
                    symbolic_binary.enumerate_qubits())
            else:
                raise TypeError(
                    'decoder component provided '
                    'is not a suitable for BinaryPolynomial',
                    symbolic_binary)

        if len(decoder_qubits) != self.n_qubits:
            raise BinaryCodeError(
                'decoder and encoder provided has different number of qubits')

        if max(decoder_qubits) + 1 > self.n_qubits:
            raise BinaryCodeError('decoder is not indexing some qubits. Qubits'
github quantumlib / OpenFermion / src / openfermion / ops / _binary_code.py View on Github external
""" Concatenates two decodings

    Args:
        decoder_1 (iterable): list of BinaryPolynomial
            decoding of the outer code layer
        decoder_2 (iterable): list of BinaryPolynomial
            decoding of the inner code layer

    Returns (list): list of BinaryPolynomial the decoding defined by
        w -> decoder_1( decoder_2(w) )
    """
    doubled_decoder = []
    for entry in decoder_1:
        tmp_sum = 0
        for summand in entry.terms:
            tmp_term = BinaryPolynomial('1')
            for factor in summand:
                if isinstance(factor, (numpy.int32, numpy.int64, int)):
                    tmp_term *= decoder_2[factor]
            tmp_sum = tmp_term + tmp_sum
        doubled_decoder += [tmp_sum]
    return doubled_decoder
github quantumlib / OpenFermion / src / openfermion / transforms / _binary_codes.py View on Github external
Args:
        matrix (np.ndarray or list): list of lists or 2D numpy array
            to derive the decoding function from

    Returns (list): list of BinaryPolynomial
    """
    matrix = numpy.array(list(map(numpy.array, matrix)))
    system_dim, code_dim = numpy.shape(matrix)
    decoder = [] * system_dim
    for row_idx in numpy.arange(system_dim):
        dec_str = ''
        for col_idx in numpy.arange(code_dim):
            if matrix[row_idx, col_idx] == 1:
                dec_str += 'W' + str(col_idx) + ' + '
        dec_str = dec_str.rstrip(' + ')
        decoder.append(BinaryPolynomial(dec_str))
    return decoder
github quantumlib / OpenFermion / src / openfermion / ops / _binary_code.py View on Github external
self.encoder = scipy.sparse.csc_matrix(encoding)
        self.n_qubits, self.n_modes = numpy.shape(encoding)

        if self.n_modes != len(decoding):
            raise BinaryCodeError(
                'size mismatch, decoder and encoder should have the same'
                ' first dimension')

        decoder_qubits = set()
        self.decoder = []

        for symbolic_binary in decoding:
            if isinstance(symbolic_binary, (tuple, list, str, int,
                                            numpy.int32, numpy.int64)):
                symbolic_binary = BinaryPolynomial(symbolic_binary)
            if isinstance(symbolic_binary, BinaryPolynomial):
                self.decoder.append(symbolic_binary)
                decoder_qubits = decoder_qubits | set(
                    symbolic_binary.enumerate_qubits())
            else:
                raise TypeError(
                    'decoder component provided '
                    'is not a suitable for BinaryPolynomial',
                    symbolic_binary)

        if len(decoder_qubits) != self.n_qubits:
            raise BinaryCodeError(
                'decoder and encoder provided has different number of qubits')

        if max(decoder_qubits) + 1 > self.n_qubits:
            raise BinaryCodeError('decoder is not indexing some qubits. Qubits'
                                  'indexed are: {}'.format(decoder_qubits))
github quantumlib / OpenFermion / src / openfermion / transforms / _binary_codes.py View on Github external
""" Helper function to fill in an encoder column/decoder component of a
    certain number.

    Args:
        digits (int): number of digits, which is the qubit number
        address (int): column index, decoder component

    Returns (tuple): encoder column, decoder component
    """
    binary_expression = BinaryPolynomial('1')

    # isolate the binary number and fill up the mismatching digits
    address = bin(address)[2:]
    address = ('0' * (digits - len(address))) + address
    for index in numpy.arange(digits):
        binary_expression *= BinaryPolynomial(
            'w' + str(index) + ' + 1 + ' + address[index])

    return list(map(int, list(address))), binary_expression