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 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')
bvCircuit.z(qr[i]) else: bvCircuit.iden(qr[i]) place_barriers(bvCircuit) for i in range(nQubits): bvCircuit.h(qr[i]) place_barriers(bvCircuit) bvCircuit.measure(qr, cr) bvCircuit.draw(output='mpl').savefig('circuit.svg') print('Circuit build. Circuit image saved locally.') return bvCircuit def accuracy(result): correct_answer = '{0:0{1}b}'.format(s, nQubits) print('Correct answer:', correct_answer) return result[correct_answer] / float(shots) if __name__ == "__main__": sim.quantumComputerExp(build_circuit(), accuracy_func=accuracy, shots=1024, mode='least_busy')
input_state(qft_qc, n) qft_qc.draw(output='mpl').savefig('input_state.svg') qft_qc.barrier() # actual QFT circuit for j in range(n): qft_qc.h(j) for k in range(j + 1, n): qft_qc.cu1(math.pi / float(2**(k - j)), k, j) qft_qc.barrier() for j in range(int(math.floor(n / 2.))): qft_qc.swap(j, n - j - 1) qft_qc.measure_all() qft_qc.draw(output='mpl').savefig('circuit.svg') print('Circuit built. Image saved locally.') return qft_qc def acc(result): correct = '1' + '0' * (n - 1) return result[correct] / 1024. if __name__ == "__main__": sim.quantumComputerExp(build_circuit(), accuracy_func=acc)
qc.cry(math.pi / 2, 0, 5) inv_qft = qft.qft(2).inverse() qc.append(inv_qft, qargs=qreg[1::-1]) # qc.measure(6, 0) for i in range(2): qc.measure(i, i) # qc.draw(output='mpl') # plt.show() return qc if __name__ == "__main__": # circuit = a_7_smaller() # sim.local_sim(circuit) circuit = a_11() result = sim.quantumComputerExp(circuit, backend='ibmq_rochester', note='a=11') circuit = a_7() result = sim.quantumComputerExp(circuit, backend='ibmq_rochester', note='a=7') circuit = a_2() result = sim.quantumComputerExp(circuit, backend='ibmq_rochester', note='a=2')
# oracle if oracle == '0': djcircuit.iden(n) elif oracle == '1': djcircuit.x(n) elif oracle == 'b': # ??? for i in range(n): if (b & (1 << i)): djcircuit.cx(i, n) print(f'applying: djcircuit.cx({i}, {n})') else: raise Exception('InvalidFunctionType') if barriers: djcircuit.barrier() djcircuit.h(range(n)) djcircuit.measure(range(n), range(n)) djcircuit.draw(output='mpl').savefig('circuit.svg') print('Circuit built. Image saved locally.') return djcircuit if __name__ == "__main__": sim.quantumComputerExp(build_circuit())