Ejemplo n.º 1
0
    def init_params(self, params, algo_input):
        """
        Initialize via parameters dictionary and algorithm input instance
        Args:
            params: parameters dictionary
            algo_input: EnergyInput instance
        """
        if algo_input is None:
            raise AlgorithmError("EnergyInput instance is required.")

        operator = algo_input.qubit_op

        qpe_params = params.get(QuantumAlgorithm.SECTION_KEY_ALGORITHM)
        num_time_slices = qpe_params.get(QPE.PROP_NUM_TIME_SLICES)
        paulis_grouping = qpe_params.get(QPE.PROP_PAULIS_GROUPING)
        expansion_mode = qpe_params.get(QPE.PROP_EXPANSION_MODE)
        expansion_order = qpe_params.get(QPE.PROP_EXPANSION_ORDER)
        num_ancillae = qpe_params.get(QPE.PROP_NUM_ANCILLAE)

        # Set up initial state, we need to add computed num qubits to params
        init_state_params = params.get(QuantumAlgorithm.SECTION_KEY_INITIAL_STATE)
        init_state_params['num_qubits'] = operator.num_qubits
        init_state = get_initial_state_instance(init_state_params['name'])
        init_state.init_params(init_state_params)

        # Set up iqft, we need to add num qubits to params which is our num_ancillae bits here
        iqft_params = params.get(QuantumAlgorithm.SECTION_KEY_IQFT)
        iqft_params['num_qubits'] = num_ancillae
        iqft = get_iqft_instance(iqft_params['name'])
        iqft.init_params(iqft_params)

        self.init_args(
            operator, init_state, iqft, num_time_slices, num_ancillae,
            paulis_grouping=paulis_grouping, expansion_mode=expansion_mode,
            expansion_order=expansion_order)
    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)
Ejemplo n.º 3
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
        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('CUSTOM')
        state_in.init_args(self.qubitOp.num_qubits,
                           state_vector=self.ref_eigenvec)

        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)

        # run qpe
        result = qpe.run()
        # self.log.debug('transformed operator paulis:\n{}'.format(self.qubitOp.print_operators('paulis')))

        # report result
        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 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=n_ancillae + 3,
                              fractional_part_only=True)))

        np.testing.assert_approx_equal(self.ref_eigenval,
                                       result['energy'],
                                       significant=2)