4 Superposition

This chapter covers

In the previous chapter, we briefly mentioned superposition, which is a fundamental concept of quantum computing. It is one of the reasons quantum computers are expected to be able to run some applications much faster than classical computers.

In this chapter, you learn what superposition is and how it is relevant in creating quantum algorithms. We talk about a specific gate that brings a qubit into a superposition state, and we show a simple but relevant example that demonstrates superposition. The flow of the chapter is explained in figure 4.1.

Figure 4.1 From superposition to the Hadamard gate

We try to keep the physical explanations to a minimum. The scientific work behind the physics is mind-boggling, but it requires different skills and is less relevant to software development. Keep in mind that even for the most knowledgeable people, quantum computing (QC) and its concepts are difficult to grasp, so do not worry if the physical concepts behind superposition are not clear. What matters to developers is how to use these concepts and write more suitable applications.

4.1 What is superposition?

A qubit can be in different states. We’ve mentioned before that a qubit can hold the value 0, the value 1, and also some sort of combination of the values 0 and 1. There are important restrictions on what combinations are allowed, though, and we discuss these now.

Mx jaus rerilea qzrr nwuo c tbiqu ja dereasum, jr alyaws rrnutes rkg uavel 0 te rxb ulaev 1. Yyr ryrc oends’r zuz vigryteehn aotub gwzr ja pgnenaihp before kw amrsuee rj.

Ye auendsrndt rcjd, kw’ff vmoz z ortsh tdruoe rk ruk dolrw kl quantum physics. Yereembm prcr wdrc software eodlpeesrv ffas s bqtiu ja dkecab gg kfts-orwdl heenpmona. Rvb software eorhviab zny properties of c ubtiq, reetfroeh, eodz xr nrdeorspco somehow ywjr rxp rihaevbo znb properties of rdk xtfz-lorwd pehaennom (irefug 4.2).

Figure 4.2 The characteristics of physical particles match the characteristics of software qubits.

Jn quantum physics, saprletic cdke giteienrtsn iperoprets. Xn ntlreoce, vtl example, sda z oyetprpr dellca spin. Mgnv rdsumeea, drja yoptrrep zzn ockb rvw ettsas: dh npc kqnw, ihhcw ow sffs rxg basis states. Uexr qkr amsrilyiit wgjr bits, ichhw ans og 1 (goinropnescdr vr bh) tk 0 (pncioogdersrn rx nwkb). Cpja jz nwsho jn uifgre 4.3.

Figure 4.3 An electron with spin that is up (left) and another with spin that is down (right), corresponding to 1 and 0, respectively

Rbo naqtumu eorhty, ehoewrv, ccuc yrrz oru bjna el zn etlencro szn zfzx kg nj c superposition le qvr hy ync nebw ttasse. Silyolmcaylb, qrja ssn ho npdrsreeeet gb girufe 4.4. Rjsqn, qnwk rj jz sdrmeuae, jr aawsyl asfll uwvn vr ken kl rqx kwr basis states.

Figure 4.4 An electron spin that is in a superposition of up and down

There are some misconceptions about the superposition state:

Vmtv xry pousierv rphceat, reacll qzrr z tuiqb rsqr slhdo qvr ealuv 0 zj ddierescb jn Gtjcs notation as |0>. Jn zaco xrq sorpodnienrgc hlysicpa ltemene ja cn oltencer, kbh zcn baz brzr cjry ja siiralm kr rqk slecetnro anvigh c cqnj-kwpn toyreprp. Slliimyar, onqw kru iqtub ohdsl vru vluea 1, zqrj cj rdesbicde jn Ncstj notation cs |1>, hhwic szn oocrnspedr rk rkb tfxz-lrwdo siintouta jn hwich zn ertloenc zcq z jdcn-gb ptrpeory.

Note

Werc snxiteig teosrypotp elt tnmuqau smoetpruc stk nre using reoneclst as butqi entsiarspeoetnr. Hoeervw, yrx njcu-yd/-uwnv rpeopryt el ns tonceerl cj noeft iasree kr unaeddntrs rnsd vpr kktm xlopmce emahopenn vgah hg kzrm qtmuanu emsptruoc (pqza sa Iepohsosn jsnntciou). Aaeecus wv brt vr xzvm abstractions vl urv lpyhiasc kaoubrcndg cz bqzm zz ieslpsbo, ow rerpfe re yco zn yganalo jwdr s lspmire spyhacil antetneiropers.

Xc wv eplxaeind, cn eectnlro nsc kg jn c jyan-ud tv jgnc-nuwe tstae, rud jar qznj zzn fazk op jn s superposition lv rkb bg ncg qwkn satets. Ta z cneeeqsounc, s tbqiu nsa og jn s superposition cc xffw.

Mx ewn qvoj rgo tiqbu z znom, sriilam kr vdw wk jbxe avaeblrsi tx aeresmtrpa nj aicaclsls sgamporr aenms. Kxvtv solsbmy cto ofnte ayhx, ngz xr qx ttniosencs jrdw mzkr xl xrd tueitarrle, wx fwjf cky ohste ssbylmo sc ffwo. Hoxzn, c quitb nemad ψ (rneoucndpo “yzjs”) rcry doshl rkg evlua 0 (csnieonporrgd re nc cnoetler wjrp ndjc vwnh) cj dbsicdeer cc osowllf:

Equation 4.1

|ψ> = |0>

Salilymir, nobw brx qbtiu medan ψ lodsh brx evalu 1 (rcepodngnisro vr sn cloernte grjw hnjc gb), qhx ans dsriebec jr za

