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('bravyi_kitaev_sf')
        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 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
     driver = cfg_mgr.get_driver_instance('PYSCF')
     molecule = driver.run(section)
     self.fer_op = FermionicOperator(h1=molecule._one_body_integrals,
                                     h2=molecule._two_body_integrals)
    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)
Exemplo n.º 4
0
    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 ModuleNotFoundError:
            self.skipTest('PYSCF driver does not appear to be installed')

        self.molecule = driver.run(section)

        ferOp = FermionicOperator(h1=self.molecule._one_body_integrals,
                                  h2=self.molecule._two_body_integrals)
        self.qubitOp = ferOp.mapping(
            map_type='PARITY', threshold=1e-10).two_qubit_reduced_operator(2)

        exact_eigensolver = get_algorithm_instance('ExactEigensolver')
        exact_eigensolver.init_args(self.qubitOp, 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.qubitOp.num_qubits + (2 if two_qubit_reduction else
                                                  0)
        qubit_mapping = 'parity'

        num_time_slices = 50
        n_ancillae = 9

        qpe = get_algorithm_instance('QPE')
        qpe.setup_quantum_backend(backend='local_qasm_simulator',
                                  shots=100,
                                  skip_transpiler=True)

        state_in = get_initial_state_instance('HartreeFock')
        state_in.init_args(self.qubitOp.num_qubits, num_orbitals,
                           qubit_mapping, two_qubit_reduction, num_particles)

        iqft = get_iqft_instance('STANDARD')
        iqft.init_args(n_ancillae)

        qpe.init_args(self.qubitOp,
                      state_in,
                      iqft,
                      num_time_slices,
                      n_ancillae,
                      paulis_grouping='random',
                      expansion_mode='suzuki',
                      expansion_order=2)

        result = qpe.run()

        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)
Exemplo n.º 5
0
def __obtener_operador_fermionico(h1, h2):
    """Esta función obtiene el operador fermiónico en función de las integrales de la molécula"""
    return FermionicOperator(h1=h1, h2=h2)