def test_tapered_op(self):
        # set_qiskit_chemistry_logging(logging.DEBUG)
        tapered_ops = []
        for coeff in itertools.product([1, -1], repeat=len(self.sq_list)):
            tapered_op = Operator.qubit_tapering(self.qubit_op, self.cliffords,
                                                 self.sq_list, list(coeff))
            tapered_ops.append((list(coeff), tapered_op))

        smallest_idx = 0  # Prior knowledge of which tapered_op has ground state
        the_tapered_op = tapered_ops[smallest_idx][1]
        the_coeff = tapered_ops[smallest_idx][0]

        optimizer = SLSQP(maxiter=1000)

        init_state = HartreeFock(
            num_qubits=the_tapered_op.num_qubits,
            num_orbitals=self.core._molecule_info['num_orbitals'],
            qubit_mapping=self.core._qubit_mapping,
            two_qubit_reduction=self.core._two_qubit_reduction,
            num_particles=self.core._molecule_info['num_particles'],
            sq_list=self.sq_list)

        var_form = UCCSD(
            num_qubits=the_tapered_op.num_qubits,
            depth=1,
            num_orbitals=self.core._molecule_info['num_orbitals'],
            num_particles=self.core._molecule_info['num_particles'],
            active_occupied=None,
            active_unoccupied=None,
            initial_state=init_state,
            qubit_mapping=self.core._qubit_mapping,
            two_qubit_reduction=self.core._two_qubit_reduction,
            num_time_slices=1,
            cliffords=self.cliffords,
            sq_list=self.sq_list,
            tapering_values=the_coeff,
            symmetries=self.symmetries)

        algo = VQE(the_tapered_op, var_form, optimizer, 'matrix')

        backend = BasicAer.get_backend('statevector_simulator')
        quantum_instance = QuantumInstance(backend=backend)

        algo_result = algo.run(quantum_instance)

        lines, result = self.core.process_algorithm_result(algo_result)

        self.assertAlmostEqual(result['energy'],
                               self.reference_energy,
                               places=6)
Beispiel #2
0
    def _build_hopping_operator(self, index):
        def check_commutativity(op_1, op_2):
            com = op_1 * op_2 - op_2 * op_1
            com.zeros_coeff_elimination()
            return True if com.is_empty() else False

        two_d_zeros = np.zeros((self._num_orbitals, self._num_orbitals))
        four_d_zeros = np.zeros((self._num_orbitals, self._num_orbitals,
                                 self._num_orbitals, self._num_orbitals))

        dummpy_fer_op = FermionicOperator(h1=two_d_zeros, h2=four_d_zeros)
        h1 = two_d_zeros.copy()
        h2 = four_d_zeros.copy()
        if len(index) == 2:
            i, j = index
            h1[i, j] = 1.0
            h1[j, i] = -1.0
        elif len(index) == 4:
            i, j, k, m = index
            h2[i, j, k, m] = 1.0
            h2[m, k, j, i] = -1.0
        dummpy_fer_op.h1 = h1
        dummpy_fer_op.h2 = h2

        qubit_op = dummpy_fer_op.mapping(self._qubit_mapping)
        qubit_op = qubit_op.two_qubit_reduced_operator(
            self._num_particles) if self._two_qubit_reduction else qubit_op

        if self._qubit_tapering:
            for symmetry in self._symmetries:
                symmetry_op = Operator(paulis=[[1.0, symmetry]])
                symm_commuting = check_commutativity(symmetry_op, qubit_op)
                if not symm_commuting:
                    break

        if self._qubit_tapering:
            if symm_commuting:
                qubit_op = Operator.qubit_tapering(qubit_op, self._cliffords,
                                                   self._sq_list,
                                                   self._tapering_values)
            else:
                qubit_op = None

        if qubit_op is None:
            logger.debug('excitation ({}) is skipped since it is not commuted '
                         'with symmetries'.format(','.join(
                             [str(x) for x in index])))
        return qubit_op
Beispiel #3
0
    def _build_hopping_operator(index, num_orbitals, num_particles,
                                qubit_mapping, two_qubit_reduction,
                                qubit_tapering, symmetries, cliffords, sq_list,
                                tapering_values):
        def check_commutativity(op_1, op_2):
            com = op_1 * op_2 - op_2 * op_1
            com.zeros_coeff_elimination()
            return True if com.is_empty() else False

        h1 = np.zeros((num_orbitals, num_orbitals))
        h2 = np.zeros((num_orbitals, num_orbitals, num_orbitals, num_orbitals))
        if len(index) == 2:
            i, j = index
            h1[i, j] = 1.0
            h1[j, i] = -1.0
        elif len(index) == 4:
            i, j, k, m = index
            h2[i, j, k, m] = 1.0
            h2[m, k, j, i] = -1.0

        dummpy_fer_op = FermionicOperator(h1=h1, h2=h2)
        qubit_op = dummpy_fer_op.mapping(qubit_mapping, num_workers=1)
        qubit_op = qubit_op.two_qubit_reduced_operator(num_particles) \
            if two_qubit_reduction else qubit_op

        if qubit_tapering:
            for symmetry in symmetries:
                symmetry_op = Operator(paulis=[[1.0, symmetry]])
                symm_commuting = check_commutativity(symmetry_op, qubit_op)
                if not symm_commuting:
                    break

        if qubit_tapering:
            if symm_commuting:
                qubit_op = Operator.qubit_tapering(qubit_op, cliffords,
                                                   sq_list, tapering_values)
            else:
                qubit_op = None

        if qubit_op is None:
            logger.debug('Excitation ({}) is skipped since it is not commuted '
                         'with symmetries'.format(','.join(
                             [str(x) for x in index])))
        return index, qubit_op