def test_trial_functions(self): """trial_circuit_ry function""" entangler_map = {0: [2], 1: [2], 3: [2], 4: [2]} m = 1 n = 6 theta = np.zeros(m * n) trial_circuit = trial_circuit_ry(n, m, theta, entangler_map) qasm_txt = trial_circuit.qasm() self.log.info(qasm_txt) self.assertEqual(len(qasm_txt), 456) self.log.info("With No measurement:\n") trial_circuit = trial_circuit_ry(n, m, theta, entangler_map, None, None) qasm_txt = trial_circuit.qasm() self.log.info(qasm_txt) self.assertEqual(len(qasm_txt), 324) self.log.info("With Y measurement:\n") meas_sting = ['Y' for x in range(n)] trial_circuit = trial_circuit_ry(n, m, theta, entangler_map, meas_sting) qasm_txt = trial_circuit.qasm() self.log.info(qasm_txt) self.assertEqual(len(qasm_txt), 564)
def obj_funct(Q_program, pauli_list, entangler_map, coupling_map, initial_layout, n, m, backend, shots, theta): """ Evaluate the objective function for a classical optimization problem. Q_program is an instance object of the class quantum program pauli_list defines the cost function as list of ising terms with weights theta are the control parameters n is the number of qubits m is the depth of the trial function backend is the type of backend to run it on shots is the number of shots to run. Taking shots = 1 only works in simulation and computes an exact average of the cost function on the quantum state """ std_cost=0 # to add later circuits = ['trial_circuit'] if shots==1: Q_program.add_circuit('trial_circuit', trial_circuit_ry(n, m, theta, entangler_map, None, False)) result = Q_program.execute(circuits, backend=backend, coupling_map=coupling_map, initial_layout=initial_layout, shots=shots) state = result.get_data('trial_circuit')['quantum_state'] cost=Energy_Estimate_Exact(state,pauli_list,True) else: Q_program.add_circuit('trial_circuit', trial_circuit_ry(n, m, theta, entangler_map, None, True)) result = Q_program.execute(circuits, backend=backend, coupling_map=coupling_map, initial_layout=initial_layout, shots=shots) data = result.get_counts('trial_circuit') cost = Energy_Estimate(data, pauli_list) return cost, std_cost
def test_eval_hamiltonian_pauli_list(self): """ Test of trial_circuit_ry and eval_hamiltonian with a pauli list """ ham_name = self._get_resource_path( "../performance/H2/H2Equilibrium.txt") pauli_list = Hamiltonian_from_file(ham_name) n = 2 m = 6 device = 'local_statevector_simulator' theta = np.array([ -0.607547697211, -0.126136414606, -0.684606358705, 0.928714748593, -1.84440103405, -0.467002424077, 2.29249034315, 0.488810054396, 0.710266990661, 1.05553444322, 0.0540731003458, 0.257953416342, 0.588281649703, 0.885244238613, -1.01700702421, -0.133693031283, -0.438185501332, 0.493443494428, -0.199009119848, -1.27498360732, 0.293494154438, 0.108950311827, 0.031726785859, 1.27263986303 ]) entangler_map = {0: [1]} energy = eval_hamiltonian( QuantumProgram(), pauli_list, trial_circuit_ry(n, m, theta, entangler_map, None, False), 1, device) np.testing.assert_almost_equal( -0.45295043823057191 + 3.3552033732997923e-18j, energy)
def test_trial_functions(self): entangler_map = {0: [2], 1: [2], 3: [2], 4: [2]} m = 1 n = 6 theta = np.zeros(m * n) trial_circuit = trial_circuit_ry(n, m, theta, entangler_map) self.log.info(trial_circuit.qasm()) self.log.info("With No measurement:\n") trial_circuit = trial_circuit_ry(n, m, theta, entangler_map, None, None) self.log.info(trial_circuit.qasm()) self.log.info("With Y measurement:\n") meas_sting = ['Y' for x in range(n)] trial_circuit = trial_circuit_ry(n, m, theta, entangler_map, meas_sting) self.log.info(trial_circuit.qasm())
def test_eval_hamiltonian_pauli_list(self): """ Test of trial_circuit_ry and eval_hamiltonian with a pauli list """ ham_name = self._get_resource_path("../performance/H2/H2Equilibrium.txt") pauli_list = Hamiltonian_from_file(ham_name) n = 2 m = 6 device = 'local_statevector_simulator' theta = np.array([-0.607547697211, -0.126136414606, - 0.684606358705, 0.928714748593, -1.84440103405, -0.467002424077, 2.29249034315, 0.488810054396, 0.710266990661, 1.05553444322, 0.0540731003458, 0.257953416342, 0.588281649703, 0.885244238613, -1.01700702421, -0.133693031283, -0.438185501332, 0.493443494428, -0.199009119848, -1.27498360732, 0.293494154438, 0.108950311827, 0.031726785859, 1.27263986303]) entangler_map = {0: [1]} energy = eval_hamiltonian(QuantumProgram(), pauli_list, trial_circuit_ry(n, m, theta, entangler_map, None, False), 1, device) np.testing.assert_almost_equal(-0.45295043823057191 + 3.3552033732997923e-18j, energy)
def cost_function(Q_program,H,n,m,entangler_map,shots,device,theta): return eval_hamiltonian(Q_program,H,trial_circuit_ry(n,m,theta,entangler_map,None,False),shots,device).real
initial_theta,SPSA_params,max_trials,save_step,1); plt.plot(np.arange(0, max_trials,save_step), cost_plus,label='C(theta_plus)') plt.plot(np.arange(0, max_trials,save_step),cost_minus,label='C(theta_minus)') plt.plot(np.arange(0, max_trials,save_step),np.ones(max_trials//save_step)*best_distance_quantum, label='Final Optimized Cost') plt.plot(np.arange(0, max_trials,save_step),np.ones(max_trials//save_step)*exact, label='Exact Cost') plt.legend() plt.xlabel('Number of trials') plt.ylabel('Cost') shots = 5000 circuits = ['final_circuit'] Q_program.add_circuit('final_circuit', trial_circuit_ry(n, m, best_theta, entangler_map, None, True)) result = Q_program.execute(circuits, backend=backend, shots=shots, coupling_map=coupling_map, initial_layout=initial_layout) data = result.get_counts('final_circuit') plot_histogram(data,5) # Getting the solution and cost from the largest component of the optimal quantum state max_value = max(data.values()) # maximum value max_keys = [k for k, v in data.items() if v == max_value] # getting all keys containing the `maximum` x_quantum=np.zeros(n) for bit in range(n): if max_keys[0][bit]=='1':
label='C(theta_minus)') plt.plot(np.arange(0, max_trials, save_step), np.ones(max_trials // save_step) * best_distance_quantum, label='Final Optimized Cost') plt.plot(np.arange(0, max_trials, save_step), np.ones(max_trials // save_step) * exact, label='Exact Cost') plt.legend() plt.xlabel('Number of trials') plt.ylabel('Cost') shots = 5000 circuits = ['final_circuit'] Q_program.add_circuit( 'final_circuit', trial_circuit_ry(n, m, best_theta, entangler_map, None, True)) result = Q_program.execute(circuits, backend=backend, shots=shots, coupling_map=coupling_map, initial_layout=initial_layout) data = result.get_counts('final_circuit') plot_histogram(data, 5) # Getting the solution and cost from the largest component of the optimal quantum state max_value = max(data.values()) # maximum value max_keys = [k for k, v in data.items() if v == max_value] # getting all keys containing the `maximum` x_quantum = np.zeros(n)