コード例 #1
0
    def test_group_subops(self, is_summed_op):
        """grouper subroutine test"""
        paulis = (I ^ X) + (2 * X ^ X) + (3 * Z ^ Y)
        if is_summed_op:
            paulis = paulis.to_pauli_op()
        grouped_sum = AbelianGrouper.group_subops(paulis)
        self.assertEqual(len(grouped_sum), 2)
        with self.subTest("test group subops 1"):
            if is_summed_op:
                expected = SummedOp(
                    [
                        SummedOp([I ^ X, 2.0 * X ^ X], abelian=True),
                        SummedOp([3.0 * Z ^ Y], abelian=True),
                    ]
                )
                self.assertEqual(grouped_sum, expected)
            else:
                self.assertSetEqual(
                    frozenset([frozenset(grouped_sum[i].primitive.to_list()) for i in range(2)]),
                    frozenset({frozenset({("ZY", 3)}), frozenset({("IX", 1), ("XX", 2)})}),
                )

        paulis = X + (2 * Y) + (3 * Z)
        if is_summed_op:
            paulis = paulis.to_pauli_op()
        grouped_sum = AbelianGrouper.group_subops(paulis)
        self.assertEqual(len(grouped_sum), 3)
        with self.subTest("test group subops 2"):
            if is_summed_op:
                self.assertEqual(grouped_sum, paulis)
            else:
                self.assertSetEqual(
                    frozenset(sum([grouped_sum[i].primitive.to_list() for i in range(3)], [])),
                    frozenset([("X", 1), ("Y", 2), ("Z", 3)]),
                )
コード例 #2
0
 def test_abelian_grouper(self, pauli_op, is_summed_op):
     """Abelian grouper test"""
     if pauli_op == "h2_op":
         paulis = (
             (-1.052373245772859 * I ^ I)
             + (0.39793742484318045 * I ^ Z)
             + (-0.39793742484318045 * Z ^ I)
             + (-0.01128010425623538 * Z ^ Z)
             + (0.18093119978423156 * X ^ X)
         )
         num_groups = 2
     else:
         paulis = (
             (I ^ I ^ X ^ X * 0.2)
             + (Z ^ Z ^ X ^ X * 0.3)
             + (Z ^ Z ^ Z ^ Z * 0.4)
             + (X ^ X ^ Z ^ Z * 0.5)
             + (X ^ X ^ X ^ X * 0.6)
             + (I ^ X ^ X ^ X * 0.7)
         )
         num_groups = 4
     if is_summed_op:
         paulis = paulis.to_pauli_op()
     grouped_sum = AbelianGrouper().convert(paulis)
     self.assertEqual(len(grouped_sum.oplist), num_groups)
     for group in grouped_sum:
         for op_1, op_2 in combinations(group, 2):
             if is_summed_op:
                 self.assertEqual(op_1 @ op_2, op_2 @ op_1)
             else:
                 self.assertTrue(commutator(op_1, op_2).is_zero())
コード例 #3
0
 def test_abelian_grouper(self, pauli_op, is_summed_op):
     """Abelian grouper test"""
     if pauli_op == 'h2_op':
         paulis = (-1.052373245772859 * I ^ I) + \
                  (0.39793742484318045 * I ^ Z) + \
                  (-0.39793742484318045 * Z ^ I) + \
                  (-0.01128010425623538 * Z ^ Z) + \
                  (0.18093119978423156 * X ^ X)
         num_groups = 2
     else:
         paulis = (I ^ I ^ X ^ X * 0.2) + \
                  (Z ^ Z ^ X ^ X * 0.3) + \
                  (Z ^ Z ^ Z ^ Z * 0.4) + \
                  (X ^ X ^ Z ^ Z * 0.5) + \
                  (X ^ X ^ X ^ X * 0.6) + \
                  (I ^ X ^ X ^ X * 0.7)
         num_groups = 4
     if is_summed_op:
         paulis = paulis.to_pauli_op()
     grouped_sum = AbelianGrouper().convert(paulis)
     self.assertEqual(len(grouped_sum.oplist), num_groups)
     for group in grouped_sum:
         for op_1, op_2 in combinations(group, 2):
             if is_summed_op:
                 self.assertTrue(op_1.commutes(op_2))
             else:
                 self.assertTrue(
                     (op_1 @ op_2 -
                      op_1 @ op_2).reduce().primitive.coeffs[0] == 0)
コード例 #4
0
 def test_abelian_grouper_random(self, is_summed_op):
     """Abelian grouper test with random paulis"""
     random.seed(1234)
     k = 10  # size of pauli operators
     n = 100  # number of pauli operators
     num_tests = 20  # number of tests
     for _ in range(num_tests):
         paulis = []
         for _ in range(n):
             pauliop = 1
             for eachop in random.choices([I] * 5 + [X, Y, Z], k=k):
                 pauliop ^= eachop
             paulis.append(pauliop)
         pauli_sum = sum(paulis)
         if is_summed_op:
             pauli_sum = pauli_sum.to_pauli_op()
         grouped_sum = AbelianGrouper().convert(pauli_sum)
         for group in grouped_sum:
             for op_1, op_2 in combinations(group, 2):
                 if is_summed_op:
                     self.assertTrue(op_1.commutes(op_2))
                 else:
                     self.assertTrue(
                         (op_1 @ op_2 -
                          op_1 @ op_2).reduce().primitive.coeffs[0] == 0)
コード例 #5
0
    def test_group_subops(self):
        """grouper subroutine test"""
        paulis = (I ^ X) + (2 * X ^ X) + (3 * Z ^ Y)
        grouped_sum = AbelianGrouper.group_subops(paulis)
        with self.subTest('test group subops 1'):
            self.assertEqual(len(grouped_sum), 2)
            self.assertListEqual([str(op.primitive) for op in grouped_sum[0]],
                                 ['IX', 'XX'])
            self.assertListEqual([op.coeff for op in grouped_sum[0]], [1, 2])
            self.assertListEqual([str(op.primitive) for op in grouped_sum[1]],
                                 ['ZY'])
            self.assertListEqual([op.coeff for op in grouped_sum[1]], [3])

        paulis = X + (2 * Y) + (3 * Z)
        grouped_sum = AbelianGrouper.group_subops(paulis)
        with self.subTest('test group subops 2'):
            self.assertEqual(len(grouped_sum), 3)
            self.assertListEqual([str(op[0].primitive) for op in grouped_sum],
                                 ['X', 'Y', 'Z'])
            self.assertListEqual([op[0].coeff for op in grouped_sum],
                                 [1, 2, 3])
コード例 #6
0
 def test_abelian_grouper_random(self):
     """Abelian grouper test with random paulis"""
     random.seed(1234)
     k = 10  # size of pauli operators
     n = 100  # number of pauli operators
     num_tests = 20  # number of tests
     for _ in range(num_tests):
         paulis = []
         for _ in range(n):
             pauliop = 1
             for eachop in random.choices([I] * 5 + [X, Y, Z], k=k):
                 pauliop ^= eachop
             paulis.append(pauliop)
         grouped_sum = AbelianGrouper().convert(sum(paulis))
         for group in grouped_sum:
             for op_1, op_2 in combinations(group, 2):
                 self.assertTrue(op_1.commutes(op_2))
コード例 #7
0
 def test_ablian_grouper_no_commute(self):
     """Abelian grouper test when non-PauliOp is given"""
     ops = Zero ^ Plus + X ^ Y
     with self.assertRaises(OpflowError):
         _ = AbelianGrouper.group_subops(ops)