import qiskit as qk qk.IBMQ.load_account() provider = qk.IBMQ.get_provider('ibm-q') qcomp = provider.get_backend('ibmq_essex') #qcomp = provider.get_backend('ibmq_london') l = 4 # number of spin orbitals / number of qubits n = 2 # Number of occupied spin orbitals delta = 1 # Level spacing g = 1 # Interaction strength h,v = get_pairing_matrix(n,l,delta,g) Efci = FCI(n,l,h,v) print('FCI energy :',Efci) import time t1 = time.time() pairing = PairingHamiltonian(n,l,h,v) t2 = time.time() print('Time:',t2-t1) pairing.group_paulis(qwc=True,gc=True) #theta = [5.829889373194686] # Hardcode good parameter #theta = [-0.4]
l = 4 # number of spin orbitals / number of qubits n = 2 # Number of occupied spin orbitals delta = 1 # Level spacing g = 1 # Interaction strength # Matrix elements h_pq = np.identity(l) for p in range(l): h_pq[p, p] *= delta * (p - (p % 2)) / 2 h_pqrs = np.zeros((l, l, l, l)) for p in range(0, l - 1, 2): for r in range(0, l - 1, 2): h_pqrs[p, p + 1, r, r + 1] = -0.5 * g Efci = FCI(n, l, h_pq, h_pqrs) print('FCI energy :', Efci) import time t1 = time.time() pairing = PairingHamiltonian(n, l, h_pq, h_pqrs) t2 = time.time() print('Time:', t2 - t1) pairing.group_paulis(qwc=False, gc=True) theta = [5.829889373194686] # Hardcode good parameter #options = {'count_states':False,'shots':1000,'print':True} #options = {'shots':10000,'print':True} options = {
l = 4 # number of spin orbitals / number of qubits n = 2 # Number of occupied spin orbitals R = 0.4 h, v, Enuc, E = get_h2_matrix(R) h2 = SecondQuantizedHamiltonian(n, l, h, v, nuclear_repulsion=Enuc, add_spin=True) print('Real FCI:', E['fci']) print('HF:', E['hf']) Efci = FCI(n, l, h2.h, h2.v) print('FCI energy :', Efci + Enuc, Efci - Enuc) h2.group_paulis(qwc=True, gc=True, ibmq=True) #options = {'count_states':False,'shots':1000,'print':True} options_i = { 'shots': 2000, 'optimization_level': 1, #'noise_model':True, #'meas_fit':True, # 'device':'ibmq_london', 'print': True } options_q = { 'shots': 2000, #'seed':1,
sys.path.append('../../optimizers') sys.path.append('../../../../QuantumCircuitOptimizer') from quantum_circuit import SecondQuantizedHamiltonian from optimizer import Minimizer from vqe import VQE l = 8 # number of spin orbitals / number of qubits n = 4 # Number of occupied spin orbitals omega = 1 h, v, E = get_qdot_matrix(n, l, omega) eig, sta = FCI(n, l, h, v, ret_all=True) ground = sta[:, 0] ground[np.absolute(ground) < 1e-8] = 0 #print(ground) from itertools import combinations states = [] states_int = [] for state in combinations(range(l), n): states_int.append([orb for orb in state]) states_str = [['0' for j in range(l)] for i in states_int] for state, inds in zip(states_str, states_int): for ind in inds: state[ind] = '1' states.append(''.join(state))
omegas = np.arange(0.1, 3.0, 0.1) FCIs = np.zeros_like(omegas) HFs = np.zeros_like(omegas) fci_coeffs = np.zeros((len(omegas), 6)) Es_ideal = np.zeros_like(omegas) VARs_ideal = np.zeros_like(omegas) vqe_coeffs_ideal = np.zeros((len(omegas), 6)) Es_noisy = np.zeros_like(omegas) VARs_noisy = np.zeros_like(omegas) vqe_coeffs_noisy = np.zeros((len(omegas), 6)) i = 0 for omega in tqdm(omegas): h, v, E = get_qdot_matrix(n, l, omega) ev, vv = FCI(n, l, h, v, ret_all=True) FCIs[i] = ev[0] HFs[i] = E[0].real fci_coeffs[i] = vv[:, 0].real h2 = SecondQuantizedHamiltonian(n, l, h, v, anti_symmetric=True) h2.group_paulis(qwc=True, gc=True) # Ideal model = VQE(h2, Minimizer('Cobyla', tol=1 / (100 * shots), disp=False), 'UCCSD', options=option_ideal) theta = model.optimize() Es_ideal[i], VARs_ideal[i] = model.get_mean(theta, N=10000, M=10) vqe_coeffs_ideal[i] = model.get_state_coeff(theta) # Noisy
'noise_model': True, 'basis_gates': True, 'coupling_map': True, #'seed':1, 'meas_fit': False, 'print': False } option3 = { 'shots': shots, 'optimization_level': 1, 'device': 'ibmq_{}'.format(device), 'layout': [1, 0, 2, 3], 'noise_model': True, 'basis_gates': True, 'coupling_map': True, #'seed':1, 'print': False } optionz = [option1, option2, option3] for o, options in enumerate(optionz): model = VQE(pairing, Minimizer('Cobyla', tol=1e-04, disp=False), ansatz, options=options) theta = model.optimize() data[i, o], _ = model.get_mean(theta) fci[i] = FCI(n, l, h_pq, h_pqrs) i += 1 np.save('{}_{}_{}shots.npy'.format(device, ansatz, shots), data) np.save('fci.npy', fci)
'device':'ibmq_{}'.format(device), 'layout':[1,0,2,3], 'noise_model':True, 'basis_gates':True, 'coupling_map':True, #'seed':1, 'meas_fit':False, 'print':False} option3 = {'shots':shots, 'optimization_level':1, 'device':'ibmq_{}'.format(device), 'layout':[1,0,2,3], 'noise_model':True, 'basis_gates':True, 'coupling_map':True, #'seed':1, 'print':False} optionz = [option1,option2,option3] model = VQE(pairing,Minimizer('Cobyla',tol=1e-05,disp=False),ansatz,options=optionz[0]) theta = model.optimize() mean,var = model.get_mean(theta) coeff = model.get_state_coeff(theta) fciE,fciV = FCI(n,l,h_pq,h_pqrs,ret_all=True) print(fciE[0]) print(mean,var) fciCoef = np.power(fciV[:,0],2) print(fciCoef) print(coeff) print(np.linalg.norm(fciCoef-coeff))
sys.path.append('../../optimizers') sys.path.append('../../../../QuantumCircuitOptimizer') from quantum_circuit import SecondQuantizedHamiltonian from optimizer import Minimizer from vqe import VQE l = 4 # number of spin orbitals / number of qubits n = 2 # Number of occupied spin orbitals omega = 1 h, v, E = get_qdot_matrix(n, l, omega) print(E, FCI(n, l, h, v)) qdot = SecondQuantizedHamiltonian(n, l, h, v, anti_symmetric=True) qdot.group_paulis() options = { 'shots': 10000, 'print': True, 'device': 'ibmq_london', 'noise_model': True, 'meas_fit': True, 'coupling_map': True, 'layout': [1, 0, 2, 3], 'basis_gates': True } model = VQE(qdot,
VARs_ideal = np.zeros_like(Rs) vqe_coeffs_ideal = np.zeros((len(Rs), 6)) Es_noisy = np.zeros_like(Rs) VARs_noisy = np.zeros_like(Rs) vqe_coeffs_noisy = np.zeros((len(Rs), 6)) i = 0 for R in tqdm(Rs): h, v, Enuc, E = get_h2_matrix(R) h2 = SecondQuantizedHamiltonian(n, l, h, v, nuclear_repulsion=Enuc, add_spin=True) ev, vv = FCI(n, l, h2.h, h2.v, ret_all=True) fci_coeffs[i] = vv[:, 0].real FCIs[i] = E['fci'] HFs[i] = E['hf'] CCs[i] = E['ccsd'] h2.group_paulis(qwc=True, gc=True) # Ideal model = VQE(h2, Minimizer('Cobyla', tol=1 / (100 * shots), disp=False), 'RYPAIRING', options=option_ideal) theta = model.optimize() Es_ideal[i], VARs_ideal[i] = model.get_mean(theta, N=10000, M=10) vqe_coeffs_ideal[i] = model.get_state_coeff(theta) # Noisy