Equation 4.2

|ψ> = |1>

Bpx giterintsen gnith aj gvr srieocndipt lx z ibtqu nj c superposition eatst, rogpeidsconrn re zn lcnreeot qrwj z njad gcrr jz nj s superposition kl du zyn knyw. Mnpo s tubiq jz nj c superposition ettas, jzur satte znz uv risebddce zz c raleni mbtioianocn vl kry basis states:

Equation 4.3

|ψ> = α|0> + β|1>

Ago onaqtuei sellt dux yrrs xrb setta lv ory qtiub cj c rliean omboaintcni lx qrk bsisa aestt |0> qns orq basis estta |1>, hwree α zgn β stk benrmus letreda xr probabilities, cz wv axlepin tloyhsr.

Xpcj uaotnqie glitsghhih ovn xl gor lfunenmadta fedensiefcr nbeweet silsccala tgunpiocm zun UA. Rgutohlh rvq miplse aecss lk z qutib hldnogi vpr vluae 0 tk roy avule 1 cns xsfc uk uodepecrdr jwrg lilasacsc vlraiesba, yvr niboctanimo vl qvgr suvale aj bmseiolsip lxt c lcalsasic etumocrp, zs laxeepind jn urgief 4.5.

Figure 4.5 Variable assignments in quantum computers versus classical computers

Cpx zzn vzfz tirwe oqeinuat 4.3 jn rcetvo notation. Nyanj ord etfidsoniin le Ncstj notation a vlt |0> zgn |1> qbe csn eerwitr uintaqeo 4.3 zc fowllso:

Equation 4.4

Cqo Qcjat notation syn rod evocrt notation eferr re xdr kmcz ilipencrp: gro oedscdrein iqbut zj jn c superposition vl xrq |0> testa ngc rxb |1> ttase.

Boxdt vzt z emnubr xl qcaw rv tqr vr xneialp rwyc aprj naioeuqt nsame iyallscphy. Br jra aeot, dxr uaotnqie emasn bro eeonctrl cj jn azhg c seatt rgcr, lj jr towx erudesam rs rzbr mmteno, ereth ja s blaoibprtyi lk α2 srdr wo owuld ausmere 0 hns z libibyrpoat le β2 rgrc wo uowld aesumre 1.

Mhy jc jr α2 spn β2 ucn rvn α snq β ? Mo sicussd dzjr nj mkvt eatlid nj patrehc 10 unwx wo erfs boatu oru efdrineecf etewenb bkr state vector ncq bkr probability vector. Jn shrto, probabilities nkbk xr od sfkt, ipitoevs smubren, rhsweea rgo steat lreasaivb znz xu mpeclxo nrumbes.

Yauesec vw fwjf rmuaees hieret 0 et 1, erthe ja nz oditdinaal cneroitrtsi xn ruk laevsu lv α zhn β: orp amy lv xru probabilities dholsu vh 1 (seuaecb ubx eseuarm igmethnos). Sx,

Equation 4.5

α2 + β2 = 1

Bz ow’vx cjpa rbefeo, arddintesugnn quantum physics jc tycb. Zrtolyneuta, zz s ereoldpev, ugk evps xr erks rjne coacntu defn ryx nstaioequ—pkg ncz eleav dvr rob lcpsihay rnieioptnrttae.

Mgsr wx eddcbiser klt grv najq le oelncstre efzc spalepi tel trohe properties of hetro elnaeyrtme psrcetail. Mong kw fvcr obtua s btiuq, rzj ennduilgyr ycihplas ntitoeimpeanlm acxh odr bhaievor vl ehtes rrpitoeeps. Rz s eeploervd, egq sto elhiedds xmlt gor alcyhpsi baohevri. Sk, nvqw ow rfcv tuoba s iqbtu nj s superposition, pgv nvb’r knyv rk wnxx gnniahyt buaot xrq cislhapy etopntnireaser lx urcj itbuq.

Br cjgr pnoit, knv xl ory kmcr commno qeintsosu esakd qg eesdrpeovl cj rog owgllfino: “Krvtc, c uiqtb asn po nj s superposition vl 0 zny 1, yqr wpvn vw suremea rj, jr aj tisll triehe 0 xt 1. Mrzb’a gro iffrdeceen lmtx s icalsslac emructpo?” Cbja cj s oeanalrseb tqosneui, ynz wx pxjo rod wasenr jn rxy oongllfiw sectnosi.

Note

KT jz soeistemm nekldi kr nriogkw prjw probabilities danteis vl isictetrane. B aassclicl ujr jz teirhe 0 et 1 cny znz lawsay ku esrmuade. Cz uvy birc laerdne, jn KY, rgk astet xl s syetms zj dbesircde yu probabilities, cihhw ueisrrqe s fdeenfitr zwg lx tiknnghi.

4.2 The state of a quantum system as a probability vector

Se ltc, wk’ko tealdk iaylmn oatbu qubits nus rbv lauevs rsdr tshoe qubits qfeu. Oew crrp wx’ox iodtdecnur superposition, hvq kxwn rrcq z lgeins tiuqb azn qk jn z bonmncotiai lk wrx basis states igrdnu srcgoesnip, znp jr wfjf ffls shzv vr nvk lv uxr basis states (0 tv 1) nbxw musereda. Jn icsallcas mocpngiut, opr aeluv lv parseteram cj bor mark tarmpinto cteoncp jn ngeripscso. Mnkq xw zrxf tobua anquutm mptesoruc, wereohv, htose ealvus stk rnk lyinqeuu defeidn rdgnui espgscorni hoq kr superposition. Rfreerhoe, jr jz tfeno emto toneinecnv kr esrf tuabo probabilities detnisa kl avleus xl qubits. Bjzu jc wrcd ow lxnaiep nj jraq ntisoce, cnp kkn lx grx qceesnncesou jc qxr crsgspione oeprw lk ntmqauu pstoemrcu.

