[{"data":1,"prerenderedAt":4075},["ShallowReactive",2],{"blog-post-quantum-optimization":3,"sibling-dives-quantum-optimization":4073,"learn-track-quantum-optimization":4074},{"id":4,"title":5,"authors":6,"body":8,"breadcrumb":4031,"builders":4035,"byline":4048,"category":4049,"categoryName":4049,"challenge":4049,"courseAuthor":4049,"courseLead":4049,"dek":4050,"description":4051,"draft":4052,"extension":4053,"eyebrow":4049,"featured":4052,"finish":4049,"fork":4054,"hero":4049,"heroAlt":4049,"heroCta":4049,"heroImage":4049,"homepageFeatured":4052,"kind":4056,"lessonCount":4049,"meta":4057,"navigation":4058,"newsItems":4049,"next":4049,"ogImage":4059,"order":263,"outcomes":4049,"path":4060,"publishDate":4061,"readingTime":4062,"related":4063,"relatedProjects":4049,"seo":4064,"stem":4067,"tags":4068,"track":4049,"trackName":4049,"__hash__":4072},"blog\u002Fblog\u002Flearn\u002Fquantum-optimization.md","Quantum Optimization and QAOA",[7],"nico",{"type":9,"value":10,"toc":4016},"minimark",[11,21,24,29,63,66,69,73,76,79,82,85,88,91,94,97,101,104,119,122,125,129,132,135,138,147,154,161,170,173,177,191,534,551,871,885,888,893,904,907,927,934,938,941,948,1312,1319,1327,1330,1349,1365,1369,1373,1376,1391,1413,1424,1440,1450,1454,1457,1460,1737,1740,1745,1748,1945,1948,1957,1962,1965,1968,1977,2058,2061,2067,2074,2078,2081,2084,2094,2097,2243,2246,2249,2252,2262,2453,2456,2459,2466,2469,2473,2476,2776,2782,3255,3258,3511,3517,3520,3527,3531,3534,3537,3540,3543,3550,3771,3774,3777,3781,3786,3796,3940,3943,3946,3954,3957,3960,3963,3966,3970,3973,3976,3990,3998,4012],[12,13,14,15,20],"p",{},"Quantum optimization means using a quantum computer to find the best of a huge number of possible answers, such as the best way to split a graph into two groups. QAOA, the standard circuit for the job, does not check the answers. It changes how often each one comes out when you ",[16,17,19],"a",{"href":18},"\u002Flearn\u002Fquantum-measurement","measure",", and two adjustable angles set by how much.",[12,22,23],{},"The smallest version has one qubit, three gates and a measurement, and its two angles take that qubit from a coin toss to a certain answer. Everything on this page is that same circuit on more qubits, sometimes stacked in more than one layer, up to a graph of twenty-four nodes.",[25,26,28],"h2",{"id":27},"what-is-quantum-optimization","What is quantum optimization?",[30,31,32,35,48,60],"quick-answer",{},[12,33,34],{},"Quantum optimization means rewriting a problem as a score over strings of bits, then building a circuit whose measured strings lean towards better scores. You keep the best string you see. Ordinary code scores each sampled string, and the circuit only supplies the samples.",[12,36,37,38,42,43,47],{},"QAOA, the quantum approximate optimization algorithm, is the standard circuit for the job. It starts with ",[16,39,41],{"href":40},"\u002Flearn\u002Fquantum-superposition","equal weight on every string",", and one layer is then two steps. A cost step gives each string a phase, an angle that measurement never shows, set by its score. A mixer step ",[16,44,46],{"href":45},"\u002Flearn\u002Fquantum-interference","turns those phases into weight",", and at the right angles better strings then come out more often.",[12,49,50,51,55,56,59],{},"Each layer has two angles, ",[52,53,54],"code",{},"gamma"," for the cost step and ",[52,57,58],{},"beta"," for the mixer step, tuned by an ordinary classical optimizer.",[12,61,62],{},"Approximate is in the name because the guarantee, where one has been proved, is a floor. The average score of the samples reaches no less than a fixed fraction of the best possible. What comes back is a good string, not a proof that it is the best one.",[12,64,65],{},"Splitting a graph so that as many edges as possible run between the two groups is MaxCut. It is the running example on this page, and every other problem here is rewritten into the same form.",[12,67,68],{},"One layer of the MaxCut circuit needs one two-qubit interaction per edge and one rotation per node. Only two numbers in that layer are free to change, the angles.",[25,70,72],{"id":71},"cooling-slowly","Cooling slowly",[12,74,75],{},"Annealing is what a metalworker does to steel that has to bend without breaking. A blade plunged into water from red heat comes out hard and brittle, because the atoms freeze where they are, defects and all.",[12,77,78],{},"Cooled slowly, the same steel settles into a lower-energy arrangement, because at each temperature the atoms still have enough motion to leave a bad position for a better one.",[12,80,81],{},"In 1983 Kirkpatrick, Gelatt and Vecchi turned annealing into an algorithm. Simulated annealing starts from a random answer and accepts a worse neighbouring answer with a probability that shrinks as a temperature variable drops. It ends up in a good answer for the same reason the blade does.",[12,83,84],{},"Simulated annealing is also one of the classical solvers in Quantum Courier, the Qollab project the 24-node graph named in the opening comes from. There it beats the quantum solvers on the game's vehicle-routing stage.",[12,86,87],{},"Adiabatic quantum computation, proposed in 2000, is the quantum version of cooling slowly. A Hamiltonian is a problem's cost written as an energy, and its ground state, the lowest-energy state, encodes the best assignment.",[12,89,90],{},"Start in the easy ground state of one Hamiltonian and change it into the problem's; if the change is slow enough, the state stays close to the ground state throughout.",[12,92,93],{},"QAOA cuts the adiabatic path into a fixed number of steps and lets a classical optimizer choose how big each step is. Farhi, Goldstone and Gutmann showed that with enough layers the circuit can follow the adiabatic path.",[12,95,96],{},"A single layer is the fast-cooling version. On the 24-node graph further down it lifts the average cut from 18.5 edges to 25.5, out of a possible 33.",[25,98,100],{"id":99},"where-quantum-optimization-came-from","Where quantum optimization came from",[12,102,103],{},"Six dates carry the route from annealing to QAOA and on to the current scaling experiments:",[105,106],"timeline",{"l1":107,"l2":108,"l3":109,"l4":110,"l5":111,"l6":112,"w1":113,"w2":114,"w3":115,"w4":116,"w5":117,"w6":118},"Kirkpatrick, Gelatt and Vecchi publish simulated annealing: cool a random answer slowly and it settles into a good one.","Goemans and Williamson guarantee 0.878 of the best possible cut with a classical algorithm, a bar that still stands.","Farhi, Goldstone, Gutmann and Sipser propose adiabatic quantum computation: change a Hamiltonian slowly and the ground state follows.","Farhi, Goldstone and Gutmann introduce QAOA: the slow path cut into layers, with two angles per layer.","Google runs QAOA on 23 superconducting qubits; problems that match the chip's wiring improve with depth, problems that do not get worse as they grow.","Shaydulin and colleagues report evidence of a scaling advantage for QAOA on a problem where the best exact classical solvers scale worse.","1983","1995","2000","2014","2021","2024",[12,120,121],{},"The 2014 paper carried a guarantee: on any graph where every node has three edges, one QAOA layer's samples average at least 0.6924 times the best possible cut.",[12,123,124],{},"A month later the same three authors showed one layer beating the best known classical guarantee on a constraint problem called E3LIN2. By May 2015 a classical algorithm had a better guarantee; the lead had lasted five months, and the QAOA bound itself still stands.",[25,126,128],{"id":127},"what-one-layer-does","What one layer does",[12,130,131],{},"A qubit carries two numbers for each outcome it can give. Its weight is how often that outcome comes back when you measure. Its phase is an angle that measurement never shows, and the cost step works on that one. Two qubits carry both numbers for four outcomes, twenty-four qubits for sixteen million.",[12,133,134],{},"One QAOA layer is two steps. The cost step touches only the phases; the mixer step is where the weights change.",[12,136,137],{},"In the cost step, every outcome's phase turns by an amount set by that outcome's score. A good outcome and a bad one end up with different phases, and nothing else has happened: measure now and the counts are exactly what they were before.",[12,139,140,141,143,144,146],{},"In the mixer step, phase differences become weight differences. Outcomes whose phases were turned one way gain weight and outcomes turned the other way lose it. The two angles decide how far the phases turn and how much weight follows: ",[52,142,54],{}," for the cost step, ",[52,145,58],{}," for the mixer step.",[12,148,149,150,153],{},"On one qubit, with a score that prefers ",[52,151,152],{},"1",", the panels below show each step, and what smaller angles do:",[155,156],"blog-figure",{"alt":157,"caption":158,"no":159,"src":160},"Four panels of two bars each, for the weight on outcome 0 and outcome 1, with a small dial above each bar for its phase: equal bars and matching dials at the start, the dial over 1 turned after the cost step, all the weight on 1 after the mixer step, and 85 percent on 1 at smaller angles","One layer on one qubit. The cost step turns a dial and moves no bar; the mixer step moves the bars, and smaller angles move them less. The dials are drawn only while the phase is what changes.","","\u002F_content\u002Fimages\u002Foptimization\u002Fone-layer-panels.svg",[12,162,163,164,166,167,169],{},"Both bars start at one half with the same phase. After the cost step the hand over ",[52,165,152],{}," has turned and both bars are where they were. After the mixer step the weight has moved, and at the right pair of angles all of it lands on ",[52,168,152],{},"; at smaller angles, less of it. Measure a thousand times and the counts follow the bars.",[12,171,172],{},"The one-qubit circuit in the next section is the first three panels as three gates, then a measurement that reads the bars.",[25,174,176],{"id":175},"the-smallest-circuit-you-can-make","The smallest circuit you can make",[12,178,179,180,182,183,186,187,190],{},"A cost that prefers ",[52,181,152],{}," is one ",[52,184,185],{},"rz",", the mixer is one ",[52,188,189],{},"rx",", and measuring reads the bars:",[192,193,197],"code-block",{"name":194,"run-href":195,"tag":196},"one_qubit_qaoa.py","\u002Fu\u002Fqollab\u002Fshowcase-qaoa-qubo","Python · Playground version",[198,199,203],"pre",{"className":200,"code":201,"language":202,"meta":159,"style":159},"language-python shiki shiki-themes one-dark-pro","# 'backend' is pre-created for you in the Qollab Playground.\nfrom math import pi\nfrom qiskit import QuantumCircuit\n\ngamma, beta = -pi \u002F 4, pi \u002F 4\n\nqc = QuantumCircuit(1, 1)\nqc.h(0)\nqc.rz(2 * gamma, 0)\nqc.rx(2 * beta, 0)\nqc.measure(0, 0)\n\nprint(backend.run(qc, shots=1000).result().get_counts())\n","python",[52,204,205,219,238,253,261,295,302,329,348,372,395,414,421,463],{"__ignoreMap":159},[206,207,210,215],"span",{"class":208,"line":209},"line",1,[206,211,214],{"class":212,"aria-hidden":213},"qg-cue qg-cue--empty","true","​",[206,216,218],{"class":217},"sV9Aq","# 'backend' is pre-created for you in the Qollab Playground.\n",[206,220,222,224,228,232,235],{"class":208,"line":221},2,[206,223,214],{"class":212,"aria-hidden":213},[206,225,227],{"class":226},"seHd6","from",[206,229,231],{"class":230},"sn6KH"," math ",[206,233,234],{"class":226},"import",[206,236,237],{"class":230}," pi\n",[206,239,241,243,245,248,250],{"class":208,"line":240},3,[206,242,214],{"class":212,"aria-hidden":213},[206,244,227],{"class":226},[206,246,247],{"class":230}," qiskit ",[206,249,234],{"class":226},[206,251,252],{"class":230}," QuantumCircuit\n",[206,254,256,258],{"class":208,"line":255},4,[206,257,214],{"class":212,"aria-hidden":213},[206,259,260],{},"​\n",[206,262,264,267,270,274,277,280,283,287,290,292],{"class":208,"data-cue":152,"line":263},5,[206,265,152],{"class":266},"qg-cue",[206,268,269],{"class":230},"gamma, beta ",[206,271,273],{"class":272},"sjrmR","=",[206,275,276],{"class":272}," -",[206,278,279],{"class":230},"pi ",[206,281,282],{"class":272},"\u002F",[206,284,286],{"class":285},"sVC51"," 4",[206,288,289],{"class":230},", pi ",[206,291,282],{"class":272},[206,293,294],{"class":285}," 4\n",[206,296,298,300],{"class":208,"line":297},6,[206,299,214],{"class":212,"aria-hidden":213},[206,301,260],{},[206,303,305,307,310,312,316,319,321,324,326],{"class":208,"line":304},7,[206,306,214],{"class":212,"aria-hidden":213},[206,308,309],{"class":230},"qc ",[206,311,273],{"class":272},[206,313,315],{"class":314},"sVbv2"," QuantumCircuit",[206,317,318],{"class":230},"(",[206,320,152],{"class":285},[206,322,323],{"class":230},", ",[206,325,152],{"class":285},[206,327,328],{"class":230},")\n",[206,330,333,335,338,341,343,346],{"class":208,"data-cue":331,"line":332},"2",8,[206,334,331],{"class":266},[206,336,337],{"class":230},"qc.",[206,339,340],{"class":314},"h",[206,342,318],{"class":230},[206,344,345],{"class":285},"0",[206,347,328],{"class":230},[206,349,352,354,356,358,360,362,365,368,370],{"class":208,"data-cue":350,"line":351},"3",9,[206,353,350],{"class":266},[206,355,337],{"class":230},[206,357,185],{"class":314},[206,359,318],{"class":230},[206,361,331],{"class":285},[206,363,364],{"class":272}," *",[206,366,367],{"class":230}," gamma, ",[206,369,345],{"class":285},[206,371,328],{"class":230},[206,373,376,378,380,382,384,386,388,391,393],{"class":208,"data-cue":374,"line":375},"4",10,[206,377,374],{"class":266},[206,379,337],{"class":230},[206,381,189],{"class":314},[206,383,318],{"class":230},[206,385,331],{"class":285},[206,387,364],{"class":272},[206,389,390],{"class":230}," beta, ",[206,392,345],{"class":285},[206,394,328],{"class":230},[206,396,398,400,402,404,406,408,410,412],{"class":208,"line":397},11,[206,399,214],{"class":212,"aria-hidden":213},[206,401,337],{"class":230},[206,403,19],{"class":314},[206,405,318],{"class":230},[206,407,345],{"class":285},[206,409,323],{"class":230},[206,411,345],{"class":285},[206,413,328],{"class":230},[206,415,417,419],{"class":208,"line":416},12,[206,418,214],{"class":212,"aria-hidden":213},[206,420,260],{},[206,422,425,427,430,433,436,439,443,445,448,451,454,457,460],{"class":208,"data-cue":423,"line":424},"5",13,[206,426,423],{"class":266},[206,428,429],{"class":272},"print",[206,431,432],{"class":230},"(backend.",[206,434,435],{"class":314},"run",[206,437,438],{"class":230},"(qc, ",[206,440,442],{"class":441},"s_ZVi","shots",[206,444,273],{"class":272},[206,446,447],{"class":285},"1000",[206,449,450],{"class":230},").",[206,452,453],{"class":314},"result",[206,455,456],{"class":230},"().",[206,458,459],{"class":314},"get_counts",[206,461,462],{"class":230},"())\n",[464,465,467,479,492,505,512],"ol",{"class":466},"qg-cues",[468,469,470,472],"li",{"data-cue":152},[206,471,152],{"class":266},[206,473,475,476,478],{"class":474},"qg-cues__text","The two angles. This pair is exact for one qubit: every shot will read ",[52,477,152],{},".",[468,480,481,483],{"data-cue":331},[206,482,331],{"class":266},[206,484,485,486,488,489,491],{"class":474},"Equal weight on ",[52,487,345],{}," and ",[52,490,152],{},", the starting point of every QAOA circuit on this page.",[468,493,494,496],{"data-cue":350},[206,495,350],{"class":266},[206,497,498,499,501,502,504],{"class":474},"The cost step. It writes the score into the phase: the ",[52,500,152],{}," half of the state gets a different phase from the ",[52,503,345],{}," half. Nothing in the counts has changed yet. Qiskit's rotation gates take twice the angle, a convention every listing here follows.",[468,506,507,509],{"data-cue":374},[206,508,374],{"class":266},[206,510,511],{"class":474},"The mixer step. It turns that phase difference into a weight difference.",[468,513,514,516],{"data-cue":423},[206,515,423],{"class":266},[206,517,518,521,522,524,525,527,528,530,531,533],{"class":474},[52,519,520],{},"{'1': 1000}",": every shot reads ",[52,523,152],{},". Set ",[52,526,54],{}," to +π\u002F4 and every shot reads ",[52,529,345],{},"; halve either angle and ",[52,532,152],{}," comes back about 85% of the time.",[12,535,536,537,488,540,543,544,488,547,550],{},"One qubit has nothing to cut, so the score there is a preference. The first real problem is one edge between two nodes: it is cut when the two bits differ, so ",[52,538,539],{},"01",[52,541,542],{},"10"," are the answers and ",[52,545,546],{},"00",[52,548,549],{},"11"," are not. The circuit adds one two-qubit interaction for the edge, written as a three-gate sandwich, and keeps its two angles:",[192,552,554],{"name":553,"run-href":195,"tag":196},"one_edge_qaoa.py",[198,555,557],{"className":200,"code":556,"language":202,"meta":159,"style":159},"# 'backend' is pre-created for you in the Qollab Playground.\nfrom math import pi\nfrom qiskit import QuantumCircuit\n\ngamma, beta = -pi \u002F 4, pi \u002F 8\n\nqc = QuantumCircuit(2, 2)\nqc.h([0, 1])\nqc.cx(0, 1)\nqc.rz(2 * gamma, 1)\nqc.cx(0, 1)\nqc.rx(2 * beta, [0, 1])\nqc.measure([0, 1], [0, 1])\n\nprint(backend.run(qc, shots=1000).result().get_counts())\n",[52,558,559,565,577,589,595,618,624,644,664,683,703,721,746,773,780,809],{"__ignoreMap":159},[206,560,561,563],{"class":208,"line":209},[206,562,214],{"class":212,"aria-hidden":213},[206,564,218],{"class":217},[206,566,567,569,571,573,575],{"class":208,"line":221},[206,568,214],{"class":212,"aria-hidden":213},[206,570,227],{"class":226},[206,572,231],{"class":230},[206,574,234],{"class":226},[206,576,237],{"class":230},[206,578,579,581,583,585,587],{"class":208,"line":240},[206,580,214],{"class":212,"aria-hidden":213},[206,582,227],{"class":226},[206,584,247],{"class":230},[206,586,234],{"class":226},[206,588,252],{"class":230},[206,590,591,593],{"class":208,"line":255},[206,592,214],{"class":212,"aria-hidden":213},[206,594,260],{},[206,596,597,599,601,603,605,607,609,611,613,615],{"class":208,"data-cue":152,"line":263},[206,598,152],{"class":266},[206,600,269],{"class":230},[206,602,273],{"class":272},[206,604,276],{"class":272},[206,606,279],{"class":230},[206,608,282],{"class":272},[206,610,286],{"class":285},[206,612,289],{"class":230},[206,614,282],{"class":272},[206,616,617],{"class":285}," 8\n",[206,619,620,622],{"class":208,"line":297},[206,621,214],{"class":212,"aria-hidden":213},[206,623,260],{},[206,625,626,628,630,632,634,636,638,640,642],{"class":208,"line":304},[206,627,214],{"class":212,"aria-hidden":213},[206,629,309],{"class":230},[206,631,273],{"class":272},[206,633,315],{"class":314},[206,635,318],{"class":230},[206,637,331],{"class":285},[206,639,323],{"class":230},[206,641,331],{"class":285},[206,643,328],{"class":230},[206,645,646,648,650,652,655,657,659,661],{"class":208,"data-cue":331,"line":332},[206,647,331],{"class":266},[206,649,337],{"class":230},[206,651,340],{"class":314},[206,653,654],{"class":230},"([",[206,656,345],{"class":285},[206,658,323],{"class":230},[206,660,152],{"class":285},[206,662,663],{"class":230},"])\n",[206,665,666,668,670,673,675,677,679,681],{"class":208,"data-cue":350,"line":351},[206,667,350],{"class":266},[206,669,337],{"class":230},[206,671,672],{"class":314},"cx",[206,674,318],{"class":230},[206,676,345],{"class":285},[206,678,323],{"class":230},[206,680,152],{"class":285},[206,682,328],{"class":230},[206,684,685,687,689,691,693,695,697,699,701],{"class":208,"line":375},[206,686,214],{"class":212,"aria-hidden":213},[206,688,337],{"class":230},[206,690,185],{"class":314},[206,692,318],{"class":230},[206,694,331],{"class":285},[206,696,364],{"class":272},[206,698,367],{"class":230},[206,700,152],{"class":285},[206,702,328],{"class":230},[206,704,705,707,709,711,713,715,717,719],{"class":208,"line":397},[206,706,214],{"class":212,"aria-hidden":213},[206,708,337],{"class":230},[206,710,672],{"class":314},[206,712,318],{"class":230},[206,714,345],{"class":285},[206,716,323],{"class":230},[206,718,152],{"class":285},[206,720,328],{"class":230},[206,722,723,725,727,729,731,733,735,738,740,742,744],{"class":208,"data-cue":374,"line":416},[206,724,374],{"class":266},[206,726,337],{"class":230},[206,728,189],{"class":314},[206,730,318],{"class":230},[206,732,331],{"class":285},[206,734,364],{"class":272},[206,736,737],{"class":230}," beta, [",[206,739,345],{"class":285},[206,741,323],{"class":230},[206,743,152],{"class":285},[206,745,663],{"class":230},[206,747,748,750,752,754,756,758,760,762,765,767,769,771],{"class":208,"line":424},[206,749,214],{"class":212,"aria-hidden":213},[206,751,337],{"class":230},[206,753,19],{"class":314},[206,755,654],{"class":230},[206,757,345],{"class":285},[206,759,323],{"class":230},[206,761,152],{"class":285},[206,763,764],{"class":230},"], [",[206,766,345],{"class":285},[206,768,323],{"class":230},[206,770,152],{"class":285},[206,772,663],{"class":230},[206,774,776,778],{"class":208,"line":775},14,[206,777,214],{"class":212,"aria-hidden":213},[206,779,260],{},[206,781,783,785,787,789,791,793,795,797,799,801,803,805,807],{"class":208,"data-cue":423,"line":782},15,[206,784,423],{"class":266},[206,786,429],{"class":272},[206,788,432],{"class":230},[206,790,435],{"class":314},[206,792,438],{"class":230},[206,794,442],{"class":441},[206,796,273],{"class":272},[206,798,447],{"class":285},[206,800,450],{"class":230},[206,802,453],{"class":314},[206,804,456],{"class":230},[206,806,459],{"class":314},[206,808,462],{"class":230},[464,810,811,818,834,848,855],{"class":466},[468,812,813,815],{"data-cue":152},[206,814,152],{"class":266},[206,816,817],{"class":474},"The two angles. This pair is exact for one edge: every shot will cut it. Change either one and the counts change with it.",[468,819,820,822],{"data-cue":331},[206,821,331],{"class":266},[206,823,824,825,323,827,323,829,323,831,833],{"class":474},"Even weight on all four strings: ",[52,826,546],{},[52,828,539],{},[52,830,542],{},[52,832,549],{},". The standard QAOA circuit starts here.",[468,835,836,838],{"data-cue":350},[206,837,350],{"class":266},[206,839,840,841,323,843,323,845,847],{"class":474},"The cost step for one edge. The ",[52,842,672],{},[52,844,185],{},[52,846,672],{}," sandwich shifts the phase of a string by an amount that depends on whether its two bits differ. Cut strings get one phase, uncut strings the other.",[468,849,850,852],{"data-cue":374},[206,851,374],{"class":266},[206,853,854],{"class":474},"The mixer step. A rotation on each qubit that turns the phase difference the cost step set into a weight difference. Phases alone change no count; this is the step that makes them count.",[468,856,857,859],{"data-cue":423},[206,858,423],{"class":266},[206,860,861,864,865,867,868,870],{"class":474},[52,862,863],{},"{'01': 503, '10': 497}",": about half each, and no ",[52,866,546],{}," or ",[52,869,549],{}," at all. The circuit never proposes an uncut edge.",[12,872,873,874,876,877,488,879,881,882,884],{},"Flip the sign of ",[52,875,54],{}," and the counts flip with it: all ",[52,878,546],{},[52,880,549],{},", an uncut edge on every shot. Set both angles to zero and the four strings come back at a quarter each. Halve ",[52,883,54],{}," to −π\u002F8 and the cut strings carry 85% of the weight instead of all of it.",[12,886,887],{},"All four results come from the same gates; only the two numbers changed. The angles set what a QAOA circuit computes, and for one edge the average cut at every pair of angles fits in one figure:",[155,889],{"alt":890,"caption":891,"no":159,"src":892},"A heatmap of the expected cut for the one-edge circuit over every pair of angles, with two purple peaks at expected cut one and two pale troughs at zero","Expected cut of the one-edge circuit at every pair of angles. Two peaks reach 1.0, two troughs reach 0.0, and the lines through the centre sit at a coin toss. The circled pairs are exact.","\u002F_content\u002Fimages\u002Foptimization\u002Fone-edge-landscape.svg",[12,894,895,896,899,900,903],{},"Every row and column through the centre is a coin toss. At ",[52,897,898],{},"beta = 0"," the mixer does nothing and the cost step's phases never reach the counts. At ",[52,901,902],{},"gamma = 0"," there are no phases to turn into weight.",[12,905,906],{},"The circuit only does anything when both angles are away from zero, and which peak or trough you land in decides whether it finds the cut or avoids it.",[12,908,909,910,913,914,917,918,921,922,488,924,926],{},"The official ",[52,911,912],{},"showcase-qaoa-qubo"," example is this same problem, written as ",[52,915,916],{},"maximize x + y - 2xy"," and handed to Qiskit's ",[52,919,920],{},"QAOA"," class with the COBYLA optimizer in charge of the angles. Run it and ",[52,923,539],{},[52,925,542],{}," carry almost all the weight, which is COBYLA finding one of the two peaks on its own.",[928,929],"project-card",{"description":930,"framework":931,"owner":932,"title":933,"to":195},"QUBO (Quadratic Unconstrained Binary Optimization) is the standard form for combinatorial problems on quantum computers, and QAOA (Quantum Approximate Optimization Algorithm) is the leading quantum heuristic for solving it. This example models a two-variable QUBO with qiskit-optimization, solves it with QAOA from qiskit-algorithms, and plots the sampled solutions.","Python + Qiskit","qollab","QUBO with QAOA",[25,935,937],{"id":936},"turning-a-problem-into-a-cost","Turning a problem into a cost",[12,939,940],{},"Between one edge and a real graph sits the bookkeeping. The problem has to become a number that a string of bits can be scored by, and the score has to become the phases in the cost step. The standard form, used by every project on this page, is a QUBO. Pick 0 or 1 for each variable to minimize a polynomial with single terms, pairwise terms and nothing higher.",[12,942,943,944,947],{},"Flip the sign and a score to maximize becomes a cost to minimize, which is why MaxCut fits. Qollab's ",[52,945,946],{},"showcase-qubo-exact",", one of the official Playground examples, builds a QUBO with three variables and prints the whole landscape:",[192,949,952],{"name":950,"run-href":951,"tag":196},"qubo_exact.py","\u002Fu\u002Fqollab\u002Fshowcase-qubo-exact",[198,953,955],{"className":200,"code":954,"language":202,"meta":159,"style":159},"# minimize -x - 2y - 3z + 2xy + 2yz\n# (each variable wants to be 1, but x-y and y-z \"repel\" each other)\nqp = QuadraticProgram(\"qubo\")\nqp.binary_var(\"x\")\nqp.binary_var(\"y\")\nqp.binary_var(\"z\")\nqp.minimize(linear={\"x\": -1, \"y\": -2, \"z\": -3}, quadratic={(\"x\", \"y\"): 2, (\"y\", \"z\"): 2})\nprint(qp.prettyprint())\n\n# Brute-force all 8 assignments - only possible because the problem is tiny\nprint(\"\\n x y z   objective\")\nfor bits in product((0, 1), repeat=3):\n    value = qp.objective.evaluate(list(bits))\n    print(f\" {bits[0]} {bits[1]} {bits[2]}   {value:+.0f}\")\n",[52,956,957,964,971,991,1008,1023,1038,1125,1139,1145,1152,1171,1209,1232,1296],{"__ignoreMap":159},[206,958,959,961],{"class":208,"line":209},[206,960,214],{"class":212,"aria-hidden":213},[206,962,963],{"class":217},"# minimize -x - 2y - 3z + 2xy + 2yz\n",[206,965,966,968],{"class":208,"line":221},[206,967,214],{"class":212,"aria-hidden":213},[206,969,970],{"class":217},"# (each variable wants to be 1, but x-y and y-z \"repel\" each other)\n",[206,972,973,975,978,980,983,985,989],{"class":208,"line":240},[206,974,214],{"class":212,"aria-hidden":213},[206,976,977],{"class":230},"qp ",[206,979,273],{"class":272},[206,981,982],{"class":314}," QuadraticProgram",[206,984,318],{"class":230},[206,986,988],{"class":987},"subq3","\"qubo\"",[206,990,328],{"class":230},[206,992,993,995,998,1001,1003,1006],{"class":208,"line":255},[206,994,214],{"class":212,"aria-hidden":213},[206,996,997],{"class":230},"qp.",[206,999,1000],{"class":314},"binary_var",[206,1002,318],{"class":230},[206,1004,1005],{"class":987},"\"x\"",[206,1007,328],{"class":230},[206,1009,1010,1012,1014,1016,1018,1021],{"class":208,"line":263},[206,1011,214],{"class":212,"aria-hidden":213},[206,1013,997],{"class":230},[206,1015,1000],{"class":314},[206,1017,318],{"class":230},[206,1019,1020],{"class":987},"\"y\"",[206,1022,328],{"class":230},[206,1024,1025,1027,1029,1031,1033,1036],{"class":208,"line":297},[206,1026,214],{"class":212,"aria-hidden":213},[206,1028,997],{"class":230},[206,1030,1000],{"class":314},[206,1032,318],{"class":230},[206,1034,1035],{"class":987},"\"z\"",[206,1037,328],{"class":230},[206,1039,1040,1042,1044,1047,1049,1052,1054,1057,1059,1062,1065,1067,1069,1071,1073,1075,1077,1079,1081,1083,1085,1087,1090,1093,1095,1098,1100,1102,1104,1107,1109,1112,1114,1116,1118,1120,1122],{"class":208,"data-cue":152,"line":304},[206,1041,152],{"class":266},[206,1043,997],{"class":230},[206,1045,1046],{"class":314},"minimize",[206,1048,318],{"class":230},[206,1050,1051],{"class":441},"linear",[206,1053,273],{"class":272},[206,1055,1056],{"class":230},"{",[206,1058,1005],{"class":987},[206,1060,1061],{"class":230},": ",[206,1063,1064],{"class":272},"-",[206,1066,152],{"class":285},[206,1068,323],{"class":230},[206,1070,1020],{"class":987},[206,1072,1061],{"class":230},[206,1074,1064],{"class":272},[206,1076,331],{"class":285},[206,1078,323],{"class":230},[206,1080,1035],{"class":987},[206,1082,1061],{"class":230},[206,1084,1064],{"class":272},[206,1086,350],{"class":285},[206,1088,1089],{"class":230},"}, ",[206,1091,1092],{"class":441},"quadratic",[206,1094,273],{"class":272},[206,1096,1097],{"class":230},"{(",[206,1099,1005],{"class":987},[206,1101,323],{"class":230},[206,1103,1020],{"class":987},[206,1105,1106],{"class":230},"): ",[206,1108,331],{"class":285},[206,1110,1111],{"class":230},", (",[206,1113,1020],{"class":987},[206,1115,323],{"class":230},[206,1117,1035],{"class":987},[206,1119,1106],{"class":230},[206,1121,331],{"class":285},[206,1123,1124],{"class":230},"})\n",[206,1126,1127,1129,1131,1134,1137],{"class":208,"line":332},[206,1128,214],{"class":212,"aria-hidden":213},[206,1130,429],{"class":272},[206,1132,1133],{"class":230},"(qp.",[206,1135,1136],{"class":314},"prettyprint",[206,1138,462],{"class":230},[206,1140,1141,1143],{"class":208,"line":351},[206,1142,214],{"class":212,"aria-hidden":213},[206,1144,260],{},[206,1146,1147,1149],{"class":208,"line":375},[206,1148,214],{"class":212,"aria-hidden":213},[206,1150,1151],{"class":217},"# Brute-force all 8 assignments - only possible because the problem is tiny\n",[206,1153,1154,1156,1158,1160,1163,1166,1169],{"class":208,"line":397},[206,1155,214],{"class":212,"aria-hidden":213},[206,1157,429],{"class":272},[206,1159,318],{"class":230},[206,1161,1162],{"class":987},"\"",[206,1164,1165],{"class":272},"\\n",[206,1167,1168],{"class":987}," x y z   objective\"",[206,1170,328],{"class":230},[206,1172,1173,1175,1178,1181,1184,1187,1190,1192,1194,1196,1199,1202,1204,1206],{"class":208,"data-cue":331,"line":416},[206,1174,331],{"class":266},[206,1176,1177],{"class":226},"for",[206,1179,1180],{"class":230}," bits ",[206,1182,1183],{"class":226},"in",[206,1185,1186],{"class":314}," product",[206,1188,1189],{"class":230},"((",[206,1191,345],{"class":285},[206,1193,323],{"class":230},[206,1195,152],{"class":285},[206,1197,1198],{"class":230},"), ",[206,1200,1201],{"class":441},"repeat",[206,1203,273],{"class":272},[206,1205,350],{"class":285},[206,1207,1208],{"class":230},"):\n",[206,1210,1211,1213,1216,1218,1221,1224,1226,1229],{"class":208,"line":424},[206,1212,214],{"class":212,"aria-hidden":213},[206,1214,1215],{"class":230},"    value ",[206,1217,273],{"class":272},[206,1219,1220],{"class":230}," qp.objective.",[206,1222,1223],{"class":314},"evaluate",[206,1225,318],{"class":230},[206,1227,1228],{"class":272},"list",[206,1230,1231],{"class":230},"(bits))\n",[206,1233,1234,1236,1239,1241,1244,1247,1249,1252,1254,1257,1260,1263,1265,1267,1269,1271,1273,1275,1277,1279,1281,1284,1287,1290,1292,1294],{"class":208,"line":775},[206,1235,214],{"class":212,"aria-hidden":213},[206,1237,1238],{"class":272},"    print",[206,1240,318],{"class":230},[206,1242,1243],{"class":226},"f",[206,1245,1246],{"class":987},"\" ",[206,1248,1056],{"class":285},[206,1250,1251],{"class":230},"bits[",[206,1253,345],{"class":285},[206,1255,1256],{"class":230},"]",[206,1258,1259],{"class":285},"}",[206,1261,1262],{"class":285}," {",[206,1264,1251],{"class":230},[206,1266,152],{"class":285},[206,1268,1256],{"class":230},[206,1270,1259],{"class":285},[206,1272,1262],{"class":285},[206,1274,1251],{"class":230},[206,1276,331],{"class":285},[206,1278,1256],{"class":230},[206,1280,1259],{"class":285},[206,1282,1283],{"class":285},"   {",[206,1285,1286],{"class":230},"value",[206,1288,1289],{"class":226},":+.0f",[206,1291,1259],{"class":285},[206,1293,1162],{"class":987},[206,1295,328],{"class":230},[464,1297,1298,1305],{"class":466},[468,1299,1300,1302],{"data-cue":152},[206,1301,152],{"class":266},[206,1303,1304],{"class":474},"Each single term rewards a variable for being 1. Each pair term is a penalty for two of them being 1 together. A graph's edges are exactly a set of pair terms.",[468,1306,1307,1309],{"data-cue":331},[206,1308,331],{"class":266},[206,1310,1311],{"class":474},"Eight strings for three bits. The loop scores every one, which is the thing no solver can do once the string is long: at sixty variables the table has more rows than there have been seconds since the Big Bang.",[12,1313,1314,1315,1318],{},"The table the example prints has eight rows, and one of them, ",[52,1316,1317],{},"1 0 1",", reaches −4:",[198,1320,1325],{"className":1321,"code":1323,"language":1324,"meta":159},[1322],"language-text"," x y z   objective\n 0 0 0   +0\n 0 0 1   -3\n 0 1 0   -2\n 0 1 1   -3\n 1 0 0   -1\n 1 0 1   -4\n 1 1 0   -1\n 1 1 1   -2\n","text",[52,1326,1323],{"__ignoreMap":159},[12,1328,1329],{},"No other row gets there. That number is the ground truth for this example, and every quantum run below has one like it. The file's own header comment says so: it is \"the trusted baseline every quantum solver gets measured against\".",[12,1331,1332,1333,1336,1337,1340,1341,1344,1345,1348],{},"To become phases, the 0\u002F1 variables become spins. The substitution is ",[52,1334,1335],{},"x = (1 - z) \u002F 2",": a bit that is 0 becomes a spin that is +1 and a bit that is 1 becomes −1. A pair term ",[52,1338,1339],{},"x·y"," becomes ",[52,1342,1343],{},"(1 − z_x − z_y + z_x·z_y) \u002F 4",": a constant, a term on each spin, and a ",[52,1346,1347],{},"z·z"," term.",[12,1350,1351,1352,1354,1355,323,1357,323,1359,1361,1362,1364],{},"The ",[52,1353,1347],{}," term is the product of the two ±1 signs that two measured bits map to. That product is what the ",[52,1356,672],{},[52,1358,185],{},[52,1360,672],{}," sandwich in the one-edge circuit gives a phase to. So an edge of the graph becomes one two-qubit interaction in the circuit, and a variable's own reward becomes a single-qubit rotation, the ",[52,1363,185],{}," from the one-qubit circuit.",[928,1366],{"description":1367,"framework":931,"owner":932,"title":1368,"to":951},"How to express a combinatorial problem as a QUBO (Quadratic Unconstrained Binary Optimization) with qiskit-optimization, inspect the full solution landscape by brute force, and solve it through the same Ising-Hamiltonian route that quantum algorithms use, with an exact classical eigensolver as a trusted baseline.","QUBO, solved exactly",[25,1370,1372],{"id":1371},"the-gates-in-these-circuits","The gates in these circuits",[12,1374,1375],{},"Four gates and one instruction cover every excerpt on this page.",[1377,1378,1380],"gate",{"token":340,"name":1379},"Hadamard",[12,1381,1382,1383,1385,1386,488,1388,1390],{},"Takes a qubit sitting at ",[52,1384,345],{}," and leaves it weighted evenly between ",[52,1387,345],{},[52,1389,152],{},". Applied to every qubit it puts equal weight on every string at once, which is where each circuit here begins.",[1377,1392,1394,1407],{"token":672,"name":1393},"CNOT",[12,1395,1396,1397,1399,1400,1402,1403,478],{},"Flips the second qubit when the first is ",[52,1398,152],{},". When the first qubit is in superposition and the second sits at ",[52,1401,345],{},", this is what ties the two into ",[16,1404,1406],{"href":1405},"\u002Flearn\u002Fquantum-entanglement","one shared state",[12,1408,1409,1410,1412],{},"Here the CNOT appears in a pair around an ",[52,1411,185],{},". That sandwich is the standard way to write a two-qubit phase using only gates the hardware provides, and there is one per edge.",[1377,1414,1415,1418],{"token":185},[12,1416,1417],{},"A rotation that changes no count on its own. It moves the phase, the relative angle between the two halves of a qubit's state.",[12,1419,1420,1421,1423],{},"Inside the sandwich the ",[52,1422,185],{}," gives every string a phase that depends on the edge being cut or not. On a register, the strings that differ at this qubit are the ones that pick up different phases. Added up, one per edge, these phases are the whole cost step.",[1377,1425,1426,1434,1437],{"token":189},[12,1427,1428,1429,488,1431,1433],{},"A rotation about a different axis. On a qubit with equal weight on ",[52,1430,345],{},[52,1432,152],{}," and no relative phase, which is what the opening Hadamard leaves, it changes no count at all.",[12,1435,1436],{},"Once the cost step has set a phase between the two halves, the same rotation moves weight from one outcome to the other. How much moves depends on that phase.",[12,1438,1439],{},"This is the mixer, and it is the only step after the opening Hadamards that changes how much weight each string carries. The cost step as a whole leaves every string's weight where it was and changes only its phase. Without the mixer, those phases would never show up in a measurement.",[1377,1441,1442],{"token":19},[12,1443,1444,1445,867,1447,1449],{},"Reads a qubit into an ordinary bit and forces it to a definite ",[52,1446,345],{},[52,1448,152],{},". One run gives one string, so every sampled circuit here runs thousands of times and the answer is read off the tally.",[25,1451,1453],{"id":1452},"two-angles-sixteen-million-splits","Two angles, sixteen million splits",[12,1455,1456],{},"Dr. Siti Fariya's Quantum Courier is a browser game that races classical and quantum solvers across five logistics problems, and its fourth stage is MaxCut on twenty-four nodes. The Playground version is the script the project publishes for that stage's run on IonQ Forte.",[12,1458,1459],{},"The script's circuit is the one-edge circuit above with the cost sandwich repeated for every edge and the mixer repeated for every node:",[192,1461,1464],{"name":1462,"run-href":1463,"tag":196},"quantum_courier_maxcut_forte.py","\u002Fu\u002FSitifar\u002Fquantum-game-pizza-race",[198,1465,1467],{"className":200,"code":1466,"language":202,"meta":159,"style":159},"def qaoa_circuit(gamma, beta):\n    qc = QuantumCircuit(N_NODES, N_NODES)\n\n    # Initial state: equal superposition\n    qc.h(range(N_NODES))\n\n    # Cost layer: e^{-i gamma C}\n    for (u, v) in EDGES:\n        qc.cx(u, v)\n        qc.rz(2 * gamma, v)\n        qc.cx(u, v)\n\n    # Mixer layer: e^{-i beta B}\n    for q in range(N_NODES):\n        qc.rx(2 * beta, q)\n\n    qc.measure(range(N_NODES), range(N_NODES))\n    return qc\n",[52,1468,1469,1490,1512,1518,1525,1546,1552,1559,1577,1589,1606,1616,1622,1629,1649,1666,1673,1700,1711],{"__ignoreMap":159},[206,1470,1471,1473,1476,1479,1481,1484,1486,1488],{"class":208,"line":209},[206,1472,214],{"class":212,"aria-hidden":213},[206,1474,1475],{"class":226},"def",[206,1477,1478],{"class":314}," qaoa_circuit",[206,1480,318],{"class":230},[206,1482,54],{"class":1483},"sb9H8",[206,1485,323],{"class":230},[206,1487,58],{"class":1483},[206,1489,1208],{"class":230},[206,1491,1492,1494,1497,1499,1501,1503,1506,1508,1510],{"class":208,"line":221},[206,1493,214],{"class":212,"aria-hidden":213},[206,1495,1496],{"class":230},"    qc ",[206,1498,273],{"class":272},[206,1500,315],{"class":314},[206,1502,318],{"class":230},[206,1504,1505],{"class":285},"N_NODES",[206,1507,323],{"class":230},[206,1509,1505],{"class":285},[206,1511,328],{"class":230},[206,1513,1514,1516],{"class":208,"line":240},[206,1515,214],{"class":212,"aria-hidden":213},[206,1517,260],{},[206,1519,1520,1522],{"class":208,"line":255},[206,1521,214],{"class":212,"aria-hidden":213},[206,1523,1524],{"class":217},"    # Initial state: equal superposition\n",[206,1526,1527,1529,1532,1534,1536,1539,1541,1543],{"class":208,"data-cue":152,"line":263},[206,1528,152],{"class":266},[206,1530,1531],{"class":230},"    qc.",[206,1533,340],{"class":314},[206,1535,318],{"class":230},[206,1537,1538],{"class":272},"range",[206,1540,318],{"class":230},[206,1542,1505],{"class":285},[206,1544,1545],{"class":230},"))\n",[206,1547,1548,1550],{"class":208,"line":297},[206,1549,214],{"class":212,"aria-hidden":213},[206,1551,260],{},[206,1553,1554,1556],{"class":208,"line":304},[206,1555,214],{"class":212,"aria-hidden":213},[206,1557,1558],{"class":217},"    # Cost layer: e^{-i gamma C}\n",[206,1560,1561,1563,1566,1569,1571,1574],{"class":208,"data-cue":331,"line":332},[206,1562,331],{"class":266},[206,1564,1565],{"class":226},"    for",[206,1567,1568],{"class":230}," (u, v) ",[206,1570,1183],{"class":226},[206,1572,1573],{"class":285}," EDGES",[206,1575,1576],{"class":230},":\n",[206,1578,1579,1581,1584,1586],{"class":208,"line":351},[206,1580,214],{"class":212,"aria-hidden":213},[206,1582,1583],{"class":230},"        qc.",[206,1585,672],{"class":314},[206,1587,1588],{"class":230},"(u, v)\n",[206,1590,1591,1593,1595,1597,1599,1601,1603],{"class":208,"line":375},[206,1592,214],{"class":212,"aria-hidden":213},[206,1594,1583],{"class":230},[206,1596,185],{"class":314},[206,1598,318],{"class":230},[206,1600,331],{"class":285},[206,1602,364],{"class":272},[206,1604,1605],{"class":230}," gamma, v)\n",[206,1607,1608,1610,1612,1614],{"class":208,"line":397},[206,1609,214],{"class":212,"aria-hidden":213},[206,1611,1583],{"class":230},[206,1613,672],{"class":314},[206,1615,1588],{"class":230},[206,1617,1618,1620],{"class":208,"line":416},[206,1619,214],{"class":212,"aria-hidden":213},[206,1621,260],{},[206,1623,1624,1626],{"class":208,"line":424},[206,1625,214],{"class":212,"aria-hidden":213},[206,1627,1628],{"class":217},"    # Mixer layer: e^{-i beta B}\n",[206,1630,1631,1633,1635,1638,1640,1643,1645,1647],{"class":208,"data-cue":350,"line":775},[206,1632,350],{"class":266},[206,1634,1565],{"class":226},[206,1636,1637],{"class":230}," q ",[206,1639,1183],{"class":226},[206,1641,1642],{"class":272}," range",[206,1644,318],{"class":230},[206,1646,1505],{"class":285},[206,1648,1208],{"class":230},[206,1650,1651,1653,1655,1657,1659,1661,1663],{"class":208,"line":782},[206,1652,214],{"class":212,"aria-hidden":213},[206,1654,1583],{"class":230},[206,1656,189],{"class":314},[206,1658,318],{"class":230},[206,1660,331],{"class":285},[206,1662,364],{"class":272},[206,1664,1665],{"class":230}," beta, q)\n",[206,1667,1669,1671],{"class":208,"line":1668},16,[206,1670,214],{"class":212,"aria-hidden":213},[206,1672,260],{},[206,1674,1676,1678,1680,1682,1684,1686,1688,1690,1692,1694,1696,1698],{"class":208,"line":1675},17,[206,1677,214],{"class":212,"aria-hidden":213},[206,1679,1531],{"class":230},[206,1681,19],{"class":314},[206,1683,318],{"class":230},[206,1685,1538],{"class":272},[206,1687,318],{"class":230},[206,1689,1505],{"class":285},[206,1691,1198],{"class":230},[206,1693,1538],{"class":272},[206,1695,318],{"class":230},[206,1697,1505],{"class":285},[206,1699,1545],{"class":230},[206,1701,1703,1705,1708],{"class":208,"line":1702},18,[206,1704,214],{"class":212,"aria-hidden":213},[206,1706,1707],{"class":226},"    return",[206,1709,1710],{"class":230}," qc\n",[464,1712,1713,1720,1727],{"class":466},[468,1714,1715,1717],{"data-cue":152},[206,1716,152],{"class":266},[206,1718,1719],{"class":474},"Twenty-four Hadamards, and the register carries equal weight on all 16,777,216 splits.",[468,1721,1722,1724],{"data-cue":331},[206,1723,331],{"class":266},[206,1725,1726],{"class":474},"One sandwich per edge, thirty-seven of them. Each shifts a split's phase by an amount that depends on whether that edge is cut, so a split's total phase depends only on how many edges it cuts.",[468,1728,1729,1731],{"data-cue":350},[206,1730,350],{"class":266},[206,1732,1733,1734,1736],{"class":474},"One ",[52,1735,189],{}," per node, the same mixer as before. Still the only place weight moves between splits.",[12,1738,1739],{},"The graph is the one from the top of the page: twenty-four nodes, thirty-seven edges, a ring with chords across it. Scoring all 16,777,216 splits takes a laptop under a second. A random split cuts 18.5 edges on average, no split cuts more than 33, and exactly six splits reach that.",[155,1741],{"alt":1742,"caption":1743,"no":159,"src":1744},"Quantum Courier's 24-node graph drawn on a ring with chords, nodes coloured by an optimal split, 33 solid edges crossing between the groups and 4 dashed edges staying inside one","The instance Quantum Courier ships, coloured by one of its six best splits. Thirty-three of the thirty-seven edges cross between the groups.","\u002F_content\u002Fimages\u002Foptimization\u002Fcourier-graph-cut.svg",[12,1746,1747],{},"Scoring is classical code. For each measured string the script counts the edges whose endpoints landed on different sides, then averages over the shots:",[192,1749,1750],{"name":1462,"run-href":1463,"tag":196},[198,1751,1753],{"className":200,"code":1752,"language":202,"meta":159,"style":159},"def cut_value(bitstring):\n    # Qiskit returns bitstrings in reverse: bit i lives at position N-1-i\n    bits = [int(b) for b in bitstring[::-1]]\n    return sum(1 for (u, v) in EDGES if bits[u] != bits[v])\n\n\ndef expected_cut(counts, shots):\n    return sum(cut_value(bs) * c for bs, c in counts.items()) \u002F shots\n",[52,1754,1755,1771,1778,1813,1847,1853,1859,1879,1922],{"__ignoreMap":159},[206,1756,1757,1759,1761,1764,1766,1769],{"class":208,"line":209},[206,1758,214],{"class":212,"aria-hidden":213},[206,1760,1475],{"class":226},[206,1762,1763],{"class":314}," cut_value",[206,1765,318],{"class":230},[206,1767,1768],{"class":1483},"bitstring",[206,1770,1208],{"class":230},[206,1772,1773,1775],{"class":208,"line":221},[206,1774,214],{"class":212,"aria-hidden":213},[206,1776,1777],{"class":217},"    # Qiskit returns bitstrings in reverse: bit i lives at position N-1-i\n",[206,1779,1780,1782,1785,1787,1790,1793,1796,1798,1801,1803,1806,1808,1810],{"class":208,"data-cue":152,"line":240},[206,1781,152],{"class":266},[206,1783,1784],{"class":230},"    bits ",[206,1786,273],{"class":272},[206,1788,1789],{"class":230}," [",[206,1791,1792],{"class":272},"int",[206,1794,1795],{"class":230},"(b) ",[206,1797,1177],{"class":226},[206,1799,1800],{"class":230}," b ",[206,1802,1183],{"class":226},[206,1804,1805],{"class":230}," bitstring[::",[206,1807,1064],{"class":272},[206,1809,152],{"class":285},[206,1811,1812],{"class":230},"]]\n",[206,1814,1815,1817,1819,1822,1824,1826,1829,1831,1833,1835,1838,1841,1844],{"class":208,"data-cue":331,"line":255},[206,1816,331],{"class":266},[206,1818,1707],{"class":226},[206,1820,1821],{"class":272}," sum",[206,1823,318],{"class":230},[206,1825,152],{"class":285},[206,1827,1828],{"class":226}," for",[206,1830,1568],{"class":230},[206,1832,1183],{"class":226},[206,1834,1573],{"class":285},[206,1836,1837],{"class":226}," if",[206,1839,1840],{"class":230}," bits[u] ",[206,1842,1843],{"class":272},"!=",[206,1845,1846],{"class":230}," bits[v])\n",[206,1848,1849,1851],{"class":208,"line":263},[206,1850,214],{"class":212,"aria-hidden":213},[206,1852,260],{},[206,1854,1855,1857],{"class":208,"line":297},[206,1856,214],{"class":212,"aria-hidden":213},[206,1858,260],{},[206,1860,1861,1863,1865,1868,1870,1873,1875,1877],{"class":208,"line":304},[206,1862,214],{"class":212,"aria-hidden":213},[206,1864,1475],{"class":226},[206,1866,1867],{"class":314}," expected_cut",[206,1869,318],{"class":230},[206,1871,1872],{"class":1483},"counts",[206,1874,323],{"class":230},[206,1876,442],{"class":1483},[206,1878,1208],{"class":230},[206,1880,1881,1883,1885,1887,1889,1892,1895,1898,1901,1903,1906,1908,1911,1914,1917,1919],{"class":208,"data-cue":350,"line":332},[206,1882,350],{"class":266},[206,1884,1707],{"class":226},[206,1886,1821],{"class":272},[206,1888,318],{"class":230},[206,1890,1891],{"class":314},"cut_value",[206,1893,1894],{"class":230},"(bs) ",[206,1896,1897],{"class":272},"*",[206,1899,1900],{"class":230}," c ",[206,1902,1177],{"class":226},[206,1904,1905],{"class":230}," bs, c ",[206,1907,1183],{"class":226},[206,1909,1910],{"class":230}," counts.",[206,1912,1913],{"class":314},"items",[206,1915,1916],{"class":230},"()) ",[206,1918,282],{"class":272},[206,1920,1921],{"class":230}," shots\n",[464,1923,1924,1931,1938],{"class":466},[468,1925,1926,1928],{"data-cue":152},[206,1927,152],{"class":266},[206,1929,1930],{"class":474},"A measured string is one split of the graph. Reversing it is a Qiskit convention: qubit 0 is the last character.",[468,1932,1933,1935],{"data-cue":331},[206,1934,331],{"class":266},[206,1936,1937],{"class":474},"The cut: one point per edge whose ends disagree. This is the score, and it never touches a qubit.",[468,1939,1940,1942],{"data-cue":350},[206,1941,350],{"class":266},[206,1943,1944],{"class":474},"The average over every shot, which is the number an optimizer steers by.",[12,1946,1947],{},"On an exact simulator, which keeps the weight of every split rather than sampling, the circuit's output distribution can be computed for any pair of angles. Because every split can be scored, the figure below does that for two.",[12,1949,1950,1951,323,1954,478],{},"Grey is a random split, or equally the circuit with both angles at zero. Purple is the pair tuned for this graph, ",[52,1952,1953],{},"gamma = -0.30",[52,1955,1956],{},"beta = 0.39",[155,1958],{"alt":1959,"caption":1960,"no":159,"src":1961},"Two overlaid bar charts of how often each cut value appears: a grey bell centred on 18.5 for random splits and a purple bell centred near 25.5 for the tuned circuit, with the best possible value of 33 marked","How often each cut value comes up. A random split averages 18.5 edges. One tuned QAOA layer averages 25.5, and the best possible split, 33, moves from one shot in 2.8 million to one in a thousand.","\u002F_content\u002Fimages\u002Foptimization\u002Fcut-distribution.svg",[12,1963,1964],{},"One layer moves weight off the bad splits and onto the good ones, and the bell slides seven edges to the right. Nothing in the circuit finds the best split and hands it over; the best split becomes a more frequent sample.",[12,1966,1967],{},"A sample from the purple bell is optimal about once in a thousand shots, against once in 2.8 million from the grey one. In a 4,096-shot job the best split therefore shows up about four times, and the best string in the tally is the best split on almost every run.",[12,1969,1970,1971,1973,1974,1976],{},"The table below is one layer on the same 24-node, 37-edge instance at four pairs of angles, every row derived from the exact distribution. In it, ",[52,1972,54],{}," is the cost angle and ",[52,1975,58],{}," the mixer angle. The best possible cut is 33:",[1978,1979,1980,1999],"table",{},[1981,1982,1983],"thead",{},[1984,1985,1986,1990,1993,1996],"tr",{},[1987,1988,1989],"th",{},"angles (gamma, beta)",[1987,1991,1992],{},"average cut",[1987,1994,1995],{},"chance of a 33-edge cut, per sample",[1987,1997,1998],{},"expected best cut in 4,096 shots",[2000,2001,2002,2017,2030,2044],"tbody",{},[1984,2003,2004,2008,2011,2014],{},[2005,2006,2007],"td",{},"0, 0 (a random split)",[2005,2009,2010],{},"18.5",[2005,2012,2013],{},"1 in 2,800,000",[2005,2015,2016],{},"29",[1984,2018,2019,2022,2024,2027],{},[2005,2020,2021],{},"−π\u002F4, π\u002F8 (exact for one edge)",[2005,2023,2010],{},[2005,2025,2026],{},"1 in 34,000,000",[2005,2028,2029],{},"28",[1984,2031,2032,2035,2038,2041],{},[2005,2033,2034],{},"−0.30, 0.39 (tuned for this graph)",[2005,2036,2037],{},"25.5",[2005,2039,2040],{},"1 in 980",[2005,2042,2043],{},"33",[1984,2045,2046,2049,2052,2055],{},[2005,2047,2048],{},"+0.30, 0.39 (same size, other sign)",[2005,2050,2051],{},"11.5",[2005,2053,2054],{},"under 1 in a trillion",[2005,2056,2057],{},"22",[12,2059,2060],{},"On twenty-four nodes the pair that is exact on one edge leaves the average at 18.5, the same as a random split, and makes the best split rarer. Which angles are best depends on how many edges meet at each node, so angles do not transfer from a toy to a real instance.",[12,2062,2063,2064,2066],{},"And the sign pairing matters as much as the size. With ",[52,2065,54],{}," flipped the same circuit averages 11.5 edges, well below a random split, because it is now concentrating weight on the splits that cut the fewest edges.",[928,2068],{"description":2069,"framework":931,"owner":2070,"title":2071,"to":1463,"builder":2072,"thumb":2073},"Dr. Siti Fariya built a browser game that races classical and quantum solvers across five real logistics problems, and shows honestly which one wins, and why.","Sitifar","Quantum Courier","Dr. Siti Fariya","\u002F_content\u002Fimages\u002Fquantum-courier\u002Fscreenshot.webp",[25,2075,2077],{"id":2076},"the-loop-runs-on-the-simulator","The loop runs on the simulator",[12,2079,2080],{},"Finding the purple pair is the classical optimizer's job, and each step of that job is a full run of the circuit. Nothing quantum happens in the loop. Starting from a guess, the optimizer runs the circuit at that pair and reads the average cut. Then it nudges the pair and runs again: fifty times in Courier's benchmark and the official example, a few hundred in the Oracle.",[12,2082,2083],{},"When the budget runs out the optimizer returns the best pair it saw. Quantum Courier's script defines the objective that loop calls.",[12,2085,2086,2087,488,2090,2093],{},"For a single Forte job the script skips the loop and fixes ",[52,2088,2089],{},"GAMMA_STAR",[52,2091,2092],{},"BETA_STAR"," before submitting, with a comment that says so. In a fork, those two constants are where the pairs from the table go.",[12,2095,2096],{},"The objective itself wraps one run:",[192,2098,2099],{"name":1462,"run-href":1463,"tag":196},[198,2100,2102],{"className":200,"code":2101,"language":202,"meta":159,"style":159},"def objective(params):\n    \"\"\"Negative expected cut — minimised by COBYLA.\"\"\"\n    bound = template.assign_parameters({gamma_p: params[0], beta_p: params[1]})\n    job = backend.run(bound, shots=SHOTS)\n    counts = job.result().get_counts()\n    return -expected_cut(counts, SHOTS)\n",[52,2103,2104,2120,2127,2155,2181,2202,2220],{"__ignoreMap":159},[206,2105,2106,2108,2110,2113,2115,2118],{"class":208,"line":209},[206,2107,214],{"class":212,"aria-hidden":213},[206,2109,1475],{"class":226},[206,2111,2112],{"class":314}," objective",[206,2114,318],{"class":230},[206,2116,2117],{"class":1483},"params",[206,2119,1208],{"class":230},[206,2121,2122,2124],{"class":208,"line":221},[206,2123,214],{"class":212,"aria-hidden":213},[206,2125,2126],{"class":987},"    \"\"\"Negative expected cut — minimised by COBYLA.\"\"\"\n",[206,2128,2129,2131,2134,2136,2139,2142,2145,2147,2150,2152],{"class":208,"data-cue":152,"line":240},[206,2130,152],{"class":266},[206,2132,2133],{"class":230},"    bound ",[206,2135,273],{"class":272},[206,2137,2138],{"class":230}," template.",[206,2140,2141],{"class":314},"assign_parameters",[206,2143,2144],{"class":230},"({gamma_p: params[",[206,2146,345],{"class":285},[206,2148,2149],{"class":230},"], beta_p: params[",[206,2151,152],{"class":285},[206,2153,2154],{"class":230},"]})\n",[206,2156,2157,2159,2162,2164,2167,2169,2172,2174,2176,2179],{"class":208,"data-cue":331,"line":255},[206,2158,331],{"class":266},[206,2160,2161],{"class":230},"    job ",[206,2163,273],{"class":272},[206,2165,2166],{"class":230}," backend.",[206,2168,435],{"class":314},[206,2170,2171],{"class":230},"(bound, ",[206,2173,442],{"class":441},[206,2175,273],{"class":272},[206,2177,2178],{"class":285},"SHOTS",[206,2180,328],{"class":230},[206,2182,2183,2185,2188,2190,2193,2195,2197,2199],{"class":208,"line":263},[206,2184,214],{"class":212,"aria-hidden":213},[206,2186,2187],{"class":230},"    counts ",[206,2189,273],{"class":272},[206,2191,2192],{"class":230}," job.",[206,2194,453],{"class":314},[206,2196,456],{"class":230},[206,2198,459],{"class":314},[206,2200,2201],{"class":230},"()\n",[206,2203,2204,2206,2208,2210,2213,2216,2218],{"class":208,"data-cue":350,"line":297},[206,2205,350],{"class":266},[206,2207,1707],{"class":226},[206,2209,276],{"class":272},[206,2211,2212],{"class":314},"expected_cut",[206,2214,2215],{"class":230},"(counts, ",[206,2217,2178],{"class":285},[206,2219,328],{"class":230},[464,2221,2222,2229,2236],{"class":466},[468,2223,2224,2226],{"data-cue":152},[206,2225,152],{"class":266},[206,2227,2228],{"class":474},"One candidate pair of angles goes into the circuit template.",[468,2230,2231,2233],{"data-cue":331},[206,2232,331],{"class":266},[206,2234,2235],{"class":474},"One evaluation is one job of 4,096 shots. On hardware that is a queue, a wait and a bill, and an optimizer typically wants fifty of them.",[468,2237,2238,2240],{"data-cue":350},[206,2239,350],{"class":266},[206,2241,2242],{"class":474},"The average cut, negated because COBYLA minimizes. The optimizer never sees a qubit, only this number.",[12,2244,2245],{},"Fifty evaluations at 4,096 shots is 204,800 shots for two numbers, and on a shared machine each evaluation waits in a queue. So both hardware projects on this page tune their angles on a simulator and send the finished circuit to the machine once.",[12,2247,2248],{},"Jamie Dominguez's Quantum Systemic Oracle picks a portfolio of eight crypto assets the same way. Its QUBO is built from market data, and the next section takes that cost apart.",[12,2250,2251],{},"The circuit it sends to the machine has fourteen qubits: one decision qubit per asset, plus six that carry market state. The tuning runs on the eight asset qubits alone, inside the Playground file, with a Nelder-Mead optimizer written in plain Python.",[12,2253,2254,2255,2257,2258,2261],{},"Below is how that optimizer scores each candidate pair. In it, ",[52,2256,340],{}," holds one number per asset and ",[52,2259,2260],{},"J_pairs"," one number per pair of assets: the two kinds of term the cost is made of.",[192,2263,2266],{"name":2264,"run-href":2265,"tag":196},"Qollab.py","\u002Fu\u002Fjamie\u002Fsystemic-oracle",[198,2267,2269],{"className":200,"code":2268,"language":202,"meta":159,"style":159},"def optimize_angles(h, J_pairs, n, layers) -> Tuple[List[float], List[float], float]:\n    \"\"\"Best (gammas, betas) by multi-start Nelder-Mead on \u003CH_C>.\"\"\"\n    H = build_hamiltonian(h, J_pairs, n)\n\n    def energy(angles: List[float]) -> float:\n        gammas, betas = angles[:layers], angles[layers:]\n        sv = Statevector.from_instruction(\n            asset_only_qaoa_circuit(gammas, betas, h, J_pairs, n))\n        return float(np.real(sv.expectation_value(H)))\n",[52,2270,2271,2317,2324,2339,2345,2372,2384,2402,2412,2437],{"__ignoreMap":159},[206,2272,2273,2275,2277,2280,2282,2284,2286,2288,2290,2293,2295,2298,2301,2304,2307,2309,2312,2314],{"class":208,"line":209},[206,2274,214],{"class":212,"aria-hidden":213},[206,2276,1475],{"class":226},[206,2278,2279],{"class":314}," optimize_angles",[206,2281,318],{"class":230},[206,2283,340],{"class":1483},[206,2285,323],{"class":230},[206,2287,2260],{"class":1483},[206,2289,323],{"class":230},[206,2291,2292],{"class":1483},"n",[206,2294,323],{"class":230},[206,2296,2297],{"class":1483},"layers",[206,2299,2300],{"class":230},") -> Tuple[List[",[206,2302,2303],{"class":272},"float",[206,2305,2306],{"class":230},"], List[",[206,2308,2303],{"class":272},[206,2310,2311],{"class":230},"], ",[206,2313,2303],{"class":272},[206,2315,2316],{"class":230},"]:\n",[206,2318,2319,2321],{"class":208,"line":221},[206,2320,214],{"class":212,"aria-hidden":213},[206,2322,2323],{"class":987},"    \"\"\"Best (gammas, betas) by multi-start Nelder-Mead on \u003CH_C>.\"\"\"\n",[206,2325,2326,2328,2331,2333,2336],{"class":208,"line":240},[206,2327,214],{"class":212,"aria-hidden":213},[206,2329,2330],{"class":230},"    H ",[206,2332,273],{"class":272},[206,2334,2335],{"class":314}," build_hamiltonian",[206,2337,2338],{"class":230},"(h, J_pairs, n)\n",[206,2340,2341,2343],{"class":208,"line":255},[206,2342,214],{"class":212,"aria-hidden":213},[206,2344,260],{},[206,2346,2347,2349,2352,2355,2357,2360,2363,2365,2368,2370],{"class":208,"line":263},[206,2348,214],{"class":212,"aria-hidden":213},[206,2350,2351],{"class":226},"    def",[206,2353,2354],{"class":314}," energy",[206,2356,318],{"class":230},[206,2358,2359],{"class":1483},"angles",[206,2361,2362],{"class":230},": List[",[206,2364,2303],{"class":272},[206,2366,2367],{"class":230},"]) -> ",[206,2369,2303],{"class":272},[206,2371,1576],{"class":230},[206,2373,2374,2376,2379,2381],{"class":208,"line":297},[206,2375,214],{"class":212,"aria-hidden":213},[206,2377,2378],{"class":230},"        gammas, betas ",[206,2380,273],{"class":272},[206,2382,2383],{"class":230}," angles[:layers], angles[layers:]\n",[206,2385,2386,2388,2391,2393,2396,2399],{"class":208,"line":304},[206,2387,214],{"class":212,"aria-hidden":213},[206,2389,2390],{"class":230},"        sv ",[206,2392,273],{"class":272},[206,2394,2395],{"class":230}," Statevector.",[206,2397,2398],{"class":314},"from_instruction",[206,2400,2401],{"class":230},"(\n",[206,2403,2404,2406,2409],{"class":208,"data-cue":152,"line":332},[206,2405,152],{"class":266},[206,2407,2408],{"class":314},"            asset_only_qaoa_circuit",[206,2410,2411],{"class":230},"(gammas, betas, h, J_pairs, n))\n",[206,2413,2414,2416,2419,2422,2425,2428,2431,2434],{"class":208,"data-cue":331,"line":351},[206,2415,331],{"class":266},[206,2417,2418],{"class":226},"        return",[206,2420,2421],{"class":272}," float",[206,2423,2424],{"class":230},"(np.",[206,2426,2427],{"class":314},"real",[206,2429,2430],{"class":230},"(sv.",[206,2432,2433],{"class":314},"expectation_value",[206,2435,2436],{"class":230},"(H)))\n",[464,2438,2439,2446],{"class":466},[468,2440,2441,2443],{"data-cue":152},[206,2442,152],{"class":266},[206,2444,2445],{"class":474},"The full state of the eight asset qubits, computed exactly. No shots and no noise, which is what makes each step cheap enough to take hundreds of them.",[468,2447,2448,2450],{"data-cue":331},[206,2449,331],{"class":266},[206,2451,2452],{"class":474},"The average cost under that state, read straight off the weight of every string. A real machine can only estimate this number from a tally.",[12,2454,2455],{},"On the simulator the objective is exact. On hardware it is an estimate, and how good an estimate is a number you can derive. At the tuned pair a single sample's cut has a standard deviation of 2.6 edges. Over 4,096 shots the average is known to about 0.04 of an edge, one standard error.",[12,2457,2458],{},"Shot noise is a small part of what makes the loop hard. Most of the difficulty is that every evaluation is a job, and that the landscape being searched has the shape of the one-edge figure above with more ridges.",[12,2460,2461,2465],{},[16,2462,2464],{"href":2463},"\u002Flearn\u002Fdocs\u002Fcompute-backends","Three ways of running a circuit"," appear on this page. An exact simulator keeps every weight and needs no shots; the Oracle's tuner uses one. A sampled simulator returns a tally the way hardware does, with shot noise but none of the device's.",[12,2467,2468],{},"The machine itself returns a tally with the device's noise in it. Courier's objective, which always asks for shots, runs on whichever of the last two its backend is pointed at.",[25,2470,2472],{"id":2471},"constraints-become-penalties","Constraints become penalties",[12,2474,2475],{},"The Oracle's problem is portfolio selection: which of eight crypto assets to hold, given their expected returns and how their prices move together. A budget rule says how many to hold, and a QUBO has no constraints by definition, so the rule has to become part of the cost. The file does it like this:",[192,2477,2478],{"name":2264,"run-href":2265,"tag":196},[198,2479,2481],{"className":200,"code":2480,"language":202,"meta":159,"style":159},"Q = [[0.0] * n for _ in range(n)]\nfor i in range(n):\n    Q[i][i] += lam * var[i] - mu[i] - ALPHA_POLY * poly[i] + BETA_FUNDING * fnd[i]\n    Q[i][i] += ALPHA_DISAGREE * disagree[i] - ALPHA_SIDEWAYS * sideways[i] + macro_tilt\n    for j in range(n):\n        if i != j:\n            Q[i][j] += lam * corr[i][j] * math.sqrt(var[i] * var[j])\nfor i in range(n):\n    Q[i][i] += BUDGET_PENALTY * (1 - 2 * k_target)\n    for j in range(n):\n        if i != j:\n            Q[i][j] += BUDGET_PENALTY\n",[52,2482,2483,2518,2534,2578,2609,2624,2638,2670,2684,2712,2726,2738,2749],{"__ignoreMap":159},[206,2484,2485,2487,2490,2492,2495,2498,2501,2503,2506,2508,2511,2513,2515],{"class":208,"line":209},[206,2486,214],{"class":212,"aria-hidden":213},[206,2488,2489],{"class":230},"Q ",[206,2491,273],{"class":272},[206,2493,2494],{"class":230}," [[",[206,2496,2497],{"class":285},"0.0",[206,2499,2500],{"class":230},"] ",[206,2502,1897],{"class":272},[206,2504,2505],{"class":230}," n ",[206,2507,1177],{"class":226},[206,2509,2510],{"class":230}," _ ",[206,2512,1183],{"class":226},[206,2514,1642],{"class":272},[206,2516,2517],{"class":230},"(n)]\n",[206,2519,2520,2522,2524,2527,2529,2531],{"class":208,"line":221},[206,2521,214],{"class":212,"aria-hidden":213},[206,2523,1177],{"class":226},[206,2525,2526],{"class":230}," i ",[206,2528,1183],{"class":226},[206,2530,1642],{"class":272},[206,2532,2533],{"class":230},"(n):\n",[206,2535,2536,2538,2541,2544,2547,2549,2552,2554,2557,2559,2562,2564,2567,2570,2573,2575],{"class":208,"data-cue":152,"line":240},[206,2537,152],{"class":266},[206,2539,2540],{"class":230},"    Q[i][i] ",[206,2542,2543],{"class":272},"+=",[206,2545,2546],{"class":230}," lam ",[206,2548,1897],{"class":272},[206,2550,2551],{"class":230}," var[i] ",[206,2553,1064],{"class":272},[206,2555,2556],{"class":230}," mu[i] ",[206,2558,1064],{"class":272},[206,2560,2561],{"class":285}," ALPHA_POLY",[206,2563,364],{"class":272},[206,2565,2566],{"class":230}," poly[i] ",[206,2568,2569],{"class":272},"+",[206,2571,2572],{"class":285}," BETA_FUNDING",[206,2574,364],{"class":272},[206,2576,2577],{"class":230}," fnd[i]\n",[206,2579,2580,2582,2584,2586,2589,2591,2594,2596,2599,2601,2604,2606],{"class":208,"line":255},[206,2581,214],{"class":212,"aria-hidden":213},[206,2583,2540],{"class":230},[206,2585,2543],{"class":272},[206,2587,2588],{"class":285}," ALPHA_DISAGREE",[206,2590,364],{"class":272},[206,2592,2593],{"class":230}," disagree[i] ",[206,2595,1064],{"class":272},[206,2597,2598],{"class":285}," ALPHA_SIDEWAYS",[206,2600,364],{"class":272},[206,2602,2603],{"class":230}," sideways[i] ",[206,2605,2569],{"class":272},[206,2607,2608],{"class":230}," macro_tilt\n",[206,2610,2611,2613,2615,2618,2620,2622],{"class":208,"line":263},[206,2612,214],{"class":212,"aria-hidden":213},[206,2614,1565],{"class":226},[206,2616,2617],{"class":230}," j ",[206,2619,1183],{"class":226},[206,2621,1642],{"class":272},[206,2623,2533],{"class":230},[206,2625,2626,2628,2631,2633,2635],{"class":208,"line":297},[206,2627,214],{"class":212,"aria-hidden":213},[206,2629,2630],{"class":226},"        if",[206,2632,2526],{"class":230},[206,2634,1843],{"class":272},[206,2636,2637],{"class":230}," j:\n",[206,2639,2640,2642,2645,2647,2649,2651,2654,2656,2659,2662,2665,2667],{"class":208,"data-cue":331,"line":304},[206,2641,331],{"class":266},[206,2643,2644],{"class":230},"            Q[i][j] ",[206,2646,2543],{"class":272},[206,2648,2546],{"class":230},[206,2650,1897],{"class":272},[206,2652,2653],{"class":230}," corr[i][j] ",[206,2655,1897],{"class":272},[206,2657,2658],{"class":230}," math.",[206,2660,2661],{"class":314},"sqrt",[206,2663,2664],{"class":230},"(var[i] ",[206,2666,1897],{"class":272},[206,2668,2669],{"class":230}," var[j])\n",[206,2671,2672,2674,2676,2678,2680,2682],{"class":208,"line":332},[206,2673,214],{"class":212,"aria-hidden":213},[206,2675,1177],{"class":226},[206,2677,2526],{"class":230},[206,2679,1183],{"class":226},[206,2681,1642],{"class":272},[206,2683,2533],{"class":230},[206,2685,2686,2688,2690,2692,2695,2697,2700,2702,2704,2707,2709],{"class":208,"data-cue":350,"line":351},[206,2687,350],{"class":266},[206,2689,2540],{"class":230},[206,2691,2543],{"class":272},[206,2693,2694],{"class":285}," BUDGET_PENALTY",[206,2696,364],{"class":272},[206,2698,2699],{"class":230}," (",[206,2701,152],{"class":285},[206,2703,276],{"class":272},[206,2705,2706],{"class":285}," 2",[206,2708,364],{"class":272},[206,2710,2711],{"class":230}," k_target)\n",[206,2713,2714,2716,2718,2720,2722,2724],{"class":208,"line":375},[206,2715,214],{"class":212,"aria-hidden":213},[206,2717,1565],{"class":226},[206,2719,2617],{"class":230},[206,2721,1183],{"class":226},[206,2723,1642],{"class":272},[206,2725,2533],{"class":230},[206,2727,2728,2730,2732,2734,2736],{"class":208,"line":397},[206,2729,214],{"class":212,"aria-hidden":213},[206,2731,2630],{"class":226},[206,2733,2526],{"class":230},[206,2735,1843],{"class":272},[206,2737,2637],{"class":230},[206,2739,2740,2742,2744,2746],{"class":208,"line":416},[206,2741,214],{"class":212,"aria-hidden":213},[206,2743,2644],{"class":230},[206,2745,2543],{"class":272},[206,2747,2748],{"class":285}," BUDGET_PENALTY\n",[464,2750,2751,2758,2765],{"class":466},[468,2752,2753,2755],{"data-cue":152},[206,2754,152],{"class":266},[206,2756,2757],{"class":474},"The single terms: one number per asset from its own return, risk and market signals.",[468,2759,2760,2762],{"data-cue":331},[206,2761,331],{"class":266},[206,2763,2764],{"class":474},"The pair terms. Two assets that move together are penalised for being held together, which is Markowitz's diversification written as a QUBO.",[468,2766,2767,2769],{"data-cue":350},[206,2768,350],{"class":266},[206,2770,2771,2772,2775],{"class":474},"The budget rule, folded into the cost rather than checked outside it: a term per asset that depends on the target count ",[52,2773,2774],{},"k_target",", and a term per pair that charges for holding two assets at once. The number of assets held is priced, not enforced.",[12,2777,2778,2779,2781],{},"Converting to spins uses the substitution from before, ",[52,2780,1335],{},", named in the file's own comment, and the file then rescales the result, a step the one-edge circuit did not need:",[192,2783,2784],{"name":2264,"run-href":2265,"tag":196},[198,2785,2787],{"className":200,"code":2786,"language":202,"meta":159,"style":159},"# QUBO -> Ising via x_i = (1 - Z_i) \u002F 2\nh = [0.0] * n\nJ = [[0.0] * n for _ in range(n)]\nfor i in range(n):\n    h[i] += -Q[i][i] \u002F 2.0\n    for j in range(n):\n        if i != j:\n            h[i] += -Q[i][j] \u002F 4.0\n            J[i][j] += Q[i][j] \u002F 4.0\nJ_pairs = [{\"i\": i, \"j\": j, \"value\": J[i][j] + J[j][i]}\n           for i in range(n) for j in range(i + 1, n)]\n\n# Normalize so the largest |coeff| ~ 1 (keeps QAOA angles meaningful; the\n# classical optimum and reported costs always use the raw Q matrix).\nscale = max([abs(x) for x in h] + [abs(p[\"value\"]) for p in J_pairs] + [1e-9])\nreturn {\n    \"lambda\": lam,\n    \"target_k\": k_target,\n    \"Q_matrix\": Q,\n    \"ising_h\": [x \u002F scale for x in h],\n    \"ising_J_pairs\": [{\"i\": p[\"i\"], \"j\": p[\"j\"], \"value\": p[\"value\"] \u002F scale} for p in J_pairs],\n}\n",[52,2788,2789,2796,2816,2845,2859,2878,2892,2904,2923,2939,2974,3009,3015,3022,3029,3092,3102,3112,3122,3133,3158,3208,3216],{"__ignoreMap":159},[206,2790,2791,2793],{"class":208,"line":209},[206,2792,214],{"class":212,"aria-hidden":213},[206,2794,2795],{"class":217},"# QUBO -> Ising via x_i = (1 - Z_i) \u002F 2\n",[206,2797,2798,2800,2803,2805,2807,2809,2811,2813],{"class":208,"line":221},[206,2799,214],{"class":212,"aria-hidden":213},[206,2801,2802],{"class":230},"h ",[206,2804,273],{"class":272},[206,2806,1789],{"class":230},[206,2808,2497],{"class":285},[206,2810,2500],{"class":230},[206,2812,1897],{"class":272},[206,2814,2815],{"class":230}," n\n",[206,2817,2818,2820,2823,2825,2827,2829,2831,2833,2835,2837,2839,2841,2843],{"class":208,"line":240},[206,2819,214],{"class":212,"aria-hidden":213},[206,2821,2822],{"class":230},"J ",[206,2824,273],{"class":272},[206,2826,2494],{"class":230},[206,2828,2497],{"class":285},[206,2830,2500],{"class":230},[206,2832,1897],{"class":272},[206,2834,2505],{"class":230},[206,2836,1177],{"class":226},[206,2838,2510],{"class":230},[206,2840,1183],{"class":226},[206,2842,1642],{"class":272},[206,2844,2517],{"class":230},[206,2846,2847,2849,2851,2853,2855,2857],{"class":208,"line":255},[206,2848,214],{"class":212,"aria-hidden":213},[206,2850,1177],{"class":226},[206,2852,2526],{"class":230},[206,2854,1183],{"class":226},[206,2856,1642],{"class":272},[206,2858,2533],{"class":230},[206,2860,2861,2863,2866,2868,2870,2873,2875],{"class":208,"data-cue":152,"line":263},[206,2862,152],{"class":266},[206,2864,2865],{"class":230},"    h[i] ",[206,2867,2543],{"class":272},[206,2869,276],{"class":272},[206,2871,2872],{"class":230},"Q[i][i] ",[206,2874,282],{"class":272},[206,2876,2877],{"class":285}," 2.0\n",[206,2879,2880,2882,2884,2886,2888,2890],{"class":208,"line":297},[206,2881,214],{"class":212,"aria-hidden":213},[206,2883,1565],{"class":226},[206,2885,2617],{"class":230},[206,2887,1183],{"class":226},[206,2889,1642],{"class":272},[206,2891,2533],{"class":230},[206,2893,2894,2896,2898,2900,2902],{"class":208,"line":304},[206,2895,214],{"class":212,"aria-hidden":213},[206,2897,2630],{"class":226},[206,2899,2526],{"class":230},[206,2901,1843],{"class":272},[206,2903,2637],{"class":230},[206,2905,2906,2908,2911,2913,2915,2918,2920],{"class":208,"line":332},[206,2907,214],{"class":212,"aria-hidden":213},[206,2909,2910],{"class":230},"            h[i] ",[206,2912,2543],{"class":272},[206,2914,276],{"class":272},[206,2916,2917],{"class":230},"Q[i][j] ",[206,2919,282],{"class":272},[206,2921,2922],{"class":285}," 4.0\n",[206,2924,2925,2927,2930,2932,2935,2937],{"class":208,"data-cue":331,"line":351},[206,2926,331],{"class":266},[206,2928,2929],{"class":230},"            J[i][j] ",[206,2931,2543],{"class":272},[206,2933,2934],{"class":230}," Q[i][j] ",[206,2936,282],{"class":272},[206,2938,2922],{"class":285},[206,2940,2941,2943,2946,2948,2951,2954,2957,2960,2963,2966,2969,2971],{"class":208,"line":375},[206,2942,214],{"class":212,"aria-hidden":213},[206,2944,2945],{"class":230},"J_pairs ",[206,2947,273],{"class":272},[206,2949,2950],{"class":230}," [{",[206,2952,2953],{"class":987},"\"i\"",[206,2955,2956],{"class":230},": i, ",[206,2958,2959],{"class":987},"\"j\"",[206,2961,2962],{"class":230},": j, ",[206,2964,2965],{"class":987},"\"value\"",[206,2967,2968],{"class":230},": J[i][j] ",[206,2970,2569],{"class":272},[206,2972,2973],{"class":230}," J[j][i]}\n",[206,2975,2976,2978,2981,2983,2985,2987,2990,2992,2994,2996,2998,3001,3003,3006],{"class":208,"line":397},[206,2977,214],{"class":212,"aria-hidden":213},[206,2979,2980],{"class":226},"           for",[206,2982,2526],{"class":230},[206,2984,1183],{"class":226},[206,2986,1642],{"class":272},[206,2988,2989],{"class":230},"(n) ",[206,2991,1177],{"class":226},[206,2993,2617],{"class":230},[206,2995,1183],{"class":226},[206,2997,1642],{"class":272},[206,2999,3000],{"class":230},"(i ",[206,3002,2569],{"class":272},[206,3004,3005],{"class":285}," 1",[206,3007,3008],{"class":230},", n)]\n",[206,3010,3011,3013],{"class":208,"line":416},[206,3012,214],{"class":212,"aria-hidden":213},[206,3014,260],{},[206,3016,3017,3019],{"class":208,"line":424},[206,3018,214],{"class":212,"aria-hidden":213},[206,3020,3021],{"class":217},"# Normalize so the largest |coeff| ~ 1 (keeps QAOA angles meaningful; the\n",[206,3023,3024,3026],{"class":208,"line":775},[206,3025,214],{"class":212,"aria-hidden":213},[206,3027,3028],{"class":217},"# classical optimum and reported costs always use the raw Q matrix).\n",[206,3030,3031,3033,3036,3038,3041,3043,3046,3049,3051,3054,3056,3059,3061,3063,3065,3068,3070,3073,3075,3078,3080,3083,3085,3087,3090],{"class":208,"line":782},[206,3032,214],{"class":212,"aria-hidden":213},[206,3034,3035],{"class":230},"scale ",[206,3037,273],{"class":272},[206,3039,3040],{"class":272}," max",[206,3042,654],{"class":230},[206,3044,3045],{"class":272},"abs",[206,3047,3048],{"class":230},"(x) ",[206,3050,1177],{"class":226},[206,3052,3053],{"class":230}," x ",[206,3055,1183],{"class":226},[206,3057,3058],{"class":230}," h] ",[206,3060,2569],{"class":272},[206,3062,1789],{"class":230},[206,3064,3045],{"class":272},[206,3066,3067],{"class":230},"(p[",[206,3069,2965],{"class":987},[206,3071,3072],{"class":230},"]) ",[206,3074,1177],{"class":226},[206,3076,3077],{"class":230}," p ",[206,3079,1183],{"class":226},[206,3081,3082],{"class":230}," J_pairs] ",[206,3084,2569],{"class":272},[206,3086,1789],{"class":230},[206,3088,3089],{"class":285},"1e-9",[206,3091,663],{"class":230},[206,3093,3094,3096,3099],{"class":208,"line":1668},[206,3095,214],{"class":212,"aria-hidden":213},[206,3097,3098],{"class":226},"return",[206,3100,3101],{"class":230}," {\n",[206,3103,3104,3106,3109],{"class":208,"line":1675},[206,3105,214],{"class":212,"aria-hidden":213},[206,3107,3108],{"class":987},"    \"lambda\"",[206,3110,3111],{"class":230},": lam,\n",[206,3113,3114,3116,3119],{"class":208,"line":1702},[206,3115,214],{"class":212,"aria-hidden":213},[206,3117,3118],{"class":987},"    \"target_k\"",[206,3120,3121],{"class":230},": k_target,\n",[206,3123,3125,3127,3130],{"class":208,"line":3124},19,[206,3126,214],{"class":212,"aria-hidden":213},[206,3128,3129],{"class":987},"    \"Q_matrix\"",[206,3131,3132],{"class":230},": Q,\n",[206,3134,3136,3138,3141,3144,3146,3149,3151,3153,3155],{"class":208,"data-cue":350,"line":3135},20,[206,3137,350],{"class":266},[206,3139,3140],{"class":987},"    \"ising_h\"",[206,3142,3143],{"class":230},": [x ",[206,3145,282],{"class":272},[206,3147,3148],{"class":230}," scale ",[206,3150,1177],{"class":226},[206,3152,3053],{"class":230},[206,3154,1183],{"class":226},[206,3156,3157],{"class":230}," h],\n",[206,3159,3161,3163,3166,3169,3171,3174,3176,3178,3180,3182,3184,3186,3188,3190,3192,3194,3196,3199,3201,3203,3205],{"class":208,"line":3160},21,[206,3162,214],{"class":212,"aria-hidden":213},[206,3164,3165],{"class":987},"    \"ising_J_pairs\"",[206,3167,3168],{"class":230},": [{",[206,3170,2953],{"class":987},[206,3172,3173],{"class":230},": p[",[206,3175,2953],{"class":987},[206,3177,2311],{"class":230},[206,3179,2959],{"class":987},[206,3181,3173],{"class":230},[206,3183,2959],{"class":987},[206,3185,2311],{"class":230},[206,3187,2965],{"class":987},[206,3189,3173],{"class":230},[206,3191,2965],{"class":987},[206,3193,2500],{"class":230},[206,3195,282],{"class":272},[206,3197,3198],{"class":230}," scale} ",[206,3200,1177],{"class":226},[206,3202,3077],{"class":230},[206,3204,1183],{"class":226},[206,3206,3207],{"class":230}," J_pairs],\n",[206,3209,3211,3213],{"class":208,"line":3210},22,[206,3212,214],{"class":212,"aria-hidden":213},[206,3214,3215],{"class":230},"}\n",[464,3217,3218,3228,3242],{"class":466},[468,3219,3220,3222],{"data-cue":152},[206,3221,152],{"class":266},[206,3223,3224,3225,3227],{"class":474},"Each variable's own reward becomes a field on one spin: a single-qubit ",[52,3226,185],{}," in the cost step.",[468,3229,3230,3232],{"data-cue":331},[206,3231,331],{"class":266},[206,3233,3234,3235,323,3237,323,3239,3241],{"class":474},"Each pair term becomes a coupling between two spins: one ",[52,3236,672],{},[52,3238,185],{},[52,3240,672],{}," sandwich.",[468,3243,3244,3246],{"data-cue":350},[206,3245,350],{"class":266},[206,3247,3248,3249,3251,3252,3254],{"class":474},"Every field and coupling divided by the largest of them, so the biggest coefficient is about 1. ",[52,3250,54],{}," multiplies every coefficient, so without this the useful range of ",[52,3253,54],{}," would depend on the units the returns were quoted in.",[12,3256,3257],{},"With fields and couplings in hand, the layer itself is the Courier loop with one extra line, in its general form:",[192,3259,3260],{"name":2264,"run-href":2265,"tag":196},[198,3261,3263],{"className":200,"code":3262,"language":202,"meta":159,"style":159},"def apply_cost_layer(qc, gamma, h, J_pairs, n):\n    for i in range(n):\n        qc.rz(2.0 * gamma * h[i], i)\n    for pair in J_pairs:\n        i, j, v = pair[\"i\"], pair[\"j\"], pair[\"value\"]\n        qc.cx(i, j)\n        qc.rz(2.0 * gamma * v, j)\n        qc.cx(i, j)\n\n\ndef apply_mixer_layer(qc, beta, n):\n    for i in range(n):\n        qc.rx(2.0 * beta, i)\n",[52,3264,3265,3297,3311,3334,3348,3374,3385,3406,3416,3422,3428,3451,3465,3482],{"__ignoreMap":159},[206,3266,3267,3269,3271,3274,3276,3279,3281,3283,3285,3287,3289,3291,3293,3295],{"class":208,"line":209},[206,3268,214],{"class":212,"aria-hidden":213},[206,3270,1475],{"class":226},[206,3272,3273],{"class":314}," apply_cost_layer",[206,3275,318],{"class":230},[206,3277,3278],{"class":1483},"qc",[206,3280,323],{"class":230},[206,3282,54],{"class":1483},[206,3284,323],{"class":230},[206,3286,340],{"class":1483},[206,3288,323],{"class":230},[206,3290,2260],{"class":1483},[206,3292,323],{"class":230},[206,3294,2292],{"class":1483},[206,3296,1208],{"class":230},[206,3298,3299,3301,3303,3305,3307,3309],{"class":208,"line":221},[206,3300,214],{"class":212,"aria-hidden":213},[206,3302,1565],{"class":226},[206,3304,2526],{"class":230},[206,3306,1183],{"class":226},[206,3308,1642],{"class":272},[206,3310,2533],{"class":230},[206,3312,3313,3315,3317,3319,3321,3324,3326,3329,3331],{"class":208,"data-cue":152,"line":240},[206,3314,152],{"class":266},[206,3316,1583],{"class":230},[206,3318,185],{"class":314},[206,3320,318],{"class":230},[206,3322,3323],{"class":285},"2.0",[206,3325,364],{"class":272},[206,3327,3328],{"class":230}," gamma ",[206,3330,1897],{"class":272},[206,3332,3333],{"class":230}," h[i], i)\n",[206,3335,3336,3338,3340,3343,3345],{"class":208,"line":255},[206,3337,214],{"class":212,"aria-hidden":213},[206,3339,1565],{"class":226},[206,3341,3342],{"class":230}," pair ",[206,3344,1183],{"class":226},[206,3346,3347],{"class":230}," J_pairs:\n",[206,3349,3350,3352,3355,3357,3360,3362,3365,3367,3369,3371],{"class":208,"line":263},[206,3351,214],{"class":212,"aria-hidden":213},[206,3353,3354],{"class":230},"        i, j, v ",[206,3356,273],{"class":272},[206,3358,3359],{"class":230}," pair[",[206,3361,2953],{"class":987},[206,3363,3364],{"class":230},"], pair[",[206,3366,2959],{"class":987},[206,3368,3364],{"class":230},[206,3370,2965],{"class":987},[206,3372,3373],{"class":230},"]\n",[206,3375,3376,3378,3380,3382],{"class":208,"line":297},[206,3377,214],{"class":212,"aria-hidden":213},[206,3379,1583],{"class":230},[206,3381,672],{"class":314},[206,3383,3384],{"class":230},"(i, j)\n",[206,3386,3387,3389,3391,3393,3395,3397,3399,3401,3403],{"class":208,"data-cue":331,"line":304},[206,3388,331],{"class":266},[206,3390,1583],{"class":230},[206,3392,185],{"class":314},[206,3394,318],{"class":230},[206,3396,3323],{"class":285},[206,3398,364],{"class":272},[206,3400,3328],{"class":230},[206,3402,1897],{"class":272},[206,3404,3405],{"class":230}," v, j)\n",[206,3407,3408,3410,3412,3414],{"class":208,"line":332},[206,3409,214],{"class":212,"aria-hidden":213},[206,3411,1583],{"class":230},[206,3413,672],{"class":314},[206,3415,3384],{"class":230},[206,3417,3418,3420],{"class":208,"line":351},[206,3419,214],{"class":212,"aria-hidden":213},[206,3421,260],{},[206,3423,3424,3426],{"class":208,"line":375},[206,3425,214],{"class":212,"aria-hidden":213},[206,3427,260],{},[206,3429,3430,3432,3434,3437,3439,3441,3443,3445,3447,3449],{"class":208,"line":397},[206,3431,214],{"class":212,"aria-hidden":213},[206,3433,1475],{"class":226},[206,3435,3436],{"class":314}," apply_mixer_layer",[206,3438,318],{"class":230},[206,3440,3278],{"class":1483},[206,3442,323],{"class":230},[206,3444,58],{"class":1483},[206,3446,323],{"class":230},[206,3448,2292],{"class":1483},[206,3450,1208],{"class":230},[206,3452,3453,3455,3457,3459,3461,3463],{"class":208,"line":416},[206,3454,214],{"class":212,"aria-hidden":213},[206,3456,1565],{"class":226},[206,3458,2526],{"class":230},[206,3460,1183],{"class":226},[206,3462,1642],{"class":272},[206,3464,2533],{"class":230},[206,3466,3467,3469,3471,3473,3475,3477,3479],{"class":208,"data-cue":350,"line":424},[206,3468,350],{"class":266},[206,3470,1583],{"class":230},[206,3472,189],{"class":314},[206,3474,318],{"class":230},[206,3476,3323],{"class":285},[206,3478,364],{"class":272},[206,3480,3481],{"class":230}," beta, i)\n",[464,3483,3484,3494,3504],{"class":466},[468,3485,3486,3488],{"data-cue":152},[206,3487,152],{"class":266},[206,3489,3490,3491,3493],{"class":474},"The field on each spin, scaled by ",[52,3492,54],{},". MaxCut had none of these; a portfolio has one per asset.",[468,3495,3496,3498],{"data-cue":331},[206,3497,331],{"class":266},[206,3499,3500,3501,3503],{"class":474},"The coupling, scaled by ",[52,3502,54],{}," and by its own strength. On the MaxCut graph every coupling was the same size; here each pair has its own.",[468,3505,3506,3508],{"data-cue":350},[206,3507,350],{"class":266},[206,3509,3510],{"class":474},"The mixer does not change with the problem. Only the cost step knows what is being optimized.",[12,3512,3513,3514,3516],{},"Every pair of the eight assets gets a coupling, twenty-eight in all, so one cost step is 56 ",[52,3515,672],{}," gates, the figure the project's README gives for one layer.",[12,3518,3519],{},"The six extra qubits the project measures alongside the eight asset qubits carry market state and are prepared with single-qubit gates only. Its showcase describes that as the choice that keeps the circuit growing by one qubit per asset rather than per signal.",[928,3521],{"description":3522,"framework":931,"owner":3523,"title":3524,"to":2265,"builder":3525,"thumb":3526},"Blockchain oracle that publishes a quantum-computed 'systemic risk score' for crypto markets every day. Smart contracts and prediction markets can use the quantum-derived risk data as a primitive. Commoditizing quantum compute as an on-chain data feed.","jamie","Quantum Systemic Oracle","Jamie Dominguez","\u002F_content\u002Fimages\u002Fsystemic-oracle\u002Fscreenshot.webp",[25,3528,3530],{"id":3529},"what-approximate-means","What approximate means",[12,3532,3533],{},"Two numbers set the expectations for one layer of QAOA. Farhi, Goldstone and Gutmann's guarantee is an average cut of at least 0.6924 times the best possible on any graph where every node has three edges. Goemans and Williamson's classical algorithm from 1995 guarantees 0.878.",[12,3535,3536],{},"On the Courier graph the tuned layer averages 25.5 of a possible 33, which is 0.77. A greedy local search from a random split, the simplest classical heuristic there is, averages about 31 on the same graph and finds 33 within a few restarts, in milliseconds.",[12,3538,3539],{},"So on this graph the average sample from one layer is worse than what a classical heuristic returns. On any problem small enough to score exhaustively, that comparison can be made exactly.",[12,3541,3542],{},"Quantum Courier's README says which stages the classical solvers win, and keeps the stage where four QAOA variants lost on Forte to classical simulated annealing.",[12,3544,3545,3546,3549],{},"A QAOA run therefore reports the best string in the tally, checked classically, rather than the average. The Oracle's file shows that step; ",[52,3547,3548],{},"ranked"," is its tally with the strings below a noise floor already removed:",[192,3551,3552],{"name":2264,"run-href":2265,"tag":196},[198,3553,3555],{"className":200,"code":3554,"language":202,"meta":159,"style":159},"nonempty = [(b, c) for b, c in ranked if any(b)]\nrec_pool = nonempty or ranked\nrec_bits, rec_count = min(rec_pool, key=lambda kv: classical_cost(list(kv[0]), Q_matrix))\ntop_bits = list(rec_bits)\ntop_held = [ASSETS[i] for i, b in enumerate(top_bits) if b]\ntop_cost = classical_cost(top_bits, Q_matrix)\ngap = (top_cost - opt_cost) \u002F abs(opt_cost) * 100.0 if opt_cost else 0.0\n",[52,3556,3557,3588,3606,3649,3664,3699,3714,3755],{"__ignoreMap":159},[206,3558,3559,3561,3564,3566,3569,3571,3574,3576,3579,3582,3585],{"class":208,"line":209},[206,3560,214],{"class":212,"aria-hidden":213},[206,3562,3563],{"class":230},"nonempty ",[206,3565,273],{"class":272},[206,3567,3568],{"class":230}," [(b, c) ",[206,3570,1177],{"class":226},[206,3572,3573],{"class":230}," b, c ",[206,3575,1183],{"class":226},[206,3577,3578],{"class":230}," ranked ",[206,3580,3581],{"class":226},"if",[206,3583,3584],{"class":272}," any",[206,3586,3587],{"class":230},"(b)]\n",[206,3589,3590,3592,3595,3597,3600,3603],{"class":208,"line":221},[206,3591,214],{"class":212,"aria-hidden":213},[206,3593,3594],{"class":230},"rec_pool ",[206,3596,273],{"class":272},[206,3598,3599],{"class":230}," nonempty ",[206,3601,3602],{"class":226},"or",[206,3604,3605],{"class":230}," ranked\n",[206,3607,3608,3610,3613,3615,3618,3621,3624,3626,3629,3632,3634,3637,3639,3641,3644,3646],{"class":208,"data-cue":152,"line":240},[206,3609,152],{"class":266},[206,3611,3612],{"class":230},"rec_bits, rec_count ",[206,3614,273],{"class":272},[206,3616,3617],{"class":272}," min",[206,3619,3620],{"class":230},"(rec_pool, ",[206,3622,3623],{"class":441},"key",[206,3625,273],{"class":272},[206,3627,3628],{"class":226},"lambda",[206,3630,3631],{"class":1483}," kv",[206,3633,1061],{"class":230},[206,3635,3636],{"class":314},"classical_cost",[206,3638,318],{"class":230},[206,3640,1228],{"class":272},[206,3642,3643],{"class":230},"(kv[",[206,3645,345],{"class":285},[206,3647,3648],{"class":230},"]), Q_matrix))\n",[206,3650,3651,3653,3656,3658,3661],{"class":208,"line":255},[206,3652,214],{"class":212,"aria-hidden":213},[206,3654,3655],{"class":230},"top_bits ",[206,3657,273],{"class":272},[206,3659,3660],{"class":272}," list",[206,3662,3663],{"class":230},"(rec_bits)\n",[206,3665,3666,3668,3671,3673,3675,3678,3681,3683,3686,3688,3691,3694,3696],{"class":208,"line":263},[206,3667,214],{"class":212,"aria-hidden":213},[206,3669,3670],{"class":230},"top_held ",[206,3672,273],{"class":272},[206,3674,1789],{"class":230},[206,3676,3677],{"class":285},"ASSETS",[206,3679,3680],{"class":230},"[i] ",[206,3682,1177],{"class":226},[206,3684,3685],{"class":230}," i, b ",[206,3687,1183],{"class":226},[206,3689,3690],{"class":272}," enumerate",[206,3692,3693],{"class":230},"(top_bits) ",[206,3695,3581],{"class":226},[206,3697,3698],{"class":230}," b]\n",[206,3700,3701,3703,3706,3708,3711],{"class":208,"line":297},[206,3702,214],{"class":212,"aria-hidden":213},[206,3704,3705],{"class":230},"top_cost ",[206,3707,273],{"class":272},[206,3709,3710],{"class":314}," classical_cost",[206,3712,3713],{"class":230},"(top_bits, Q_matrix)\n",[206,3715,3716,3718,3721,3723,3726,3728,3731,3733,3736,3739,3741,3744,3746,3749,3752],{"class":208,"data-cue":331,"line":304},[206,3717,331],{"class":266},[206,3719,3720],{"class":230},"gap ",[206,3722,273],{"class":272},[206,3724,3725],{"class":230}," (top_cost ",[206,3727,1064],{"class":272},[206,3729,3730],{"class":230}," opt_cost) ",[206,3732,282],{"class":272},[206,3734,3735],{"class":272}," abs",[206,3737,3738],{"class":230},"(opt_cost) ",[206,3740,1897],{"class":272},[206,3742,3743],{"class":285}," 100.0",[206,3745,1837],{"class":226},[206,3747,3748],{"class":230}," opt_cost ",[206,3750,3751],{"class":226},"else",[206,3753,3754],{"class":285}," 0.0\n",[464,3756,3757,3764],{"class":466},[468,3758,3759,3761],{"data-cue":152},[206,3760,152],{"class":266},[206,3762,3763],{"class":474},"Every sampled portfolio above the noise floor is re-scored with the classical cost function, and the cheapest non-empty one, if any, is the recommendation. The most frequent string is only reported, never trusted on its own.",[468,3765,3766,3768],{"data-cue":331},[206,3767,331],{"class":266},[206,3769,3770],{"class":474},"The gap to the brute-force optimum over all 256 portfolios. At eight assets the true answer is checkable, so the quantum run is graded rather than believed.",[12,3772,3773],{},"The gap to the optimum feeds a solver-quality signal that contributes 5% of the Oracle's published index. Its README describes the result as a headline number that never hinges on solver perfection but degrades with it.",[12,3775,3776],{},"QAOA's case rests on two things: strings long enough that the brute-force table disappears, and layers deep enough that the average climbs. Both are questions about hardware.",[25,3778,3780],{"id":3779},"depth-that-helps-on-the-simulator-and-hurts-on-the-machine","Depth that helps on the simulator and hurts on the machine",[12,3782,3783,3784,478],{},"Each extra layer adds another cost step and another mixer step with their own pair of angles. On an ideal simulator the best achievable average never falls as layers are added, and usually rises. How many layers there are is called ",[52,3785,12],{},[12,3787,3788,3789,3791,3792,3795],{},"In the Oracle's file ",[52,3790,12],{}," is one constant, ",[52,3793,3794],{},"QAOA_LAYERS = 2",", and the whole circuit is a loop over that constant:",[192,3797,3798],{"name":2264,"run-href":2265,"tag":196},[198,3799,3801],{"className":200,"code":3800,"language":202,"meta":159,"style":159},"def asset_only_qaoa_circuit(gammas, betas, h, J_pairs, n):\n    qc = QuantumCircuit(n)\n    for i in range(n):\n        qc.h(i)\n    for layer in range(len(gammas)):\n        apply_cost_layer(qc, gammas[layer], h, J_pairs, n)\n        apply_mixer_layer(qc, betas[layer], n)\n    return qc\n",[52,3802,3803,3836,3849,3863,3874,3895,3905,3915,3923],{"__ignoreMap":159},[206,3804,3805,3807,3809,3812,3814,3817,3819,3822,3824,3826,3828,3830,3832,3834],{"class":208,"line":209},[206,3806,214],{"class":212,"aria-hidden":213},[206,3808,1475],{"class":226},[206,3810,3811],{"class":314}," asset_only_qaoa_circuit",[206,3813,318],{"class":230},[206,3815,3816],{"class":1483},"gammas",[206,3818,323],{"class":230},[206,3820,3821],{"class":1483},"betas",[206,3823,323],{"class":230},[206,3825,340],{"class":1483},[206,3827,323],{"class":230},[206,3829,2260],{"class":1483},[206,3831,323],{"class":230},[206,3833,2292],{"class":1483},[206,3835,1208],{"class":230},[206,3837,3838,3840,3842,3844,3846],{"class":208,"line":221},[206,3839,214],{"class":212,"aria-hidden":213},[206,3841,1496],{"class":230},[206,3843,273],{"class":272},[206,3845,315],{"class":314},[206,3847,3848],{"class":230},"(n)\n",[206,3850,3851,3853,3855,3857,3859,3861],{"class":208,"line":240},[206,3852,214],{"class":212,"aria-hidden":213},[206,3854,1565],{"class":226},[206,3856,2526],{"class":230},[206,3858,1183],{"class":226},[206,3860,1642],{"class":272},[206,3862,2533],{"class":230},[206,3864,3865,3867,3869,3871],{"class":208,"line":255},[206,3866,214],{"class":212,"aria-hidden":213},[206,3868,1583],{"class":230},[206,3870,340],{"class":314},[206,3872,3873],{"class":230},"(i)\n",[206,3875,3876,3878,3880,3883,3885,3887,3889,3892],{"class":208,"data-cue":152,"line":263},[206,3877,152],{"class":266},[206,3879,1565],{"class":226},[206,3881,3882],{"class":230}," layer ",[206,3884,1183],{"class":226},[206,3886,1642],{"class":272},[206,3888,318],{"class":230},[206,3890,3891],{"class":272},"len",[206,3893,3894],{"class":230},"(gammas)):\n",[206,3896,3897,3899,3902],{"class":208,"line":297},[206,3898,214],{"class":212,"aria-hidden":213},[206,3900,3901],{"class":314},"        apply_cost_layer",[206,3903,3904],{"class":230},"(qc, gammas[layer], h, J_pairs, n)\n",[206,3906,3907,3909,3912],{"class":208,"line":304},[206,3908,214],{"class":212,"aria-hidden":213},[206,3910,3911],{"class":314},"        apply_mixer_layer",[206,3913,3914],{"class":230},"(qc, betas[layer], n)\n",[206,3916,3917,3919,3921],{"class":208,"line":332},[206,3918,214],{"class":212,"aria-hidden":213},[206,3920,1707],{"class":226},[206,3922,1710],{"class":230},[464,3924,3925],{"class":466},[468,3926,3927,3929],{"data-cue":152},[206,3928,152],{"class":266},[206,3930,3931,3932,488,3934,3936,3937,3939],{"class":474},"One pass per layer, each with its own ",[52,3933,54],{},[52,3935,58],{},". Two layers means four angles to tune. The eight-asset cost step is 56 ",[52,3938,672],{}," gates, so two layers are 112 and three are 168.",[12,3941,3942],{},"The project tested one, two and three layers on IonQ Forte and wrote down what happened. In its own words the binding constraint was two-qubit gate error rather than circuit depth, meaning the number of gate layers the compiled circuit runs in sequence. About ninety two-qubit gates was the envelope.",[12,3944,3945],{},"One layer, at 56 gates, performed best. Deeper circuits \"look marginally better on the ideal simulator but flatten toward uniform on the QPU\", the quantum processor.",[12,3947,3948,3949,3953],{},"Every two-qubit gate carries an error, the errors compound, and past a certain count the distribution the circuit was shaping is ",[16,3950,3952],{"href":3951},"\u002Flearn\u002Fdocs\u002Fwhy-your-results-look-wrong","lost to the noise",". On the Oracle's runs it flattened back towards the uniform one the circuit started from.",[12,3955,3956],{},"Every QAOA run on today's hardware trades layers against fidelity. More layers can raise the ceiling and cost fidelity on the machine, and the layer count that wins on the simulator is rarely the one that wins on the machine.",[12,3958,3959],{},"Google's 2021 experiment found the same trade on a different machine. On its superconducting chip, problems whose graph matched the qubit wiring improved with more layers. Problems that needed extra gates to route around the wiring got worse as they grew.",[12,3961,3962],{},"A trapped-ion machine connects every qubit to every other, which is why the Oracle's twenty-eight couplings need no routing, and why its limit is the gate count itself.",[12,3964,3965],{},"The working rule: tune and debug on the simulator, then use the machine for the one question it alone answers. Does the distribution you shaped survive the gate count at the layer count you chose?",[25,3967,3969],{"id":3968},"start-with-one-qubit","Start with one qubit",[12,3971,3972],{},"A quantum optimizer is a circuit whose shape is fixed by the problem and whose behaviour is fixed by a handful of angles. The problem becomes a cost, the cost becomes phases, and a mixer turns the phases into weight. A classical loop chooses the angles that push the weight towards the answers you want.",[12,3974,3975],{},"What comes back is a tally, and the answer you keep is the best string in it, scored by ordinary code.",[12,3977,3978,3979,3981,3982,488,3984,3986,3987,3989],{},"Start with the one-qubit circuit. Fork the QUBO-with-QAOA example, paste the one-qubit listing over its code, and run it: what comes back can be predicted before the run. Set ",[52,3980,54],{}," to zero and ",[52,3983,345],{},[52,3985,152],{}," come back about equally; set it back to −π\u002F4 and every shot reads ",[52,3988,152],{},", with every gate unchanged.",[12,3991,3992,3993,488,3995,3997],{},"Then the one-edge listing the same way: at −π\u002F4 and π\u002F8 only ",[52,3994,539],{},[52,3996,542],{}," come back. Then Quantum Courier's script at the pairs from the table above.",[3999,4000,4002],"make-it-yours",{"fork-href":195,"title":4001},"Change one angle, then change the graph.",[12,4003,4004,4005,4007,4008],{},"Fork the example, paste the one-edge listing over its code, and move ",[52,4006,54],{}," from −π\u002F4 towards zero, reading the cut off the counts before the run confirms it. Then add a third node and an edge, and find the new pair. ",[4009,4010,4011],"strong",{},"Everything here is open and yours to build on.",[4013,4014,4015],"style",{},"html pre.shiki code .sV9Aq, html code.shiki .sV9Aq{--shiki-default:#7F848E;--shiki-default-font-style:italic}html pre.shiki code .seHd6, html code.shiki .seHd6{--shiki-default:#C678DD}html pre.shiki code .sn6KH, html code.shiki .sn6KH{--shiki-default:#ABB2BF}html pre.shiki code .sjrmR, html code.shiki .sjrmR{--shiki-default:#56B6C2}html pre.shiki code .sVC51, html code.shiki .sVC51{--shiki-default:#D19A66}html pre.shiki code .sVbv2, html code.shiki .sVbv2{--shiki-default:#61AFEF}html pre.shiki code .s_ZVi, html code.shiki .s_ZVi{--shiki-default:#E06C75;--shiki-default-font-style:italic}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .subq3, html code.shiki .subq3{--shiki-default:#98C379}html pre.shiki code .sb9H8, html code.shiki .sb9H8{--shiki-default:#D19A66;--shiki-default-font-style:italic}",{"title":159,"searchDepth":221,"depth":221,"links":4017},[4018,4019,4020,4021,4022,4023,4024,4025,4026,4027,4028,4029,4030],{"id":27,"depth":221,"text":28},{"id":71,"depth":221,"text":72},{"id":99,"depth":221,"text":100},{"id":127,"depth":221,"text":128},{"id":175,"depth":221,"text":176},{"id":936,"depth":221,"text":937},{"id":1371,"depth":221,"text":1372},{"id":1452,"depth":221,"text":1453},{"id":2076,"depth":221,"text":2077},{"id":2471,"depth":221,"text":2472},{"id":3529,"depth":221,"text":3530},{"id":3779,"depth":221,"text":3780},{"id":3968,"depth":221,"text":3969},[4032,4033,4034],"Qollab","Learn","Quantum optimization",[4036],{"username":7,"name":4037,"role":4038,"avatar":4039,"bio":4040,"links":4041},"Nicolaas Spijker","Community manager, Qollab","\u002F_content\u002Fimages\u002Fbuilders\u002Fnicolaas-spijker.webp","Nico runs the builder community at Qollab. These concept articles come out of that work: forking the projects people publish and running them. And talking with authors to learn more about their projects.",[4042,4045],{"label":4043,"href":4044},"Qollab ↗","https:\u002F\u002Fqollab.xyz\u002Fu\u002Fnico",{"label":4046,"href":4047},"GitHub ↗","https:\u002F\u002Fgithub.com\u002Fnicolaasspijker",{"username":7,"name":4037,"role":4038,"avatar":4039},null,"QAOA makes the good answers come up more often when you measure, and two numbers decide by how much. How that works, from one qubit to a real graph, with code you can run and fork.","What QAOA is and how it works, explained from one qubit up to a 24-node graph, with code from published Qollab projects that you can run and fork.",false,"md",{"href":195,"label":4055},"Fork the smallest QAOA","article",{},true,"\u002F_content\u002Fimages\u002Foptimization\u002Fog.png","\u002Fblog\u002Flearn\u002Fquantum-optimization","2026-09-15","15 min read",[],{"title":4065,"description":4066},"Quantum Optimization and QAOA, Explained with Code You Can Run","Quantum optimization and QAOA explained from a cost, a circuit and two angles, derived on a 24-node instance from a published project, with code to run and fork.","blog\u002Flearn\u002Fquantum-optimization",[4069,4070,4071],"optimization","education","quantum","hjXHk3dCVrpDc3K2fmtHWMfImVRSjkh5KQmSS4RUOpA",[],[],1789514630739]