How to use the qiskit.quantum_info.Pauli.from_label function in qiskit

To help you get started, we’ve selected a few qiskit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Qiskit / qiskit-aqua / qiskit / aqua / operators / tapered_weighed_pauli_operator.py View on Github external
num_qubits = operator.num_qubits
        last_idx = num_qubits - 1
        mid_idx = num_qubits // 2 - 1
        sq_list = [mid_idx, last_idx]

        # build symmetries, sq_paulis, cliffords:
        symmetries, sq_paulis, cliffords = [], [], []
        for idx in sq_list:
            pauli_str = ['I'] * num_qubits

            pauli_str[idx] = 'Z'
            z_sym = Pauli.from_label(''.join(pauli_str)[::-1])
            symmetries.append(z_sym)

            pauli_str[idx] = 'X'
            sq_pauli = Pauli.from_label(''.join(pauli_str)[::-1])
            sq_paulis.append(sq_pauli)

            clifford = WeightedPauliOperator(paulis=[[1. / np.sqrt(2), z_sym], [1. / np.sqrt(2), sq_pauli]])
            cliffords.append(clifford)

        return cls.taper(operator, symmetries, cliffords, sq_list, tapering_values)
github Qiskit / qiskit-aqua / test / aqua / operators / test_weighted_pauli_operator.py View on Github external
result = self.quantum_instance_statevector.execute(circuits)
        actual_value = op.evaluate_with_result(result=result, statevector_mode=True)
        self.assertAlmostEqual(1.0, actual_value[0].real, places=5)
        # - 1 eigenstate
        wave_function = QuantumCircuit(qr)
        wave_function.x(qr[0])
        wave_function.h(qr[0])
        wave_function.s(qr[0])
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=True)
        result = self.quantum_instance_statevector.execute(circuits)
        actual_value = op.evaluate_with_result(result=result, statevector_mode=True)
        self.assertAlmostEqual(-1.0, actual_value[0].real, places=5)

        # Z
        op = WeightedPauliOperator.from_list([Pauli.from_label('Z')])
        qr = QuantumRegister(1, name='q')
        wave_function = QuantumCircuit(qr)
        # + 1 eigenstate
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=True)
        result = self.quantum_instance_statevector.execute(circuits)
        actual_value = op.evaluate_with_result(result=result, statevector_mode=True)
        self.assertAlmostEqual(1.0, actual_value[0].real, places=5)
        # - 1 eigenstate
        wave_function = QuantumCircuit(qr)
        wave_function.x(qr[0])
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=True)
        result = self.quantum_instance_statevector.execute(circuits)
        actual_value = op.evaluate_with_result(result=result, statevector_mode=True)
        self.assertAlmostEqual(-1.0, actual_value[0].real, places=5)
