Skip to content

Commit

Permalink
Merge pull request #19 from rigetticomputing/feature/maxcut_takes_sam…
Browse files Browse the repository at this point in the history
…ples

Updated MaxCut to take samples
  • Loading branch information
willzeng committed Apr 12, 2017
2 parents 9e0dd20 + 7d786aa commit 2de2913
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
13 changes: 0 additions & 13 deletions grove/pyqaoa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ pairs and implements the cost function for MAX-CUT problems.
`numberpartiition_qaoa.py` takes a list of numbers and set sup a QAOA instance
for determining the equal biparitioning of the list.

`graphpartion_qaoa.py` contains the `graphpart_qaoa()` method that can be used
to construct the QAOA instance for finding the minimum cut between two equal
sized partitions. Like `maxcut_qaoa`, `graphpart_qaoa` accepts a NetworkX
graph of a list of tuples defining the edges. The method returns a QAOA instance
with the appropriate cost Hamiltonians and driver Hamiltonians instantiated.

`graphpartition_jaynescummingsdriver.py` contains the `graphpart_jc_qaoa()`
method that can be used to construct the minimum cut between equal partition
QAOA instance. In this method the driver Hamiltonian is exchanged for the
XY-Hamiltonian and the penalty term enforcing equal partitions is removed from
the cost Hamiltonian. The returned QAOA instance is an example of using the
XY-Hamiltonian to satisfy linear constraints on the bit strings.

## Run

The simplest way to interact with the QAOA library is through the methods provided for each problem instance. For example, to run max cut import `maxcut_qaoa` from `maxcut_qaoa.py` and pass graph to the script.
Expand Down
50 changes: 42 additions & 8 deletions grove/pyqaoa/maxcut_qaoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,35 @@ def print_fun(x):
print x


def maxcut_qaoa(graph, steps=1, rand_seed=None, connection=None):
def maxcut_qaoa(graph, steps=1, rand_seed=None, connection=None, samples=None,
initial_beta=None, initial_gamma=None, minimizer_kwargs=None,
vqe_option=None):
"""
Max cut set up method
:param graph: Graph definition. Either networkx or list of tuples
:param steps: (Optional. Default=1) Trotterization order for the
QAOA algorithm.
:param rand_seed: (Optional. Default=None) random seed when beta and
gamma angles are not provided.
:param connection: (Optional) connection to the QVM. Default is None.
:param samples: (Optional. Default=None) VQE option. Number of samples
(circuit preparation and measurement) to use in operator
averaging.
:param initial_beta: (Optional. Default=None) Initial guess for beta
parameters.
:param initial_gamma: (Optional. Default=None) Initial guess for gamma
parameters.
:param minimizer_kwargs: (Optional. Default=None). Minimizer optional
arguments. If None set to
{'method': 'Nelder-Mead',
'options': {'ftol': 1.0e-2, 'xtol': 1.0e-2,
'disp': False}
:param vqe_option: (Optional. Default=None). VQE optional
arguments. If None set to
vqe_option = {'disp': print_fun, 'return_all': True,
'samples': samples}
"""
if not isinstance(graph, nx.Graph) and isinstance(graph, list):
maxcut_graph = nx.Graph()
Expand All @@ -49,15 +75,23 @@ def maxcut_qaoa(graph, steps=1, rand_seed=None, connection=None):

if connection is None:
connection = CXN

if minimizer_kwargs is None:
minimizer_kwargs = {'method': 'Nelder-Mead',
'options': {'ftol': 1.0e-2, 'xtol': 1.0e-2,
'disp': False}}
if vqe_option is None:
vqe_option = {'disp': print_fun, 'return_all': True,
'samples': samples}

qaoa_inst = QAOA(connection, len(graph.nodes()), steps=steps, cost_ham=cost_operators,
ref_hamiltonian=driver_operators, store_basis=True,
rand_seed=rand_seed,
init_betas=initial_beta,
init_gammas=initial_gamma,
minimizer=minimize,
minimizer_kwargs={'method': 'Nelder-Mead',
'options': {'ftol': 1.0e-2,
'xtol': 1.0e-2,
'disp': False}},
vqe_options={'disp': print_fun, 'return_all': True})
minimizer_kwargs=minimizer_kwargs,
vqe_options=vqe_option)

return qaoa_inst

Expand All @@ -66,13 +100,13 @@ def maxcut_qaoa(graph, steps=1, rand_seed=None, connection=None):
# Sample Run:
# Cutting 0 - 1 - 2 graph!
inst = maxcut_qaoa([(0, 1), (1, 2)],
steps=2, rand_seed=42)
steps=2, rand_seed=42, samples=None)
betas, gammas = inst.get_angles()
probs = inst.probabilities(np.hstack((betas, gammas)))
for state, prob in zip(inst.states, probs):
print state, prob

print "Most frequent bitstring from sampling"
most_freq_string, sampling_results = inst.get_string(
betas, gammas, samples=100)
betas, gammas)
print most_freq_string
14 changes: 9 additions & 5 deletions grove/pyvqe/vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,22 @@ def expectation(self, quil_prog, pauli_sum, samples, qvm):
for j, term in enumerate(pauli_sum.terms):
meas_basis_change = pq.Program()
qubits_to_measure = []
for index, gate in term:
if gate != 'I':
if term.id() == "":
meas_outcome = 1.0
else:
for index, gate in term:
qubits_to_measure.append(index)
if gate == 'X':
meas_basis_change.inst(RY(-np.pi / 2, index))
elif gate == 'Y':
meas_basis_change.inst(RX(np.pi / 2, index))

meas_outcome = expectation_from_sampling(quil_prog + meas_basis_change,
qubits_to_measure,
qvm, samples)
meas_outcome = expectation_from_sampling(quil_prog + meas_basis_change,
qubits_to_measure,
qvm, samples)

expectation += term.coefficient * meas_outcome

return expectation.real


Expand Down

0 comments on commit 2de2913

Please sign in to comment.