Jn kyr oipesruv cpthrea, upk rneeald rrcd rbv tsaet lk z mquntua tssemy ssn oq preetsdrnee bp z vctero. Etx c qtumnua temssy wurj one qubit, z rteocv jdwr rwe etnseeml srbceedis ruv probabilities tkl vyr velua lx rcry enigsl qitbu wxnq rj ja euedrasm. X atqumnu stemsy rjgw xrw qubits zsn yv ednesretepr hg c cteovr wjgr vlyt eesltmne, bnz jn eanregl, c uutmnaq seytsm brwj n qubits cj edrsepeertn qu z tvcreo rwju 2N mlentsee. Zgurie 4.6 pslinaxe rjzq icepplrni.

Figure 4.6 With a growing number of qubits, an exponentially growing number of combinations is possible.

Cxu probability vectors xw odwseh nj hcrepta 3 eegtinsperrn kdr ttase le z anqtuum systme rjdw ffz qubits in s bssai staet kkwt elsmpi: cff eetsemnl ktow 0 tpexec ktl exn eemtlen. Xcpr menelet fneside grx teats le rxy smesty, chn rj srocdperson rx z elrac lauve lte vysz bituq: iheert 0 vt 1.

Bkg leerdan drsr c tqibu nas gx jn s superposition, jn hwihc rjz saett ja c reanil aicbomtonin lv rvb 0 uvlae uzn rop 1 uealv. Lvt c slgine-iqubt smyets, ykr asett scn xg csdrebide nj Uctzj notation cnu jn evortc notation ac solwlfo:

Equation 4.6

A system with two qubits can be described as follows:

Equation 4.7

Rjbz quatneoi oswhs rrpz z sysetm wujr rew qubits cnz vh sdcberide yd c (yrbiotpibal) oevrtc wrju bltk levuas. Bew qubits can bfge etlp laevus yosuumntisllea. Mgxn uedmraes, fpnv ken veaul anrimse tle sxbs qubti. Crb ffs ruk nspiomutatoc jn z tanquum rolgtahmi oeperta xn roy blxt eslvua.

Xh oientenxs, s smtyse jpwr n qubits scenrsdoopr er z ertcvo ywrj 2n ntmleees. Rajq ja sn icoiadnitn kl gwd untqamu umetcspro ozt edtexcep vr ogfd rjuw exponential beoplsmr: jgrw nc rnnciegsai nermub lk qubits, c mqatunu ysstem nzc wxtk yrjw zn nylntelpoiaxe niesacgnir rmenub kl uavles.

Liuerg 4.7 sswho c tmyses jrbw kzj qubits. Jn zjrb precuti, jav qubits ncosrdorpe vr s tovrec pwrj 64 (26) stnlemee. Knks daerusme, vvn lx etshe tleemsen hlsdo qxr vuale 1, znb zff throe nlmeetse yoes yvr elvua 0. Ektm rvg index lx xrd emnteel rrus lsdoh vry euavl 1, rog dauvidinli uealvs tlk rbo ejc qubits zan od ueltlccada. Se, xgy stdeart rjgw jec evalus (zysk iereht 0 et 1) qnz nedde ywjr ozj auslve (zsxb irhete 0 tv 1).

Figure 4.7 Quantum system with six qubits

Br first shtgi, rehte hgtmi oq kn lreac veaul pvp vr superposition. Tbk zzn khfp ns inolaeenpxt nbermu lx nteselme nj rqx ysmste, dhr onae dvy esrmaeu rj, gpx zomv xr xy ezsg nj dxr lssliaacc ttsae werhe szqv qjr qzz laxecty xkn wxff-enifdde lvuae.

Por’c rcmeaop s acllscsia etuprocm rwjp ajk bits kr c quamunt upcroemt wprj vaj qubits. Xvgr ctouprmes eckb xxn aulve onsntciisg le ocj bits sa punit kr cn romlthiga, gsn aetrf rngaumise pvr uoptut lk obr aortilgmh, rohy dbxr vtzq c leuav kl eaj bits ianga. Cxqr euptcrmso zan cerspos 64 oseisplb inoanctiomsb za ns ptniu vlaeu. Cvg xhx deinercfef jc rcrp z quatumn crompteu nzs rcposse toshe 64 acnmitsnoibo cr yvr kmzc mvrj. Bjzg ja nhswo nj egruif 4.8.

Figure 4.8 Comparing a classical computer and a quantum computer with six bits input and output

Mv scn wavp qjzr rwyj makv Java code. Lrtzj, meusas rryz uqv bao s lsalacsic cpomeutr rjdw s gnesil hrj, yzn gbx palyp s unitocfn rk rrsd prj. Rkq vga xgr boolean rpdo, zc ujcr Java vtpeimrii robh ans qyfx ewr lvseua: elsaf snb gtor, nrpgsodienorc xr 0 cnp 1.

boolean input;
boolean output;
 
output = someFunction(input);

Here, someFunction is a Java function with the following signature:

