def test_vqe_2_iqpe(self): """ vqe to iqpe test """ backend = BasicAer.get_backend('qasm_simulator') num_qbits = self.qubit_op.num_qubits wavefunction = TwoLocal(num_qbits, ['ry', 'rz'], 'cz', reps=3, insert_barriers=True) optimizer = SPSA(maxiter=10) algo = VQE(self.qubit_op, wavefunction, optimizer) quantum_instance = QuantumInstance(backend, seed_simulator=self.seed, seed_transpiler=self.seed) result = algo.run(quantum_instance) self.log.debug('VQE result: %s.', result) ref_eigenval = -1.85727503 # Known reference value num_time_slices = 1 num_iterations = 6 param_dict = result.optimal_parameters state_in = VarFormBased(wavefunction, param_dict) iqpe = IQPE(self.qubit_op, state_in, num_time_slices, num_iterations, expansion_mode='suzuki', expansion_order=2, shallow_circuit_concat=True) quantum_instance = QuantumInstance(backend, shots=100, seed_transpiler=self.seed, seed_simulator=self.seed) result = iqpe.run(quantum_instance) self.log.debug('top result str label: %s', result.top_measurement_label) self.log.debug('top result in decimal: %s', result.top_measurement_decimal) self.log.debug('stretch: %s', result.stretch) self.log.debug('translation: %s', result.translation) self.log.debug('final eigenvalue from QPE: %s', result.eigenvalue) self.log.debug('reference eigenvalue: %s', ref_eigenval) self.log.debug('ref eigenvalue (transformed): %s', (ref_eigenval + result.translation) * result.stretch) self.log.debug( 'reference binary str label: %s', decimal_to_binary( (ref_eigenval.real + result.translation) * result.stretch, max_num_digits=num_iterations + 3, fractional_part_only=True)) self.assertAlmostEqual(result.eigenvalue.real, ref_eigenval.real, delta=1e-2)
def test_vqe_2_iqpe(self): """ vqe to iqpe test """ backend = BasicAer.get_backend('qasm_simulator') num_qbits = self.algo_input.qubit_op.num_qubits var_form = RYRZ(num_qbits, 3) optimizer = SPSA(max_trials=10) # optimizer.set_options(**{'max_trials': 500}) algo = VQE(self.algo_input.qubit_op, var_form, optimizer) quantum_instance = QuantumInstance(backend, seed_simulator=self.seed, seed_transpiler=self.seed) result = algo.run(quantum_instance) self.log.debug('VQE result: %s.', result) ref_eigenval = -1.85727503 num_time_slices = 1 num_iterations = 6 state_in = VarFormBased(var_form, result['opt_params']) iqpe = IQPE(self.algo_input.qubit_op, state_in, num_time_slices, num_iterations, expansion_mode='suzuki', expansion_order=2, shallow_circuit_concat=True) quantum_instance = QuantumInstance(backend, shots=100, seed_transpiler=self.seed, seed_simulator=self.seed) result = iqpe.run(quantum_instance) self.log.debug('top result str label: %s', result['top_measurement_label']) self.log.debug('top result in decimal: %s', result['top_measurement_decimal']) self.log.debug('stretch: %s', result['stretch']) self.log.debug('translation: %s', result['translation']) self.log.debug('final eigenvalue from QPE: %s', result['energy']) self.log.debug('reference eigenvalue: %s', ref_eigenval) self.log.debug('ref eigenvalue (transformed): %s', (ref_eigenval + result['translation']) * result['stretch']) self.log.debug( 'reference binary str label: %s', decimal_to_binary( (ref_eigenval + result['translation']) * result['stretch'], max_num_digits=num_iterations + 3, fractional_part_only=True)) np.testing.assert_approx_equal(result['energy'], ref_eigenval, significant=2)
def test_vqe_2_iqpe(self): backend = get_aer_backend('qasm_simulator') num_qbits = self.algo_input.qubit_op.num_qubits var_form = RYRZ(num_qbits, 3) optimizer = SPSA(max_trials=10) # optimizer.set_options(**{'max_trials': 500}) algo = VQE(self.algo_input.qubit_op, var_form, optimizer, 'paulis') quantum_instance = QuantumInstance(backend) result = algo.run(quantum_instance) self.log.debug('VQE result: {}.'.format(result)) self.ref_eigenval = -1.85727503 num_time_slices = 50 num_iterations = 11 state_in = VarFormBased(var_form, result['opt_params']) iqpe = IQPE(self.algo_input.qubit_op, state_in, num_time_slices, num_iterations, expansion_mode='suzuki', expansion_order=2, shallow_circuit_concat=True) quantum_instance = QuantumInstance(backend, shots=100, pass_manager=PassManager(), seed_mapper=self.random_seed) result = iqpe.run(quantum_instance) self.log.debug('top result str label: {}'.format( result['top_measurement_label'])) self.log.debug('top result in decimal: {}'.format( result['top_measurement_decimal'])) self.log.debug('stretch: {}'.format( result['stretch'])) self.log.debug('translation: {}'.format( result['translation'])) self.log.debug('final eigenvalue from QPE: {}'.format( result['energy'])) self.log.debug('reference eigenvalue: {}'.format( self.ref_eigenval)) self.log.debug('ref eigenvalue (transformed): {}'.format( (self.ref_eigenval + result['translation']) * result['stretch'])) self.log.debug('reference binary str label: {}'.format( decimal_to_binary((self.ref_eigenval + result['translation']) * result['stretch'], max_num_digits=num_iterations + 3, fractional_part_only=True))) np.testing.assert_approx_equal(self.ref_eigenval, result['energy'], significant=2)
def test_vqe_2_iqpe(self, wavefunction_type): """ vqe to iqpe test """ backend = BasicAer.get_backend('qasm_simulator') num_qbits = self.qubit_op.num_qubits if wavefunction_type == 'wrapped': warnings.filterwarnings('ignore', category=DeprecationWarning) wavefunction = RYRZ(num_qbits, 3) else: wavefunction = TwoLocal(num_qbits, ['ry', 'rz'], 'cz', reps=3, insert_barriers=True) theta = ParameterVector('theta', wavefunction.num_parameters) wavefunction.assign_parameters(theta, inplace=True) if wavefunction_type == 'circuit': wavefunction = QuantumCircuit(num_qbits).compose(wavefunction) optimizer = SPSA(max_trials=10) algo = VQE(self.qubit_op, wavefunction, optimizer) if wavefunction_type == 'wrapped': warnings.filterwarnings('always', category=DeprecationWarning) quantum_instance = QuantumInstance(backend, seed_simulator=self.seed, seed_transpiler=self.seed) result = algo.run(quantum_instance) self.log.debug('VQE result: %s.', result) ref_eigenval = -1.85727503 # Known reference value num_time_slices = 1 num_iterations = 6 if wavefunction_type == 'wrapped': param_dict = result.optimal_point else: param_dict = result.optimal_parameters state_in = VarFormBased(wavefunction, param_dict) iqpe = IQPE(self.qubit_op, state_in, num_time_slices, num_iterations, expansion_mode='suzuki', expansion_order=2, shallow_circuit_concat=True) quantum_instance = QuantumInstance(backend, shots=100, seed_transpiler=self.seed, seed_simulator=self.seed) result = iqpe.run(quantum_instance) self.log.debug('top result str label: %s', result.top_measurement_label) self.log.debug('top result in decimal: %s', result.top_measurement_decimal) self.log.debug('stretch: %s', result.stretch) self.log.debug('translation: %s', result.translation) self.log.debug('final eigenvalue from QPE: %s', result.eigenvalue) self.log.debug('reference eigenvalue: %s', ref_eigenval) self.log.debug('ref eigenvalue (transformed): %s', (ref_eigenval + result.translation) * result.stretch) self.log.debug( 'reference binary str label: %s', decimal_to_binary( (ref_eigenval.real + result.translation) * result.stretch, max_num_digits=num_iterations + 3, fractional_part_only=True)) np.testing.assert_approx_equal(result.eigenvalue.real, ref_eigenval, significant=2)