github Qiskit / qiskit-aqua / test / aqua / operators / test_weighted_pauli_operator.py View on Github external
statevector_mode=False)
        result = self.quantum_instance_qasm.execute(circuits)
        actual_value = op.evaluate_with_result(result=result, statevector_mode=False)
        self.assertAlmostEqual(1.0, actual_value[0].real, places=5)
        # - 1 eigenstate
        wave_function = QuantumCircuit(qr)
        wave_function.x(qr[0])
        wave_function.h(qr[0])
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=False)
        result = self.quantum_instance_qasm.execute(circuits)
        actual_value = op.evaluate_with_result(result=result, statevector_mode=False)
        self.assertAlmostEqual(-1.0, actual_value[0].real, places=5)

        # Y
        op = WeightedPauliOperator.from_list([Pauli.from_label('Y')])
        qr = QuantumRegister(1, name='q')
        wave_function = QuantumCircuit(qr)
        # + 1 eigenstate
        wave_function.h(qr[0])
        wave_function.s(qr[0])
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
                                                   statevector_mode=False)
        result = self.quantum_instance_qasm.execute(circuits)
        actual_value = op.evaluate_with_result(result=result, statevector_mode=False)
        self.assertAlmostEqual(1.0, actual_value[0].real, places=5)
        # - 1 eigenstate
        wave_function = QuantumCircuit(qr)
        wave_function.x(qr[0])
        wave_function.h(qr[0])
        wave_function.s(qr[0])
        circuits = op.construct_evaluation_circuit(wave_function=wave_function,
github Qiskit / qiskit-aqua / test / aqua / operators / test_weighted_pauli_operator.py View on Github external
def test_simplify(self):
        """ simplify test """
        pauli_a = 'IXYZ'
        pauli_b = 'IXYZ'
        coeff_a = 0.5
        coeff_b = -0.5
        pauli_term_a = [coeff_a, Pauli.from_label(pauli_a)]
        pauli_term_b = [coeff_b, Pauli.from_label(pauli_b)]
        op_a = WeightedPauliOperator(paulis=[pauli_term_a])
        op_b = WeightedPauliOperator(paulis=[pauli_term_b])
        new_op = op_a + op_b
        new_op.simplify()

        self.assertEqual(0, len(new_op.paulis), "{}".format(new_op.print_details()))
        self.assertTrue(new_op.is_empty())

        paulis = [Pauli.from_label(x) for x in ['IXYZ', 'XXZY', 'IIZZ', 'XXYY', 'ZZXX', 'YYYY']]
        coeffs = [0.2, 0.6, 0.8, -0.2, -0.6, -0.8]
        op1 = WeightedPauliOperator.from_list(paulis, coeffs)

        for i, pauli in enumerate(paulis):
            tmp_op = WeightedPauliOperator(paulis=[[-coeffs[i], pauli]])
            op1 += tmp_op
github Qiskit / qiskit-aqua / test / aqua / operators / test_weighted_pauli_operator.py View on Github external
def setUp(self):
        super().setUp()
        seed = 0
        aqua_globals.random_seed = seed

        self.num_qubits = 3
        paulis = [Pauli.from_label(pauli_label)
                  for pauli_label in itertools.product('IXYZ', repeat=self.num_qubits)]
        weights = aqua_globals.random.random_sample(len(paulis))
        self.qubit_op = WeightedPauliOperator.from_list(paulis, weights)
        self.var_form = RYRZ(self.qubit_op.num_qubits, 1)

        qasm_simulator = BasicAer.get_backend('qasm_simulator')
        self.quantum_instance_qasm = QuantumInstance(qasm_simulator, shots=65536,
                                                     seed_simulator=seed, seed_transpiler=seed)
        statevector_simulator = BasicAer.get_backend('statevector_simulator')
        self.quantum_instance_statevector = \
            QuantumInstance(statevector_simulator, shots=1,
                            seed_simulator=seed, seed_transpiler=seed)
github Qiskit / qiskit-aqua / test / chemistry / test_bksf_mapping.py View on Github external
def test_bksf_edge_op_bi(self):
        """Test bksf mapping, edge operator bi"""
        edge_matrix = np.triu(np.ones((4, 4)))
        edge_list = np.array(np.nonzero(np.triu(edge_matrix) - np.diag(np.diag(edge_matrix))))
        qterm_b0 = edge_operator_bi(edge_list, 0)
        qterm_b1 = edge_operator_bi(edge_list, 1)
        qterm_b2 = edge_operator_bi(edge_list, 2)
        qterm_b3 = edge_operator_bi(edge_list, 3)

        ref_qterm_b0 = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('IIIZZZ')]])
        ref_qterm_b1 = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('IZZIIZ')]])
        ref_qterm_b2 = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('ZIZIZI')]])
        ref_qterm_b3 = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('ZZIZII')]])

        self.assertEqual(qterm_b0, ref_qterm_b0, "\n{} vs \n{}".format(
            qterm_b0.print_details(), ref_qterm_b0.print_details()))
        self.assertEqual(qterm_b1, ref_qterm_b1, "\n{} vs \n{}".format(
            qterm_b1.print_details(), ref_qterm_b1.print_details()))
        self.assertEqual(qterm_b2, ref_qterm_b2, "\n{} vs \n{}".format(
            qterm_b2.print_details(), ref_qterm_b2.print_details()))
        self.assertEqual(qterm_b3, ref_qterm_b3, "\n{} vs \n{}".format(
            qterm_b3.print_details(), ref_qterm_b3.print_details()))
