def test_bksf_mapping(self): """Test bksf mapping The spectrum of bksf mapping should be half of jordan wigner mapping. """ cfg_mgr = ConfigurationManager() pyscf_cfg = OrderedDict([('atom', 'H .0 .0 0.7414; H .0 .0 .0'), ('unit', 'Angstrom'), ('charge', 0), ('spin', 0), ('basis', 'sto3g')]) section = {} section['properties'] = pyscf_cfg driver = cfg_mgr.get_driver_instance('PYSCF') molecule = driver.run(section) fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) jw_op = fer_op.mapping('jordan_wigner') bksf_op = fer_op.mapping('bksf') jw_op.to_matrix() bksf_op.to_matrix() jw_eigs = np.linalg.eigvals(jw_op.matrix.toarray()) bksf_eigs = np.linalg.eigvals(bksf_op.matrix.toarray()) jw_eigs = np.sort(np.around(jw_eigs.real, 6)) bksf_eigs = np.sort(np.around(bksf_eigs.real, 6)) overlapped_spectrum = np.sum(np.isin(jw_eigs, bksf_eigs)) self.assertEqual(overlapped_spectrum, jw_eigs.size // 2)
def test_bksf_mapping(self): """Test bksf mapping. The spectrum of bksf mapping should be half of jordan wigner mapping. """ driver = PySCFDriver(atom='H .0 .0 0.7414; H .0 .0 .0', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) jw_op = fer_op.mapping('jordan_wigner') bksf_op = fer_op.mapping('bksf') jw_op.to_matrix() bksf_op.to_matrix() jw_eigs = np.linalg.eigvals(jw_op.matrix.toarray()) bksf_eigs = np.linalg.eigvals(bksf_op.matrix.toarray()) jw_eigs = np.sort(np.around(jw_eigs.real, 6)) bksf_eigs = np.sort(np.around(bksf_eigs.real, 6)) overlapped_spectrum = np.sum(np.isin(jw_eigs, bksf_eigs)) self.assertEqual(overlapped_spectrum, jw_eigs.size // 2)
def get_qubit_operator(h1, h2=None, qubit_reduction=False, map_type="parity", threshold=1.e-10): # half-filling num_particles = h1.shape[0]/2 fop = FermionicOperator(h1=h1, h2=h2) qop = fop.mapping(map_type=map_type, threshold=threshold) if qubit_reduction: qop = qop.two_qubit_reduced_operator(num_particles) qop.chop(threshold) return qop
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_freezing_core(self): cfg_mgr = ConfigurationManager() pyscf_cfg = OrderedDict([('atom', 'H .0 .0 -1.160518; Li .0 .0 0.386839'), ('unit', 'Angstrom'), ('charge', 0), ('spin', 0), ('basis', 'sto3g')]) section = {} section['properties'] = pyscf_cfg driver = cfg_mgr.get_driver_instance('PYSCF') molecule = driver.run(section) fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) fer_op, energy_shift = fer_op.fermion_mode_freezing([0, 6]) gt = -7.8187092970493755 diff = abs(energy_shift - gt) self.assertLess(diff, 1e-6) cfg_mgr = ConfigurationManager() pyscf_cfg = OrderedDict([('atom', 'H .0 .0 .0; Na .0 .0 1.888'), ('unit', 'Angstrom'), ('charge', 0), ('spin', 0), ('basis', 'sto3g')]) section = {} section['properties'] = pyscf_cfg driver = cfg_mgr.get_driver_instance('PYSCF') molecule = driver.run(section) fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) fer_op, energy_shift = fer_op.fermion_mode_freezing([0, 1, 2, 3, 4, 10, 11, 12, 13, 14]) gt = -162.58414559586748 diff = abs(energy_shift - gt) self.assertLess(diff, 1e-6)
def test_freezing_core(self): driver = PySCFDriver(atom='H .0 .0 -1.160518; Li .0 .0 0.386839', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) fer_op, energy_shift = fer_op.fermion_mode_freezing([0, 6]) gt = -7.8187092970493755 diff = abs(energy_shift - gt) self.assertLess(diff, 1e-6) driver = PySCFDriver(atom='H .0 .0 .0; Na .0 .0 1.888', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') molecule = driver.run() fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) fer_op, energy_shift = fer_op.fermion_mode_freezing( [0, 1, 2, 3, 4, 10, 11, 12, 13, 14]) gt = -162.58414559586748 diff = abs(energy_shift - gt) self.assertLess(diff, 1e-6)
def setUp(self): try: driver = PySCFDriver(atom='Li .0 .0 .0; H .0 .0 1.595', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g') except QiskitChemistryError: self.skipTest('PYSCF driver does not appear to be installed') molecule = driver.run() self.fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals)
def setUp(self): cfg_mgr = ConfigurationManager() pyscf_cfg = OrderedDict([('atom', 'Li .0 .0 .0; H .0 .0 1.595'), ('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') molecule = driver.run(section) self.fer_op = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals)
# convert all negative idx to positive remove_list = [x % molecule.num_orbitals for x in remove_list] freeze_list = [x % molecule.num_orbitals for x in freeze_list] # update the idx in remove_list of the idx after frozen, since the idx of orbitals are changed after freezing remove_list = [x - len(freeze_list) for x in remove_list] remove_list += [ x + molecule.num_orbitals - len(freeze_list) for x in remove_list ] freeze_list += [x + molecule.num_orbitals for x in freeze_list] # prepare fermionic hamiltonian with orbital freezing and eliminating, and then map to qubit hamiltonian # and if PARITY mapping is selected, reduction qubits energy_shift = 0.0 qubit_reduction = True if map_type == 'parity' else False ferOp = FermionicOperator(h1=h1, h2=h2) # if len(freeze_list) > 0: # ferOp, energy_shift = ferOp.fermion_mode_freezing(freeze_list) # num_spin_orbitals -= len(freeze_list) # num_particles -= len(freeze_list) # if len(remove_list) > 0: # ferOp = ferOp.fermion_mode_elimination(remove_list) # num_spin_orbitals -= len(remove_list) qubitOp = ferOp.mapping(map_type=map_type, threshold=0.00000001) qubitOp = qubitOp.two_qubit_reduced_operator( num_particles) if qubit_reduction else qubitOp qubitOp.chop(10**-10) print(qubitOp.print_operators()) print(qubitOp)
] qubit_mappings = { 'JW': 'jordan_wigner' # 'P':'parity' # 'BK':'bravyi_kitaev' } for basis in bases: for qm_name, qubit_mapping in qubit_mappings.items(): for var_form_name, var_form_class in var_forms.items(): for mol_name, molecule_str in molecules.items(): driver = PySCFDriver(molecule_str, basis=basis) mol = driver.run() n_qubits = mol.one_body_integrals.shape[0] n_electrons = mol.num_alpha + mol.num_beta - mol.molecular_charge ferOp = FermionicOperator(h1=mol.one_body_integrals, h2=mol.two_body_integrals) qubitOp = ferOp.mapping(map_type=qubit_mapping, threshold=1e-8) qubitOp.chop(1e-10) initial_hf = HartreeFock(num_qubits=n_qubits, num_orbitals=n_qubits, qubit_mapping=qubit_mapping, two_qubit_reduction=False, num_particles=n_electrons) var_form = UCCSD(num_qubits=n_qubits, num_orbitals=n_qubits, num_particles=n_electrons, depth=1, initial_state=initial_hf, qubit_mapping=qubit_mapping) number_amplitudes = len(var_form._single_excitations) + len( var_form._double_excitations)
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)
from qiskit_chemistry import FermionicOperator from qiskit_chemistry.drivers import PySCFDriver, UnitsType from pyscf import mp, fci # Use PySCF, a classical computational chemistry software package, to compute the one-body and two-body integrals in # molecular-orbital basis, necessary to form the Fermionic operator driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 0.735; H .0 .0 1.2', unit=UnitsType.ANGSTROM, spin=1, basis='sto3g') molecule = driver.run() num_particles = molecule.num_alpha + molecule.num_beta num_spin_orbitals = molecule.num_orbitals * 2 # Build the qubit operator, which is the input to the VQE algorithm in Aqua ferOp = FermionicOperator(h1=molecule.one_body_integrals, h2=molecule.two_body_integrals) map_type = 'PARITY' qubitOp = ferOp.mapping(map_type) qubitOp = qubitOp.two_qubit_reduced_operator(num_particles) num_qubits = qubitOp.num_qubits # set the backend for the quantum computation from qiskit import Aer backend = Aer.get_backend('statevector_simulator') # setup a classical optimizer for VQE from qiskit_aqua.components.optimizers import L_BFGS_B optimizer = L_BFGS_B() # setup the initial state for the variational form from qiskit_chemistry.aqua_extensions.components.initial_states import HartreeFock