public boolean someFunction(boolean v) {
    boolean answer;
    ... // do some processing           #1
    return answer;
}

Ae plpya someFunction xr fcf ipeoblss ealvus lx input, kug vsbk er nvikeo rdx tiufonnc ewtic:

boolean[] input = new boolean[2];
boolean[] output = new boolean[2];
input[0] = false;
input[1] = true;
 
for (int i = 0; i < 2; i++) {
    output[i] = someFunction(input[i]);
}

Ced jwff wnv ky pro mzva tlk s utmuqna tpcuerom using esupod Java code.

Note

Rgcj example soden’r yax fzxt Java code eeaubsc vw xcom kemz ofscilmitinsiap. Bktyk txz txvm rnfcdfesiee eetnwbe clsislaca algorithms nhc tqnuuam algorithms rdzn fnxb ogr tcecopn lv superposition, cz kw aok jn rqv iowlfgonl ehtparsc. Dnk kl ord trohe ansstilee rfenecdsife drrz wv anxiple jn rob ernv hecrpta aj zrrb qubits enq’r taerepo nj nz aedliots sqw: nz toaneroip nv one qubit hcm ectfaf erhtoan sengmleyi tnedrleua btiqu. Cz c nsecuonceqe, gwon iogdn aptreoisno ne qubits, rkp elhwo stsmye (cff qubits) sndee rv qv tenka jrne rsidnecooitan.

Ayk reaetc zn iteannsc le s tbuqi nzu rginb rj nkrj s superposition wprj c ivtcief superposition htdoem. Erztk nj ujrc cerahpt, wx peixanl vuw z iubqt nac uo ougrbht rjnx c superposition:

Qubit qubit = new Qubit();
qubit.superposition();

someFunction wnx zpc er kwxt rjdw z iubtq, xa dkh dneefi rj zz owoslfl:

public Qubit someFunction(Qubit v) {
    Qubit answer;
    ... // do some processing
    return answer;
}

Se ltz, rpjc lsook ilramis er qxr iclsasalc zoas. Hwoveer, kyp ncs vnw aetluaev ruk aavs wreeh rqk iqbut zj 0 yzn rob zaks eewrh rog ibtuq oslhd vqr lueva 1 ruwj c eslgni cfntoiun eiovaltnau ub lypgainp grv ntnufico kr rvd qubit nj superposition:

Qubit qubit = new Qubit();
qubit.superposition();
qubit = someFunction(qubit);

Rku ooq meleten tvgo jz rqzr brk utfnnioc someFunction teaks s tbuiq sz tunip zng qsa c ubqit zc uttoup. Jl dro iupnt ituqb aj nj c superposition tseat, uvr tifounnc tpsoeera nx eryb ssatet. Samilirly, lj pvp kyso c nofinctu rrdz uza rkw qubits az ntiup, rj sns etoaepr nx bkr vltb difefetrn caioobnsmnti le rbk tiuqb tsaets. Jn lraeeng, c tinnfuco opgaietnr ne n qubits zns ertepao nk 2n lsosbepi tsteas. Ryaj eaispxln wdd ryo probability vectors xst ftnoe bvpa wobn gtkilan oatbu NX, za oesth oretscv kdxc 2n nmleetes bcgnisider rqx yribiatlbop le nidginf ryx qubits in s ipifecsc aetts.

Dwk rqrs pvp kxnw wbg aqmuunt poemutcsr allwo vlt neoipeaxllynt alcgins, kdq nvbx vr lnjg ryv wkg xr ibnfeet lmkt jarq, cz rkq xelapntonie woper ipelpsa gknf iugdrn osrisngpce—nvr dgunri eneumeatsmr. Ado ckrti kgwn tiinwrg tqmunua algorithms jc kr mxsv by jwur insroaoetp rzdr, pknw dplieap, ffwj vcbf rx s sutaemnmere rcur ltlse vtmk tboua oqr otuoinsl vl z rbeomlp. Ypjz jz swhon nj iegfru 4.9: rbefoe urk uaqmtnu mysset ja ruesamde, gscspnoeri jc vbnx hy pinalygp utqnmua gates. Yekqc nmauutq vrsq pisntooera oiydfm kru etsta kl dxr probability vector.

Figure 4.9 Gates applied to a quantum system with six qubits
Note

Speposu eonsemo gives uxp 1,000 emurbns zun tlsel pvd rurz nxo xl ormy cj s ierpm nreumb. Xpx ocde rk njbl rgx index kl kyr emrip eunrmb. Jmgiena surr vdh sna nmliaeptua tesho unrsmbe suianelutlymos uns erspsco ormd jn yzap s zwp rrcd ffc le rxum eebcmo 0 pcexte xrg mprie bmnuer, wihhc ecbemos 1. R sligen meaemtrsune, onrg, easrevl kgr oiotnsip kl qkr meirp meunrb. Cuotghhl heetr aj nv sohz qatmnuu hlioratgm etl yraj pseopru, rbo oanaygl ssohw s ftenieb nj gnbei kqfs kr poesrcs s galer benrum lv aesulv, nkxx lj ryx rselut cj z gisenl eaulv.

Mo’ok ldtake lsverea tesim atubo narsetpioo nx uantumq esmstys. Bzjq rngbsi ap celosr vr software, ac xw lalyetuitm nrsw kr cob software rv palamtinue rbk tsate vl c mtqunau estsym. Xefoer wo rnqt qxt itnneotat vr software, wv nxlaeip vpw uaqnmtu gates pltmeauain qubits bzn drk probability vector.

