def main(n): intro() q = qclib.qcsim(n) print "Building Uf operator ..." Ufgate = Uf(q) print "Buiding UinvM operator ..." UinvMgate = UinvM(q) print "Take from the following, best of 5 results..." for m in range(5): # Look for best of 5 q.qreset() # Hn on x-register q.qgate(q.Hn(n - 1), range(n - 1, 0, -1)) # prepare b as |-> q.qgate(q.X(), [0]) q.qgate(q.H(), [0]) numitrs = int(q.pi * (2.0**((n - 1.0) / 2.0)) / 4.0) # optimal # iter, less or more dont work for itr in range(numitrs): q.qgate(Ufgate, list(reversed(range(n)))) q.qgate(q.Hn(n - 1), range(n - 1, 0, -1)) q.qgate(UinvMgate, list(reversed(range(n)))) q.qgate(q.Hn(n - 1), range(n - 1, 0, -1)) res = q.qmeasure(range(n - 1, 0, -1)) print "Result", m, "=", res
import qclib import numpy as np try: init = np.transpose(np.matrix([0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],dtype=complex)) q = qclib.qcsim(4, initstate=init) sq2 = np.sqrt(2) mybasis = ["MY BASIS",np.matrix([[1.0/sq2,1.0/sq2],[1.0/sq2,-1.0/sq2]],dtype=complex)] v = q.qmeasure([1],basis=mybasis) print v q.qreport() except qclib.QClibError, ex: print ex.args
import numpy as np import qclib for e in range(11): # 1. one-after-another create states of different entangelment with D istarr = [1.0-e/10.0,e/10.0,e/10.0,1.0-e/10.0] norm = 0.0 for s in istarr: norm += s*s norm = np.sqrt(norm) istarr = istarr/norm ist = np.transpose(np.matrix(istarr,dtype=complex)) # 2. Initialize the QSIM with that as the initial state q = qclib.qcsim(2,initstate=ist,validation=True) # 3. now run the test 'sample' number of times to get statistics samples = 1000 dist = [0]*2 for i in range(samples): q.qreset() q.qgate(q.H(),[0]) m = q.qmeasure([0]) v = m[0] dist[v] += 1 # 4. Pretty print the results str_dist = "" for i in range(2): str_dist += " |"+str(i)+"> "+str(np.round(100.0*float(dist[i])/float(samples),decimals=2))+"%"
import qclib q = qclib.qcsim(4, qtrace=True, qzeros=True, visualize=True) q.qgate(q.H(), [1]) q.qgate(q.C(), [1, 0]) q.qgate(q.H(), [2]) q.qgate(q.H(), [3])
#!/usr/bin/python import qclib import numpy as np from fractions import gcd # Initialize the Quantum Computer nqbits = 8 # per the definition of f(x) below, must be >= 4 q = qclib.qcsim(nqbits) M = 2**(nqbits-2) # setup the periodic function op1 = q.qstretch(q.C(),[2,0]) op2 = q.qstretch(q.C(),[3,1]) f = q.qcombine_seq("F(x)",[op1,op2]) print "Psst ... f(x) defined as having period of 4\n" # Now loop to repeatedly find values of multiples of M/r by # running the QC repeatedly and reading the outputs idx = 0 vals = np.zeros(2) while idx < 2: # Restart the QC machine q.qreset() # QFT(x) - F(x) - QFT(x) - Measure q.qgate(q.QFT(nqbits-2),range(nqbits-1,1,-1)) q.qgate(f,list(reversed(range(nqbits)))) # measure this if you like - q.qmeasure([1,0]) q.qgate(q.QFT(nqbits-2),range(nqbits-1,1,-1))
import qclib import numpy as np try: init = np.transpose( np.matrix([0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=complex)) q = qclib.qcsim(4, initstate=init) sq2 = np.sqrt(2) mybasis = [ "MY BASIS", np.matrix([[1.0 / sq2, 1.0 / sq2], [1.0 / sq2, -1.0 / sq2]], dtype=complex) ] v = q.qmeasure([1], basis=mybasis) print v q.qreport() except qclib.QClibError, ex: print ex.args
import qclib q = qclib.qcsim(4, qtrace=True) ## X gate q.qgate(q.X(), [0]) q.qgate(q.X(), [1]) q.qgate(q.X(), [2]) q.qgate(q.X(), [3]) # Y gate q.qgate(q.Y(), [0]) q.qgate(q.Y(), [1]) invY = q.qinverse(q.Y()) q.qgate(invY, [1]) q.qgate(invY, [0]) # Z gate q.qgate(q.Z(), [0]) q.qgate(q.Z(), [1]) q.qgate(q.Z(), [2]) q.qgate(q.Z(), [3]) # H gate q.qreset() q.qgate(q.H(), [2]) q.qgate(q.H(), [3]) q.qgate(q.C(), [2, 0]) q.qgate(q.C(), [3, 1]) # Rphi gate
------------------------------------------------------------------------------------------------------- """ import qclib import numpy as np nqbits = 8 try: # setup an initial state to try out QFT initstate = [None]*(2**nqbits) p = 0 stsz = 2**nqbits for i in range(stsz): if (i % (stsz/8)) == 0: initstate[i] = 1 else: initstate[i] = 0 p += np.absolute(initstate[i])**2 initstate = np.transpose(np.matrix(initstate,dtype=complex))/np.sqrt(p) # Start the Quantum Computer Simulator q = qclib.qcsim(nqbits,initstate=initstate, qtrace=True) # Perform QFT qftgate = q.QFT(nqbits) q.qgate(qftgate, list(reversed(range(nqbits)))) except qclib.QClibError,ex: print ex.args
ci+1 ----------o---o----- --.-- ci ------.---|---.----- --|-- | | | | bi ----.-|---.---|----- --|-- ai --.-|-|---.---|----- --|-- | | | | | si+1 --|-|-|-------|----- --o-- si --o-o-o-----x-.-x--- ----- Need to copy the final carry-out bit into the highest order result bit, as shown. """ import qclib q = qclib.qcsim(10,qtrace=True) s = [0, 1, 2] # sum bits - Result of addition c = [3, 4, 5] # carry bits, scratch pad register - Junk bits b = [6, 7] # input number B a = [8, 9] # input number A H4 = q.qcombine_par("H4",[q.H(),q.H(),q.H(),q.H()]) q.qgate(H4,[a[0],a[1],b[0],b[1]]) # run the addition for i in range(2): print "Processing bit", i q.qgate(q.C(),[a[i],s[i]]) q.qgate(q.C(),[b[i],s[i]]) q.qgate(q.C(),[c[i],s[i]])
import qclib q = qclib.qcsim(4,qtrace=True,qzeros=True,visualize=True) q.qgate(q.H(),[1]) q.qgate(q.C(),[1,0]) q.qgate(q.H(),[2]) q.qgate(q.H(),[3])
import qclib q = qclib.qcsim(4) q.qgate(q.H(),[0]) q.qgate(q.C(),[0,1]) q.qmeasure([0,1],basis=q.BELL_BASIS(),qtrace=True) q = qclib.qcsim(4) q.qgate(q.H(),[0]) q.qgate(q.X(),[1]) q.qgate(q.C(),[0,1]) q.qmeasure([0,1],basis=q.BELL_BASIS(),qtrace=True) q = qclib.qcsim(4) q.qgate(q.X(),[0]) q.qgate(q.H(),[0]) q.qgate(q.C(),[0,1]) q.qmeasure([0,1],basis=q.BELL_BASIS(),qtrace=True) q = qclib.qcsim(4) q.qgate(q.X(),[0]) q.qgate(q.H(),[0]) q.qgate(q.X(),[1]) q.qgate(q.C(),[0,1]) q.qmeasure([0,1],basis=q.BELL_BASIS(),qtrace=True)
import qclib q = qclib.qcsim(4,qtrace=True) ## X gate q.qgate(q.X(),[0]) q.qgate(q.X(),[1]) q.qgate(q.X(),[2]) q.qgate(q.X(),[3]) # Y gate q.qgate(q.Y(),[0]) q.qgate(q.Y(),[1]) invY = q.qinverse(q.Y()) q.qgate(invY,[1]) q.qgate(invY,[0]) # Z gate q.qgate(q.Z(),[0]) q.qgate(q.Z(),[1]) q.qgate(q.Z(),[2]) q.qgate(q.Z(),[3]) # H gate q.qreset() q.qgate(q.H(),[2]) q.qgate(q.H(),[3]) q.qgate(q.C(),[2,0]) q.qgate(q.C(),[3,1])
ci+1 ----------o---o----- --.-- ci ------.---|---.----- --|-- | | | | bi ----.-|---.---|----- --|-- ai --.-|-|---.---|----- --|-- | | | | | si+1 --|-|-|-------|----- --o-- si --o-o-o-----x-.-x--- ----- Need to copy the final carry-out bit into the highest order result bit, as shown. """ import qclib q = qclib.qcsim(10, qtrace=False) s = [0, 1, 2] # sum bits - Result of addition c = [3, 4, 5] # carry bits, scratch pad register - Junk bits b = [6, 7] # input number B a = [8, 9] # input number A H4 = q.qcombine_par("H4", [q.H(), q.H(), q.H(), q.H()]) q.qgate(H4, [a[0], a[1], b[0], b[1]]) # run the addition for i in range(2): print "Processing bit", i q.qgate(q.C(), [a[i], s[i]]) q.qgate(q.C(), [b[i], s[i]]) q.qgate(q.C(), [c[i], s[i]])
def initqc(n): global q q = qclib.qcsim(n, qtrace=True)
------------------------------------------------------------------------------------------------------- """ import qclib import numpy as np nqbits = 8 try: # setup an initial state to try out QFT initstate = [None] * (2**nqbits) p = 0 stsz = 2**nqbits for i in range(stsz): if (i % (stsz / 8)) == 0: initstate[i] = 1 else: initstate[i] = 0 p += np.absolute(initstate[i])**2 initstate = np.transpose(np.matrix(initstate, dtype=complex)) / np.sqrt(p) # Start the Quantum Computer Simulator q = qclib.qcsim(nqbits, initstate=initstate, qtrace=True) # Perform QFT qftgate = q.QFT(nqbits) q.qgate(qftgate, list(reversed(range(nqbits)))) except qclib.QClibError, ex: print ex.args
ci+1 ----------o---o----- --.-- ci ------.---|---.----- --|-- | | | | bi ----.-|---.---|----- --|-- ai --.-|-|---.---|----- --|-- | | | | | si+1 --|-|-|-------|----- --o-- si --o-o-o-----x-.-x--- ----- Need to copy the final carry-out bit into the highest order result bit, as shown. """ import qclib q = qclib.qcsim(10, qtrace=True) s = [0, 1, 2] # sum bits - Result of addition c = [3, 4, 5] # carry bits, scratch pad register - Junk bits b = [6, 7] # input number B a = [8, 9] # input number A H4 = q.qcombine_par("H4", [q.H(), q.H(), q.H(), q.H()]) q.qgate(H4, [a[0], a[1], b[0], b[1]]) # run the addition for i in range(2): print "Processing bit", i q.qgate(q.C(), [a[i], s[i]]) q.qgate(q.C(), [b[i], s[i]]) q.qgate(q.C(), [c[i], s[i]])
import qclib qc = qclib.qcsim(8, qtrace=True) qc.qgate(qc.H(), [3]) qc.qgate(qc.C(), [3, 0]) print print "------------------------------------------------------------" qc = qclib.qcsim(8, qtrace=True) qc.qgate(qc.H(), [3]) stC = qc.qstretch(qc.C(), [3, 0]) qc.qgate(stC, [7, 6, 5, 4, 3, 2, 1, 0])
import qclib import numpy as np try: nqbits = 6 # initialise with the default state of all qbits = |0> q = qclib.qcsim(nqbits,qtrace=True) q.qgate(q.H(),[1]) q.qgate(q.C(),[1,0]) # initialise with initial states of qbits q = qclib.qcsim(nqbits,prepqubits=[[1,0],[1,0],[1,0],[0,1],[1,0],[0,1]],qtrace=True) q.qgate(q.H(),[1]) q.qgate(q.C(),[1,0]) # or build the full state yourself (good if you need a very custom state, e.g., for testing QFT) initstate = [None]*(2**nqbits) p = 0 stsz = 2**nqbits for i in range(stsz): c = np.cos(i*2*np.pi/stsz) s = np.sin(i*2*np.pi/stsz) initstate[i] = complex(c,s) p += np.absolute(initstate[i])**2 initstate = np.transpose(np.matrix(initstate,dtype=complex))/np.sqrt(p) q = qclib.qcsim(nqbits,initstate=initstate, qtrace=True) q.qgate(q.H(),[1]) q.qgate(q.C(),[1,0]) except qclib.QClibError, ex:
""" nqbits = 8 def fx(q): toss = int(rnd.random() * 2.0) if toss == 0: print "fx is CONSTANT." elif toss == 1: print "fx is BALANCED." q.qgate(q.C(), [1, 0]) try: q = qclib.qcsim(nqbits) q.qgate(q.X(), [0]) q.qgate(q.H(), [0]) for i in range(nqbits - 1): q.qgate(q.H(), [i + 1]) fx(q) # q.qmeasure([0]) # measure it if you like, does not change anything for i in range(nqbits - 1): q.qgate(q.H(), [i + 1]) v = q.qmeasure(range(nqbits - 1, 0, -1), qtrace=False) if 1 in v: print "Found fx is BALANCED"
#!/usr/bin/python import qclib import numpy as np q = qclib.qcsim(3) # put the qubit 2 in some randoom state # this is the qubit that is to be teleported by Alice to Bob print "\nThe qubit 2 is put in some random state, it will be teleported into qubit 0." q.qgate(q.RND(),[2], qtrace=True) # get qubits 0 and 1 in the triplet state, |00> + |11> # Alice and Bob have one of each of these entangled qubits q.qgate(q.H(),[0]) q.qgate(q.C(),[0,1]) ''' 2 -.-[H]-(/)------.-- | | 1 -o-----(/)--.---|-- | | 0 -----------[X]-[Z]-- qubit 2 teleported into qubit 0 Clearly, while much simpler to understand, this is not a practical way of doing it, as the controlled X and controlled Z gates will need to span the physical distance betwee where Alice and Bob are. ''' print "\nStarting the teleportation protocol..."
# Prepare a qubit in some random state theta = rnd.random() * 2 * np.pi c = np.cos(theta) s = np.sin(theta) # prepare the initial state with the above qubit as qbit-2 tbit = np.transpose(np.matrix([[c,s]],dtype=complex)) initstate = np.transpose(np.matrix([[1,0]],dtype=complex)) for i in range(NQUBITS-1): if i == 2: initstate = np.kron(initstate,tbit) else: initstate = np.kron(initstate,np.transpose(np.matrix([[1,0]],dtype=complex))) # now get started ... q = qclib.qcsim(NQUBITS,initstate=initstate) q.qreport(header="Initial State") # first put qbits 0 and 1 in bell state q.qgate(q.H(),[0]) q.qgate(q.C(),[0,1]) q.qreport(header="0 and 1 in bell state") # qbit 1 is kept 'here' and qbit 0 is sent 'far away'; and we have # the input qbit to be teleported, qbit 2, also 'here'. # at 'here' we CNOT qbit 2 (control qubit) and qbit 1 q.qgate(q.C(), [2,1]) # at 'here' perform H on qubit 2 (the qubit to be teleported) q.qgate(q.H(),[2]) q.qreport(header="C(2,1), H(2)")
import qclib print print "Apply H on 0 then C on 0,3 then Measure 0" qc = qclib.qcsim(8) qc.qreport(header="Initial State") qc.qgate(qc.H(), [0]) qc.qgate(qc.C(), [0, 3]) qc.qreport() qc.qmeasure([0]) qc.qreport()
########################################################################### ## Step 2: Now apply the secret function f() qbit_list = range(self.inputsz + 1) qbit_list.reverse() self.qc.qgate(fx, qbit_list) print "Step 2: Applied FX to |b>|x>" ########################################################################### ## Step 3: Again apply H on all qbits of |x> for i in range(self.inputsz): self.qc.qgate(self.qc.H(), [i]) print "Step 3: Again Applied H to all |x> qbits" ########################################################################### ## Step 4: Measure all qbits of |x> v = self.qc.qmeasure(range( self.inputsz)) # this will give [LSB, ..., MSB] res = 0 for i in range(self.inputsz): res += (v[i] << i) print "Step 4: Measured all qbits of |x>" print "Result = " + code_fmt.format(res) if __name__ == "__main__": qc = qclib.qcsim(7) bv = bernvazi(qc) bv.run()
#!/usr/bin/python import qclib import numpy as np q = qclib.qcsim(3) # put the qubit 2 in some randoom state # this is the qubit that is to be teleported by Alice to Bob print "\nThe qubit 2 is put in some random state, it will be teleported into qubit 0." q.qgate(q.RND(), [2], qtrace=True) # get qubits 0 and 1 in the triplet state, |00> + |11> # Alice and Bob have one of each of these entangled qubits q.qgate(q.H(), [0]) q.qgate(q.C(), [0, 1]) ''' 2 -.-[H]-(/)------.-- | | 1 -o-----(/)--.---|-- | | 0 -----------[X]-[Z]-- qubit 2 teleported into qubit 0 Clearly, while much simpler to understand, this is not a practical way of doing it, as the controlled X and controlled Z gates will need to span the physical distance betwee where Alice and Bob are. ''' print "\nStarting the teleportation protocol..." q.qgate(q.C(), [2, 1])
import qclib qc = qclib.qcsim(8,qtrace=True) qc.qgate(qc.H(),[3]) qc.qgate(qc.C(),[3,0]) print print "------------------------------------------------------------" qc = qclib.qcsim(8,qtrace=True) qc.qgate(qc.H(),[3]) stC = qc.qstretch(qc.C(),[3,0]) qc.qgate(stC,[7,6,5,4,3,2,1,0])
import qclib q = qclib.qcsim(4) q.qgate(q.H(), [0]) q.qgate(q.C(), [0, 1]) q.qmeasure([0, 1], basis=q.BELL_BASIS(), qtrace=True) q = qclib.qcsim(4) q.qgate(q.H(), [0]) q.qgate(q.X(), [1]) q.qgate(q.C(), [0, 1]) q.qmeasure([0, 1], basis=q.BELL_BASIS(), qtrace=True) q = qclib.qcsim(4) q.qgate(q.X(), [0]) q.qgate(q.H(), [0]) q.qgate(q.C(), [0, 1]) q.qmeasure([0, 1], basis=q.BELL_BASIS(), qtrace=True) q = qclib.qcsim(4) q.qgate(q.X(), [0]) q.qgate(q.H(), [0]) q.qgate(q.X(), [1]) q.qgate(q.C(), [0, 1]) q.qmeasure([0, 1], basis=q.BELL_BASIS(), qtrace=True)