def test(): machine=pq.init_quantum_machine(pq.QMachineType.CPU) qlist=machine.qAlloc_many(4) clist = machine.cAlloc_many(4) prog=pq.QProg() prog.insert(pq.H(qlist[2])).insert(pq.meas_all(qlist,clist)) data = {'shots':1000} result=machine.run_with_configuration(prog,clist,data) pq.destroy_quantum_machine(machine) machine2=pq.init_quantum_machine(pq.QMachineType.CPU) qlist2=machine2.qAlloc_many(3) prog2=pq.QProg() prog2.insert(pq.H(qlist2[0])).insert(pq.CNOT(qlist2[0],qlist2[1])) result2=machine2.prob_run_dict(prog2,qlist2,-1) pq.destroy_quantum_machine(machine2) pq.init(pq.QMachineType.CPU) qlist3=pq.qAlloc_many(5) clist3=pq.cAlloc_many(5) prog3=pq.QProg() prog3.insert(pq.H(qlist3[0])).insert(pq.CNOT(qlist3[0],qlist3[1]))\ .insert(pq.CNOT(qlist3[1],qlist3[2])).insert(pq.CNOT(qlist3[2],qlist3[3]))\ .insert(pq.meas_all(qlist3,clist3)) result3=pq.run_with_configuration(prog3,clist3,100) pq.finalize() return result,result2,result3
def cal_circuit(thetas): pq.init(pq.QMachineType.CPU) qubitlist = pq.qAlloc_many(1) prog = pq.QProg() for theta, qubit in zip(thetas, qubitlist): prog.insert(pq.H(qubit)) prog.insert(pq.RY(qubit, theta)) result = pq.prob_run_dict(prog, qubitlist) states = [] probabilities = [] for key, val in result.items(): states.append(int(key, 2)) probabilities.append(val) states = np.array(states, 'float') probabilities = np.array(probabilities) expectation = np.sum(states * probabilities) pq.finalize() return np.array([expectation])