Example #1
0
    def test_qpe(self, qubitOp):
        self.algorithm = 'QPE'
        self.log.debug('Testing QPE')

        self.qubitOp = qubitOp

        exact_eigensolver = get_algorithm_instance('ExactEigensolver')
        exact_eigensolver.init_args(self.qubitOp, k=1)
        results = exact_eigensolver.run()

        w = results['eigvals']
        v = results['eigvecs']

        self.qubitOp._check_representation('matrix')
        np.testing.assert_almost_equal(self.qubitOp.matrix @ v[0], w[0] * v[0])
        np.testing.assert_almost_equal(
            expm(-1.j * 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
        num_iterations = 12

        iqpe = get_algorithm_instance('IQPE')
        iqpe.setup_quantum_backend(backend='local_qasm_simulator',
                                   shots=100,
                                   skip_transpiler=True)

        state_in = get_initial_state_instance('CUSTOM')
        state_in.init_args(self.qubitOp.num_qubits,
                           state_vector=self.ref_eigenvec)

        iqpe.init_args(
            self.qubitOp,
            state_in,
            num_time_slices,
            num_iterations,
            paulis_grouping='random',
            expansion_mode='suzuki',
            expansion_order=2,
        )

        result = iqpe.run()
        # self.log.debug('operator paulis:\n{}'.format(self.qubitOp.print_operators('paulis')))
        # self.log.debug('qpe circuit:\n\n{}'.format(result['circuit']['complete'].qasm()))

        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 + result['translation']) *
                              result['stretch'],
                              max_num_digits=num_iterations + 3,
                              fractional_part_only=True)))

        np.testing.assert_approx_equal(self.ref_eigenval,
                                       result['energy'],
                                       significant=2)
    def test_qpe(self, distance):
        self.algorithm = 'QPE'
        self.log.debug(
            'Testing End-to-End with QPE on H2 with interatomic 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
        driver = cfg_mgr.get_driver_instance('PYSCF')
        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)

        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,
                      use_basis_gates=True)

        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)