Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing up maxcut notebook #218

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 64 additions & 84 deletions grove/pyqaoa/getting-started/QAOA_overview_maxcut.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"import pyquil.forest as qvm_module\n",
"import numpy as np\n",
"from grove.pyqaoa.maxcut_qaoa import maxcut_qaoa\n",
"barbell = [(0, 1)] # graph is a defined by a list of edges. Edge weights are assumed to be 1.0\n",
Expand All @@ -49,16 +46,15 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"from functools import reduce\n",
"cost_list, ref_list = inst.cost_ham, inst.ref_ham\n",
"cost_ham = reduce(lambda x,y: x + y, cost_list)\n",
"ref_ham = reduce(lambda x,y: x + y, ref_list)\n",
"print cost_ham\n",
"print ref_ham"
"print(cost_ham)\n",
"print(ref_ham)"
]
},
{
Expand Down Expand Up @@ -87,14 +83,12 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"param_prog = inst.get_parameterized_program()\n",
"prog = param_prog([1.2, 4.2])\n",
"print prog"
"print(prog)"
]
},
{
Expand All @@ -107,13 +101,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"betas, gammas = inst.get_angles()\n",
"print betas, gammas"
"print(betas, gammas)"
]
},
{
Expand All @@ -126,18 +118,19 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"from pyquil.api import WavefunctionSimulator\n",
"\n",
"param_prog = inst.get_parameterized_program()\n",
"t = np.hstack((betas, gammas))\n",
"prog = param_prog(t)\n",
"wf, _ = inst.qvm.wavefunction(prog)\n",
"\n",
"wf = WavefunctionSimulator().wavefunction(prog)\n",
"wf = wf.amplitudes\n",
"for ii in range(2**inst.n_qubits):\n",
" print inst.states[ii], np.conj(wf[ii])*wf[ii]"
"for ii in range(2**len(inst.qubits)):\n",
" print(inst.states[ii], np.conj(wf[ii])*wf[ii])"
]
},
{
Expand All @@ -164,9 +157,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
Expand All @@ -176,17 +167,13 @@
"import matplotlib.pyplot as plt\n",
"from pyquil.paulis import PauliSum, PauliTerm\n",
"import pyquil.quil as pq\n",
"from pyquil.gates import H\n",
"import pyquil.forest as qvm_module\n",
"CXN = qvm_module.Connection()"
"from pyquil.gates import H"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# define a 6-qubit ring\n",
Expand All @@ -199,9 +186,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"nx.draw_circular(graph, node_color=\"#6CAFB7\")"
Expand All @@ -224,9 +209,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"cost_operators = []\n",
Expand All @@ -247,9 +230,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"prog = pq.Program()\n",
Expand All @@ -267,21 +248,21 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"ring_cut_inst = QAOA(CXN, len(graph.nodes()), steps=1, ref_ham=driver_operators, cost_ham=cost_operators,\n",
" driver_ref=prog, store_basis=True, rand_seed=42)"
"from pyquil.api import local_forest_runtime\n",
"from pyquil import get_qc, Program\n",
"with local_forest_runtime():\n",
" qvm = get_qc('6q-qvm')\n",
" ring_cut_inst = QAOA(qvm, range(len(graph.nodes())), steps=1, ref_ham=driver_operators, cost_ham=cost_operators,\n",
" driver_ref=prog, store_basis=True, rand_seed=42)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"betas, gammas = ring_cut_inst.get_angles()"
Expand All @@ -297,9 +278,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"from collections import Counter\n",
Expand All @@ -309,15 +288,18 @@
"sampling_prog = param_prog(np.hstack((betas, gammas)))\n",
"\n",
"# use the run_and_measure QVM API to prepare a circuit and then measure on the qubits\n",
"bitstring_samples = CXN.run_and_measure(sampling_prog, range(len(graph.nodes())), 1000)\n",
"bitstring_tuples = map(tuple, bitstring_samples)\n",
"with local_forest_runtime():\n",
" qvm = get_qc('6q-qvm')\n",
" bitstring_samples = qvm.run_and_measure(sampling_prog, 1000)\n",
" shots = np.array([bitstring_samples[qubit] for qubit in sorted(bitstring_samples)]).T\n",
" bitstring_tuples = map(tuple, shots)\n",
"\n",
"# aggregate the statistics\n",
"freq = Counter(bitstring_tuples)\n",
"most_frequent_bit_string = max(freq, key=lambda x: freq[x])\n",
"print freq\n",
"print(freq)\n",
"\n",
"print \"The most frequently sampled string is \", most_frequent_bit_string"
"print(\"The most frequently sampled string is \", most_frequent_bit_string)"
]
},
{
Expand All @@ -330,9 +312,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# plotting strings!\n",
Expand All @@ -345,9 +325,9 @@
" ax.set_xlabel(\"state\",fontsize=20)\n",
" ax.set_ylabel(\"Probability\",fontsize=20)\n",
" ax.set_xlim([0, 2**n_qubits ])\n",
" rec = ax.bar(range(2**n_qubits), probs, )\n",
" num_states = [0, int(\"\".join(str(x) for x in [0,1] * (n_qubits/2)), 2), \n",
" int(\"\".join(str(x) for x in [1,0] * (n_qubits/2)), 2), 2**n_qubits - 1 ]\n",
" rec = ax.bar(range(2**n_qubits),height=list(probs.T[0]))\n",
" num_states = [0, int(\"\".join(str(x) for x in [0,1] * int(n_qubits/2)), 2), \n",
" int(\"\".join(str(x) for x in [1,0] * int(n_qubits/2)), 2), 2**n_qubits - 1 ]\n",
" ax.set_xticks(num_states)\n",
" ax.set_xticklabels(map(lambda x: inst.states[x], num_states), rotation=90)\n",
" plt.grid(True)\n",
Expand All @@ -358,9 +338,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"t = np.hstack((betas, gammas))\n",
Expand All @@ -378,9 +356,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# get the angles from the last run\n",
Expand All @@ -390,8 +366,10 @@
"betas = np.hstack((beta[0]/3, beta[0]*2/3))\n",
"gammas = np.hstack((gamma[0]/3, gamma[0]*2/3))\n",
"# set up a new QAOA instance.\n",
"ring_cut_inst_2 = QAOA(CXN, len(graph.nodes()), steps=2, ref_ham=driver_operators, cost_ham=cost_operators,\n",
" driver_ref=prog, store_basis=True, init_betas=betas, init_gammas=gammas)\n",
"with local_forest_runtime():\n",
" qvm = get_qc('6q-qvm')\n",
" ring_cut_inst_2 = QAOA(qvm, range(len(graph.nodes())), steps=2, ref_ham=driver_operators, cost_ham=cost_operators,\n",
" driver_ref=prog, store_basis=True, init_betas=list(betas), init_gammas=list(gammas))\n",
"# run VQE to determine the optimal angles\n",
"betas, gammas = ring_cut_inst_2.get_angles()\n",
"t = np.hstack((betas, gammas))\n",
Expand All @@ -410,42 +388,44 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"scrolled": true
},
"outputs": [],
"source": [
"from scipy.optimize import fmin_bfgs\n",
"\n",
"ring_cut_inst_3 = QAOA(CXN, len(graph.nodes()), steps=3, ref_ham=driver_operators, cost_ham=cost_operators,\n",
" driver_ref=prog, store_basis=True, minimizer=fmin_bfgs, minimizer_kwargs={'gtol':1.0e-3},\n",
" rand_seed=42)\n",
"betas, gammas = ring_cut_inst_3.get_angles()\n",
"t = np.hstack((betas, gammas))\n",
"probs = ring_cut_inst_3.probabilities(t)\n",
"plot(ring_cut_inst_3, probs)"
"with local_forest_runtime():\n",
" qvm = get_qc('6q-qvm')\n",
" ring_cut_inst_3 = QAOA(qvm, range(len(graph.nodes())), steps=3, ref_ham=driver_operators, cost_ham=cost_operators,\n",
" driver_ref=prog, store_basis=True, minimizer=fmin_bfgs, minimizer_kwargs={'gtol':1.0e-3},\n",
" rand_seed=42)\n",
" betas, gammas = ring_cut_inst_3.get_angles()\n",
" t = np.hstack((betas, gammas))\n",
" probs = ring_cut_inst_3.probabilities(t)\n",
" plot(ring_cut_inst_3, probs)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}