示例#1
0
 def setUp(self):
     super().setUp()
     input_file = self._get_resource_path('sample.setpacking')
     with open(input_file) as f:
         self.list_of_subsets = json.load(f)
         qubitOp, offset = setpacking.get_setpacking_qubitops(
             self.list_of_subsets)
         self.algo_input = EnergyInput(qubitOp)
 def setUp(self):
     super().setUp()
     np.random.seed(100)
     self.num_nodes = 3
     self.w = vertexcover.random_graph(self.num_nodes,
                                       edge_prob=0.8,
                                       weight_range=10)
     self.qubit_op, self.offset = vertexcover.get_vertexcover_qubitops(
         self.w)
     self.algo_input = EnergyInput(self.qubit_op)
示例#3
0
 def setUp(self):
     super().setUp()
     np.random.seed(100)
     self.num_nodes = 4
     self.w = graphpartition.random_graph(self.num_nodes,
                                          edge_prob=0.8,
                                          weight_range=10)
     self.qubit_op, self.offset = graphpartition.get_graphpartition_qubitops(
         self.w)
     self.algo_input = EnergyInput(self.qubit_op)
示例#4
0
 def setUp(self):
     super().setUp()
     self.K = 5  # K means the size of the clique
     np.random.seed(100)
     self.num_nodes = 5
     self.w = clique.random_graph(self.num_nodes,
                                  edge_prob=0.8,
                                  weight_range=10)
     self.qubit_op, self.offset = clique.get_clique_qubitops(self.w, self.K)
     self.algo_input = EnergyInput(self.qubit_op)
