QAOA Optimization API

class QAOA[source]

QAOA(g, task=None, n_layers=4, constrained=False, backend='local', device_arn=None, s3_folder=None, params=None) :: QuantumOptimizer

quantum-based combinatorial optimization with Amazon Braket using QAOA

Usage:

>>> qo = QAOA(g,
              task='maximum_clique',
              backend='aws', # AWS quantum device
              device_arn='device_arn',
              s3_folder='s3_folder')
    qo.execute()
    results = qo.results()

Parameters:

  • g : a networkx Graph object
  • task : one of {'maximum_clique'}
  • backend : one of {'local', 'aws'}, where
    • local will use braket.local.qubit, a local quantum simulator
    • aws will use braket.aws.qubit, a quantum device managed by Amazon AWS
  • device_arn : Device ARN. Only required if backend != 'local'.
  • s3_folder : S3 folder. Only required if name != 'local'.
  • n_layers : number of layers. default:4
  • constrained : default:False
  • params : user-supplied initialization of params. If None, a default random initialization is used.

QAOA.execute[source]

QAOA.execute(iterations=10, verbose=1)

approximate a solution to given task

QAOA.results[source]

QAOA.results(shots=1024, return_probs=False)

Sample and return results. Returns a tuple of the form: (nodes, probs), where nodes are the list of vertices comprising the most likely solution and probs are the raw solution probabilities.

QAOA.plot_samples[source]

QAOA.plot_samples(probs)

Plot sample for toy problems for testing purposes.

QAOA.supported_tasks[source]

QAOA.supported_tasks()

Print supported tasks (valid values for the task parameter.)

Example: Maximum Clique with QAOA

The task is to find the nodes comprising the maximum clique in a graph. Let's construct a small 6-node Erdos-Renyi graph as input.

import networkx as nx

n_nodes = 6
p = 0.5  # probability of an edge
seed = 1967

g = nx.erdos_renyi_graph(n_nodes, p=p, seed=seed)
positions = nx.spring_layout(g, seed=seed)

nx.draw(g, with_labels=True, pos=positions, node_size=600)
qo = QAOA(g, task='maximum_clique')
qo.execute()
Optimize for 10 iterations...
[========================================] 100%	  cost: -1.8832  iteration:10
<__main__.QAOA at 0x7f80ac213c70>
nodes, probs = qo.results(return_probs=True)
qo.plot_samples(probs)
sub = g.subgraph(nodes)
nx.draw(g, pos=positions, with_labels=True)
nx.draw(sub, pos=positions, node_color="r", edge_color="r")
assert set(nodes) == set([0,2,3,4])