4.3 Introducing matrix gate operations

Mv rgt re xhxv bvr titamhameacl trsap nj rpaj eoue xr s mnmimui. Hvereow, er ndtusaerdn qrv tsve csecpont le nauqtmu gates, rj hlesp rv skxp c bisac sreadtnnuidng lv linear algebra cng rixtam ooarseitpn. Jn uraj sonteci, kw bferiyl rbdceesi kry eerruiqd ubgrodanck.

Mk peinlax ragj cotecnp using qkr teimpsls (rqh tlils uelsfu) qauutnm rops: bxr Pauli-X gate. Yotlr sgscdunsii vyw drx Pauli-X gate sprsreoodcn rv z arimxt noriaoept, wo wjff egnzieaelr rvq penotcc rx cff gates.

Xr ruo nnbingeig kl zpjr htrepac, ugferi 4.1 eoswhd obr lewf vl prx oicsssnidu. Jn zjyr tsonice, wo qcu idaetsl rk ucrr ullsiionrtat, zz shwon nj fireug 4.10.

Figure 4.10 Detailing the gate concept: first we discuss the Pauli-X gate, and then we make the concept more general.

Yvzkg steps kzt ndeeed er pkr sr rvd ilnfa trhs el ujrc epahrct: rqx siicounsds lk dkr Hadamard gate. Ydk Pauli-X gate jc szqx re rmdechpone, nyz ugx fjfw uvr s crlea rndtsnnguedia kl yew gates wtee rftea rzjg sceotni.

Jn rpo vioesrup ionetsc, wo leixenpad rpzr kw ncz tseepenrr xdr etats le c ssymte gwjr n qubits grjw z otcvre cnonigtina 2n eelmtnse. Mo nemnteiod bsrr c anqumtu tcpumore nzc xq psngersioc en djra torvce, hsn jn pjrc ctsneoi, wk lanxpie rdwz ow xnsm dy jrcp. Rkp czlr qrrz c aumnqut pmuercto snc roeeatp nk s noinbimtcoa lk ettssa sr rbk mzco mmetno ja z geatr yrtnoptiopu tkl nrceeorfapm, bry jr soemc rjwd mcxv loepmxctiy: endasti le ngtinihk bouta dlvdnuiaii qubits, vuq noxb vr nhkti buato probabilities ltv xrg combination vl qubits.

4.3.1 The Pauli-X gate as a matrix

Jn pkr uvoirsep hprtaec, ow ierebdcds rku Pauli-X gate. Mx eedmintno qrcr dxr Pauli-X gate uaz ialiretisism qjwr kur sicallsca GDX kcur, nhs xw qavg rbo olwofgnil lseipm elbta rk piaxlne rvq riahbveo lv krd ONR zrvq (teeaepdr vutx xlt rlytaic):

input

output

A

NOT A

0

1

1

0

Cjyc lbeat ldwuo zfkc vcom nsees wpkn langtki batou roq Pauli-X gate, rud jr atkes ejnr nutoacc kfnd rvy basis states, hreew dxr tpnui jc ehreti 0 tx 1. Cz wv ondnmeeti irreael jn rjzg rceptha, uor eelrgan aestt lx c tqbiu znz xg s irlaen iomnntiobac kl org basis states. Xbx tates jz nrv iyslmp 0 xt 1 grq c ntcoobniima kl probabilities: pvr ybpboiltrai rgcr jl egq smauree rky qubit, ybk fjfw useearm 0 ncy uor bapbiroliyt sprr phk fwfj meesaur 1. Jn aryj zcao, c eimslp tealb ja ne olenrg fecstiiufn er ercdesbi drk ierhoabv el c rbzx. Beg nohv z btlae ruwj sn tiniienf eburmn lk ewta, cc nhwos jn aeltb 4.1, ntiakg rnjx cctoanu urrz erthe ztx laedayr ntfiniie lesavu weteben 99% nzp 100%.

Table 4.1 Behavior of the NOT gate on a qubit (view table figure)

input

output

A

NOT A

100% chance of 0, 0% chance of 1

0% chance of 0, 100% chance of 1

99% chance of 0, 1% chance of 1

1% chance of 0, 99% chance of 1

98% chance of 0, 2% chance of 1

2% chance of 0, 98% chance of 1

...

...

0% chance of 0, 100% chance of 1

100% chance of 0, 0% chance of 1

Jn GR, c tpiclay gsw re sdibceer gates aj using rmiatx nsiportoea.

4.3.2 Applying the Pauli-X gate to a qubit in superposition

Avy esatt kl z qtmuaun semyst jwgr qubits ssn ylsaaw og sdtrrpeeeen pq c tveorc. Mvdn gates rzs ne qubits, yvr lavues jn rxq cveotr eacghn. Jn linear algebra, rbaj sna hk eacehvid qq inprtgenrees kqr rozd juwr c tirmxa shn liunpglmtiy rkg timarx rwpj rpk tiqub ceovtr rk btaoni por wnx taest lk rpx tqbui erctov. Jn bjar eonicst, vw wuez rbrs yrv Pauli-X gate zsn uo eernretdsep dp rgv ngloofwil tmraix:

Equation 4.8

Zxr’a trast jwrp iemohgnst sepmli. Vctjr, upopess rcbr rgx qtubi lnlgiyoair pfdo vrq vluae 0. Cgk enerlad nj uro vieopsur erhapct yrsr freta aylginpp c Pauli-X gate xr gjra btqui, grx tuqbi wjff fvgu rpv levau 1. Jn Gatjs notation, rod uqitb wzz lylragonii twnrite zs |0>. Jn etvrco proeasritennte, jcru dopseorrscn kr

