Пример #1
0
    def test_edge_driver_errors(self):
        """Tests that the edge driver Hamiltonian throws the correct errors"""

        with pytest.raises(ValueError, match=r"Encountered invalid entry in 'reward', expected 2-bit bitstrings."):
            qaoa.edge_driver(Graph([(0, 1), (1, 2)]), ['10', '11', 21, 'g'])

        with pytest.raises(
                ValueError,
                match=r"'reward' cannot contain either '10' or '01', must contain neither or both."
        ):
            qaoa.edge_driver(Graph([(0, 1), (1, 2)]), ['11', '00', '01'])

        with pytest.raises(ValueError, match=r"Input graph must be a nx.Graph"):
            qaoa.edge_driver([(0, 1), (1, 2)], ['00', '11'])
Пример #2
0
    def test_edge_driver_output(self, graph, reward, hamiltonian):
        """Tests that the edge driver Hamiltonian throws the correct errors"""

        H = qaoa.edge_driver(graph, reward)
        assert decompose_hamiltonian(H) == decompose_hamiltonian(hamiltonian)
Пример #3
0
# one of the core principles behind the entire PennyLane library is customizability, and this principle hold true for
# QAOA submodule as well!
#
# The QAOA workflow above gave us two optimal solutions: :math:`|6\rangle = |0110\rangle`
# and :math:`|10\rangle = |1010\rangle`. What if we add a constraint
# that made one of these solutions "better" than the other? Let's imagine that we are interested in
# solutions that minimize the original cost function,
# *but also colour the first and third vertices* :math:`1`. A constraint of this form will
# favour :math:`|10\rangle`, making it the only true ground state.
#
# It is easy to introduce constraints of this form in PennyLane.
# We can use the :func:`~.pennylane.qaoa.edge_driver` cost
# Hamiltonian to "reward" cases in which the first and last vertices of the graph
# are :math:`0`:

reward_h = qaoa.edge_driver(nx.Graph([(0, 2)]), ['11'])

######################################################################
#
# We then weigh and add the constraining term
# to the original minimum vertex cover Hamiltonian:

new_cost_h = cost_h + 2 * reward_h

######################################################################
#
# Notice that PennyLane allows for simple addition and multiplication of
# Hamiltonian objects using inline arithmetic operations ➕ ➖ ✖️➗! Finally, we can
# use this new cost Hamiltonian to define a new QAOA workflow: