예제 #1
0
    def test_fenwick_tree_ancestor_children(self):
        """Ancestor children test. Checks get_remainder_set(j) on 8 qubit
        register.

        Tests:    Checks the example given in the paper."""
        f = FenwickTree(16)
        self.assertEqual(f.get_remainder_set(9)[0].index, 7)
예제 #2
0
 def test_parity_set(self):
     """Test getting the parity set."""
     f = FenwickTree(16)
     for j in range(1, 16):
         parity_set_nodes = f.get_parity_set(j)
         parity_set = [node.index for node in parity_set_nodes]
         correct = _parity_set(j - 1)
         self.assertEqual(set(parity_set), set(correct))
         print(parity_set)
예제 #3
0
def bravyi_kitaev(operator, n_qubits=None):
    """Apply the Bravyi-Kitaev transform and return qubit operator.
    (arxiv1701.07072)

    Args:
        operator (openfermion.ops.FermionOperator):
            A FermionOperator to transform.
        n_qubits (int|None):
            Can force the number of qubits in the resulting operator above the
            number that appear in the input operator.

    Returns:
        transformed_operator: An instance of the QubitOperator class.

    Raises:
        ValueError: Invalid number of qubits specified.
    """
    # Compute the number of qubits.
    from openfermion.utils import count_qubits
    if n_qubits is None:
        n_qubits = count_qubits(operator)
    if n_qubits < count_qubits(operator):
        raise ValueError('Invalid number of qubits specified.')

    # Build the Fenwick tree.
    fenwick_tree = FenwickTree(n_qubits)

    # Compute transformed operator.
    transformed_terms = (_transform_operator_term(
        term=term, coefficient=operator.terms[term], fenwick_tree=fenwick_tree)
                         for term in operator.terms)
    return inline_sum(seed=QubitOperator(), summands=transformed_terms)
예제 #4
0
    def test_fenwick_tree_structure(self):
        """A lookup test on 5-qubit fenwick tree.

        Test:    Verifies structure of the Fenwick Tree on 5 sites."""
        f = FenwickTree(5)
        self.assertEqual(f.root.children[0].index, 2)
        self.assertEqual(f.root.children[1].index, 3)
        self.assertEqual(f.root.children[0].children[0].index, 1)
        self.assertEqual(f.root.children[0].children[0].children[0].index, 0)
예제 #5
0
    def test_fenwick_tree_children(self):
        """Children test. Checks get_F(j) on 8 qubit register.

        Test:     Verifies integrity of child nodes of the root."""
        f = FenwickTree(8)
        self.assertEqual(f.get_node(7).children[0], f.get_node(3))
        self.assertEqual(f.get_node(7).children[1], f.get_node(5))
        self.assertEqual(f.get_node(7).children[2], f.get_node(6))
        self.assertEqual(len(f.get_children_set(7)), 3)
def bravyi_kitaev_tree(operator, n_qubits=None):
    """Apply the "tree" Bravyi-Kitaev transform.

    Implementation from arxiv:1701.07072

    Note that this implementation is different from the one described in
    arXiv:quant-ph/0003137. In particular, it gives different results
    when the total number of modes is not a power of 2. The one described
    in arXiv:quant-ph/0003137 is the same as the one described in
    arXiv:1208.5986, and it is implemented in OpenFermion under the name
    `bravyi_kitaev`.

    Args:
        operator (openfermion.ops.FermionOperator):
            A FermionOperator to transform.
        n_qubits (int|None):
            Can force the number of qubits in the resulting operator above the
            number that appear in the input operator.

    Returns:
        transformed_operator: An instance of the QubitOperator class.

    Raises:
        ValueError: Invalid number of qubits specified.
    """
    # Compute the number of qubits.
    from openfermion.utils import count_qubits
    if n_qubits is None:
        n_qubits = count_qubits(operator)
    if n_qubits < count_qubits(operator):
        raise ValueError('Invalid number of qubits specified.')

    # Build the Fenwick tree.
    fenwick_tree = FenwickTree(n_qubits)

    # Compute transformed operator.
    transformed_terms = (
        _transform_operator_term(term=term,
                                 coefficient=operator.terms[term],
                                 fenwick_tree=fenwick_tree)
        for term in operator.terms
    )
    return inline_sum(seed=QubitOperator(), summands=transformed_terms)
예제 #7
0
    def test_fenwick_tree_ancestors(self):
        """Ancestor test. Check validity of the get_update_set(j) method on 8
        qubit register. Note that root is the last node.

        Test:     Verifies integrity of ancestors of nodes within the tree."""
        f = FenwickTree(8)
        self.assertEqual(len(f.get_update_set(7)), 0)

        # Is the parent of the middle child the root?
        self.assertEqual(f.get_update_set(3)[0], f.root)

        # Are the ancestors chained correctly?
        self.assertEqual(f.get_update_set(0)[0], f.get_node(1))
        self.assertEqual(f.get_update_set(0)[1], f.get_node(3))