def demonstrate_adder(a, b, mode='sim'): if mode == 'sim': result = sim.sort_by_prob(sim.local_sim(adder(a, b), printresult=False)) ans = int(result[0][0], 2) print(ans) print(ans == a+b) elif mode == 'exp': simresult = sim.quantumComputerExp(adder(a, b), printresult=True, shots=8192) probsort = sim.sort_by_prob(simresult) keysort = sim.sort_by_key(simresult) l1 = len(probsort) l2 = len(keysort) print('sorted by probability') for i in range(l1): print(i, probsort[i]) print('sorted by keys') for i in range(l2): print(i, keysort[i]) else: raise Exception('InvalidMode')
def demonstrate_cua(x, a, N, c='1', simulation='sim'): bitlen = math.ceil(math.log2(N + 1)) + 1 #??? print(f'bitlen: {bitlen}') gate = cu_a(bitlen, a, N) qc = qk.QuantumCircuit(2 * bitlen + 2, 2 * bitlen + 2) # flip control if c == '1': qc.x(0) # initialize x_bin = '{n:0{bit}b}'.format(n=x, bit=bitlen) for i in range(bitlen): if x_bin[i] == '1': qc.x(i + 1) qc.append(gate, qargs=qc.qregs[0]) for i in range(bitlen * 2 + 2): qc.measure(i, bitlen * 2 + 1 - i) # print(qc.draw()) if simulation == 'sim': full_result = sim.sort_by_prob(sim.local_sim(qc, printresult=False)) # print(full_result) top_result = full_result[0][0] if top_result[0] != c: raise Exception('ControlBitWasChanged') check = int(top_result[bitlen + 1:], 2) if check > 0: raise Exception('AncillaBitNotClean') prod = int(top_result[1:bitlen + 1], 2) print('Calculated result:', prod) print('Correct result:', x * a % N) else: raise Exception('UnsupportedSimulationMode')
def demonstrating_cc_phi_add_a_mod_N(a, b, N, control='11', simulation='sim'): bitlen = math.ceil(math.log2(N + 1)) + 1 print(f'bitlen: {bitlen}') modadd = cc_phi_add_a_mod_N(bitlen, a, N) qft_gate = qft.qft(bitlen) qc = qk.QuantumCircuit(bitlen + 3, bitlen) # flip controls for i in range(2): if control[i] == '1': qc.x(i) a_bin = '{n:0{bit}b}'.format(n=a, bit=bitlen) b_bin = '{n:0{bit}b}'.format(n=b, bit=bitlen) for i in range(bitlen): if b_bin[i] == '1': qc.x(i + 2) qc.append(qft_gate, qargs=qc.qregs[0][2:bitlen + 2]) qc.append(modadd, qargs=qc.qregs[0]) qc.append(qft_gate.inverse(), qargs=qc.qregs[0][2:bitlen + 2]) for i in range(bitlen): qc.measure(i + 2, bitlen - i - 1) if simulation == 'sim': ans = int( sim.sort_by_prob(sim.local_sim(qc, printresult=True))[0][0], 2) print(f'{a}+{b} mod {N} = {ans}, control: {control}') print('actual ans:', (a + b) % N) elif simulation == 'exp': result = sim.sort_by_prob(sim.quantumComputerExp(qc, shots=8192)) print(result) elif simulation == None: pass else: raise Exception('InvalidSimulationMode')
register[n - 1]) circuit.barrier() circuit.x(register) circuit.h(register) qr = qk.QuantumRegister(3) cr = qk.ClassicalRegister(3) groverCircuit = qk.QuantumCircuit(qr, cr) groverCircuit.h(qr) groverCircuit.barrier() phase_oracle(groverCircuit, qr) groverCircuit.barrier() inversion_about_average(groverCircuit, qr, 3) groverCircuit.barrier() groverCircuit.measure_all() return groverCircuit if __name__ == "__main__": sim.local_sim(find_101_110())
print() def a_8(): qreg = qk.QuantumRegister(8) creg = qk.ClassicalRegister(3) qc = qk.QuantumCircuit(qreg, creg) # initialize qc.x(7) qc.h([0, 1, 2]) # multiplier qc.cx(2, 7) qc.cx(2, 4) inv_qft = qft.qft(3) qc.append(inv_qft, qargs=qreg[:3]) for i in range(3): qc.measure(i, i) # qc.draw(output='mpl') # plt.savefig('../presentation/20200528/figs/11circuit.pdf') return qc if __name__ == "__main__": qc = a_8() sim.local_sim(qc)
for measresult in result.keys(): result_input = measresult[-1:n-1:-1] categorized_result[result_input] = ( categorized_result.get(result_input, 0) + result[measresult]) print(categorized_result) if '1' in s: correct = True for y in categorized_result.keys(): ip = 0 for b in zip(s, y): ip += int(b[0])*int(b[1]) if (ip % 2) : correct = False if correct: print('The result is correct.') else: print('The result is incorrect.') else: if len(categorized_result) == 2**n: print('The result is correct.') else: print('The result is incorrect.') qk.visualization.plot_histogram(categorized_result).savefig('local_sim.svg') if __name__ == "__main__": sim.local_sim(build_circuit())
qc.h(qreg) iterations = 1 for qubit in reversed(qreg[:4]): for i in range(iterations): qc.append(cgrit, qargs=[qubit] + qreg[4:]) iterations *= 2 qc.append(qft_dagger, qargs=qreg[:4]) qc.measure(qreg[:4], creg) qc.draw(output='mpl').savefig('circuit.svg') print('Circuit built.') return qc if __name__ == "__main__": result = sim.local_sim(build_circuit()) sorted_result = sorted(result.items(), key=lambda t: t[1], reverse=True) measured_int = min( [int(sorted_result[0][0][::-1], 2), int(sorted_result[1][0][::-1], 2)]) print('Register output = ', measured_int) theta = (measured_int / (2**t)) * math.pi * 2 print('theta =', theta) N = 2**n M = N * (math.sin(theta / 2)**2) print('No of solutions =', N - M)
qpe.barrier() qft_dagger(qpe, reg_size) # measure for i in range(reg_size): qpe.measure(i, reg_size - i - 1) # qpe.draw(output='mpl').savefig('circuit.svg') return qpe def qft_dagger(qc, n): # Remember to swap the qubits for qubit in range(n // 2): qc.swap(qubit, n - qubit - 1) for j in range(n, 0, -1): k = n - j for m in range(k): qc.cu1(-math.pi / float(2**(k - m)), n - m - 1, n - k - 1) qc.h(n - k - 1) if __name__ == "__main__": result = sim.local_sim(build_circuit(), shots=8192) maximum = max(result.values()) for keys in result: if result[keys] == maximum: print(int(keys, 2) / (2**reg_size))