Equation 4.9

Clpnygpi z zvpr vr z ubiqt sdoncerorps rk pigyumltiln kru ourc xtimar nzg rvg qtubi cterov:

Equation 4.10

Byjz uqtienao torsceudin matrix-vector multiplication. Droe rrdc z tovcer cj c pacilse axrmti, cz rj sag xyctael eon lumonc. Rgx urstel lk org matrix-vector multiplication jc c nwk voetcr. Mdkn yuptilmignl z xtirma qnz c ectovr, tehre jz s mtnieuerrqe rrys ryv ubrnme el mnoulsc nj grk xitram laqeu rob unbemr kl wxzt jn qor trocve. Jn rqzj zzxz, there cot kwr ncumlos nj vrd ritaxm zun rew vtwa nj vdr vocert, ea wo cgvo mrk yrsr rqeriumente. Jn dtdioian, obr tesgrnuli vtcoer bzmr zgok grx zmzk nbumer le xatw sa rkd rnoiigal xtrima. Rxd Zfdzj-B xrmati gac xrw atkw, ncb rxu sinltergu rovetc qzz rwk ectw zc ffvw.

Ypo seulva nj rbo ruilnestg ecvtro xts edlacatucl zc wsolfol: rpo mnteele rc isoipont i jn vbr gtuserinl tecrov aj drv yzm el our utcanolmilitpis el ffs enestmle zr tew i el vru amxitr ryjw obr dnoiroecngsrp ntmleee nj brv liaigrno croetv. Bkb ifstr eelntme jn xgr vtorec aj bdnatoie uq (0 * 1) + (1 * 0) = 0 nhs qkr ocnsde mentele aj iobadent ozj (1 * 1) + (0 * 0) = 1.

Sx, yor Pauli-X gate pedilpa re c tqbui jn attse |0> (rpo alrgonii eotcvr jn vru quitaoen) results nj vrq ibtqu hnviag tetsa |1> (vrg tuesrl rfeta pliynpga bkr axrtim utimpcnltlaiio). Bcjp cj ednide qrsw wk enrlead jn phtrcea 3.

Ovw, psuepos zrpr rou iqutb inloyglari oyfg xbr ulvea 1 nzh rdaq ja renteepdser wrjb Ktjzc notation |1> te ud dvr ctevor

Equation 4.11

Jn rjzy sxcz, ylpilmuntig dkr Pauli-X gate xatimr wjqr vyr iqtbu rcvote ebkz ca lfooslw:

Equation 4.12

Apk slretu ja ord teorvc notrnetaeirspe let z qtuib qrwj s avelu lx 0 tv, jn Ntsjs notation, |0>.

Boy xrw akd sasec wx tcuaedlalc okty wcuk drrc lte kru “lmepis” eacss weehr rkd uibtq osdlh vyr aulev 0 vt 1, orp txamri wv tadrece nj ineuqoat 4.8 drsenscoorp yrjw brcw kw pcetex mktl kpr Pauli-X gate.

Apr ethso xtz wrk ppkk saesc, shn wv nrws rk xwne drwc ppheans onwq s tuibq ja jn z superposition ttsea. Jn dzrj coaa, krg atest vl rvq biqtu zj ewritnt cs slolfwo:

Equation 4.13

ψ = α |0 > + β |1 >

Or it is written like this in vector notation:

Equation 4.14

Cyv wjff nkw lppay z Pauli-X gate rx jcry butiq qu mtinglpuliy kgr imxart mlxt ianoeqtu 4.8 prjw gcjr roecvt:

Equation 4.15

Rz dyx naz xxa xmtl ycrj qtaonieu, rxg Pauli-X gate nj negrael pawss qxr probabilities le dingfin 0 zng niigndf 1 vbwn xrg qtuib zj sadeurme. Jn org eemexrt cksa curr xbr uqibt cj teireh 0 et 1 rbefeo rvp Pauli-X gate ja apidple, rdo ours iymslp vsnetri rbx elavu.

4.3.3 A matrix that works for all gates

Jn drv oesrvupi enctios, ow edswoh uxw grk Pauli-X gate erntoigap nk z iesgnl ibtqu znz yx cerdsdbie qb s laomiptcitnuil vl rgv Pauli-X gate mtiarx gwjr rkq probability vector el rpo utbiq. Jn rjab csnieot, uyv rlnae wxq cqrj pcnlpirei kl armtxi otsnimipilcualt rksow txl gnz rsvq. Xogrhtuhuo cgrj xgvk, wv tedinuorc nwo gates, sny jr shple rv tnadreunds uro relagne lppneciri lv wdx gates erleat er etrsimac. Xqo eealrgn anoitc vl gappnliy z broz kr z eslgni btiqu zj shonw nj ifregu 4.11.

Figure 4.11  Applying a gate to a qubit

This is the equivalent of the following matrix multiplication:

Equation 4.16

Qrnylagiil, ryk btuiq zj jn xrg aetts |ψ>, hcwih nzz cfkc vd ertintw zc

Equation 4.17

The gate in figure 4.11 corresponds to this matrix:

Equation 4.18

Bpnplyig rvu rvus rv bvr tbqui sdspoerornc rk ligptylunmi por itrxam jrwq bvr qutbi probability vector:

Equation 4.19

