def qc_solver(h_qop, num_spin_orbitals, num_particles, map_type, \ qubit_reduction, aux_qops=None): # backends = Aer.backends() backends = IBMQ.backends(simulator=False) print(backends) # backend = IBMQ.get_backend('ibmq_qasm_simulator') # backend = IBMQ.get_backend('ibmq_16_melbourne') # backend = Aer.get_backend('statevector_simulator') backend = Aer.get_backend('qasm_simulator') # setup COBYLA optimizer max_eval = 1000 cobyla = COBYLA(maxiter=max_eval) # setup hartreeFock state hf_state = HartreeFock(h_qop.num_qubits, num_spin_orbitals, num_particles, map_type, qubit_reduction) # setup UCCSD variational form var_form = UCCSD(h_qop.num_qubits, depth=1, num_orbitals=num_spin_orbitals, num_particles=num_particles, active_occupied=[0], active_unoccupied=[0], initial_state=hf_state, qubit_mapping=map_type, two_qubit_reduction=qubit_reduction, num_time_slices=1) # setup VQE vqe = VQE(h_qop, var_form, cobyla, operator_mode='matrix', \ aux_operators=aux_qops) quantum_instance = QuantumInstance(backend=backend, shots=1024, max_credits=10) ret = vqe.run(quantum_instance) print(ret['aux_ops']) print('The computed ground state energy is: {:.12f}'.format(\ ret['eigvals'][0]))
def test_end2end_h2(self, name, optimizer, backend, mode, shots): if optimizer == 'COBYLA': optimizer = COBYLA() optimizer.set_options(maxiter=1000) elif optimizer == 'SPSA': optimizer = SPSA(max_trials=2000) ryrz = RYRZ(self.algo_input.qubit_op.num_qubits, depth=3, entanglement='full') vqe = VQE(self.algo_input.qubit_op, ryrz, optimizer, mode, aux_operators=self.algo_input.aux_ops) quantum_instance = QuantumInstance(backend, shots=shots) results = vqe.run(quantum_instance) self.assertAlmostEqual(results['energy'], self.reference_energy, places=6)
def test_qaoa(self, w, p, solutions): self.log.debug('Testing {}-step QAOA with MaxCut on graph\n{}'.format( p, w)) np.random.seed(0) backend = get_aer_backend('statevector_simulator') optimizer = COBYLA() qubitOp, offset = maxcut.get_maxcut_qubitops(w) qaoa = QAOA(qubitOp, optimizer, p, operator_mode='matrix') quantum_instance = QuantumInstance(backend) result = qaoa.run(quantum_instance) x = maxcut.sample_most_likely(result['eigvecs'][0]) graph_solution = maxcut.get_graph_solution(x) self.log.debug('energy: {}'.format(result['energy'])) self.log.debug('time: {}'.format(result['eval_time'])) self.log.debug('maxcut objective: {}'.format(result['energy'] + offset)) self.log.debug('solution: {}'.format(graph_solution)) self.log.debug('solution objective: {}'.format( maxcut.maxcut_value(x, w))) self.assertIn(''.join([str(int(i)) for i in graph_solution]), solutions)
print(qubitOp) exact_eigensolver = ExactEigensolver(qubitOp, k=1) ret = exact_eigensolver.run() print('The computed energy is: {:.12f}'.format(ret['eigvals'][0].real)) print('The total ground state energy is: {:.12f}'.format( ret['eigvals'][0].real + energy_shift + nuclear_repulsion_energy)) from qiskit import IBMQ IBMQ.load_accounts() backend = Aer.get_backend('statevector_simulator') # setup COBYLA optimizer max_eval = 200 cobyla = COBYLA(maxiter=max_eval) # setup HartreeFock state HF_state = HartreeFock(qubitOp.num_qubits, num_spin_orbitals, num_particles, map_type, qubit_reduction) # setup UCCSD variational form var_form = UCCSD(qubitOp.num_qubits, depth=1, num_orbitals=num_spin_orbitals, num_particles=num_particles, active_occupied=[0], active_unoccupied=[0, 1], initial_state=HF_state, qubit_mapping=map_type, two_qubit_reduction=qubit_reduction,
def test_cobyla(self): optimizer = COBYLA(maxiter=100000, tol=1e-06) res = self._optimize(optimizer) self.assertLessEqual(res[2], 100000)
print("Exact solution: Energy=", solution["eigvals"][0] + shift) for s in exact_alignment: print(s) print() # QAOA if method == "QAOA": # initial_state = np.zeros(2**Hamilt.num_qubits) # state_bit_arr = np.zeros(Hamilt.num_qubits) # for s in range(len(sequences)): # for n in range(len(sequences[s])): # state_bit_arr[seq_inds[(s,n,n)]] = 1 # print(state_bit_arr) # initial_state[int(np.sum(state_bit_arr*np.power(2, np.arange(Hamilt.num_qubits,dtype=np.int32))))] = 1 # initial_state = CustomState(Hamilt.num_qubits, state_vector=initial_state) opt = COBYLA() p = 1 angles = [0, 0] sol_found = False data_file = "QAOA_run2" seq_array = np.array(sequences) cost_array = np.array(costs) coeff_arr = np.array([1, Hamiltonian_penalty]) insert_vals = np.array(inserts) times = [] energies = [] angle_arr = [] states = [] while not sol_found: print("Starting iteration p =", p)
# ====================== # setting up the circuit # ====================== # define the initial state init_state = Zero(num_qubits) # get a variational ansatz ansatz = RY(num_qubits, initial_state=init_state) # operator from hamiltonian qubit_op = Operator.load_from_dict(pauli_dict) # get an optimizer optimizer = COBYLA(maxiter=1000, disp=True) # form the algorithm vqe = VQE(qubit_op, ansatz, optimizer) # get a backend backend = get_aer_backend("statevector_simulator") # get a quantum instance qinstance = QuantumInstance(backend, shots=1024) # =================== # do the optimization # =================== result = vqe.run(qinstance)
def test_cobyla(self): optimizer = COBYLA(tol=1e-06) optimizer.set_options(**{'maxiter': 100000}) res = self._optimize(optimizer) self.assertLessEqual(res[2], 100000)