def test_parity(self): hamiltonian_cfg = OrderedDict([('name', 'hamiltonian'), ('transformation', 'full'), ('qubit_mapping', 'parity'), ('two_qubit_reduction', True), ('freeze_core', False), ('orbital_reduction', [])]) core = get_chemistry_operator_class('hamiltonian').init_params( hamiltonian_cfg) input_object = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core, actual_two_qubit_reduction=True) self._validate_input_object(input_object, num_qubits=10)
def test_freeze_core_orb_reduction(self): hamiltonian_cfg = OrderedDict([('name', 'hamiltonian'), ('transformation', 'full'), ('qubit_mapping', 'parity'), ('two_qubit_reduction', False), ('freeze_core', True), ('orbital_reduction', [-3, -2])]) core = get_chemistry_operator_class('hamiltonian').init_params( hamiltonian_cfg) input_object = core.run(self.qmolecule) self._validate_vars(core, energy_shift=-7.7962196) self._validate_info(core, num_particles=2, num_orbitals=6) self._validate_input_object(input_object, num_qubits=6, num_paulis=118)
def test_freeze_core(self): # Should be in effect a no-op for H2 hamiltonian_cfg = OrderedDict([('name', 'hamiltonian'), ('transformation', 'full'), ('qubit_mapping', 'jordan_wigner'), ('two_qubit_reduction', False), ('freeze_core', True), ('orbital_reduction', [])]) core = get_chemistry_operator_class('hamiltonian').init_params( hamiltonian_cfg) input_object = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core) self._validate_input_object(input_object)
def test_particle_hole(self): hamiltonian_cfg = OrderedDict([('name', 'hamiltonian'), ('transformation', 'particle_hole'), ('qubit_mapping', 'jordan_wigner'), ('two_qubit_reduction', False), ('freeze_core', False), ('orbital_reduction', [])]) core = get_chemistry_operator_class('hamiltonian').init_params( hamiltonian_cfg) input_object = core.run(self.qmolecule) self._validate_vars(core, ph_energy_shift=-1.83696799) self._validate_info(core) self._validate_input_object(input_object)
def test_jordan_wigner_2q(self): hamiltonian_cfg = OrderedDict([('name', 'hamiltonian'), ('transformation', 'full'), ('qubit_mapping', 'jordan_wigner'), ('two_qubit_reduction', True), ('freeze_core', False), ('orbital_reduction', [])]) core = get_chemistry_operator_class('hamiltonian').init_params( hamiltonian_cfg) input_object = core.run(self.qmolecule) self._validate_vars(core) # Reported effective 2 qubit reduction should be false self._validate_info(core, actual_two_qubit_reduction=False) self._validate_input_object(input_object)
def test_orbital_reduction( self ): # Remove virtual orbital just for test purposes (not sensible!) hamiltonian_cfg = OrderedDict([('name', 'hamiltonian'), ('transformation', 'full'), ('qubit_mapping', 'jordan_wigner'), ('two_qubit_reduction', False), ('freeze_core', False), ('orbital_reduction', [-1])]) core = get_chemistry_operator_class('hamiltonian').init_params( hamiltonian_cfg) input_object = core.run(self.qmolecule) self._validate_vars(core) self._validate_info(core, num_orbitals=2) self._validate_input_object(input_object, num_qubits=2, num_paulis=4)
def test_freeze_core_all_reduction_ph(self): hamiltonian_cfg = OrderedDict([('name', 'hamiltonian'), ('transformation', 'particle_hole'), ('qubit_mapping', 'parity'), ('two_qubit_reduction', True), ('freeze_core', True), ('orbital_reduction', [-2, -1])]) core = get_chemistry_operator_class('hamiltonian').init_params( hamiltonian_cfg) input_object = core.run(self.qmolecule) self._validate_vars(core, energy_shift=-7.7962196, ph_energy_shift=-1.05785247) self._validate_info(core, num_particles=2, num_orbitals=6, actual_two_qubit_reduction=True) self._validate_input_object(input_object, num_qubits=4, num_paulis=52)
def _run_driver_from_parser(self, p, save_json_algo_file): if p is None: raise QiskitChemistryError("Missing parser") # before merging defaults attempts to find a provider for the backend in case no # provider was passed if p.get_section_property(JSONSchema.BACKEND, JSONSchema.PROVIDER) is None: backend_name = p.get_section_property(JSONSchema.BACKEND, JSONSchema.NAME) if backend_name is not None: p.set_section_property(JSONSchema.BACKEND, JSONSchema.PROVIDER, get_provider_from_backend(backend_name)) p.validate_merge_defaults() # logger.debug('ALgorithm Input Schema: {}'.format(json.dumps(p.to_JSON(), sort_keys=True, indent=4))) experiment_name = "-- no &NAME section found --" if JSONSchema.NAME in p.get_section_names(): name_sect = p.get_section(JSONSchema.NAME) if 'data' in name_sect: experiment_name = name_sect['data'] logger.info('Running chemistry problem from input file: {}'.format( p.get_filename())) logger.info('Experiment description: {}'.format( experiment_name.rstrip())) driver_name = p.get_section_property(InputParser.DRIVER, JSONSchema.NAME) if driver_name is None: raise QiskitChemistryError( 'Property "{0}" missing in section "{1}"'.format( JSONSchema.NAME, InputParser.DRIVER)) hdf5_file = p.get_section_property(InputParser.DRIVER, QiskitChemistry.KEY_HDF5_OUTPUT) section = p.get_section(driver_name) if 'data' not in section: raise QiskitChemistryError( 'Property "data" missing in section "{0}"'.format(driver_name)) if driver_name not in local_drivers(): raise QiskitChemistryError( 'Driver "{0}" missing in local drivers'.format(driver_name)) work_path = None input_file = p.get_filename() if input_file is not None: work_path = os.path.dirname(os.path.realpath(input_file)) driver = get_driver_class(driver_name).init_from_input(section) driver.work_path = work_path molecule = driver.run() if work_path is not None and hdf5_file is not None and not os.path.isabs( hdf5_file): hdf5_file = os.path.abspath(os.path.join(work_path, hdf5_file)) molecule.log() if hdf5_file is not None: molecule._origin_driver_name = driver_name molecule._origin_driver_config = section['data'] molecule.save(hdf5_file) text = "HDF5 file saved '{}'".format(hdf5_file) logger.info(text) if not save_json_algo_file: logger.info('Run ended with hdf5 file saved.') return QiskitChemistry._DRIVER_RUN_TO_HDF5, text # Run the Hamiltonian to process the QMolecule and get an input for algorithms cls = get_chemistry_operator_class( p.get_section_property(InputParser.OPERATOR, JSONSchema.NAME)) self._core = cls.init_params( p.get_section_properties(InputParser.OPERATOR)) input_object = self._core.run(molecule) logger.debug('Core computed substitution variables {}'.format( self._core.molecule_info)) result = p.process_substitutions(self._core.molecule_info) logger.debug('Substitutions {}'.format(result)) params = {} for section_name, section in p.get_sections().items(): if section_name == JSONSchema.NAME or \ section_name == InputParser.DRIVER or \ section_name == driver_name.lower() or \ section_name == InputParser.OPERATOR or \ 'properties' not in section: continue params[section_name] = copy.deepcopy(section['properties']) if JSONSchema.PROBLEM == section_name and \ InputParser.AUTO_SUBSTITUTIONS in params[section_name]: del params[section_name][InputParser.AUTO_SUBSTITUTIONS] return QiskitChemistry._DRIVER_RUN_TO_ALGO_INPUT, params, input_object