fixing the classical optimization, state tomography and superposition

This commit is contained in:
Jay M. Gambetta 2017-06-08 19:15:41 -04:00
parent c2bf2daa6a
commit a0e94a7cbe
5 changed files with 440 additions and 345 deletions

View File

@ -85,6 +85,7 @@ def make_Hamiltonian(n, alpha, beta):
return Hamiltonian
def trial_funtion_optimization(n, m, theta, entangler_map):
"""Trial function for classical optimization problems.
@ -113,3 +114,30 @@ def trial_funtion_optimization(n, m, theta, entangler_map):
for j in range(n):
trial_circuit.measure(q[j], c[j])
return trial_circuit
def trial_funtion_optimization_no_meas(n, m, theta, entangler_map):
"""Trial function for classical optimization problems.
n = number of qubits
m = depth
theta = control vector of size n*m stacked as theta[n*i+j] where j counts
the qubits and i the depth
entangler_map = {0: [2, 1],
1: [2],
3: [2],
4: [2]}
control is the key and values are the target
"""
q = QuantumRegister("q", n)
c = ClassicalRegister("c", n)
trial_circuit = QuantumCircuit(q, c)
trial_circuit.h(q)
for i in range(m):
trial_circuit.barrier(q)
for node in entangler_map:
for j in entangler_map[node]:
trial_circuit.cz(q[node], q[j])
for j in range(n):
trial_circuit.ry(theta[n * i + j], q[j])
return trial_circuit

View File

@ -48,6 +48,7 @@
"\n",
"In this first topic, we break down the tools in this QISKit SDK, and introduce all the different parts to make this useful. Our list of introductory notebooks:\n",
"* [Getting Started with QISKit SDK](sections/tutorial4developer.ipynb) shows how to use the the QISKit SDK tools. \n",
"* Compiling and Running a Quantum Progam [coming soon]\n",
"* Loading and Saving a Quantum Program [coming soon]\n",
"* Visualizing a Quantum State [coming soon]"
]
@ -77,6 +78,7 @@
"\n",
"The third set of notebooks allows you to explore tools for verification of quantum systems with commonly-used techniques such as tomography and randomized benchmarking.\n",
"\n",
"* Quantum coherence studies [coming soon]\n",
"* Quantum state tomography [coming soon]\n",
"* Quantum process tomography [coming soon]\n",
"* Pauli randomized benchmarking [coming soon]\n",
@ -96,7 +98,7 @@
"To fully grasp the possibilities, this set of notebooks shows how you can explore applications of short depth quantum computers.\n",
"\n",
"### Sampling \n",
"* Quantum optimization [coming soon] - illustrates how to use a quantum computer to look at optimization problems. \n",
"* [Quantum optimization](sections/classical_optimization.ipynb) - illustrates how to use a quantum computer to look at optimization problems. \n",
"* Quantum chemistry by variational quantum eigensolver method [coming soon] - illustrates how to use a quantum computer to look at chemistry problems. \n",
"\n",
"### Non Sampling \n",

View File

@ -28,8 +28,41 @@
"source": [
"## Introduction\n",
"\n",
"Many problems in finance and optimization can be cast into classical Ising models. Here we show a method that is very similar to Ref. [Fahri] for finding the ground state of the Ising problem using short depth quantum circuits. \n",
"\n",
"\n",
"Many problems in finance and business are optimization problems. \n",
"\n",
"Optimization or combinatorial optimization means searching for an optimal solution in a finite or countably infinite set of potential solutions. Optimality is defined with respect to some criterion function, which is to be minimized or maximized, which is typically called the cost function. \n",
"\n",
"**Types of optimization problems**\n",
"\n",
"Minimization: cost, distance, length of a traversal, weight, processing time, material, energy consumption, number of objects\n",
"\n",
"Maximization: profit, value, output, return, yield, utility, efficiency, capacity, number of objects. \n",
"\n",
"\n",
"Many optimization problem can be formulated in a generic form\n",
"\n",
"\\begin{equation*}\n",
"\\begin{aligned}\n",
"& \\underset{x}{\\text{minimize}}\n",
"& & c(x) \\\\\n",
"& \\text{subject to}\n",
"& & x \\in S\n",
"\\end{aligned}\n",
"\\end{equation*}\n",
"\n",
"where $x$ is a binary variable $0/1$, $c(x0$ is the cost function, and $S$ is the set of feasible solutions.\n",
"\n",
"**Mapping this to quantum problem**\n",
"\n",
"The total cost is a sum of the problem and the constraint. \n",
"\n",
"$$ C = - A c(x) - B (b-S x)\\cdot (b-Sx) $$\n",
"\n",
"where $A$ and $B$ are real positive input paramters that are chosen to weight the constraints to the problem.\n",
"\n",
"To map this to qubits $x\\rightarrow(z+1)/2$\n",
"Here we propose that we map the cost function of the problem to a Hamiltonian of the form\n",
"\n",
"$$ H_\\mathrm{Ising} = \\alpha_i Z_i + \\beta_{i,j} Z_jZ_i,$$\n",
@ -100,10 +133,91 @@
"from qiskit.basicplotter import plot_histogram\n",
"\n",
"# import optimization tools\n",
"from tools.optimizationtools import trial_funtion_optimization\n",
"from tools.optimizationtools import trial_funtion_optimization, trial_funtion_optimization_no_meas\n",
"from tools.optimizationtools import cost_function, make_Hamiltonian"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def SPSA_Minimization(Q_program, alpha, beta, n, device, shots, entangler_map, SPSA_parameters, max_trials, max_depth):\n",
" \n",
" # Initial controls and we do three different depths at once \n",
" theta = [] \n",
" for p in range(max_depth):\n",
" theta.append(np.random.randn((p+1)*n))\n",
"\n",
" cost_save = []\n",
"\n",
" for k in range(max_trials):\n",
" \n",
" \n",
" #SPSA Paramaters\n",
" a_spsa = float(SPSA_parameters[0])/np.power(k+1+SPSA_parameters[2], SPSA_parameters[1])\n",
" c_spsa = float(SPSA_parameters[3])/np.power(k+1, SPSA_parameters[4])\n",
"\n",
" circuits = [\"trial_circuit_plus_depth_1\", \"trial_circuit_minus_depth_1\",\"trial_circuit_plus_depth_2\", \"trial_circuit_minus_depth_2\",\"trial_circuit_plus_depth_3\", \"trial_circuit_minus_depth_3\"]\n",
" Delta = []\n",
" theta_plus = []\n",
" theta_minus = []\n",
" trial_circuits = []\n",
" for p in range(max_depth):\n",
" m = p + 1\n",
" Delta.append(2*np.random.randint(2,size = n*(m)) - 1) # \\pm 1 random distribution \n",
" theta_plus.append(theta[p] + c_spsa*Delta[p])\n",
" theta_minus.append(theta[p] - c_spsa*Delta[p])\n",
" trial_circuits.append(trial_funtion_optimization(n,m,theta_plus[p],entangler_map)) \n",
" trial_circuits.append(trial_funtion_optimization(n,m,theta_minus[p],entangler_map))\n",
" Q_program.add_circuit(circuits[2*p], trial_circuits[2*p])\n",
" Q_program.add_circuit(circuits[2*p+1], trial_circuits[2*p+1]) \n",
" \n",
" #running the program on the hardware\n",
" Q_program.execute(circuits, device, shots)\n",
" \n",
" #estimating the next controls \n",
" cost_temp = [] \n",
" for p in range(max_depth):\n",
" cost_plus = cost_function(Q_program.get_counts(circuits[2*p]),n,alpha,beta)\n",
" cost_minus = cost_function(Q_program.get_counts(circuits[2*p+1]),n,alpha,beta)\n",
" g_spsa = (cost_plus-cost_minus)*Delta[p]/(2.0*c_spsa)\n",
" theta[p] = theta[p] - a_spsa*g_spsa\n",
" cost_temp.append((cost_plus+cost_minus)/2)\n",
" cost_save.append(cost_temp)\n",
" \n",
" if k % 10 == 0:\n",
" print('trial ' + str(k) + \" of \" + str(max_trials) + \" cost \" + str((cost_plus+cost_minus)/2))\n",
"\n",
" #finally run\n",
" trial_circuits = [] \n",
" circuits = [\"trial_circuit_depth_1\", \"trial_circuit_depth_2\",\"trial_circuit_depth_3\"]\n",
" for p in range(max_depth):\n",
" trial_circuits.append(trial_funtion_optimization(n,p+1,theta[p],entangler_map))\n",
" Q_program.add_circuit(circuits[p], trial_circuits[p])\n",
"\n",
" #execuation \n",
" Q_program.execute(circuits, device, shots)\n",
" cost = [] \n",
" data_save = []\n",
" \n",
" \n",
" for i in range(len(cost_save)):\n",
" # iterate through columns\n",
" for j in range(len(cost_save[0])):\n",
" cost_save_transpose[j][i] = cost_save[i][j]\n",
" \n",
" for p in range(max_depth):\n",
" cost.append(cost_function(Q_program.get_counts(circuits[p]),n,alpha,beta))\n",
" data_save.append(Q_program.get_counts(circuits[p]))\n",
" \n",
" return cost, data_save, cost_save_transpose\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -116,7 +230,10 @@
"metadata": {},
"source": [
"* [Max Cut](#sectionmaxcut)\n",
" * [Two Qubits](#sectionmaxcuttwo)\n"
" * [Two Qubits](#sectionmaxcuttwo)\n",
" * [Three Qubits](#sectionmaxcutthree)\n",
" * [Four Qubits](#sectionmaxcutfour)\n",
" * [Ten Qubits](#sectionmaxcutten)"
]
},
{
@ -130,20 +247,32 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Two Qubits<a id='sectionmaxcuttwo'></a>"
"Given a finite graph, color the vertices blue (X) and red (0) such that the number of edges between a blue vertex and a red vertex is as large as possible. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Two Qubits<a id='sectionmaxcuttwo'></a>\n",
"\n",
" Graph: Q --- Q\n",
" \n",
"The ground state is degenerate and is either $|01\\rangle$ (X---0) and $|10\\rangle$ (0-X)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# cost function H = alpha_i z_i + beta_ij z_i z_j\n",
"n=2 # number of qubits\n",
"# number of qubits\n",
"n=2 \n",
"\n",
"# cost function H = alpha_i z_i + beta_ij z_i z_j\n",
"alpha = np.zeros(n)\n",
"beta = np.zeros((n, n))\n",
"beta[0, 1] = 1"
@ -151,7 +280,32 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Setting up a quantum program and connecting to the Quantum Experience API\n",
"Q_program = QuantumProgram()\n",
"# set the APIToken and API url\n",
"Q_program.set_api(Qconfig.APItoken, Qconfig.config[\"url\"])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
@ -173,9 +327,11 @@
}
],
"source": [
"#Making the Hamiltonian in its full form and getting the lowest eigenvalue and egienvector\n",
"#Making the Hamiltonian in its full form\n",
"H = make_Hamiltonian(n,alpha,beta)\n",
"print(H)\n",
"\n",
"#geting the lowest eigenvalue and egienvector\n",
"w, v = la.eigh(H, eigvals=(0, 0))\n",
"print(\"Eigenvalue is %f and the state is\" %(w))\n",
"print(v)"
@ -183,7 +339,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {
"collapsed": false
},
@ -192,66 +348,33 @@
"name": "stdout",
"output_type": "stream",
"text": [
">> quantum_registers created: q 2\n",
">> classical_registers created: c 2\n",
"running on backend: local_qasm_simulator\n"
"trial 0 of 100 cost 0.0849609375\n",
"trial 10 of 100 cost -0.9951171875\n",
"trial 20 of 100 cost -0.982543945312\n",
"trial 30 of 100 cost -0.986938476562\n",
"trial 40 of 100 cost -0.996459960938\n",
"trial 50 of 100 cost -0.98779296875\n",
"trial 60 of 100 cost -0.996704101562\n",
"trial 70 of 100 cost -0.9814453125\n",
"trial 80 of 100 cost -0.999267578125\n",
"trial 90 of 100 cost -0.972290039062\n"
]
},
{
"data": {
"text/plain": [
"-1.0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# making a classical trial function for the problem \n",
"device = 'local_qasm_simulator' # the device to run on\n",
"shots = 1024 # the number of shots in the experiment. \n",
"\n",
"Q_program = QuantumProgram()\n",
"Q_program.set_api(Qconfig.APItoken, Qconfig.config[\"url\"]) # set the APIToken and API url\n",
"\n",
"# Creating registers\n",
"q = Q_program.create_quantum_registers(\"q\", n)\n",
"c = Q_program.create_classical_registers(\"c\", n)\n",
"\n",
"# Quantum circuit \n",
"trial_circuit = Q_program.create_circuit(\"trial_exact\", [\"q\"], [\"c\"])\n",
"trial_circuit.x(q[1])\n",
"\n",
"for j in range (n):\n",
" trial_circuit.measure(q[j], c[j])\n",
"\n",
"circuits = [\"trial_exact\"]\n",
"Q_program.execute(circuits, device, shots)\n",
"\n",
"# calculating the cost for this trial function\n",
"cost_function(Q_program.get_counts(\"trial_exact\"),n,alpha,beta)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"trial 0 of 100\n",
"running on backend: local_qasm_simulator\n"
"ename": "IndexError",
"evalue": "list assignment index out of range",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-8-c5a8a930d9e2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mmax_depth\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mcost\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata_save\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcost_save\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSPSA_Minimization\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mQ_program\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0malpha\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbeta\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdevice\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshots\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mentangler_map\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mSPSA_parameters\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_trials\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_depth\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'm = 1 '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcost\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m' m = 2 '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcost\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'm = 3 '\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcost\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-7-b47d5fb16f0e>\u001b[0m in \u001b[0;36mSPSA_Minimization\u001b[0;34m(Q_program, alpha, beta, n, device, shots, entangler_map, SPSA_parameters, max_trials, max_depth)\u001b[0m\n\u001b[1;32m 62\u001b[0m \u001b[0;31m# iterate through columns\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 63\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mj\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcost_save\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 64\u001b[0;31m \u001b[0mcost_save_transpose\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcost_save\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mj\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 65\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mp\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmax_depth\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mIndexError\u001b[0m: list assignment index out of range"
]
}
],
"source": [
"# quantum circuit parameters\n",
"# Quantum circuit parameters\n",
"device = 'local_qasm_simulator' # the device to run on\n",
"shots = 8192 # the number of shots in the experiment. \n",
"entangler_map = {0: [1]} # the map of two-qubit gates with control at key and target at values\n",
@ -259,81 +382,11 @@
"# Numerical parameters \n",
"SPSA_parameters = np.array([.3,0.602,0,.1,0.101]) #[a, alpha, A, c, gamma]\n",
"max_trials = 100;\n",
"theta_depth_1 = np.random.randn(1*n) # initial controls \n",
"theta_depth_2 = np.random.randn(2*n) # initial controls \n",
"theta_depth_3 = np.random.randn(3*n) # initial controls \n",
"max_depth = 3\n",
"\n",
"cost_plus_depth_1=np.zeros(max_trials)\n",
"cost_minus_depth_1=np.zeros(max_trials)\n",
"cost_plus_depth_2=np.zeros(max_trials)\n",
"cost_minus_depth_2=np.zeros(max_trials)\n",
"cost_plus_depth_3=np.zeros(max_trials)\n",
"cost_minus_depth_3=np.zeros(max_trials)\n",
"cost, data_save, cost_save = SPSA_Minimization(Q_program, alpha, beta, n, device, shots, entangler_map, SPSA_parameters, max_trials, max_depth)\n",
"\n",
"for k in range(max_trials):\n",
" if k % 10 == 0:\n",
" print('trial ' + str(k) + \" of \" + str(max_trials))\n",
" a_spsa = float(SPSA_parameters[0])/np.power(k+1+SPSA_parameters[2], SPSA_parameters[1])\n",
" c_spsa = float(SPSA_parameters[3])/np.power(k+1, SPSA_parameters[4])\n",
"\n",
" Delta_depth_1 = 2*np.random.randint(2,size=n*1)-1 # \\pm 1 random distribution \n",
" Delta_depth_2 = 2*np.random.randint(2,size=n*2)-1 # \\pm 1 random distribution \n",
" Delta_depth_3 = 2*np.random.randint(2,size=n*3)-1 # \\pm 1 random distribution \n",
" \n",
" theta_plus_depth_1 = theta_depth_1 + c_spsa*Delta_depth_1\n",
" theta_minus_depth_1 = theta_depth_1 - c_spsa*Delta_depth_1\n",
" theta_plus_depth_2 = theta_depth_2 + c_spsa*Delta_depth_2\n",
" theta_minus_depth_2 = theta_depth_2 - c_spsa*Delta_depth_2\n",
" theta_plus_depth_3 = theta_depth_3 + c_spsa*Delta_depth_3\n",
" theta_minus_depth_3 = theta_depth_3 - c_spsa*Delta_depth_3\n",
"\n",
" trial_circuit_plus_depth_1 = trial_funtion_optimization(n,1,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_1 = trial_funtion_optimization(n,1,theta_minus_depth_1,entangler_map) \n",
" trial_circuit_plus_depth_2 = trial_funtion_optimization(n,2,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_2 = trial_funtion_optimization(n,2,theta_minus_depth_2,entangler_map)\n",
" trial_circuit_plus_depth_3 = trial_funtion_optimization(n,3,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_3 = trial_funtion_optimization(n,3,theta_minus_depth_3,entangler_map)\n",
" \n",
" Q_program.add_circuit(\"trial_circuit_plus_depth_1\", trial_circuit_plus_depth_1)\n",
" Q_program.add_circuit(\"trial_circuit_minus_depth_1\", trial_circuit_minus_depth_1)\n",
" Q_program.add_circuit(\"trial_circuit_plus_depth_2\", trial_circuit_plus_depth_2)\n",
" Q_program.add_circuit(\"trial_circuit_minus_depth_2\", trial_circuit_minus_depth_2)\n",
" Q_program.add_circuit(\"trial_circuit_plus_depth_3\", trial_circuit_plus_depth_3)\n",
" Q_program.add_circuit(\"trial_circuit_minus_depth_3\", trial_circuit_minus_depth_3)\n",
" \n",
" circuits = [\"trial_circuit_plus_depth_1\", \"trial_circuit_minus_depth_1\",\"trial_circuit_plus_depth_2\", \"trial_circuit_minus_depth_2\",\"trial_circuit_plus_depth_3\", \"trial_circuit_minus_depth_3\"]\n",
" Q_program.execute(circuits, device, shots)\n",
" \n",
" cost_plus_depth_1[k] = cost_function(Q_program.get_counts(\"trial_circuit_plus_depth_1\"),n,alpha,beta)\n",
" cost_minus_depth_1[k] = cost_function(Q_program.get_counts(\"trial_circuit_minus_depth_1\"),n,alpha,beta)\n",
" cost_plus_depth_2[k] = cost_function(Q_program.get_counts(\"trial_circuit_plus_depth_2\"),n,alpha,beta)\n",
" cost_minus_depth_2[k] = cost_function(Q_program.get_counts(\"trial_circuit_minus_depth_2\"),n,alpha,beta)\n",
" cost_plus_depth_3[k] = cost_function(Q_program.get_counts(\"trial_circuit_plus_depth_3\"),n,alpha,beta)\n",
" cost_minus_depth_3[k] = cost_function(Q_program.get_counts(\"trial_circuit_minus_depth_3\"),n,alpha,beta)\n",
" \n",
" g_spsa_depth_1 = (cost_plus_depth_1[k]-cost_minus_depth_1[k])*Delta_depth_1/(2.0*c_spsa)\n",
" g_spsa_depth_2 = (cost_plus_depth_2[k]-cost_minus_depth_2[k])*Delta_depth_2/(2.0*c_spsa)\n",
" g_spsa_depth_3 = (cost_plus_depth_3[k]-cost_minus_depth_3[k])*Delta_depth_3/(2.0*c_spsa)\n",
"\n",
" theta_depth_1 = theta_depth_1 - a_spsa*g_spsa_depth_1\n",
" theta_depth_2 = theta_depth_2 - a_spsa*g_spsa_depth_2\n",
" theta_depth_3 = theta_depth_3 - a_spsa*g_spsa_depth_3\n",
"\n",
"trial_circuit_depth_1 = trial_funtion_optimization(n,1,theta_depth_1,entangler_map) \n",
"trial_circuit_depth_2 = trial_funtion_optimization(n,2,theta_depth_2,entangler_map) \n",
"trial_circuit_depth_3 = trial_funtion_optimization(n,3,theta_depth_3,entangler_map) \n",
"\n",
"Q_program.add_circuit(\"trial_circuit_depth_1\", trial_circuit_depth_1)\n",
"Q_program.add_circuit(\"trial_circuit_depth_2\", trial_circuit_depth_2)\n",
"Q_program.add_circuit(\"trial_circuit_depth_3\", trial_circuit_depth_3)\n",
"\n",
"circuits = [\"trial_circuit_depth_1\", \"trial_circuit_depth_2\",\"trial_circuit_depth_3\"]\n",
"Q_program.execute(circuits, device, shots)\n",
"\n",
"cost_depth_1 = cost_function(Q_program.get_counts(\"trial_circuit_depth_1\"),n,alpha,beta)\n",
"cost_depth_2 = cost_function(Q_program.get_counts(\"trial_circuit_depth_2\"),n,alpha,beta)\n",
"cost_depth_3 = cost_function(Q_program.get_counts(\"trial_circuit_depth_3\"),n,alpha,beta)\n",
"print('m=1 ' + str(cost_depth_1) + ' m=2 ' + str(cost_depth_2) + 'm=3 ' + str(cost_depth_3))"
"print('m = 1 ' + str(cost[0]) + ' m = 2 ' + str(cost[1]) + 'm = 3 ' + str(cost[2]))"
]
},
{
@ -345,19 +398,28 @@
"outputs": [],
"source": [
"# plotting data\n",
"plt.plot(range(max_trials), cost_plus_depth_1, range(max_trials), cost_minus_depth_1)\n",
"plot_histogram(Q_program.get_counts(\"trial_circuit_depth_1\"))\n",
"plt.plot(range(max_trials), cost_plus_depth_2, range(max_trials), cost_minus_depth_2)\n",
"plot_histogram(Q_program.get_counts(\"trial_circuit_depth_1\"))\n",
"plt.plot(range(max_trials), cost_plus_depth_3, range(max_trials), cost_minus_depth_3)\n",
"plot_histogram(Q_program.get_counts(\"trial_circuit_depth_1\"))"
"plt.plot(range(max_trials), cost_save[0])\n",
"plot_histogram(data_save[0],3)\n",
"\n",
"plt.plot(range(max_trials), cost_save[1])\n",
"plot_histogram(data_save[1])\n",
"\n",
"plt.plot(range(max_trials), cost_save[2])\n",
"plot_histogram(data_save[2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Three Qubits<a id='sectionmaxcutthree'></a>"
"### Three Qubits<a id='sectionmaxcutthree'></a>\n",
"\n",
" Graph: \n",
" X \n",
" / \\\n",
" X --- X\n",
" \n",
"The ground state is degenerate and is either $|100\\rangle$, $|010\\rangle$ and $|001\\rangle$."
]
},
{
@ -368,8 +430,9 @@
},
"outputs": [],
"source": [
"# cost function H = alpha_i z_i + beta_ij z_i z_j\n",
"n = 3 \n",
"\n",
"# cost function H = alpha_i z_i + beta_ij z_i z_j\n",
"alpha = np.zeros(n)\n",
"beta = np.zeros((n, n))\n",
"beta[0, 1] = 1\n",
@ -385,9 +448,25 @@
},
"outputs": [],
"source": [
"#Making the Hamiltonian in its full form and getting the lowest eigenvalue and egienvector\n",
"#Setting up a quantum program and connecting to the Quantum Experience API\n",
"Q_program = QuantumProgram()\n",
"# set the APIToken and API url\n",
"Q_program.set_api(Qconfig.APItoken, Qconfig.config[\"url\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Making the Hamiltonian in its full form\n",
"H = make_Hamiltonian(n,alpha,beta)\n",
"print(H)\n",
"\n",
"#geting the lowest eigenvalue and egienvector\n",
"w, v = la.eigh(H, eigvals=(0, 0))\n",
"print(\"Eigenvalue is %f and the state is\" %(w))\n",
"print(v)"
@ -397,33 +476,130 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
"# making a classical trial function for the problem \n",
"# Quantum circuit parameters\n",
"device = 'local_qasm_simulator' # the device to run on\n",
"shots = 1024 # the number of shots in the experiment. \n",
"shots = 8192 # the number of shots in the experiment. \n",
"entangler_map = {0: [1], 1: [2]} # the map of two-qubit gates with control at key and target at values\n",
"\n",
"Q_program = QuantumProgram()\n",
"Q_program.set_api(Qconfig.APItoken, Qconfig.config[\"url\"]) # set the APIToken and API url\n",
"# Numerical parameters \n",
"SPSA_parameters = np.array([.3,0.602,0,.1,0.101]) #[a, alpha, A, c, gamma]\n",
"max_trials = 100;\n",
"max_depth = 3\n",
"\n",
"# Creating registers\n",
"q = Q_program.create_quantum_registers(\"q\", n)\n",
"c = Q_program.create_classical_registers(\"c\", n)\n",
"cost, data_dave = SPSA_Minimization(Q_program, alpha, beta, n, device, shots, entangler_map, SPSA_parameters, max_trials, max_depth)\n",
"\n",
"# Quantum circuit \n",
"trial_circuit = Q_program.create_circuit(\"trial_exact\", [\"q\"], [\"c\"])\n",
"trial_circuit.x(q[1])\n",
"print('m = 1 ' + str(cost[0]) + ' m = 2 ' + str(cost[1]) + 'm = 3 ' + str(cost[2]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# plotting data\n",
"plt.plot(range(max_trials), cost[0])\n",
"plot_histogram(data_dave[0])\n",
"\n",
"for j in range (n):\n",
" trial_circuit.measure(q[j], c[j])\n",
"plt.plot(range(max_trials), cost[1])\n",
"plot_histogram(data_dave[1])\n",
"\n",
"circuits = [\"trial_exact\"]\n",
"Q_program.execute(circuits, device, shots)\n",
"plt.plot(range(max_trials), cost[2])\n",
"plot_histogram(data_dave[2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Four Qubits <a id='sectionmaxcutfour'></a>\n",
"\n",
"# calculating the cost for this trial function\n",
"cost_function(Q_program.get_counts(\"trial_exact\"),n,alpha,beta)"
" Graph: \n",
" X---X \n",
" | \\ |\n",
" X---X\n",
" \n",
"The ground state is degenerate and is either $|100\\rangle$, $|010\\rangle$ and $|001\\rangle$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# cost function H = alpha_i z_i + beta_ij z_i z_j\n",
"n =4\n",
"alpha = np.zeros(n)\n",
"beta = np.zeros((n, n))\n",
"beta[0, 1] = 1\n",
"beta[0, 2] = 1\n",
"beta[1, 2] = 1\n",
"beta[1, 3] = 1\n",
"beta[2, 3] = 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#Making the Hamiltonian in its full form and getting the lowest eigenvalue and egienvector\n",
"H = make_Hamiltonian(n,alpha,beta)\n",
"w, v = la.eigh(H, eigvals=(0, 1))\n",
"print(w)\n",
"v"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Quantum circuit parameters\n",
"device = 'local_qasm_simulator' # the device to run on\n",
"shots = 8192 # the number of shots in the experiment. \n",
"entangler_map = {0: [1]} # the map of two-qubit gates with control at key and target at values\n",
"\n",
"# Numerical parameters \n",
"SPSA_parameters = np.array([.3,0.602,0,.1,0.101]) #[a, alpha, A, c, gamma]\n",
"max_trials = 100;\n",
"max_depth = 3\n",
"\n",
"cost, data_dave = SPSA_Minimization(Q_program, alpha, beta, n, device, shots, entangler_map, SPSA_parameters, max_trials, max_depth)\n",
"\n",
"print('m = 1 ' + str(cost[0]) + ' m = 2 ' + str(cost[1]) + 'm = 3 ' + str(cost[2]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# plotting data\n",
"plt.plot(range(max_trials), cost_plus_depth_1, range(max_trials), cost_minus_depth_1)\n",
"plot_histogram(Q_program.get_counts(\"trial_circuit_depth_1\"),6)\n",
"plt.plot(range(max_trials), cost_plus_depth_2, range(max_trials), cost_minus_depth_2)\n",
"plot_histogram(Q_program.get_counts(\"trial_circuit_depth_2\"),6)\n",
"plt.plot(range(max_trials), cost_plus_depth_3, range(max_trials), cost_minus_depth_3)\n",
"plot_histogram(Q_program.get_counts(\"trial_circuit_depth_3\"),6)"
]
},
{
@ -436,7 +612,7 @@
"source": [
"# quantum circuit parameters\n",
"device = 'local_qasm_simulator' # the device to run on\n",
"shots = 8192 # the number of shots in the experiment. \n",
"shots = 1 # the number of shots in the experiment. \n",
"entangler_map = {0: [1]} # the map of two-qubit gates with control at key and target at values\n",
"\n",
"# Numerical parameters \n",
@ -470,12 +646,12 @@
" theta_plus_depth_3 = theta_depth_3 + c_spsa*Delta_depth_3\n",
" theta_minus_depth_3 = theta_depth_3 - c_spsa*Delta_depth_3\n",
"\n",
" trial_circuit_plus_depth_1 = trial_funtion_optimization(n,1,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_1 = trial_funtion_optimization(n,1,theta_minus_depth_1,entangler_map) \n",
" trial_circuit_plus_depth_2 = trial_funtion_optimization(n,2,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_2 = trial_funtion_optimization(n,2,theta_minus_depth_2,entangler_map)\n",
" trial_circuit_plus_depth_3 = trial_funtion_optimization(n,3,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_3 = trial_funtion_optimization(n,3,theta_minus_depth_3,entangler_map)\n",
" trial_circuit_plus_depth_1 = trial_funtion_optimization_no_meas(n,1,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_1 = trial_funtion_optimization_no_meas(n,1,theta_minus_depth_1,entangler_map) \n",
" trial_circuit_plus_depth_2 = trial_funtion_optimization_no_meas(n,2,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_2 = trial_funtion_optimization_no_meas(n,2,theta_minus_depth_2,entangler_map)\n",
" trial_circuit_plus_depth_3 = trial_funtion_optimization_no_meas(n,3,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_3 = trial_funtion_optimization_no_meas(n,3,theta_minus_depth_3,entangler_map)\n",
" \n",
" Q_program.add_circuit(\"trial_circuit_plus_depth_1\", trial_circuit_plus_depth_1)\n",
" Q_program.add_circuit(\"trial_circuit_minus_depth_1\", trial_circuit_minus_depth_1)\n",
@ -487,12 +663,12 @@
" circuits = [\"trial_circuit_plus_depth_1\", \"trial_circuit_minus_depth_1\",\"trial_circuit_plus_depth_2\", \"trial_circuit_minus_depth_2\",\"trial_circuit_plus_depth_3\", \"trial_circuit_minus_depth_3\"]\n",
" Q_program.execute(circuits, device, shots)\n",
" \n",
" cost_plus_depth_1[k] = cost_function(Q_program.get_counts(\"trial_circuit_plus_depth_1\"),n,alpha,beta)\n",
" cost_minus_depth_1[k] = cost_function(Q_program.get_counts(\"trial_circuit_minus_depth_1\"),n,alpha,beta)\n",
" cost_plus_depth_2[k] = cost_function(Q_program.get_counts(\"trial_circuit_plus_depth_2\"),n,alpha,beta)\n",
" cost_minus_depth_2[k] = cost_function(Q_program.get_counts(\"trial_circuit_minus_depth_2\"),n,alpha,beta)\n",
" cost_plus_depth_3[k] = cost_function(Q_program.get_counts(\"trial_circuit_plus_depth_3\"),n,alpha,beta)\n",
" cost_minus_depth_3[k] = cost_function(Q_program.get_counts(\"trial_circuit_minus_depth_3\"),n,alpha,beta)\n",
" cost_plus_depth_1[k] = np.vdot(Q_program.get_data(\"trial_circuit_plus_depth_1\")['quantum_state'], np.dot(H,Q_program.get_data(\"trial_circuit_plus_depth_1\")['quantum_state'])).real\n",
" cost_minus_depth_1[k] = np.vdot(Q_program.get_data(\"trial_circuit_minus_depth_1\")['quantum_state'], np.dot(H,Q_program.get_data(\"trial_circuit_minus_depth_1\")['quantum_state'])).real\n",
" cost_plus_depth_2[k] = np.vdot(Q_program.get_data(\"trial_circuit_plus_depth_2\")['quantum_state'], np.dot(H,Q_program.get_data(\"trial_circuit_plus_depth_2\")['quantum_state'])).real\n",
" cost_minus_depth_2[k] = np.vdot(Q_program.get_data(\"trial_circuit_minus_depth_2\")['quantum_state'], np.dot(H,Q_program.get_data(\"trial_circuit_minus_depth_2\")['quantum_state'])).real\n",
" cost_plus_depth_3[k] = np.vdot(Q_program.get_data(\"trial_circuit_plus_depth_3\")['quantum_state'], np.dot(H,Q_program.get_data(\"trial_circuit_plus_depth_3\")['quantum_state'])).real\n",
" cost_minus_depth_3[k] = np.vdot(Q_program.get_data(\"trial_circuit_minus_depth_3\")['quantum_state'], np.dot(H,Q_program.get_data(\"trial_circuit_minus_depth_3\")['quantum_state'])).real\n",
" \n",
" g_spsa_depth_1 = (cost_plus_depth_1[k]-cost_minus_depth_1[k])*Delta_depth_1/(2.0*c_spsa)\n",
" g_spsa_depth_2 = (cost_plus_depth_2[k]-cost_minus_depth_2[k])*Delta_depth_2/(2.0*c_spsa)\n",
@ -511,7 +687,7 @@
"Q_program.add_circuit(\"trial_circuit_depth_3\", trial_circuit_depth_3)\n",
"\n",
"circuits = [\"trial_circuit_depth_1\", \"trial_circuit_depth_2\",\"trial_circuit_depth_3\"]\n",
"Q_program.execute(circuits, device, shots)\n",
"Q_program.execute(circuits, device, 8192)\n",
"\n",
"cost_depth_1 = cost_function(Q_program.get_counts(\"trial_circuit_depth_1\"),n,alpha,beta)\n",
"cost_depth_2 = cost_function(Q_program.get_counts(\"trial_circuit_depth_2\"),n,alpha,beta)\n",
@ -529,149 +705,18 @@
"source": [
"# plotting data\n",
"plt.plot(range(max_trials), cost_plus_depth_1, range(max_trials), cost_minus_depth_1)\n",
"plot_histogram(get_data(results,0),n)\n",
"plot_histogram(Q_program.get_counts(\"trial_circuit_depth_1\"),6)\n",
"plt.plot(range(max_trials), cost_plus_depth_2, range(max_trials), cost_minus_depth_2)\n",
"plot_histogram(get_data(results,1),n)\n",
"plot_histogram(Q_program.get_counts(\"trial_circuit_depth_2\"),6)\n",
"plt.plot(range(max_trials), cost_plus_depth_3, range(max_trials), cost_minus_depth_3)\n",
"plot_histogram(get_data(results,2),n)"
"plot_histogram(Q_program.get_counts(\"trial_circuit_depth_3\"),6)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4 qubit max cut"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# cost function H = alpha_i z_i + beta_ij z_i z_j\n",
"n =4\n",
"alpha = np.zeros(n)\n",
"beta = np.zeros((n, n))\n",
"beta[0, 1] = 0.5\n",
"beta[1, 0] = 0.5\n",
"beta[0, 2] = 0.5\n",
"beta[2, 0] = 0.5\n",
"beta[1, 2] = 0.5\n",
"beta[2, 1] = 0.5\n",
"beta[1, 3] = 0.5\n",
"beta[3, 1] = 0.5\n",
"beta[2, 3] = 0.5\n",
"beta[3, 2] = 0.5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# quantum circuit parameters\n",
"device = 'simulator' # the device to run on\n",
"shots = 8192 # the number of shots in the experiment \n",
"entangler_map = {0: [1], 1: [2], 3: [2]} # the map of two-qubit gates with control at key and target at values\n",
"\n",
"# Numerical parameters \n",
"SPSA_parameters = np.array([.3,0.602,0,.1,0.101]) #[a, alpha, A, c, gamma]\n",
"max_trials = 100;\n",
"theta_depth_1 = np.random.randn(1*n) # initial controls \n",
"theta_depth_2 = np.random.randn(2*n) # initial controls \n",
"theta_depth_3 = np.random.randn(3*n) # initial controls \n",
"\n",
"cost_plus_depth_1=np.zeros(max_trials)\n",
"cost_minus_depth_1=np.zeros(max_trials)\n",
"cost_plus_depth_2=np.zeros(max_trials)\n",
"cost_minus_depth_2=np.zeros(max_trials)\n",
"cost_plus_depth_3=np.zeros(max_trials)\n",
"cost_minus_depth_3=np.zeros(max_trials)\n",
"\n",
"for k in range(max_trials):\n",
" print('trial ' + str(k) + \" of \" + str(max_trials))\n",
" a_spsa = float(SPSA_parameters[0])/np.power(k+1+SPSA_parameters[2], SPSA_parameters[1])\n",
" c_spsa = float(SPSA_parameters[3])/np.power(k+1, SPSA_parameters[4])\n",
"\n",
" Delta_depth_1 = 2*np.random.randint(2,size=n*1)-1 # \\pm 1 random distribution \n",
" Delta_depth_2 = 2*np.random.randint(2,size=n*2)-1 # \\pm 1 random distribution \n",
" Delta_depth_3 = 2*np.random.randint(2,size=n*3)-1 # \\pm 1 random distribution \n",
" \n",
" theta_plus_depth_1 = theta_depth_1 + c_spsa*Delta_depth_1\n",
" theta_minus_depth_1 = theta_depth_1 - c_spsa*Delta_depth_1\n",
" theta_plus_depth_2 = theta_depth_2 + c_spsa*Delta_depth_2\n",
" theta_minus_depth_2 = theta_depth_2 - c_spsa*Delta_depth_2\n",
" theta_plus_depth_3 = theta_depth_3 + c_spsa*Delta_depth_3\n",
" theta_minus_depth_3 = theta_depth_3 - c_spsa*Delta_depth_3\n",
"\n",
" trial_circuit_plus_depth_1 = trial_funtion_optimization(n,1,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_1 = trial_funtion_optimization(n,1,theta_minus_depth_1,entangler_map) \n",
" trial_circuit_plus_depth_2 = trial_funtion_optimization(n,2,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_2 = trial_funtion_optimization(n,2,theta_minus_depth_2,entangler_map)\n",
" trial_circuit_plus_depth_3 = trial_funtion_optimization(n,3,theta_plus_depth_3,entangler_map) \n",
" trial_circuit_minus_depth_3 = trial_funtion_optimization(n,3,theta_minus_depth_3,entangler_map)\n",
" \n",
" program = [trial_circuit_plus_depth_1,trial_circuit_minus_depth_1,trial_circuit_plus_depth_2\n",
" ,trial_circuit_minus_depth_2,trial_circuit_plus_depth_3,trial_circuit_minus_depth_3]\n",
" out = run_program(program,api,device,shots,max_credits=3)\n",
" results=combine_jobs([out['id']], api, wait=20, timeout=440)\n",
" \n",
" cost_plus_depth_1[k] = cost_classical(get_data(results,0),n,alpha,beta)\n",
" cost_minus_depth_1[k] = cost_classical(get_data(results,1),n,alpha,beta)\n",
" cost_plus_depth_2[k] = cost_classical(get_data(results,2),n,alpha,beta)\n",
" cost_minus_depth_2[k] = cost_classical(get_data(results,3),n,alpha,beta)\n",
" cost_plus_depth_3[k] = cost_classical(get_data(results,4),n,alpha,beta)\n",
" cost_minus_depth_3[k] = cost_classical(get_data(results,5),n,alpha,beta)\n",
" \n",
" g_spsa_depth_1 = (cost_plus_depth_1[k]-cost_minus_depth_1[k])*Delta_depth_1/(2.0*c_spsa)\n",
" g_spsa_depth_2 = (cost_plus_depth_2[k]-cost_minus_depth_2[k])*Delta_depth_2/(2.0*c_spsa)\n",
" g_spsa_depth_3 = (cost_plus_depth_3[k]-cost_minus_depth_3[k])*Delta_depth_3/(2.0*c_spsa)\n",
"\n",
" theta_depth_1 = theta_depth_1 - a_spsa*g_spsa_depth_1\n",
" theta_depth_2 = theta_depth_2 - a_spsa*g_spsa_depth_2\n",
" theta_depth_3 = theta_depth_3 - a_spsa*g_spsa_depth_3\n",
"\n",
"trial_circuit_depth_1 = trial_funtion_optimization(n,1,theta_depth_1,entangler_map) \n",
"trial_circuit_depth_2 = trial_funtion_optimization(n,2,theta_depth_2,entangler_map) \n",
"trial_circuit_depth_3 = trial_funtion_optimization(n,3,theta_depth_3,entangler_map) \n",
"\n",
"program = [trial_circuit_depth_1,trial_circuit_depth_2,trial_circuit_depth_3]\n",
"out = run_program(program,api,device,shots,max_credits=3)\n",
"results=combine_jobs([out['id']], api, wait=20, timeout=440) \n",
"cost_depth_1 = cost_classical(get_data(results,0),n,alpha,beta)\n",
"cost_depth_2 = cost_classical(get_data(results,1),n,alpha,beta)\n",
"cost_depth_3 = cost_classical(get_data(results,2),n,alpha,beta)\n",
"print('m=1 ' + str(cost_depth_1) + ' m=2 ' + str(cost_depth_2) + 'm=3 ' + str(cost_depth_3))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# plotting data\n",
"plt.plot(range(max_trials), cost_plus_depth_1, range(max_trials), cost_minus_depth_1)\n",
"plot_histogram(get_data(results,0),n)\n",
"plt.plot(range(max_trials), cost_plus_depth_2, range(max_trials), cost_minus_depth_2)\n",
"plot_histogram(get_data(results,1),n)\n",
"plt.plot(range(max_trials), cost_plus_depth_3, range(max_trials), cost_minus_depth_3)\n",
"plot_histogram(get_data(results,2),n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 10 Qubit - Max Cut"
"## 10 qubits<a id='sectionmaxcutten'></a>"
]
},
{

View File

@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"../../images/QISKit-c.gif\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"250 px\" align=\"left\">"
"<img src=\"../images/QISKit-c.gif\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"250 px\" align=\"left\">"
]
},
{
@ -40,7 +40,9 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Checking the version of PYTHON; we only support 3 at the moment\n",
@ -95,7 +97,9 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
@ -129,7 +133,9 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
@ -178,7 +184,9 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "AttributeError",
@ -219,6 +227,7 @@
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
@ -253,7 +262,9 @@
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
@ -307,7 +318,9 @@
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
@ -340,7 +353,9 @@
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
@ -388,7 +403,9 @@
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
@ -430,7 +447,9 @@
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
@ -475,6 +494,7 @@
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"scrolled": false
},
"outputs": [

File diff suppressed because one or more lines are too long