示例#5
0
文件: test_vqe.py 项目: CQCL/aqua
 def setUp(self):
     np.random.seed(50)
     pauli_dict = {
         'paulis': [{"coeff": {"imag": 0.0, "real": -1.052373245772859}, "label": "II"},
                    {"coeff": {"imag": 0.0, "real": 0.39793742484318045}, "label": "IZ"},
                    {"coeff": {"imag": 0.0, "real": -0.39793742484318045}, "label": "ZI"},
                    {"coeff": {"imag": 0.0, "real": -0.01128010425623538}, "label": "ZZ"},
                    {"coeff": {"imag": 0.0, "real": 0.18093119978423156}, "label": "XX"}
                    ]
     }
     qubit_op = Operator.load_from_dict(pauli_dict)
     self.algo_input = EnergyInput(qubit_op)
 def setUp(self):
     super().setUp()
     np.random.seed(8123179)
     self.w = maxcut.random_graph(4, edge_prob=0.5, weight_range=10)
     self.qubit_op, self.offset = maxcut.get_maxcut_qubitops(self.w)
     self.algo_input = EnergyInput(self.qubit_op)
    def run(self, qmolecule):
        logger.debug('Processing started...')
        # Save these values for later combination with the quantum computation result
        self._hf_energy = qmolecule.hf_energy
        self._nuclear_repulsion_energy = qmolecule.nuclear_repulsion_energy
        self._nuclear_dipole_moment = qmolecule.nuclear_dipole_moment
        self._reverse_dipole_sign = qmolecule.reverse_dipole_sign

        core_list = qmolecule.core_orbitals if self._freeze_core else []
        reduce_list = self._orbital_reduction

        if self._freeze_core:
            logger.info(
                "Freeze_core specified. Core orbitals to be frozen: {}".format(
                    core_list))
        if len(reduce_list) > 0:
            logger.info(
                "Configured orbital reduction list: {}".format(reduce_list))
            reduce_list = [
                x + qmolecule.num_orbitals if x < 0 else x for x in reduce_list
            ]

        freeze_list = []
        remove_list = []

        # Orbitals are specified by their index from 0 to n-1, where n is the number of orbitals the
        # molecule has. The combined list of the core orbitals, when freeze_core is true, with any
        # user supplied orbitals is what will be used. Negative numbers may be used to indicate the
        # upper virtual orbitals, so -1 is the highest, then -2 etc. and these will be converted to the
        # positive 0-based index for computation.
        # In the combined list any orbitals that are occupied are added to a freeze list and an
        # energy is stored from these orbitals to be added later. Unoccupied orbitals are just discarded.
        # Because freeze and eliminate is done in separate steps, with freeze first, we have to re-base
        # the indexes for elimination according to how many orbitals were removed when freezing.
        #
        orbitals_list = list(set(core_list + reduce_list))
        nel = qmolecule.num_alpha + qmolecule.num_beta
        new_nel = nel
        if len(orbitals_list) > 0:
            orbitals_list = np.array(orbitals_list)
            orbitals_list = orbitals_list[(orbitals_list >= 0) & (
                orbitals_list < qmolecule.num_orbitals)]

            freeze_list = [i for i in orbitals_list if i < int(nel / 2)]
            freeze_list = np.append(
                np.array(freeze_list),
                np.array(freeze_list) + qmolecule.num_orbitals)

            remove_list = [i for i in orbitals_list if i >= int(nel / 2)]
            remove_list_orig_idx = np.append(
                np.array(remove_list),
                np.array(remove_list) + qmolecule.num_orbitals)
            remove_list = np.append(
                np.array(remove_list) - int(len(freeze_list) / 2),
                np.array(remove_list) + qmolecule.num_orbitals -
                len(freeze_list))
            logger.info(
                "Combined orbital reduction list: {}".format(orbitals_list))
            logger.info(
                "  converting to spin orbital reduction list: {}".format(
                    np.append(np.array(orbitals_list),
                              np.array(orbitals_list) +
                              qmolecule.num_orbitals)))
            logger.info(
                "    => freezing spin orbitals: {}".format(freeze_list))
            logger.info(
                "    => removing spin orbitals: {} (indexes accounting for freeze {})"
                .format(remove_list_orig_idx, remove_list))

            new_nel -= len(freeze_list)

        fer_op = FermionicOperator(h1=qmolecule.one_body_integrals,
                                   h2=qmolecule.two_body_integrals)
        fer_op, self._energy_shift, did_shift = Hamiltonian._try_reduce_fermionic_operator(
            fer_op, freeze_list, remove_list)
        if did_shift:
            logger.info("Frozen orbital energy shift: {}".format(
                self._energy_shift))
        if self._transformation == Hamiltonian.TRANSFORMATION_PH:
            fer_op, ph_shift = fer_op.particle_hole_transformation(new_nel)
            self._ph_energy_shift = -ph_shift
            logger.info("Particle hole energy shift: {}".format(
                self._ph_energy_shift))
        logger.debug('Converting to qubit using {} mapping'.format(
            self._qubit_mapping))
        qubit_op = Hamiltonian._map_fermionic_operator_to_qubit(
            fer_op, self._qubit_mapping, new_nel, self._two_qubit_reduction,
            self._max_workers)
        logger.debug('  num paulis: {}, num qubits: {}'.format(
            len(qubit_op.paulis), qubit_op.num_qubits))
        algo_input = EnergyInput(qubit_op)

        def _add_aux_op(aux_op):
            algo_input.add_aux_op(
                Hamiltonian._map_fermionic_operator_to_qubit(
                    aux_op, self._qubit_mapping, new_nel,
                    self._two_qubit_reduction, self._max_workers))
            logger.debug('  num paulis: {}'.format(
                len(algo_input.aux_ops[-1].paulis)))

        logger.debug('Creating aux op for Number of Particles')
        _add_aux_op(fer_op.total_particle_number())
        logger.debug('Creating aux op for S^2')
        _add_aux_op(fer_op.total_angular_momentum())
        logger.debug('Creating aux op for Magnetization')
        _add_aux_op(fer_op.total_magnetization())

        if qmolecule.has_dipole_integrals():

            def _dipole_op(dipole_integrals, axis):
                logger.debug('Creating aux op for dipole {}'.format(axis))
                fer_op_ = FermionicOperator(h1=dipole_integrals)
                fer_op_, shift, did_shift_ = self._try_reduce_fermionic_operator(
                    fer_op_, freeze_list, remove_list)
                if did_shift_:
                    logger.info("Frozen orbital {} dipole shift: {}".format(
                        axis, shift))
                ph_shift_ = 0.0
                if self._transformation == Hamiltonian.TRANSFORMATION_PH:
                    fer_op_, ph_shift_ = fer_op_.particle_hole_transformation(
                        new_nel)
                    ph_shift_ = -ph_shift_
                    logger.info("Particle hole {} dipole shift: {}".format(
                        axis, ph_shift_))
                qubit_op_ = self._map_fermionic_operator_to_qubit(
                    fer_op_, self._qubit_mapping, new_nel,
                    self._two_qubit_reduction, self._max_workers)
                logger.debug('  num paulis: {}'.format(len(qubit_op_.paulis)))
                return qubit_op_, shift, ph_shift_

            op_dipole_x, self._x_dipole_shift, self._ph_x_dipole_shift = _dipole_op(
                qmolecule.x_dipole_integrals, 'x')
            op_dipole_y, self._y_dipole_shift, self._ph_y_dipole_shift = _dipole_op(
                qmolecule.y_dipole_integrals, 'y')
            op_dipole_z, self._z_dipole_shift, self._ph_z_dipole_shift = _dipole_op(
                qmolecule.z_dipole_integrals, 'z')

            algo_input.add_aux_op(op_dipole_x)
            algo_input.add_aux_op(op_dipole_y)
            algo_input.add_aux_op(op_dipole_z)

        logger.info(
            'Molecule num electrons: {}, remaining for processing: {}'.format(
                nel, new_nel))
        nspinorbs = qmolecule.num_orbitals * 2
        new_nspinorbs = nspinorbs - len(freeze_list) - len(remove_list)
        logger.info(
            'Molecule num spin orbitals: {}, remaining for processing: {}'.
            format(nspinorbs, new_nspinorbs))

        self._add_molecule_info(self.INFO_NUM_PARTICLES, new_nel)
        self._add_molecule_info(self.INFO_NUM_ORBITALS, new_nspinorbs)
        self._add_molecule_info(
            self.INFO_TWO_QUBIT_REDUCTION, self._two_qubit_reduction
            if self._qubit_mapping == 'parity' else False)

        logger.debug('Processing complete ready to run algorithm')
        return algo_input
