예제 #1
0
        def _build_one_sector(available_hopping_ops):

            to_be_computed_list = []
            for idx, _ in enumerate(mus):
                m_u = mus[idx]
                n_u = nus[idx]
                left_op = available_hopping_ops.get(
                    '_'.join([str(x) for x in excitations_list[m_u]]), None)
                right_op_1 = available_hopping_ops.get(
                    '_'.join([str(x) for x in excitations_list[n_u]]), None)
                right_op_2 = available_hopping_ops.get(
                    '_'.join([str(x) for x in reversed(excitations_list[n_u])]), None)
                to_be_computed_list.append((m_u, n_u, left_op, right_op_1, right_op_2))

            if logger.isEnabledFor(logging.INFO):
                logger.info("Building all commutators:")
                TextProgressBar(sys.stderr)
            results = parallel_map(QEquationOfMotion._build_commutator_rountine,
                                   to_be_computed_list,
                                   task_args=(self._untapered_op, self._z2_symmetries))
            for result in results:
                m_u, n_u, q_mat_op, w_mat_op, m_mat_op, v_mat_op = result
                q_commutators[m_u][n_u] = op_converter.to_tpb_grouped_weighted_pauli_operator(
                    q_mat_op, TPBGroupedWeightedPauliOperator.sorted_grouping) \
                    if q_mat_op is not None else q_commutators[m_u][n_u]
                w_commutators[m_u][n_u] = op_converter.to_tpb_grouped_weighted_pauli_operator(
                    w_mat_op, TPBGroupedWeightedPauliOperator.sorted_grouping) \
                    if w_mat_op is not None else w_commutators[m_u][n_u]
                m_commutators[m_u][n_u] = op_converter.to_tpb_grouped_weighted_pauli_operator(
                    m_mat_op, TPBGroupedWeightedPauliOperator.sorted_grouping) \
                    if m_mat_op is not None else m_commutators[m_u][n_u]
                v_commutators[m_u][n_u] = op_converter.to_tpb_grouped_weighted_pauli_operator(
                    v_mat_op, TPBGroupedWeightedPauliOperator.sorted_grouping) \
                    if v_mat_op is not None else v_commutators[m_u][n_u]
    def test_equal(self):
        gop_1 = op_converter.to_tpb_grouped_weighted_pauli_operator(
            self.qubit_op, TPBGroupedWeightedPauliOperator.sorted_grouping)
        gop_2 = op_converter.to_tpb_grouped_weighted_pauli_operator(
            self.qubit_op, TPBGroupedWeightedPauliOperator.unsorted_grouping)

        self.assertEqual(gop_1, gop_2)
예제 #3
0
    def _config_the_best_mode(self, operator, backend):

        if not isinstance(operator, (WeightedPauliOperator, MatrixOperator,
                                     TPBGroupedWeightedPauliOperator)):
            logger.debug("Unrecognized operator type, skip auto conversion.")
            return operator

        ret_op = operator
        if not is_statevector_backend(backend) and not (
                is_aer_provider(backend)
                and self._quantum_instance.run_config.shots == 1):
            if isinstance(operator, (WeightedPauliOperator, MatrixOperator)):
                logger.debug(
                    "When running with Qasm simulator, grouped pauli can "
                    "save number of measurements. "
                    "We convert the operator into grouped ones.")
                ret_op = op_converter.to_tpb_grouped_weighted_pauli_operator(
                    operator, TPBGroupedWeightedPauliOperator.sorted_grouping)
        else:
            if not is_aer_provider(backend):
                if not isinstance(operator, MatrixOperator):
                    logger.info(
                        "When running with non-Aer statevector simulator, "
                        "represent operator as a matrix could "
                        "achieve the better performance. We convert "
                        "the operator to matrix.")
                    ret_op = op_converter.to_matrix_operator(operator)
            else:
                if not isinstance(operator, WeightedPauliOperator):
                    logger.info("When running with Aer simulator, "
                                "represent operator as weighted paulis could "
                                "achieve the better performance. We convert "
                                "the operator to weighted paulis.")
                    ret_op = op_converter.to_weighted_pauli_operator(operator)
        return ret_op