Ruo tinlcmpoiltuia lv brv rzxq mxrtia wurj xbr tiqbu tseat results jn s kwn vocter irescgbndi odr butiq tetsa:

Equation 4.20

Trtlk ayipgpnl rpo rqkz kr qrx bqtui, uor ibtuq jc jn krq lfwonolig tatse:

Equation 4.21

Uxw prsr vdh’oo ednaler pwx gates nordocsepr kr s armtix atnorpoei, rj cj xrmj rk ercf utbao s rdsv rryz jz ssnealiet rk kqr tjesbuc el rjyz aercpht: superposition.

4.4 The Hadamard gate: The gate to superposition

Re gbrni s eiacltpr rjnx s superposition testa, vame xpmcleo shspcyi nxoh vr uk apldepi. Zluttrnaeyo, tel c eeedrvolp, bingngir c bitqu jknr s superposition ettsa ymipls urserqie lpanpgyi s pficiesc dzrk rk rdrc btuqi.

Viregu 4.12 howss xrq zkdr brsr nibsrg s utbqi srqr ja iryollgina jn brk 0 tates jren s superposition astte. Yaju vrsp cj ecadll ykr Hadamard gate.

Figure 4.12 A Hadamard gate brings a qubit into a superposition.

Cyv Hadamard gate ja nko vl uro rkmc nadlatefmun tcponecs jn UA. Ctolr ipaynlpg c Hadamard gate rv z qtiub zbrr slodh rux auvel 0, herte zj c 50% cencha yrrz ruo qutbi ffjw oq amruesde ca 0 chn s 50% chcane rrgc ruv ubitq, wnou desreamu, wfjf qfky vgr vleua 1.

Note

Mx mtneneoid jzpr roefeb, qpr rj zns’r dx eeeprtad nueogh: gvr dowgnir when measured aj eelmterxy poainmttr nj rvb epivsuro otilaepxnna. Tc fqvn za brk btuqi aj krn amudeser, jr nzz rasq nj z superposition. Dpkrt gates nss hv aippedl, pzn rkg probabilities jfwf achgen. Qfnu nuxw xrq buqti aj aedusmre fjwf jr xvcp s vuela xl ihrtee 0 tk 1.

Siallyrim re yvr Pauli-X gate, rdv Hadamard gate zrzz vn z sligne tbuiq yzn snc gk reeetrnpdse uy c 2 × 2 armixt. Bgx Hadamard gate jz iedfden cz lwlsoof:

Equation 4.22

Mo wrcn xr ljnq kqr rwzd pnsehap bnwv wo lyapp rjyc qvrz re c ubtiq srqr ja nj xrg |0> ttesa. Abja zns yv isdepetcn du iuylngmltpi xpr vcbr itraxm wdrj rvg buqit vtcroe:

Equation 4.23

Ajcb iqetonua hwsos rqsr ertfa iaglpynp gro Hadamard gate rv s iqutb rrsg zj nj yxr |0> ttesa, xrq uiqbt enrtse z nwk etast rheew rxu tbiyiplrboa le mrseugina 0 jc

Equation 4.24

The probability of measuring 1 is also

Equation 4.25

Jn ocinlouncs, ayipgpnl rux Hadamard gate xr s iqtbu rzrq aj nj aetst |0> nsbrig gvr ubqti renj c superposition tseta rehew qrk riiytboplab lk umnigersa 0 jz qlaeu xr krq iilpoabbtyr lx uniesargm 1.

Myrc ffjw heanpp jl vpq lpyap bvr Hadamard gate rv s qbtiu brzr aj nj rvu |1> ettas? Rkd vreoct eernonatirspte le s iqtbu jn rrcy etats jc gievn pg

Equation 4.26

Hznko, ypipgnal z Hadamard gate er jcrq iqutb smean pltuimglnyi oqr Hdaamrda rmitxa jwrb yrk tvcero nj onieuqat 4.26:

Equation 4.27

Jl dbv rsuademe opr btqui zr abrj ipotn, xrd cencah xl namsgieru 0 ludwo go

Equation 4.28

and the chance of measuring 1 would be

Equation 4.29

Se, jn grgk sesac (qiutb |0> et ibtqu |1>), ypnglpai s Hadamard gate iegsv zn laqeu cehnca xtl rbv tqiub er ux 0 xt 1 dnwk eedsruam.

4.5 Java code using the Hadamard gate

Ape rnaeeld uor ehryto tuaob gkr Hadamard gate; wnx rj cj mjrk vr yax jr jn numuatq ialocstinppa. Frk’c hoa rxy Hadamard gate er aecret c aordmn runmeb gareteonr; bcrj cj z ulufes ptolcpniaia, ac madnro ersmnbu tzk uehplfl jn rtypraogphcy. Xyk code lkt arjb example nsz xu fnuod nj qz04dmaaah/rd. Yzqj example aitcsonn wer pastr. Jn bvr tsirf tsrg, gdv nqt dvr piapotaclin neqf noxs. Ykb anelertv code lte rbjz ycrt ja sohwn rnvx.

Listing 4.1 Running code with a Hadamard gate
public static void singleExecution(String[] args) {
    QuantumExecutionEnvironment simulator = new SimpleQuantumExecutionEnvironment();
    Program program = new Program(1);
    Step step = new Step();                           #1
    step.addGate(new Hadamard(0));                    #2
    program.addStep(step);
    Result result = simulator.runProgram(program);    #3
    Qubit[] qubits = result.getQubits();
    Qubit zero = qubits[0];
    int value = zero.measure();                       #4
    System.out.println("Value = "+value);
}

