def QPM(): A = 4 N = 8 # Reference Genome size w = "22013220" # 22013200 #randStr(2,N) # Reference Genome M = 2 # Short Read size p = "13" #randStr(2,M) # Short Read s = ceil(log2(N-M)) print(s) config_fn = os.path.join('gateConfig.json') platform = ql.Platform('platform_none', config_fn) total_qubits = 2*s*M-2 prog = ql.Program('qg', total_qubits, platform) # Initialization qk1 = ql.Kernel('QCirc1',platform) Circ1(qk1,s,M) prog.add_kernel(qk1) # Oracle Kernels qk2a = ql.Kernel('QCirc2a',platform) qk2c = ql.Kernel('QCirc2c',platform) qk2g = ql.Kernel('QCirc2g',platform) qk2t = ql.Kernel('QCirc2t',platform) # Grover Amplitude Amplification qk3 = ql.Kernel('QCirc3',platform) Circ3(qk3,s,M) # WATSON : define custom gate from python code... not json... # WATSON : get back display output in code fa = [] fc = [] fg = [] ft = [] for wi in range(0,N): if w[wi] == '0': fa.append(True) fc.append(False) fg.append(False) ft.append(False) elif w[wi] == '1': fa.append(False) fc.append(True) fg.append(False) ft.append(False) elif w[wi] == '2': fa.append(False) fc.append(False) fg.append(True) ft.append(False) else: fa.append(False) fc.append(False) fg.append(False) ft.append(True) print(w,p) for pi in range(0,M): if p[pi] == '0': Circ2(qk2a,fa,s,pi*s,s*M) prog.add_kernel(qk2a) elif p[pi] == '1': Circ2(qk2c,fc,s,pi*s,s*M) prog.add_kernel(qk2c) elif p[pi] == '2': Circ2(qk2g,fg,s,pi*s,s*M) prog.add_kernel(qk2g) else: Circ2(qk2t,ft,s,pi*s,s*M) prog.add_kernel(qk2t) # Reset Kernels prog.add_kernel(qk3) # Run multiple times to get average result #qk4 = ql.Kernel('QCirc4',platform) #qk4.measure(9) #qk4.measure(8) #qk4.measure(7) #qk4.measure(6) #prog.add_kernel(qk4) prog.compile(False, "ASAP", False) display() #showQasm(1) return
import os import numpy as np import unittest from openql import Kernel, Program from openql import openql as ql from test_QISA_assembler_present import assemble rootDir = os.path.dirname(os.path.realpath(__file__)) curdir = os.path.dirname(__file__) config_fn = os.path.join(curdir, 'hardware_config_cc_light.json') platf = ql.Platform('seven_qubits_chip', config_fn) output_dir = os.path.join(curdir, 'test_output') ql.set_option('output_dir', output_dir) ql.set_option('optimize', 'no') ql.set_option('scheduler', 'ALAP') ql.set_option('log_level', 'LOG_WARNING') @unittest.skip class Test_single_qubit_seqs_CCL(unittest.TestCase): def test_bug(self): p = Program("bug", platf, 1) k = Kernel("bugKernel", platform=platf, qubit_count=1) k.gate('rx180', [0]) k.gate('measure', [0]) p.add_kernel(k)
def QPD(): ''' w2di = Image.open("/mnt/7A06EEA206EE5E9F/GoogleDrive/TUD_CE/Thesis/SimQG/QuInE/examples/qtm_a2/panda18.png") w2d = w2di.convert('RGBA') wbw = w2d for i in range(0,w2d.size[0]): for j in range(0,w2d.size[1]): if w2d.getpixel((i,j))[3] == 0: # Set transparent to white wbw.putpixel((i,j),(255,255,255)) elif w2d.getpixel((i,j))[0] > 150: wbw.putpixel((i,j),(255,255,255)) else: wbw.putpixel((i,j),(0,0,0)) wbw.save("/mnt/7A06EEA206EE5E9F/GoogleDrive/TUD_CE/Thesis/SimQG/QuInE/examples/qtm_a2/panda18bw.png") ''' A = 2 # Binary Alphabet {0,1} := {(0,0,0),(255,255,255)} Black and White Image D = 2 w2di = Image.open( "/mnt/7A06EEA206EE5E9F/GoogleDrive/TUD_CE/Thesis/SimQG/QuInE/examples/qtm_a2/panda18bw.png" ) w2d = w2di.convert('RGB') N0 = w2di.size[0] N1 = w2di.size[1] ''' for Msz in range(1,20): M0 = Msz M1 = Msz Q_tag = ceil(log2(N0-M0+1)) + ceil(log2(N1-M1+1)) Q_data = ceil(log2(A)) * M0 * M1 Q_anc = 1 print([Msz,Q_tag+Q_data+Q_anc]) # Template Size vs Total Qubits: [1, 14],[2, 17],[3, 22],[4, 29],[5, 38],[6, 49],[7, 62] ''' ''' M0 = 3 M1 = 3 idx0 = 9 # Expected Answer i_0 idx1 = 8 # Expected Answer i_1 p2d = w2d.crop((idx0,idx1,idx0+M0,idx1+M1)) p2d.save("/mnt/7A06EEA206EE5E9F/GoogleDrive/TUD_CE/Thesis/SimQG/QuInE/examples/qtm_a2/panda_p.png") ans = w2d for i in range(idx0,idx0+M0): for j in range(idx1,idx1+M1): if ans.getpixel((i,j))[0] == 255: ans.putpixel((i,j),(0,255,0)) else: ans.putpixel((i,j),(255,0,0)) ans.save("/mnt/7A06EEA206EE5E9F/GoogleDrive/TUD_CE/Thesis/SimQG/QuInE/examples/qtm_a2/answer.png") ''' p2di = Image.open( "/mnt/7A06EEA206EE5E9F/GoogleDrive/TUD_CE/Thesis/SimQG/QuInE/examples/qtm_a2/panda_p.png" ) p2d = p2di.convert('RGB') M0 = p2di.size[0] M1 = p2di.size[1] Q_tag = ceil(log2(N0 - M0 + 1)) + ceil(log2(N1 - M1 + 1)) Q_data = ceil(log2(A)) * M0 * M1 Q_anc = 1 anc = Q_tag + Q_data config_fn = os.path.join('gateConfig.json') platform = ql.Platform('platform_none', config_fn) Q_total = Q_tag + Q_data + Q_anc prog = ql.Program('qg', Q_total, platform) # Kernel 1: Construct Quantum Phone Directory qk1 = ql.Kernel('QCirc1', platform) Circ1(qk1, w2d, N0, N1, M0, M1, Q_total, Q_tag, Q_data, anc) # Kernel 2: Calculate Hamming Distance qk2 = ql.Kernel('QCirc2', platform) Circ2(qk2, p2d, M0, M1, Q_tag) # Kernel 3: Oracle to Mark Hamming Distance of 0 qk3 = ql.Kernel('QCirc3', platform) Circ3(qk3, Q_tag, Q_data) # Kernel 4: Amplitude Amplification qk4 = ql.Kernel('QCirc4', platform) Circ4(qk4, Q_tag, Q_data, Q_anc) # Kernel 5: Partial Amplitude Amplification qk5 = ql.Kernel('QCirc5', platform) Circ5(qk5, Q_tag, Q_data, Q_anc) prog.add_kernel(qk1) prog.add_kernel(qk2) for i in range(0, 1): # Optimal iteration from calculation is 111 prog.add_kernel(qk3) prog.add_kernel(qk5) prog.add_kernel(qk3) prog.add_kernel(qk4) prog.compile(False, "ASAP", False) display()
def QAM(): print(w, p) config_fn = os.path.join('gateConfig.json') platform = ql.Platform('platform_none', config_fn) prog = ql.Program('qam_a4', total_qubits, platform) # Kernel 1: Initialization of Quantum Phone Directory qk1 = ql.Kernel('QCirc1', platform) Circ1(qk1) # Kernel 2: Calculate Hamming Distance qk2 = ql.Kernel('QCirc2', platform) Circ2(qk2) # Kernel 3: Oracle to Mark Hamming Distance of 0 qk3 = ql.Kernel('QCirc3', platform) Circ3(qk3) # Kernel 4: Grover Amplitude Amplification qk4 = ql.Kernel('QCirc4', platform) Circ4(qk4) # Kernel 5: Oracle to Mark Memory States qk5 = ql.Kernel('QCirc5', platform) Circ5(qk5) # Kernel 6: Measurement qk6 = ql.Kernel('QCirc6', platform) Circ6(qk6) # Construct Program from Kernels prog.add_kernel(qk1) # Initialise prog.add_kernel(qk2) # Transform to Hamming distance prog.add_kernel(qk3) # Oracle call prog.add_kernel(qk4) # Inversion about mean prog.add_kernel(qk5) # Memory Oracle prog.add_kernel(qk4) # Inversion about mean for r in range(0, 3): prog.add_kernel(qk3) # Oracle call prog.add_kernel(qk4) # Inversion about mean # prog.add_kernel(qk6) # Uncomment if using measurement based analytics prog.compile() qx = qxelarator.QX() qx.set('test_output/qam_a4.qasm') # Result analytics using Internal State Vector qx.execute() qxopt = qx.get_state() isv = [0] * (2**total_qubits) ptrn = re.compile( '\(([+-]\d+.?\d*)e?(-\d*)?,([+-]\d+.?\d*)e?(-\d*)?\)\s[|]([0-1]*)>') for line in qxopt.splitlines(): mtch = ptrn.search(line) if mtch != None: ar = float(mtch.group(1)) if mtch.group(2) != None: are = float(mtch.group(2)) ar = ar * 10**are ac = float(mtch.group(3)) if mtch.group(4) != None: ace = float(mtch.group(4)) ac = ac * 10**ace state = int(mtch.group(5), 2) isv[state] = ar**2 + ac**2 ploty = isv isvt = [0] * (2**4) for tagi in range(0, 2**total_qubits): if isv[tagi] > 0.03: ti = format(tagi, '0' + str(total_qubits) + 'b') isvt[int(ti[:4:-1], 2)] = isv[tagi] for tagi in range(0, 16): print("Tag : ", tagi, "\t probability ", round(isvt[tagi], 5)) plt.plot(isv) plt.ylabel('Probability') plt.xlabel('Index') plt.ylim([0, 1]) plt.show() return
def test_config_file(self): platf_name = 'starmon_platform' config_fn = os.path.join(curdir, 'hardware_config_cc_light.json') platf = ql.Platform(platf_name, config_fn) self.assertEqual(platf.config_file, config_fn)
def unit_tests(): curdir = os.path.dirname(__file__) output_dir = os.path.join(curdir, 'qasm') ql.set_option('output_dir', output_dir) ql.set_option('write_qasm_files', 'yes') config_fn = os.path.join(curdir, 'config_qx.json') platform = ql.Platform('platform_none', config_fn) move = [0] # Addend: 1-bit Move head = [1, 2, 3, 4] # Augend: 4-bit Head anc = [5, 6, 7] # Carry: uncomputed ancilla mod = [8, 9] # Modulo for 12 and Ancilla for nCX test = [10, 11, 12, 13] circ_width = len(move) + len(head) + len(anc) + len(mod) + len(test) p = ql.Program('aritra', platform, circ_width) k_move = ql.Kernel("mod_move", platform, circ_width) # Test using full superposition of head, both inc/dec and association to initial state for i in range(0, len(head)): k_move.gate('h', [head[i]]) k_move.gate('cnot', [head[i], test[i]]) k_move.gate('h', [move[0]]) ########################################################################################################## ########################################################################################################## ########################################################################################################## # # Test modulo incrementor # # if move==1 and head==1011, mod==1 # k_move.gate('x',[head[2]]) # print("ctrl",[move[0],head[0],head[1],head[2],head[3]],"targ",[mod[0]],"anc",[mod[1]]) # qsdk.nCX(k_move, [move[0],head[0],head[1],head[2],head[3]], [mod[0]], [mod[1]]) # k_move.gate('x',[head[2]]) # # (+0.176777,+0) |0000-00-000-0000-0> + # # (+0.176777,+0) |0000-00-000-0000-1> + # # (+0.176777,+0) |0001-00-000-0001-0> + # # (+0.176777,+0) |0001-00-000-0001-1> + # # (+0.176777,+0) |0010-00-000-0010-0> + # # (+0.176777,+0) |0010-00-000-0010-1> + # # (+0.176777,+0) |0011-00-000-0011-0> + # # (+0.176777,+0) |0011-00-000-0011-1> + # # (+0.176777,+0) |0100-00-000-0100-0> + # # (+0.176777,+0) |0100-00-000-0100-1> + # # (+0.176777,+0) |0101-00-000-0101-0> + # # (+0.176777,+0) |0101-00-000-0101-1> + # # (+0.176777,+0) |0110-00-000-0110-0> + # # (+0.176777,+0) |0110-00-000-0110-1> + # # (+0.176777,+0) |0111-00-000-0111-0> + # # (+0.176777,+0) |0111-00-000-0111-1> + # # (+0.176777,+0) |1000-00-000-1000-0> + # # (+0.176777,+0) |1000-00-000-1000-1> + # # (+0.176777,+0) |1001-00-000-1001-0> + # # (+0.176777,+0) |1001-00-000-1001-1> + # # (+0.176777,+0) |1010-00-000-1010-0> + # # (+0.176777,+0) |1010-00-000-1010-1> + # # (+0.176777,+0) |1011-00-000-1011-0> + # # (+0.176777,+0) |1011-01-000-1011-1> + THIS CASE <--------------------- # # (+0.176777,+0) |1100-00-000-1100-0> + INVALID CASES # # (+0.176777,+0) |1100-00-000-1100-1> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1101-0> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1101-1> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1110-0> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1110-1> + INVALID CASES # # (+0.176777,+0) |1111-00-000-1111-0> + INVALID CASES # # (+0.176777,+0) |1111-00-000-1111-1> + INVALID CASES # # 4-bit Adder # U_add(k_move, [move[0],-1,mod[0],-1], head, anc) # # (+0.176777,+0) |0000-00-000-0000-0> + # # (+0.176777,+0) |0000-00-000-0001-1> + # # (+0.176777,+0) |0001-00-000-0001-0> + # # (+0.176777,+0) |0001-00-000-0010-1> + # # (+0.176777,+0) |0010-00-000-0010-0> + # # (+0.176777,+0) |0010-00-000-0011-1> + # # (+0.176777,+0) |0011-00-000-0011-0> + # # (+0.176777,+0) |0011-00-000-0100-1> + # # (+0.176777,+0) |0100-00-000-0100-0> + # # (+0.176777,+0) |0100-00-000-0101-1> + # # (+0.176777,+0) |0101-00-000-0101-0> + # # (+0.176777,+0) |0101-00-000-0110-1> + # # (+0.176777,+0) |0110-00-000-0110-0> + # # (+0.176777,+0) |0110-00-000-0111-1> + # # (+0.176777,+0) |0111-00-000-0111-0> + # # (+0.176777,+0) |0111-00-000-1000-1> + # # (+0.176777,+0) |1000-00-000-1000-0> + # # (+0.176777,+0) |1000-00-000-1001-1> + # # (+0.176777,+0) |1001-00-000-1001-0> + # # (+0.176777,+0) |1001-00-000-1010-1> + # # (+0.176777,+0) |1010-00-000-1010-0> + # # (+0.176777,+0) |1010-00-000-1011-1> + # # (+0.176777,+0) |1011-00-000-1011-0> + # # (+0.176777,+0) |1011-01-000-0000-1> + THIS CASE <--------------------- # # (+0.176777,+0) |1100-00-000-1100-0> + INVALID CASES # # (+0.176777,+0) |1100-00-000-1101-1> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1101-0> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1110-1> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1110-0> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1111-1> + INVALID CASES # # (+0.176777,+0) |1111-00-000-0000-1> + INVALID CASES # # (+0.176777,+0) |1111-00-000-1111-0> + INVALID CASES # # if move==1 and head==0000, mod==0 # k_move.gate('x',[head[0]]) # k_move.gate('x',[head[1]]) # k_move.gate('x',[head[2]]) # k_move.gate('x',[head[3]]) # qsdk.nCX(k_move, [move[0],head[0],head[1],head[2],head[3]], [mod[0]], [mod[1]]) # k_move.gate('x',[head[0]]) # k_move.gate('x',[head[1]]) # k_move.gate('x',[head[2]]) # k_move.gate('x',[head[3]]) # # (+0.176777,+0) |0000-00-000-0000-0> + # # (+0.176777,+0) |0000-00-000-0001-1> + # # (+0.176777,+0) |0001-00-000-0001-0> + # # (+0.176777,+0) |0001-00-000-0010-1> + # # (+0.176777,+0) |0010-00-000-0010-0> + # # (+0.176777,+0) |0010-00-000-0011-1> + # # (+0.176777,+0) |0011-00-000-0011-0> + # # (+0.176777,+0) |0011-00-000-0100-1> + # # (+0.176777,+0) |0100-00-000-0100-0> + # # (+0.176777,+0) |0100-00-000-0101-1> + # # (+0.176777,+0) |0101-00-000-0101-0> + # # (+0.176777,+0) |0101-00-000-0110-1> + # # (+0.176777,+0) |0110-00-000-0110-0> + # # (+0.176777,+0) |0110-00-000-0111-1> + # # (+0.176777,+0) |0111-00-000-0111-0> + # # (+0.176777,+0) |0111-00-000-1000-1> + # # (+0.176777,+0) |1000-00-000-1000-0> + # # (+0.176777,+0) |1000-00-000-1001-1> + # # (+0.176777,+0) |1001-00-000-1001-0> + # # (+0.176777,+0) |1001-00-000-1010-1> + # # (+0.176777,+0) |1010-00-000-1010-0> + # # (+0.176777,+0) |1010-00-000-1011-1> + # # (+0.176777,+0) |1011-00-000-0000-1> + THIS CASE <--------------------- # # (+0.176777,+0) |1011-00-000-1011-0> + # # (+0.176777,+0) |1100-00-000-1100-0> + INVALID CASES # # (+0.176777,+0) |1100-00-000-1101-1> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1101-0> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1110-1> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1110-0> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1111-1> + INVALID CASES # # (+0.176777,+0) |1111-00-000-1111-0> + INVALID CASES # # (+0.176777,+0) |1111-01-000-0000-1> + INVALID CASES ########################################################################################################## ########################################################################################################## ########################################################################################################## # # Test modulo decrementor # # if move==0 and head==0000, mod==1 # k_move.gate('x',[head[0]]) # k_move.gate('x',[head[1]]) # k_move.gate('x',[head[2]]) # k_move.gate('x',[head[3]]) # k_move.gate('x',[move[0]]) # print("ctrl",[move[0],head[0],head[1],head[2],head[3]],"targ",[mod[0]],"anc",[mod[1]]) # qsdk.nCX(k_move, [move[0],head[0],head[1],head[2],head[3]], [mod[0]], [mod[1]]) # k_move.gate('x',[head[0]]) # k_move.gate('x',[head[1]]) # k_move.gate('x',[head[2]]) # k_move.gate('x',[head[3]]) # k_move.gate('x',[move[0]]) # # (+0.176777,+0) |0000-00-000-0000-1> + # # (+0.176777,+0) |0000-01-000-0000-0> + THIS CASE <--------------------- # # (+0.176777,+0) |0001-00-000-0001-0> + # # (+0.176777,+0) |0001-00-000-0001-1> + # # (+0.176777,+0) |0010-00-000-0010-0> + # # (+0.176777,+0) |0010-00-000-0010-1> + # # (+0.176777,+0) |0011-00-000-0011-0> + # # (+0.176777,+0) |0011-00-000-0011-1> + # # (+0.176777,+0) |0100-00-000-0100-0> + # # (+0.176777,+0) |0100-00-000-0100-1> + # # (+0.176777,+0) |0101-00-000-0101-0> + # # (+0.176777,+0) |0101-00-000-0101-1> + # # (+0.176777,+0) |0110-00-000-0110-0> + # # (+0.176777,+0) |0110-00-000-0110-1> + # # (+0.176777,+0) |0111-00-000-0111-0> + # # (+0.176777,+0) |0111-00-000-0111-1> + # # (+0.176777,+0) |1000-00-000-1000-0> + # # (+0.176777,+0) |1000-00-000-1000-1> + # # (+0.176777,+0) |1001-00-000-1001-0> + # # (+0.176777,+0) |1001-00-000-1001-1> + # # (+0.176777,+0) |1010-00-000-1010-0> + # # (+0.176777,+0) |1010-00-000-1010-1> + # # (+0.176777,+0) |1011-00-000-1011-0> + # # (+0.176777,+0) |1011-00-000-1011-1> + # # (+0.176777,+0) |1100-00-000-1100-0> + INVALID CASES # # (+0.176777,+0) |1100-00-000-1100-1> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1101-0> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1101-1> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1110-0> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1110-1> + INVALID CASES # # (+0.176777,+0) |1111-00-000-1111-0> + INVALID CASES # # (+0.176777,+0) |1111-00-000-1111-1> + INVALID CASES # # 4-bit Subtractor # U_sub(k_move, [move[0],-1,mod[0],-1], head, anc) # # (+0.176777,+0) |0000-00-000-0000-1> + # # (+0.176777,+0) |0000-01-000-1011-0> + THIS CASE <--------------------- # # (+0.176777,+0) |0001-00-000-0000-0> + # # (+0.176777,+0) |0001-00-000-0001-1> + # # (+0.176777,+0) |0010-00-000-0001-0> + # # (+0.176777,+0) |0010-00-000-0010-1> + # # (+0.176777,+0) |0011-00-000-0010-0> + # # (+0.176777,+0) |0011-00-000-0011-1> + # # (+0.176777,+0) |0100-00-000-0011-0> + # # (+0.176777,+0) |0100-00-000-0100-1> + # # (+0.176777,+0) |0101-00-000-0100-0> + # # (+0.176777,+0) |0101-00-000-0101-1> + # # (+0.176777,+0) |0110-00-000-0101-0> + # # (+0.176777,+0) |0110-00-000-0110-1> + # # (+0.176777,+0) |0111-00-000-0110-0> + # # (+0.176777,+0) |0111-00-000-0111-1> + # # (+0.176777,+0) |1000-00-000-0111-0> + # # (+0.176777,+0) |1000-00-000-1000-1> + # # (+0.176777,+0) |1001-00-000-1000-0> + # # (+0.176777,+0) |1001-00-000-1001-1> + # # (+0.176777,+0) |1010-00-000-1001-0> + # # (+0.176777,+0) |1010-00-000-1010-1> + # # (+0.176777,+0) |1011-00-000-1010-0> + # # (+0.176777,+0) |1011-00-000-1011-1> + # # (+0.176777,+0) |1100-00-000-1011-0> + INVALID CASES # # (+0.176777,+0) |1100-00-000-1100-1> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1100-0> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1101-1> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1101-0> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1110-1> + INVALID CASES # # (+0.176777,+0) |1111-00-000-1110-0> + INVALID CASES # # (+0.176777,+0) |1111-00-000-1111-1> + INVALID CASES # # if move==0 and head==1011, mod==0 # k_move.gate('x',[head[2]]) # k_move.gate('x',[move[0]]) # qsdk.nCX(k_move, [move[0],head[0],head[1],head[2],head[3]], [mod[0]], [mod[1]]) # k_move.gate('x',[head[2]]) # k_move.gate('x',[move[0]]) # # (+0.176777,+0) |0000-00-000-0000-1> + # # (+0.176777,+0) |0000-00-000-1011-0> + THIS CASE <--------------------- # # (+0.176777,+0) |0001-00-000-0000-0> + # # (+0.176777,+0) |0001-00-000-0001-1> + # # (+0.176777,+0) |0010-00-000-0001-0> + # # (+0.176777,+0) |0010-00-000-0010-1> + # # (+0.176777,+0) |0011-00-000-0010-0> + # # (+0.176777,+0) |0011-00-000-0011-1> + # # (+0.176777,+0) |0100-00-000-0011-0> + # # (+0.176777,+0) |0100-00-000-0100-1> + # # (+0.176777,+0) |0101-00-000-0100-0> + # # (+0.176777,+0) |0101-00-000-0101-1> + # # (+0.176777,+0) |0110-00-000-0101-0> + # # (+0.176777,+0) |0110-00-000-0110-1> + # # (+0.176777,+0) |0111-00-000-0110-0> + # # (+0.176777,+0) |0111-00-000-0111-1> + # # (+0.176777,+0) |1000-00-000-0111-0> + # # (+0.176777,+0) |1000-00-000-1000-1> + # # (+0.176777,+0) |1001-00-000-1000-0> + # # (+0.176777,+0) |1001-00-000-1001-1> + # # (+0.176777,+0) |1010-00-000-1001-0> + # # (+0.176777,+0) |1010-00-000-1010-1> + # # (+0.176777,+0) |1011-00-000-1010-0> + # # (+0.176777,+0) |1011-00-000-1011-1> + # # (+0.176777,+0) |1100-00-000-1100-1> + INVALID CASES # # (+0.176777,+0) |1100-01-000-1011-0> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1100-0> + INVALID CASES # # (+0.176777,+0) |1101-00-000-1101-1> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1101-0> + INVALID CASES # # (+0.176777,+0) |1110-00-000-1110-1> + INVALID CASES # # (+0.176777,+0) |1111-00-000-1110-0> + INVALID CASES # # (+0.176777,+0) |1111-00-000-1111-1> + INVALID CASES ########################################################################################################## ########################################################################################################## ########################################################################################################## # Integrated # Test modulo incrementor # if move==1 and head==1011, mod==1 k_move.gate('x', [head[2]]) qsdk.nCX(k_move, [move[0], head[0], head[1], head[2], head[3]], [mod[0]], [mod[1]]) k_move.gate('x', [head[2]]) # 4-bit Adder U_add(k_move, [move[0], -1, mod[0], -1], head, anc) # if move==1 and head==0000, mod==0 k_move.gate('x', [head[0]]) k_move.gate('x', [head[1]]) k_move.gate('x', [head[2]]) k_move.gate('x', [head[3]]) qsdk.nCX(k_move, [move[0], head[0], head[1], head[2], head[3]], [mod[0]], [mod[1]]) k_move.gate('x', [head[0]]) k_move.gate('x', [head[1]]) k_move.gate('x', [head[2]]) k_move.gate('x', [head[3]]) # Test modulo decrementor # if move==0 and head==0000, mod==1 k_move.gate('x', [head[0]]) k_move.gate('x', [head[1]]) k_move.gate('x', [head[2]]) k_move.gate('x', [head[3]]) k_move.gate('x', [move[0]]) qsdk.nCX(k_move, [move[0], head[0], head[1], head[2], head[3]], [mod[0]], [mod[1]]) k_move.gate('x', [head[0]]) k_move.gate('x', [head[1]]) k_move.gate('x', [head[2]]) k_move.gate('x', [head[3]]) k_move.gate('x', [move[0]]) # 4-bit Subtractor U_sub(k_move, [move[0], -1, mod[0], -1], head, anc) # if move==0 and head==1011, mod==0 k_move.gate('x', [head[2]]) k_move.gate('x', [move[0]]) qsdk.nCX(k_move, [move[0], head[0], head[1], head[2], head[3]], [mod[0]], [mod[1]]) k_move.gate('x', [head[2]]) k_move.gate('x', [move[0]]) # (+0.176777,+0) |0000-00-000-0001-1> + # (+0.176777,+0) |0000-00-000-1011-0> + THIS CASE <--------------------- # (+0.176777,+0) |0001-00-000-0000-0> + # (+0.176777,+0) |0001-00-000-0010-1> + # (+0.176777,+0) |0010-00-000-0001-0> + # (+0.176777,+0) |0010-00-000-0011-1> + # (+0.176777,+0) |0011-00-000-0010-0> + # (+0.176777,+0) |0011-00-000-0100-1> + # (+0.176777,+0) |0100-00-000-0011-0> + # (+0.176777,+0) |0100-00-000-0101-1> + # (+0.176777,+0) |0101-00-000-0100-0> + # (+0.176777,+0) |0101-00-000-0110-1> + # (+0.176777,+0) |0110-00-000-0101-0> + # (+0.176777,+0) |0110-00-000-0111-1> + # (+0.176777,+0) |0111-00-000-0110-0> + # (+0.176777,+0) |0111-00-000-1000-1> + # (+0.176777,+0) |1000-00-000-0111-0> + # (+0.176777,+0) |1000-00-000-1001-1> + # (+0.176777,+0) |1001-00-000-1000-0> + # (+0.176777,+0) |1001-00-000-1010-1> + # (+0.176777,+0) |1010-00-000-1001-0> + # (+0.176777,+0) |1010-00-000-1011-1> + # (+0.176777,+0) |1011-00-000-0000-1> + THIS CASE <--------------------- # (+0.176777,+0) |1011-00-000-1010-0> + # (+0.176777,+0) |1100-00-000-1101-1> + INVALID CASES # (+0.176777,+0) |1100-01-000-1011-0> + INVALID CASES # (+0.176777,+0) |1101-00-000-1100-0> + INVALID CASES # (+0.176777,+0) |1101-00-000-1110-1> + INVALID CASES # (+0.176777,+0) |1110-00-000-1101-0> + INVALID CASES # (+0.176777,+0) |1110-00-000-1111-1> + INVALID CASES # (+0.176777,+0) |1111-00-000-1110-0> + INVALID CASES # (+0.176777,+0) |1111-01-000-1100-1> + INVALID CASES p.add_kernel(k_move) p.compile() print(p.qasm()) qx = qxelarator.QX() qx.set(output_dir + '/aritra.qasm') qx.execute() isv = qx.get_state() print(isv)
output_dir = os.path.join( curdir, 'cqasm_files') # From pathname of cqasm output from OpenQL on compilation ql.set_option('output_dir', output_dir) # Set output directory pathname in OpenQL ql.set_option('write_qasm_files', 'yes') # Set option to generate qasm output (Default: no) # ql.set_option('optimize', 'no') # Not so useful for us currently # ql.set_option('scheduler', 'ASAP') # Not so useful for us currently # ql.set_option('log_level', 'LOG_INFO') # Not so useful for us currently # ql.set_option('use_default_gates', 'yes') # Not so useful for us currently config_fn = os.path.join( curdir, 'config_qx.json') # From pathname of hardware specification platform = ql.Platform( 'platform_none', config_fn ) # Set hardware specification (should be minimalistic for QX simulator) num_qubits = 3 # You need to know number of qubits before initiating the program/kernel p = ql.Program('exercise_qasm_001', platform, num_qubits) # Declare the program on the platform k1 = ql.Kernel("kernel_1", platform, num_qubits) # Declare a kernel on the platform # NOTE: The final result does not matter for this experiment. This is just to get famalier with the syntax of using different gates. k1.prepz( 0 ) # Initialize qubit 0 to |0> (Default is also |0> state, so strictly not required, just for good practice) q_num = 1
def QPM(): print(w, p) config_fn = os.path.join('gateConfig.json') platform = ql.Platform('platform_none', config_fn) prog = ql.Program('qpm_a4', total_qubits, platform) # Kernel 1: Initialization qk1 = ql.Kernel('QCirc1', platform) #qk1.rz(0,0.25) qk1.gate("rz", [1], 40, 0.3) Circ1(qk1) # Kernel 2: Oracles to mark specific character qk2 = ql.Kernel('QCirc2', platform) bfa = ''.join('1' if w[i] == '0' else '0' for i in range(len(w))) bfc = ''.join('1' if w[i] == '1' else '0' for i in range(len(w))) bfg = ''.join('1' if w[i] == '2' else '0' for i in range(len(w))) bft = ''.join('1' if w[i] == '3' else '0' for i in range(len(w))) # Kernel 3: Grover Amplitude Amplification qk3 = ql.Kernel('QCirc3', platform) Circ3(qk3, s, M) # Kernel 4: Measurement qk4 = ql.Kernel('QCirc4', platform) Circ4(qk4) # Construct Program from Kernels prog.add_kernel(qk1) # Initialise #for pi in range(0,M): # Alternate iteration method for r in range(0, int(sqrt(N - M + 1))): pi = random.randint(0, M - 1) if p[pi] == '0': Circ2(qk2, bfa, pi) elif p[pi] == '1': Circ2(qk2, bfc, pi) elif p[pi] == '2': Circ2(qk2, bfg, pi) else: Circ2(qk2, bft, pi) prog.add_kernel(qk2) # Conditional kernel call del qk2 # IMPROVE: Kernel to qubit loose binding being discussed qk2 = ql.Kernel('QCirc2', platform) prog.add_kernel(qk3) # Inversion about mean # prog.add_kernel(qk4) # Uncomment if using measurement based analytics prog.compile() # showQasm() qx = qxelarator.QX() qx.set('test_output/qpm_a4.qasm') # Result analytics using Internal State Vector qx.execute() qxopt = qx.get_state() isv = [0] * (2**total_qubits) ptrn = re.compile('\(([+-]\d+.\d*),([+-]\d+[.\d*]?)\)\s[|]([0-1]*)>') for line in qxopt.splitlines(): mtch = ptrn.search(line) if mtch != None: ar = float(mtch.group(1)) ac = float(mtch.group(2)) state = int(mtch.group(3), 2) isv[state] = ar**2 + ac**2 ploty = [0] * (2**s) for i in range(0, len(isv)): stot = format(i, '0' + str(total_qubits) + 'b')[::-1] sopt = int(stot[0:s], 2) ploty[sopt] = ploty[sopt] + isv[i] print("PMax:", np.amax(ploty)) print("Index:", np.argmax(ploty)) plt.plot(ploty) plt.ylabel('Probability') plt.xlabel('Solution space') plt.ylim([0, 1]) plt.show() # Result analytics using Measurement ''' res = [0]*s STT = 1000 # Number of quantum state tomography trials true_counter = 0 for i in range(STT): qx.execute() res[0] = res[0] + qx.get_measurement_outcome(0) res[1] = res[1] + qx.get_measurement_outcome(1) res[2] = res[2] + qx.get_measurement_outcome(2) index = ''.join('1' if res[i] > STT/2 else '0' for i in range(s)) print("Index:",int(index,2)) ''' return
def test_qec(self): ql.set_option('output_dir', output_dir) ql.set_option('optimize', 'no') ql.set_option('scheduler', 'ALAP') ql.set_option('scheduler_uniform', 'yes') ql.set_option('log_level', 'LOG_WARNING') platform = ql.Platform(platform_name, config_fn) p = ql.Program('test_qec', platform, num_qubits, num_cregs) k = ql.Kernel('kernel_0', platform, num_qubits, num_cregs) # pipelined QEC: [ # see: R. Versluis et al., Phys. Rev. A 8, 034021 (2017) # - nw, ne, sw, se] -> [n, e, w, s] because we rotate grid # - H -> rym90, ry90, see Fig 2 of reference # # class SurfaceCode, qubits, tiles, width, getNeighbourN, getNeighbourE, getNeighbourW, getNeighbourS, getX, getZ, getData # define qubit aliases: # FIXME: neighbours make no sense anymore x = 7 xN = x - 5 xE = x + 1 xS = x + 5 xW = x - 1 z = 11 zN = z - 5 zE = z + 1 zS = z + 5 zW = z - 1 # create classical registers rdX = ql.CReg() rdZ = ql.CReg() # X stabilizers k.gate("rym90", [x]) k.gate("rym90", [xN]) k.gate("rym90", [xE]) k.gate("rym90", [xW]) k.gate("rym90", [xS]) k.wait(all_qubits, 0) # k.wait({x, xN, xE, xW, xS}, 0) k.gate("cz", [x, xE]) k.gate("cz", [x, xN]) k.gate("cz", [x, xS]) k.gate("cz", [x, xW]) k.wait(all_qubits, 0) # k.wait({x, xN, xE, xW, xS}, 0) k.gate("ry90", [x]) k.gate("ry90", [xN]) k.gate("ry90", [xE]) k.gate("ry90", [xW]) k.gate("ry90", [xS]) k.wait(all_qubits, 0) # k.wait({x, xN, xE, xW, xS}, 0) k.gate("measure", [x], rdX) # k.wait(all_qubits, 0) k.wait([x], 0) # Z stabilizers k.gate("rym90", [z]) k.gate("cz", [z, zE]) k.gate("cz", [z, zS]) k.gate("cz", [z, zN]) k.gate("cz", [z, zW]) k.gate("ry90", [z]) k.gate("measure", [z], rdZ) p.add_kernel(k) p.compile()
def test_uniform_scheduler_1(self): ql.set_option('output_dir', output_dir) ql.set_option('optimize', 'no') ql.set_option('scheduler', 'ALAP') ql.set_option('scheduler_uniform', 'yes') ql.set_option('log_level', 'LOG_WARNING') config_fn = os.path.join(curdir, 'test_cfg_none_s7.json') platform = ql.Platform('starmon', config_fn) num_qubits = 7 p = ql.Program('test_uniform_scheduler_1', platform, num_qubits, 0) k = ql.Kernel('kernel_1', platform, num_qubits, 0) # just as the previous one # but then more of the same for j in range(7): k.gate("x", [j]) k.gate("cnot", [0, 2]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [6, 3]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [1, 4]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [2, 5]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [3, 1]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [4, 6]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [2, 0]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [3, 6]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [4, 1]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [5, 2]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [1, 3]) for j in range(7): k.gate("x", [j]) k.gate("cnot", [6, 4]) for j in range(7): k.gate("x", [j]) p.add_kernel(k) p.compile()
def QPD(): A = 4 # DNA Alphabet {0,1,2,3} := {A,C,G,T} N = 10 # Reference String size w = "2302031020" # Reference String #randStr(A,N) M = 3 # Search String size dummyp = "000" # indices out of range will be tagged with dummyp data p = "203" # Search String #randStr(A,M) asz = ceil(log2(A)) Q1 = asz * M # Data Qubits Q2 = ceil(log2(N - M + 1)) # Tag Qubits config_fn = os.path.join('gateConfig.json') platform = ql.Platform('platform_none', config_fn) ancmax = 1 #(Q1+Q2)-2 anc = Q1 + Q2 total_qubits = Q1 + Q2 + ancmax prog = ql.Program('qg', total_qubits, platform) # Kernel 1: Construct Quantum Phone Directory qk1 = ql.Kernel('QCirc1', platform) Circ1(qk1, asz, w, N, M, total_qubits, Q2, anc) # Kernel 2: Calculate Hamming Distance qk2 = ql.Kernel('QCirc2', platform) Circ2(qk2, asz, p, M, Q1, Q2) # Kernel 3: Oracle to Mark Hamming Distance of 0 qk3 = ql.Kernel('QCirc3', platform) Circ3(qk3, asz, M, Q1, Q2, anc) # Kernel 4: Amplitude Amplification qk4 = ql.Kernel('QCirc4', platform) Circ4(qk4, Q1, Q2, anc) # Finding optimal iterations for known arbitrary initial amplitude distribution ''' A = 4; M = 3; N = 10; r = 1; iMx = N-M+1; qtag = ceil(log2(iMx)); qb = qtag + M*ceil(log2(A)); sMx = 2^qb; kavg0 = 1/sqrt(iMx); lavg0 = (iMx - r)/((sMx - r)*sqrt(iMx)); Pmax = 1 - (sMx-iMx)*lavg0^2 - (iMx-r)*(1/sqrt(iMx) - lavg0)^2 j = [0:9]; T = ((j+0.5)*pi - atan(kavg0*sqrt(r/(sMx-r))/lavg0))/acos(1-2*r/sMx)' ''' # T = [3.3964, 38.9279, 74.4593, 109.9908, 145.5223, 181.0538, 216.5853, 252.1168, 287.6483, 323.1798] prog.add_kernel(qk1) prog.add_kernel(qk2) for i in range(0, 3): prog.add_kernel(qk3) prog.add_kernel(qk4) prog.compile(False, "ASAP", False) display()
def test_platform_name(self): platf_name = 'starmon_platform' config_fn = os.path.join(curdir, 'test_cfg_cbox.json') platf = ql.Platform(platf_name, config_fn) self.assertEqual(platf.name, platf_name)
def test_mapper_lingling7(self): # parameters v = 'lingling7' config = os.path.join(rootDir, "test_mapper_s17.json") num_qubits = 9 # create and set platform prog_name = "test_mapper_" + v kernel_name = "kernel_" + v starmon = ql.Platform("starmon", config) prog = ql.Program(prog_name, starmon, num_qubits, 0) k = ql.Kernel(kernel_name, starmon, num_qubits, 0) k.gate("prepz", [7]) k.gate("prepz", [8]) k.gate("x", [7]) k.gate("ym90", [7]) k.gate("ym90", [4]) k.gate("cz", [7, 4]) k.gate("ry90", [4]) k.gate("ym90", [8]) k.gate("cz", [0, 8]) k.gate("ry90", [8]) k.gate("ym90", [8]) k.gate("cz", [7, 8]) k.gate("ry90", [8]) k.gate("ym90", [6]) k.gate("cz", [7, 6]) k.gate("ry90", [6]) k.gate("ym90", [8]) k.gate("cz", [2, 8]) k.gate("ry90", [8]) k.gate("ym90", [3]) k.gate("cz", [7, 3]) k.gate("ry90", [3]) k.gate("ym90", [8]) k.gate("cz", [4, 8]) k.gate("ry90", [8]) k.gate("ym90", [8]) k.gate("cz", [7, 8]) k.gate("ry90", [8]) k.gate("ym90", [5]) k.gate("cz", [7, 5]) k.gate("ry90", [5]) k.gate("ym90", [8]) k.gate("cz", [6, 8]) k.gate("ry90", [8]) k.gate("x", [7]) k.gate("ym90", [7]) k.gate("measure", [7]) k.gate("measure", [8]) k.gate("prepz", [7]) k.gate("prepz", [8]) k.gate("x", [7]) k.gate("ym90", [7]) k.gate("ym90", [5]) k.gate("cz", [7, 5]) k.gate("ry90", [5]) k.gate("ym90", [8]) k.gate("cz", [1, 8]) k.gate("ry90", [8]) k.gate("ym90", [8]) k.gate("cz", [7, 8]) k.gate("ry90", [8]) k.gate("ym90", [6]) k.gate("cz", [7, 6]) k.gate("ry90", [6]) k.gate("ym90", [8]) k.gate("cz", [2, 8]) k.gate("ry90", [8]) k.gate("ym90", [3]) k.gate("cz", [7, 3]) k.gate("ry90", [3]) k.gate("ym90", [8]) k.gate("cz", [5, 8]) k.gate("ry90", [8]) k.gate("ym90", [8]) k.gate("cz", [7, 8]) k.gate("ry90", [8]) k.gate("ym90", [4]) k.gate("cz", [7, 4]) k.gate("ry90", [4]) k.gate("ym90", [8]) k.gate("cz", [6, 8]) k.gate("ry90", [8]) k.gate("x", [7]) k.gate("ym90", [7]) k.gate("measure", [7]) k.gate("measure", [8]) k.gate("prepz", [7]) k.gate("prepz", [8]) k.gate("x", [7]) k.gate("ym90", [7]) k.gate("ym90", [1]) k.gate("cz", [7, 1]) k.gate("ry90", [1]) k.gate("ym90", [8]) k.gate("cz", [2, 8]) k.gate("ry90", [8]) k.gate("ym90", [8]) k.gate("cz", [7, 8]) k.gate("ry90", [8]) k.gate("ym90", [5]) k.gate("cz", [7, 5]) k.gate("ry90", [5]) k.gate("ym90", [8]) k.gate("cz", [6, 8]) k.gate("ry90", [8]) k.gate("ym90", [2]) k.gate("cz", [7, 2]) k.gate("ry90", [2]) k.gate("ym90", [8]) k.gate("cz", [0, 8]) k.gate("ry90", [8]) k.gate("ym90", [8]) k.gate("cz", [7, 8]) k.gate("ry90", [8]) k.gate("ym90", [6]) k.gate("cz", [7, 6]) k.gate("ry90", [6]) k.gate("ym90", [8]) k.gate("cz", [4, 8]) k.gate("ry90", [8]) k.gate("x", [7]) k.gate("ym90", [7]) k.gate("measure", [7]) k.gate("measure", [8]) prog.add_kernel(k) prog.compile() GOLD_fn = os.path.join(rootDir, 'golden', prog.name + '.qisa') QISA_fn = os.path.join(output_dir, prog.name + '.qisa') assemble(QISA_fn) self.assertTrue(file_compare(QISA_fn, GOLD_fn))
def test_mapper_allDopt(self): # all possible cnots in s7, avoiding collisions: # - the pair of possible CNOTs in both directions hopefully in parallel # - these pairs ordered from low distance to high distance to avoid disturbance by swaps # - and then as much as possible in opposite sides of the circuit to improve ILP # idea is to get shortest latency circuit with all possible cnots # there is no initial mapping that maps this right so initial placement cannot find it # so the heuristics must act and insert swaps/moves # parameters v = 'allDopt' config = os.path.join(rootDir, "test_mapper_s7.json") num_qubits = 7 # create and set platform prog_name = "test_mapper_" + v kernel_name = "kernel_" + v starmon = ql.Platform("starmon", config) prog = ql.Program(prog_name, starmon, num_qubits, 0) k = ql.Kernel(kernel_name, starmon, num_qubits, 0) for j in range(7): k.gate("x", [j]) k.gate("cnot", [0, 3]) k.gate("cnot", [3, 0]) k.gate("cnot", [6, 4]) k.gate("cnot", [4, 6]) k.gate("cnot", [3, 1]) k.gate("cnot", [1, 3]) k.gate("cnot", [5, 2]) k.gate("cnot", [2, 5]) k.gate("cnot", [1, 4]) k.gate("cnot", [4, 1]) k.gate("cnot", [3, 5]) k.gate("cnot", [5, 3]) k.gate("cnot", [6, 3]) k.gate("cnot", [3, 6]) k.gate("cnot", [2, 0]) k.gate("cnot", [0, 2]) k.gate("cnot", [0, 1]) k.gate("cnot", [1, 0]) k.gate("cnot", [3, 4]) k.gate("cnot", [4, 3]) k.gate("cnot", [1, 6]) k.gate("cnot", [6, 1]) k.gate("cnot", [6, 5]) k.gate("cnot", [5, 6]) k.gate("cnot", [3, 2]) k.gate("cnot", [2, 3]) k.gate("cnot", [5, 0]) k.gate("cnot", [0, 5]) k.gate("cnot", [0, 6]) k.gate("cnot", [6, 0]) k.gate("cnot", [1, 5]) k.gate("cnot", [5, 1]) k.gate("cnot", [0, 4]) k.gate("cnot", [4, 0]) k.gate("cnot", [6, 2]) k.gate("cnot", [2, 6]) k.gate("cnot", [2, 1]) k.gate("cnot", [1, 2]) k.gate("cnot", [5, 4]) k.gate("cnot", [4, 5]) k.gate("cnot", [2, 4]) k.gate("cnot", [4, 2]) for j in range(7): k.gate("x", [j]) prog.add_kernel(k) prog.compile() GOLD_fn = os.path.join(rootDir, 'golden', prog.name + '.qisa') QISA_fn = os.path.join(output_dir, prog.name + '.qisa') assemble(QISA_fn) self.assertTrue(file_compare(QISA_fn, GOLD_fn))
import os import unittest from openql import openql as ql curdir = os.path.dirname(__file__) config_fn = os.path.join(curdir, 'test_cfg_cbox.json') platf = ql.Platform("starmon", config_fn) output_dir = os.path.join(curdir, 'test_output') class Test_kernel(unittest.TestCase): @classmethod def setUpClass(self): ql.set_option('output_dir', output_dir) def minimal(self): nqubits = 1 # create a kernel k = ql.Kernel("aKernel", platf, nqubits) # populate a kernel k.prepz(0) k.identity(0) k.measure(0) sweep_points = [2] # create a program p = ql.Program("aProgram", platf, nqubits)
def QPD(): print(w, p) config_fn = os.path.join('gateConfig.json') platform = ql.Platform('platform_none', config_fn) prog = ql.Program('qpd_a4', total_qubits, platform) # Kernel 1: Initialization of Quantum Phone Directory qk1 = ql.Kernel('QCirc1', platform) Circ1(qk1) # Kernel 2: Calculate Hamming Distance qk2 = ql.Kernel('QCirc2', platform) Circ2(qk2) # Kernel 3: Oracle to Mark Hamming Distance of 0 qk3 = ql.Kernel('QCirc3', platform) Circ3(qk3) # Kernel 4: Grover Amplitude Amplification qk4 = ql.Kernel('QCirc4', platform) Circ4(qk4) # Kernel 5: Measurement qk5 = ql.Kernel('QCirc5', platform) Circ5(qk5) # Finding optimal iterations for known arbitrary initial amplitude distribution t = 1 # Expected number of solutions iMx = 2**Q_T sMx = 2**(Q_D + Q_T) kavg0 = 1 / sqrt(iMx) lavg0 = (iMx - t) / ((sMx - iMx) * sqrt(iMx)) Pmax = 1 - (sMx - iMx) * lavg0**2 - (iMx - t) * (1 / sqrt(iMx) - lavg0)**2 print("Theoretical PMax:", Pmax) T = [0] * 5 for j in range(0, 5): T[j] = ((j / 2 + 0.5) * pi - atan(kavg0 * sqrt(t / (sMx - t)) / lavg0)) / acos(1 - 2 * t / sMx) print("Suggested Iterations:", T) # IMPROVE: Use suggested iterations # Construct Program from Kernels prog.add_kernel(qk1) # Initialise prog.add_kernel(qk2) # Transform to Hamming distance for r in range(0, 1): prog.add_kernel(qk3) # Oracle call prog.add_kernel(qk4) # Inversion about mean # prog.add_kernel(qk5) # Uncomment if using measurement based analytics prog.compile() # showQasm() qx = qxelarator.QX() qx.set('test_output/qpd_a4.qasm') # Result analytics using Internal State Vector qx.execute() qxopt = qx.get_state() isv = [0] * (2**total_qubits) ptrn = re.compile('\(([+-]\d+.\d*),([+-]\d+[.\d*]?)\)\s[|]([0-1]*)>') for line in qxopt.splitlines(): mtch = ptrn.search(line) if mtch != None: ar = float(mtch.group(1)) ac = float(mtch.group(2)) state = int(mtch.group(3), 2) isv[state] = ar**2 + ac**2 ploty = isv print("PMax:", np.amax(ploty)) tag = format(np.argmax(ploty), '0' + str(total_qubits - 1) + 'b')[::-1] print("Index:", int(tag[0:3], 2)) plt.plot(ploty) plt.ylabel('Probability') plt.xlabel('State space') plt.ylim([0, 1]) plt.show() return
from openql import openql as ql import os import numpy as np curdir = os.path.dirname(__file__) output_dir = os.path.join(curdir, 'test_output') ql.set_output_dir(output_dir) config_fn = os.path.join(curdir, '/home/daniel/Master/Quantum_Computing_and_Quantum_Information/OpenQL/tests/hardware_config_cc_light.json') platform = ql.Platform('platform_none', config_fn) sweep_points = [1,2] num_circuits = 1 num_qubits = 26 p = ql.Program('benstein_vazirani_24b_secret_2', num_qubits, platform) p.set_sweep_points(sweep_points, num_circuits) k = ql.Kernel('benstein_vazirani_24b_secret_2', platform) k.gate('prepx',24) k.gate('x',24) k.gate('h',0) k.gate('h',1) k.gate('h',2) k.gate('h',3) k.gate('h',4) k.gate('h',5) k.gate('h',6) k.gate('h',7) k.gate('h',8) k.gate('h',9) k.gate('h',10) k.gate('h',11) k.gate('h',12) k.gate('h',13) k.gate('h',14)
def grover(): config_fn = os.path.join('qg_v0p1.json') platform = ql.Platform('platform_none', config_fn) num_qubits = 4 p = ql.Program('qg', num_qubits, platform) k = ql.Kernel('k_bv', platform) # Alternate Commands Syntax: k.x(6), k.y(2), k.cnot(0,1) for q in range(0, num_qubits): k.prepz(q) ''' # Creating a superposition state k.gate("h",0) k.gate("h",1) cycles = ceil(sqrt(pow(num_qubits,2))) # Quadratic Query complexity speedup # Encoding the Oracle function for f(|11>) = 1 k.gate("cz",0,1) # Grover diffusion - amplitude amplification - inversion about mean # kron(h,h) * kron(x,x*h)*cnot*kron(x,h*x) * kron(h,h) k.gate("h",0) k.gate("h",1) k.gate("x",0) k.gate("x",1) k.gate("h",1) k.gate("cnot",0,1) k.gate("h",1) k.gate("x",0) k.gate("x",1) k.gate("h",0) k.gate("h",1) for q in range (0,num_qubits): k.measure(q) ''' k.gate("h", 0) k.gate("h", 1) k.gate("cnot", 0, 2) k.gate("cnot", 1, 3) k.gate("x", 1) k.gate("cnot", 1, 3) k.gate("x", 1) k.gate("x", 0) #k.gate("toffoli",0,1,3) #k.gate("toffoli",0,1,2) k.gate("x", 0) # add the kernel to the program p.add_kernel(k) # compile the program p.compile(False, "ASAP", False) display() return