示例#1
0
    def test_cost_graph_error(self):
        """Tests that the cost Hamiltonians throw the correct error"""

        graph = [(0, 1), (1, 2)]

        with pytest.raises(ValueError, match=r"Input graph must be a nx\.Graph"):
            qaoa.maxcut(graph)
        with pytest.raises(ValueError, match=r"Input graph must be a nx\.Graph"):
            qaoa.max_independent_set(graph)
        with pytest.raises(ValueError, match=r"Input graph must be a nx\.Graph"):
            qaoa.min_vertex_cover(graph)
        with pytest.raises(ValueError, match=r"Input graph must be a nx\.Graph"):
            qaoa.max_clique(graph)
示例#2
0
    def test_mvc_output(self, graph, constrained, cost_hamiltonian, mixer_hamiltonian):
        """Tests that the output of the Min Vertex Cover method is correct"""

        cost_h, mixer_h = qaoa.min_vertex_cover(graph, constrained=constrained)

        assert decompose_hamiltonian(cost_hamiltonian) == decompose_hamiltonian(cost_h)
        assert decompose_hamiltonian(mixer_hamiltonian) == decompose_hamiltonian(mixer_h)
示例#3
0
def benchmark_qaoa(hyperparams={}):
    """
    Performs QAOA optimizations.

    Args:
            hyperparams (dict): hyperparameters to configure this benchmark

                    * 'graph': Graph represented as a NetworkX Graph class

                    * 'n_layers': Number of layers in the QAOA circuit

                    * 'params': Numpy array of trainable parameters that is fed into the circuit

                    * 'device': Device on which the circuit is run

                    * 'interface': Name of the interface to use

                    * 'diff_method': Name of differentiation method
    """

    graph, n_layers, params, device, options_dict = _qaoa_defaults(hyperparams)

    H_cost, H_mixer = qaoa.min_vertex_cover(graph, constrained=False)

    n_wires = len(graph.nodes)

    def qaoa_layer(gamma, alpha):
        qaoa.cost_layer(gamma, H_cost)
        qaoa.mixer_layer(alpha, H_mixer)

    @qml.qnode(device)
    def circuit(params):
        for w in range(n_wires):
            qml.Hadamard(wires=w)
        qml.layer(qaoa_layer, n_layers, params[0], params[1])
        return [qml.sample(qml.PauliZ(i)) for i in range(n_wires)]

    circuit(params)
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import pennylane as qml
import pennylane.qaoa as qaoa

# start with a graph
edges = [(0, 1), (1, 2), (2, 0), (2, 3)]
graph = nx.Graph(edges)

#nx.draw(graph, with_labels=True)
#plt.show()

# cost and mixer is given from pennylane
cost_h, mixer_h = qaoa.min_vertex_cover(graph, constrained=False)
print('Cost Hamiltonian', cost_h)
print('Mixer Hamiltonian', mixer_h)


# QAOA with two parameters
def qaoa_layer(gamma, alpha):
    qaoa.cost_layer(gamma, cost_h)
    qaoa.mixer_layer(alpha, mixer_h)


# full circuit
wires = range(4)
depth = 2


# initial state and QAOA
def benchmark_power(dev_name, s3=None):
    """A substantial QAOA workflow

    Args:
        dev_name (str): Either "local", "sv1", "tn1", or "ionq"
        s3 (tuple):  A tuple of (bucket, prefix) to specify the s3 storage location
    """

    if dev_name == "local":
        n_wires = 15
        device = qml.device("braket.local.qubit", wires=n_wires, shots=None)
    elif dev_name == "sv1":
        n_wires = 15
        device = qml.device(
            "braket.aws.qubit",
            device_arn="arn:aws:braket:::device/quantum-simulator/amazon/sv1",
            s3_destination_folder=s3,
            wires=n_wires,
            shots=None,
        )
    elif dev_name == "tn1":
        shots = 1000
        n_wires = 15
        device = qml.device(
            "braket.aws.qubit",
            device_arn="arn:aws:braket:::device/quantum-simulator/amazon/tn1",
            s3_destination_folder=s3,
            wires=n_wires,
            shots=shots,
        )
    elif dev_name == "ionq":
        shots = 100
        n_wires = 11
        device = qml.device(
            "braket.aws.qubit",
            device_arn="arn:aws:braket:::device/qpu/ionq/ionQdevice",
            s3_destination_folder=s3,
            wires=n_wires,
            shots=shots,
        )
    else:
        raise ValueError("dev_name not 'local', 'sv1','tn1', or 'ionq'")

    n_layers = 1
    graph = nx.complete_graph(n_wires)

    params = 0.5 * pnp.ones((2, n_layers))
    interface = "autograd"
    diff_method = "best"
    n_wires = len(graph.nodes)
    H_cost, H_mixer = qaoa.min_vertex_cover(graph, constrained=False)

    def qaoa_layer(gamma, alpha):
        qaoa.cost_layer(gamma, H_cost)
        qaoa.mixer_layer(alpha, H_mixer)

    @qml.qnode(device, interface=interface, diff_method=diff_method)
    def circuit(params):
        for w in range(n_wires):
            qml.Hadamard(wires=w)
        qml.layer(qaoa_layer, n_layers, params[0], params[1])
        return [qml.sample(qml.PauliZ(i)) for i in range(n_wires)]

    circuit(params)