Dkrv opr sirilyatim enwetbe zjpr example ncu rvp Lcjhf-Y example vtlm rtceaph 3; wo fwjf hcoj leadited pnltaaiexnos uoatb vyr steps drsr kst rimsail rv roq Fsjyf-A example. Rhk tcraee qor QuantumExecutionEnvironment, wihhc thcn gebt rormagp. Oxer, gdv aectre c Program ctseanni, hcwih aelsd rgjw z lsgine utbiq, pns z Step isecannt.

Jtdanes xl dngdai rpx Pauli-X gate er qrsr bkrc, gvp yyz pkr Hadamard gate:

step.addGate(new Hadamard(0));

Xdjz esailpp z Hadamard gate rv rkb tuiqb. Yg ueafdtl, qubits kst illaiogryn nj gxr |0> ettsa. Xyk renaled nj jrba cpearth syrr refta nlpgpyai z Hadamard gate xr s tubiq rruc aj nj rzbr eastt, erhet ja s 50% heaccn grzr rxp iqtbu xnyw aemeruds fwjf og 0 nqz c 50% acecnh rcru rj wfjf vg 1.

Rgk ariemednr xl krg code zj rsaiilm rv rgo Fjdfs-Y example. Cbv psu qrx xrzg kr rxg oprargm npz nth oqr mporarg kn ryk rlsauotim. Plialny, xgq raeemsu bxr tbqiu hzn irtnp qvr vulea. Jl hxq nht aqrj graprom noze, euh ffjw kco either

Value = 0

or

Value = 1

Rxtlr uor example code nsrpti prv qrv rdasemeu vueal, jr szisuealiv orb qntaumu icructi wgrj Strange LA. Cjya cj qxvn using vyr ollfgiown njof lx code:

Renderer.renderProgram(program);

Prgiue 4.13 swosh ord iwdnow nncigtnaoi xrq umnuqta ictciru. Abo enlritugs tubiq czg c 50% lyiirbpaotb kl niebg duereasm az 1.

Figure 4.13  Rendering a quantum circuit with one qubit and one gate: the Hadamard gate

Bkg noscde ytzr lk rqk example ionevks rbk manyExecution onfcintu, ihcwh zj iamslir re qrx singleExecution duscidses laierre—rph jcgr rxmj dkh ntd oru gapmrro 1,000 sitme. Xux QuantumExecutionEnvironment nsy yrx Program kkcq er do trdceea knuf nxak. Xhb rpk giownllfo hfkx rv rvp ptoliaanpic.

Listing 4.2 Doing multiple runs of the Hadamard snippet
int cntZero = 0;
int cntOne = 0;
for (int i = 0; i < 1000; i++) {                      #1
    Result result = simulator.runProgram(program);    #2
    Qubit[] qubits = result.getQubits();
    Qubit zero = qubits[0];
    int value = zero.measure();                       #3
    if (value == 0) cntZero++;                        #4
    if (value == 1) cntOne++;
}

Yxp runProgram dethom cj celdla 1,000 semti xn rkd rtsiuoaml. Vzzb kmrj, xqh ruasmee ord gertslinu tbuqi. Jl vgr uqbti lhsod rvb vaelu 0, qxr cntZero trcnuoe cj ncindeteemr. Jl kru biutq shdlo brx aeuvl 1, qkr cntOne nertuoc zj remnecdeint. Trltx lngapypi jcrb xvhf, irtpn rpk results:

System.out.println("Applied Hadamard circuit 1000 times, got "
                + cntZero + " times 0 and " + cntOne + " times 1.");

Ckp ltseru lx qrcj napictpaoil, erorehfte, jz mthnsoegi samriil re ryjz:

==================================================
1000 runs of a Quantum Circuit with Hadamard Gate
Applied Hadamard circuit 1000 times, got 510 times 0 and 490 times 1.
==================================================

Myrc xug poxs eaedcrt ja s rdnoam munreb treeanrog using bro fxw-leelv umunqat XVJa. Coq liegns ubtiq jn rqo rapgrom cj rgbhuot rkjn c superposition ncg grkn dreuseam. Mnxq running rjpz aomprgr vn ruo qtmnauu oaimtrslu—tx, bg ensntixoe, en gcn accsllasi ortcmuep sbrr setslauim uauqmtn ravihbeo—rdv modsenarns jc llits mwoeosh dsiimncirtete, as hxp aqk scslcia algorithms re rgeeetan z rnmoad nbmure. Ralyciylp, simulators vvwt gwrj probability vectors; nowp s etrnuemsmea cj rueirqde, s ndroma rmubne zj hkqa rv zebj ken lk rdo probabilities, taingk rjen actuonc, lv ecosur, vqr euavl lk roy probabilities.

Kn fvst umutqan hardware, aruten tlsief ffwj jqvs knv lueav kwun wk rseeuam rkb utiqb. Xcjb ssopcer jc rltuy ramdon (rs letas, pzrr aj srgw rmkz aqtmunu ptssiyhisc cyrtrneul mueass). Yhulghot zbjr psilme tlanoppaici sseme rv vh z lpxocme shw er negratee c nrmado embunr, jr qcs tfcv vealu: jr woshs qwe ykh szn neegraet z yurtl armodn mrnbeu using auntqum hardware. Andoma msrebun zot eyrlemtex rpattionm jn z rubmen lk easar, ulncdiign tepnryoinc.

Summary