def test_sub(self):
        """ sub test """
        pauli_a = 'IXYZ'
        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 = WeightedPauliOperator(paulis=[pauli_term_a])
        op_b = WeightedPauliOperator(paulis=[pauli_term_b])
        ori_op_a = op_a.copy()
        ori_op_b = op_b.copy()
        new_op = op_a - op_b

        self.assertEqual(op_a, ori_op_a)
        self.assertEqual(op_b, ori_op_b)
        self.assertEqual(1, len(op_a.paulis))
        self.assertEqual(2, len(new_op.paulis))
        self.assertEqual(0.5, new_op.paulis[0][0])
        self.assertEqual(-0.5, new_op.paulis[1][0])

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

        self.assertEqual(2, len(new_op.paulis))
        self.assertEqual(0.25, new_op.paulis[0][0])
    def test_iadd(self):
        """ iadd test """
        pauli_a = 'IXYZ'
        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 = WeightedPauliOperator(paulis=[pauli_term_a])
        op_b = WeightedPauliOperator(paulis=[pauli_term_b])
        ori_op_a = op_a.copy()
        ori_op_b = op_b.copy()
        op_a += op_b

        self.assertNotEqual(op_a, ori_op_a)
        self.assertEqual(op_b, ori_op_b)
        self.assertEqual(2, len(op_a.paulis))

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

        self.assertEqual(2, len(op_a.paulis))
        self.assertEqual(0.75, op_a.paulis[0][0])
    def test_add(self):
        """ add test """
        pauli_a = 'IXYZ'
        pauli_b = 'ZYIX'
        coeff_a = 0.5
        coeff_b = 0.5
        pauli_term_a = [coeff_a, Pauli(pauli_a)]
        pauli_term_b = [coeff_b, Pauli(pauli_b)]
        op_a = WeightedPauliOperator(paulis=[pauli_term_a])
        op_b = WeightedPauliOperator(paulis=[pauli_term_b])
        ori_op_a = op_a.copy()
        ori_op_b = op_b.copy()
        new_op = op_a + op_b

        self.assertEqual(op_a, ori_op_a)
        self.assertEqual(op_b, ori_op_b)
        self.assertEqual(1, len(op_a.paulis))
        self.assertEqual(2, len(new_op.paulis))

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

        self.assertEqual(2, len(new_op.paulis))
        self.assertEqual(0.75, new_op.paulis[0][0])
Exemplo n.º 4
0
    def test_isub(self):
        pauli_a = 'IXYZ'
        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 = WeightedPauliOperator(paulis=[pauli_term_a])
        op_b = WeightedPauliOperator(paulis=[pauli_term_b])
        ori_op_a = op_a.copy()
        ori_op_b = op_b.copy()
        op_a -= op_b

        self.assertNotEqual(op_a, ori_op_a)
        self.assertEqual(op_b, ori_op_b)
        self.assertEqual(2, len(op_a.paulis))

        pauli_c = 'IXYZ'
        coeff_c = 0.5
        pauli_term_c = [coeff_c, Pauli.from_label(pauli_c)]
        op_a -= WeightedPauliOperator(paulis=[pauli_term_c])
        # sub does not remove zero weights.
        self.assertEqual(2, len(op_a.paulis))