示例#8
0
    def setUp(self):
        super().setUp()
        np.random.seed(50)
        pauli_dict = {
            'paulis': [{
                "coeff": {
                    "imag": 0.0,
                    "real": -1.052373245772859
                },
                "label": "II"
            }, {
                "coeff": {
                    "imag": 0.0,
                    "real": 0.39793742484318045
                },
                "label": "IZ"
            }, {
                "coeff": {
                    "imag": 0.0,
                    "real": -0.39793742484318045
                },
                "label": "ZI"
            }, {
                "coeff": {
                    "imag": 0.0,
                    "real": -0.01128010425623538
                },
                "label": "ZZ"
            }, {
                "coeff": {
                    "imag": 0.0,
                    "real": 0.18093119978423156
                },
                "label": "XX"
            }]
        }
        qubit_op = Operator.load_from_dict(pauli_dict)
        self.algo_input = EnergyInput(qubit_op)

        backends = ['statevector_simulator', 'qasm_simulator']
        res = {}
        for backend in backends:
            params_no_caching = {
                'algorithm': {
                    'name': 'VQE'
                },
                'problem': {
                    'name': 'energy',
                    'random_seed': 50,
                    'circuit_caching': False,
                    'skip_qobj_deepcopy': False,
                    'skip_qobj_validation': False,
                    'circuit_cache_file': None,
                },
                'backend': {
                    'name': backend,
                    'shots': 1000
                },
            }
            res[backend] = run_algorithm(params_no_caching, self.algo_input)
        self.reference_vqe_result = res
示例#9
0
 def setUp(self):
     super().setUp()
     input_file = self._get_resource_path('sample.partition')
     number_list = partition.read_numbers_from_file(input_file)
     qubitOp, offset = partition.get_partition_qubitops(number_list)
     self.algo_input = EnergyInput(qubitOp)