github Qiskit / qiskit-aqua / test / chemistry / test_bksf_mapping.py View on Github external
def test_bksf_edge_op_bi(self):
        """Test bksf mapping, edge operator bi"""
        edge_matrix = np.triu(np.ones((4, 4)))
        edge_list = np.array(np.nonzero(np.triu(edge_matrix) - np.diag(np.diag(edge_matrix))))
        qterm_b0 = edge_operator_bi(edge_list, 0)
        qterm_b1 = edge_operator_bi(edge_list, 1)
        qterm_b2 = edge_operator_bi(edge_list, 2)
        qterm_b3 = edge_operator_bi(edge_list, 3)

        ref_qterm_b0 = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('IIIZZZ')]])
        ref_qterm_b1 = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('IZZIIZ')]])
        ref_qterm_b2 = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('ZIZIZI')]])
        ref_qterm_b3 = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('ZZIZII')]])

        self.assertEqual(qterm_b0, ref_qterm_b0, "\n{} vs \n{}".format(
            qterm_b0.print_details(), ref_qterm_b0.print_details()))
        self.assertEqual(qterm_b1, ref_qterm_b1, "\n{} vs \n{}".format(
            qterm_b1.print_details(), ref_qterm_b1.print_details()))
        self.assertEqual(qterm_b2, ref_qterm_b2, "\n{} vs \n{}".format(
            qterm_b2.print_details(), ref_qterm_b2.print_details()))
        self.assertEqual(qterm_b3, ref_qterm_b3, "\n{} vs \n{}".format(
            qterm_b3.print_details(), ref_qterm_b3.print_details()))
github Qiskit / qiskit-aqua / test / aqua / test_operator.py View on Github external
def test_chop_complex_only_1(self):
        """ chop complex only 1 test """
        paulis = ['IXYZ', 'XXZY', 'IIZZ', 'XXYY', 'ZZXX', 'YYYY']
        coeffs = [0.2 + -1j * 0.2, 0.6 + -1j * 0.6, 0.8 + -1j * 0.8,
                  -0.2 + -1j * 0.2, -0.6 - -1j * 0.6, -0.8 - -1j * 0.8]
        op = Operator(paulis=[])
        for coeff, pauli in zip(coeffs, paulis):
            pauli_term = [coeff, Pauli.from_label(pauli)]
            op += Operator(paulis=[pauli_term])

        op1 = copy.deepcopy(op)
        op1.chop(threshold=0.4)
        self.assertEqual(len(op1.paulis), 4, "\n{}".format(op1.print_operators()))
        gt_op1 = Operator(paulis=[])
        for i in range(1, 3):
            pauli_term = [coeffs[i], Pauli.from_label(paulis[i])]
            gt_op1 += Operator(paulis=[pauli_term])
            pauli_term = [coeffs[i+3], Pauli.from_label(paulis[i+3])]
            gt_op1 += Operator(paulis=[pauli_term])
        self.assertEqual(op1, gt_op1)

        op2 = copy.deepcopy(op)
        op2.chop(threshold=0.7)
        self.assertEqual(len(op2.paulis), 2, "\n{}".format(op2.print_operators()))
github Qiskit / qiskit-aqua / test / aqua / test_operator.py View on Github external
pauli_b = 'ZYIX'
        coeff_a = 0.5
        coeff_b = 0.5
        pauli_term_a = [coeff_a, Pauli.from_label(pauli_a)]
        pauli_term_b = [coeff_b, Pauli.from_label(pauli_b)]
        op_a = Operator(paulis=[pauli_term_a])
        op_b = Operator(paulis=[pauli_term_b])
        copy_op_a = copy.deepcopy(op_a)
        new_op = op_a + op_b

        self.assertEqual(copy_op_a, op_a)
        self.assertEqual(2, len(new_op.paulis))

        pauli_c = 'IXYZ'
        coeff_c = 0.25
        pauli_term_c = [coeff_c, Pauli.from_label(pauli_c)]
        new_op = new_op + Operator(paulis=[pauli_term_c])

        self.assertEqual(2, len(new_op.paulis))
        self.assertEqual(0.75, new_op.paulis[0][0])