예제 #4
0
    def test_evaluate_qasm_mode(self):
        """ evaluate qasm mode test """
        wave_function = self.var_form.construct_circuit(
            np.array(aqua_globals.random.randn(self.var_form.num_parameters)))
        wave_fn_statevector = \
            self.quantum_instance_statevector.execute(wave_function).get_statevector(wave_function)
        reference = self.qubit_op.copy().evaluate_with_statevector(wave_fn_statevector)

        shots = 65536 // len(self.qubit_op.paulis)
        self.quantum_instance_qasm.set_config(shots=shots)
        circuits = self.qubit_op.construct_evaluation_circuit(wave_function=wave_function,
                                                              statevector_mode=False)
        result = self.quantum_instance_qasm.execute(circuits)
        pauli_value = self.qubit_op.evaluate_with_result(result=result, statevector_mode=False)
        grouped_op = op_converter.to_tpb_grouped_weighted_pauli_operator(
                        self.qubit_op, TPBGroupedWeightedPauliOperator.sorted_grouping)
        shots = 65536 // grouped_op.num_groups
        self.quantum_instance_qasm.set_config(shots=shots)
        circuits = grouped_op.construct_evaluation_circuit(wave_function=wave_function,
                                                           statevector_mode=False)
        grouped_pauli_value = grouped_op.evaluate_with_result(
            result=self.quantum_instance_qasm.execute(circuits), statevector_mode=False)

        self.assertGreaterEqual(reference[0].real,
                                grouped_pauli_value[0].real - 3 * grouped_pauli_value[1].real)
        self.assertLessEqual(reference[0].real,
                             grouped_pauli_value[0].real + 3 * grouped_pauli_value[1].real)
        # this check assure the std of grouped pauli is
        # less than pauli mode under a fixed amount of total shots
        self.assertLessEqual(grouped_pauli_value[1].real, pauli_value[1].real)
    def test_sorted_grouping(self):
        """Test with color grouping approach."""
        num_qubits = 2
        paulis = [
            Pauli.from_label(pauli_label)
            for pauli_label in itertools.product('IXYZ', repeat=num_qubits)
        ]
        weights = np.random.random(len(paulis))
        op = WeightedPauliOperator.from_list(paulis, weights)
        grouped_op = op_converter.to_tpb_grouped_weighted_pauli_operator(
            op, TPBGroupedWeightedPauliOperator.sorted_grouping)

        # check all paulis are still existed.
        for g_p in grouped_op.paulis:
            passed = False
            for pauli in op.paulis:
                if pauli[1] == g_p[1]:
                    passed = pauli[0] == g_p[0]
                    break
            self.assertTrue(
                passed, "non-existed paulis in grouped_paulis: {}".format(
                    g_p[1].to_label()))

        # check the number of basis of grouped
        # one should be less than and equal to the original one.
        self.assertGreaterEqual(len(op.basis), len(grouped_op.basis))
    def test_chop(self):
        """ chop test """
        paulis = [
            Pauli.from_label(x)
            for x in ['IIXX', 'ZZXX', 'ZZZZ', 'XXZZ', 'XXXX', 'IXXX']
        ]
        coeffs = [0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
        op = WeightedPauliOperator.from_list(paulis, coeffs)
        grouped_op = op_converter.to_tpb_grouped_weighted_pauli_operator(
            op, TPBGroupedWeightedPauliOperator.sorted_grouping)

        original_num_basis = len(grouped_op.basis)
        chopped_grouped_op = grouped_op.chop(0.35, copy=True)
        self.assertLessEqual(len(chopped_grouped_op.basis), 3)
        self.assertLessEqual(len(chopped_grouped_op.basis), original_num_basis)
        # ZZXX group is remove
        for b, _ in chopped_grouped_op.basis:
            self.assertFalse(b.to_label() == 'ZZXX')

        chopped_grouped_op = grouped_op.chop(0.55, copy=True)
        self.assertLessEqual(len(chopped_grouped_op.basis), 1)
        self.assertLessEqual(len(chopped_grouped_op.basis), original_num_basis)

        for b, _ in chopped_grouped_op.basis:
            self.assertFalse(b.to_label() == 'ZZXX')
            self.assertFalse(b.to_label() == 'ZZZZ')
            self.assertFalse(b.to_label() == 'XXZZ')
예제 #7
0
    def test_unsorted_grouping(self):
        """Test with normal grouping approach."""

        num_qubits = 4
        paulis = [Pauli.from_label(pauli_label)
                  for pauli_label in itertools.product('IXYZ', repeat=num_qubits)]
        weights = aqua_globals.random.random_sample(len(paulis))
        op = WeightedPauliOperator.from_list(paulis, weights)
        grouped_op = op_converter.to_tpb_grouped_weighted_pauli_operator(
            op, TPBGroupedWeightedPauliOperator.unsorted_grouping)

        for g_p in grouped_op.paulis:
            passed = False
            for pauli in op.paulis:
                if pauli[1] == g_p[1]:
                    passed = pauli[0] == g_p[0]
                    break
            self.assertTrue(passed,
                            "non-existed paulis in grouped_paulis: {}".format(g_p[1].to_label()))

        self.assertGreaterEqual(len(op.basis), len(grouped_op.basis))