delta = 1 # Level spacing g = 1 # Interaction strength #g /= 4 # 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 # Prepare circuit list pairing = SecondQuantizedHamiltonian(n, l, h_pq, h_pqrs) grid_points = 500 x = np.linspace(0, 2 * np.pi, grid_points) params = [[i] for i in x] Es = np.zeros(grid_points) legal = np.zeros(grid_points) illegal = np.zeros(grid_points) i = 0 for theta in tqdm(params): vqe = VQE(pairing, ansatz='UCCD') vqe.options['shots'] = 1000 vqe.options['count_states'] = True vqe.options['backend'] = 'statevector_simulator' Es[i] = vqe.expval()
odho = ODQD(n, l, grid_length, num_grid_points) odho.setup_system(potential=HOPotential(omega), add_spin=True) one_body = odho.h one_body[np.absolute(one_body) < 1e-8] = 0 two_body = odho.u two_body[np.absolute(two_body) < 1e-8] = 0 # Coupled Cluster print('Reference energy:', odho.compute_reference_energy()) ccsd = CCSD(odho, verbose=False) ccsd.compute_ground_state() print('ECCSD =', ccsd.compute_energy()) # Prepare circuit list H2 = SecondQuantizedHamiltonian(n, l) H2.set_integrals(one_body, two_body, anti_symmetric=True) H2.get_circuit() circuit_list = H2.to_circuit_list(ptype='vqe') ansatz = UnitaryCoupledCluster(n, l, 'S', 1) og_params = ansatz.new_parameters(H2.h, H2.v) print(ansatz.num_S) print(ansatz.num_D) print(len(og_params)) grid_points = 500 x = np.linspace(0, 2 * np.pi, grid_points) params = [[i] for i in x] Es = np.zeros(grid_points)
R = 0.7 print('For bond length {}'.format(R)) # Get psi4 h_pq, h_pqrs, Enuc, energies = get_H2_info(R) # Save energies cisd = energies['cisd'] ccsd = energies['ccsd'] fci = energies['fci'] hf = energies['hf'] # Prepare circuit list H2 = SecondQuantizedHamiltonian(n, l, h_pq, h_pqrs, nuclear_repulsion=Enuc, add_spin=True) grid_points = 60 x = np.linspace(-0.1, 0.05, grid_points) #params = [[i] for i in x] params = [] for i in x: for j in x: params.append(np.array([i, j])) Es = np.zeros(grid_points * grid_points) shots = 1000 options = {'shots': shots} i = 0
HFs = np.zeros_like(Rs) Es = np.zeros_like(Rs) VARs = np.zeros_like(Rs) #vqe_coeffs = np.zeros((len(Rs),6)) #fci_coeffs = np.zeros((len(Rs),6)) i = 0 for R in tqdm(Rs): h, v, Enuc, E = get_H2(R) #fci_coeffs[i] = FCI(h,v) FCIs[i] = E['fci'] HFs[i] = E['hf'] h2 = SecondQuantizedHamiltonian(n, l, h, v, nuclear_repulsion=Enuc, add_spin=True) h2.group_paulis(qwc=True, gc=True) model = VQE(h2, Minimizer('Cobyla', tol=1 / (10 * shots), disp=False), 'RYRZ', options=options) theta = model.optimize() Es[i], VARs[i] = model.get_mean(theta, N=10000, M=10) #vqe_coeffs[i] = model.get_state_coeffs(theta) i += 1 np.save('RYRZ/bonds.npy', Rs)
h_pqrs = molecule.two_body_integrals Enuc = molecule.nuclear_repulsion fci_energy = molecule.fci_energy print(fci_energy) # OpenFermion Hamiltonian molecular_hamiltonian = molecule.get_molecular_hamiltonian() fermion_hamiltonian = get_fermion_operator(molecular_hamiltonian) qubit_hamiltonian = jordan_wigner(fermion_hamiltonian) qubit_hamiltonian.compress() #print('The Jordan-Wigner Hamiltonian in canonical basis follows:\n{}'.format(qubit_hamiltonian)) circuit_list = qubit_hamiltonian #h_pq, h_pqrs = molecular2sec_quant(h_pq,h_pqrs) Model = SecondQuantizedHamiltonian(2, 4, h_pq, h_pqrs, nuclear_repulsion=Enuc, add_spin=True, anti_symmetric=False) #Model.get_circuit() circuit_list2 = Model.circuit_list('vqe') print('OepnFerm') for i in circuit_list: print(i) print('New') for i in circuit_list2: print(i)
# 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 UCCD = UnitaryCoupledCluster(n, l, 'D', 1) theta = UCCD.new_parameters() theta[0] = 5.829889373194686 # Hardcode good parameter Pairing = SecondQuantizedHamiltonian(n, l) Pairing.set_integrals(h_pq, h_pqrs) Pairing.get_circuit() circuit_list = Pairing.to_circuit_list(ptype='vqe') all_shots = np.linspace(1000, 100000, 100) Es = np.zeros(100) legal = np.zeros(100) illegal = np.zeros(100) i = 0 for shot in tqdm(all_shots): model = VQE( n_qubits=l, ansatz=UCCD, circuit_list=circuit_list,
energies['ccsd'] = h2_molecule.ccsd_energy #print('electron:',h2_molecule.n_electrons) return one_body, two_body, Enuc, energies h, v, Enuc, E = get_H2_info(0.7) fci = E['fci'] hf = E['hf'] print('HF energy:', hf) print('FCI energy:', fci) mol = SecondQuantizedHamiltonian(2, 4, h, v, nuclear_repulsion=Enuc, add_spin=True) options = { 'shots': 1000, 'optimization_level': 1, #'device':'ibmq_london', #'layout':[1,0,2,3], #'noise_model':True, #'basis_gates':True, #'coupling_map':True, #'seed':1, 'print': True } #options = {'shots':10000,'print':True}
from quantum_circuit import QuantumCircuit, SecondQuantizedHamiltonian, PairingHamiltonian, CircuitList, PauliString, X l = 4 # number of spin orbitals / number of qubits n = 2 # Number of occupied spin orbitals Rs = [0.4, 0.74, 1.0, 1.6, 2.2, 2.8] fci = np.zeros_like(Rs) hf = np.zeros_like(Rs) vqe_factors = np.zeros_like(Rs) for i, R in enumerate(Rs): h, v, Enuc, E = get_h2_matrix(R) fci[i] = E['fci'] hf[i] = E['hf'] h2 = SecondQuantizedHamiltonian(n, l, h, v, nuclear_repulsion=Enuc, add_spin=True) for p in h2.circuit_list('vqe'): if len(p) == 0: print('factor:', p.factor) vqe_factors[i] = p.factor np.save('fci.npy', fci) np.save('hf.npy', hf) np.save('vqe_factors.npy', vqe_factors)
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 model = VQE(h2, Minimizer('Cobyla', tol=1 / (100 * shots), disp=False), 'UCCSD', options=option_noisy)
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 def pairing_ansatz(qc, qb, n_simulation): for qbit in range(0, n_simulation, 2): qc.h(qb[qbit]) qc.cx(qb[qbit], qb[qbit + 1]) return qc n_work = 6 Emax = 2 t = 0.49 * np.pi rho = 10 print('Setting up Hamiltonian') pairing_model = SecondQuantizedHamiltonian(n, l, h_pq, h_pqrs, exp=True) print('Setting up QPE class') qpe = QPE(pairing_model, pairing_ansatz, n_work, Emax, t=t, rho=rho) print('Running...') qpe.estimate() plt.plot(qpe.x, qpe.y) plt.show()
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, Minimizer('Cobyla'), 'RYPAIRING',