def test_ee_direct(self): algo = ExactEigensolver(self.algo_input.qubit_op, k=1, aux_operators=[]) result = algo.run() self.assertAlmostEqual(result['energy'], -1.85727503) np.testing.assert_array_almost_equal(result['energies'], [-1.85727503]) np.testing.assert_array_almost_equal(result['eigvals'], [-1.85727503 + 0j])
def test_ee_direct_k4(self): algo = ExactEigensolver(self.algo_input.qubit_op, k=4, aux_operators=[]) result = algo.run() self.assertAlmostEqual(result['energy'], -1.85727503) self.assertEqual(len(result['eigvals']), 4) self.assertEqual(len(result['eigvecs']), 4) np.testing.assert_array_almost_equal( result['energies'], [-1.85727503, -1.24458455, -0.88272215, -0.22491125])
def test_iqpe(self, distance): self.algorithm = 'IQPE' self.log.debug('Testing End-to-End with IQPE on H2 with ' 'inter-atomic distance {}.'.format(distance)) try: driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 {}'.format(distance), unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') except QiskitChemistryError: self.skipTest('PYSCF driver does not appear to be installed') self.molecule = driver.run() qubit_mapping = 'parity' fer_op = FermionicOperator(h1=self.molecule.one_body_integrals, h2=self.molecule.two_body_integrals) self.qubit_op = fer_op.mapping(map_type=qubit_mapping, threshold=1e-10).two_qubit_reduced_operator(2) exact_eigensolver = ExactEigensolver(self.qubit_op, k=1) results = exact_eigensolver.run() self.reference_energy = results['energy'] self.log.debug('The exact ground state energy is: {}'.format(results['energy'])) num_particles = self.molecule.num_alpha + self.molecule.num_beta two_qubit_reduction = True num_orbitals = self.qubit_op.num_qubits + (2 if two_qubit_reduction else 0) num_time_slices = 50 num_iterations = 12 state_in = HartreeFock(self.qubit_op.num_qubits, num_orbitals, num_particles, qubit_mapping, two_qubit_reduction) iqpe = IQPE(self.qubit_op, state_in, num_time_slices, num_iterations, paulis_grouping='random', expansion_mode='suzuki', expansion_order=2, shallow_circuit_concat=True) backend = qiskit.Aer.get_backend('qasm_simulator') quantum_instance = QuantumInstance(backend, shots=100, pass_manager=PassManager()) 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 energy from QPE: {}'.format(result['energy'])) self.log.debug('reference energy: {}'.format(self.reference_energy)) self.log.debug('ref energy (transformed): {}'.format( (self.reference_energy + result['translation']) * result['stretch']) ) self.log.debug('ref binary str label: {}'.format(decimal_to_binary( (self.reference_energy + result['translation']) * result['stretch'], max_num_digits=num_iterations + 3, fractional_part_only=True ))) np.testing.assert_approx_equal(result['energy'], self.reference_energy, significant=2)
def test_qpe(self, distance): self.algorithm = 'QPE' self.log.debug( 'Testing End-to-End with QPE on H2 with inter-atomic distance {}.'. format(distance)) cfg_mgr = ConfigurationManager() pyscf_cfg = OrderedDict([('atom', 'H .0 .0 .0; H .0 .0 {}'.format(distance)), ('unit', 'Angstrom'), ('charge', 0), ('spin', 0), ('basis', 'sto3g')]) section = {} section['properties'] = pyscf_cfg try: driver = cfg_mgr.get_driver_instance('PYSCF') except QiskitChemistryError: self.skipTest('PYSCF driver does not appear to be installed') self.molecule = driver.run(section) qubit_mapping = 'parity' fer_op = FermionicOperator(h1=self.molecule.one_body_integrals, h2=self.molecule.two_body_integrals) self.qubit_op = fer_op.mapping( map_type=qubit_mapping, threshold=1e-10).two_qubit_reduced_operator(2) exact_eigensolver = ExactEigensolver(self.qubit_op, k=1) results = exact_eigensolver.run() self.reference_energy = results['energy'] self.log.debug('The exact ground state energy is: {}'.format( results['energy'])) num_particles = self.molecule.num_alpha + self.molecule.num_beta two_qubit_reduction = True num_orbitals = self.qubit_op.num_qubits + \ (2 if two_qubit_reduction else 0) num_time_slices = 50 n_ancillae = 9 state_in = HartreeFock(self.qubit_op.num_qubits, num_orbitals, num_particles, qubit_mapping, two_qubit_reduction) iqft = Standard(n_ancillae) qpe = QPE(self.qubit_op, state_in, iqft, num_time_slices, n_ancillae, paulis_grouping='random', expansion_mode='suzuki', expansion_order=2, shallow_circuit_concat=True) backend = get_aer_backend('qasm_simulator') quantum_instance = QuantumInstance(backend, shots=100, pass_manager=PassManager()) result = qpe.run(quantum_instance) self.log.debug('measurement results: {}'.format( result['measurements'])) 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 energy from QPE: {}'.format(result['energy'])) self.log.debug('reference energy: {}'.format( self.reference_energy)) self.log.debug('ref energy (transformed): {}'.format( (self.reference_energy + result['translation']) * result['stretch'])) self.log.debug('ref binary str label: {}'.format( decimal_to_binary((self.reference_energy + result['translation']) * result['stretch'], max_num_digits=n_ancillae + 3, fractional_part_only=True))) np.testing.assert_approx_equal(result['energy'], self.reference_energy, significant=2)
def test_qpe(self, qubitOp): self.algorithm = 'QPE' self.log.debug('Testing QPE') self.qubitOp = qubitOp exact_eigensolver = ExactEigensolver(self.qubitOp, k=1) results = exact_eigensolver.run() w = results['eigvals'] v = results['eigvecs'] self.qubitOp.to_matrix() np.testing.assert_almost_equal(self.qubitOp.matrix @ v[0], w[0] * v[0]) np.testing.assert_almost_equal( expm(-1.j * sparse.csc_matrix(self.qubitOp.matrix)) @ v[0], np.exp(-1.j * w[0]) * v[0]) self.ref_eigenval = w[0] self.ref_eigenvec = v[0] self.log.debug('The exact eigenvalue is: {}'.format( self.ref_eigenval)) self.log.debug('The corresponding eigenvector: {}'.format( self.ref_eigenvec)) num_time_slices = 50 n_ancillae = 9 state_in = Custom(self.qubitOp.num_qubits, state_vector=self.ref_eigenvec) iqft = Standard(n_ancillae) qpe = QPE(self.qubitOp, state_in, iqft, num_time_slices, n_ancillae, paulis_grouping='random', expansion_mode='suzuki', expansion_order=2) backend = Aer.get_backend('qasm_simulator') quantum_instance = QuantumInstance(backend, shots=100, pass_manager=PassManager()) # run qpe result = qpe.run(quantum_instance) # self.log.debug('transformed operator paulis:\n{}'.format(self.qubitOp.print_operators('paulis'))) # report result self.log.debug('measurement results: {}'.format( result['measurements'])) 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.real + result['translation']) * result['stretch'], max_num_digits=n_ancillae + 3, fractional_part_only=True))) np.testing.assert_approx_equal(self.ref_eigenval.real, result['energy'], significant=2)