def test_symmetric_two_term_same(self):
        op = BosonOperator('0^ 0^')
        res = symmetric_ordering(op)
        self.assertTrue(res == op)

        op = QuadOperator('q0 q0')
        res = symmetric_ordering(op)
        self.assertTrue(res == op)
    def test_symmetric_one_term_multimode(self):
        op = BosonOperator('0^ 1^ 2 3')
        res = symmetric_ordering(op)
        self.assertTrue(res == op)

        op = QuadOperator('q0 q1 p2 p3')
        res = symmetric_ordering(op)
        self.assertTrue(res == op)
    def test_symmetric_identity(self):
        for op in (BosonOperator, QuadOperator):
            res = symmetric_ordering(op(''), ignore_identity=False)
            self.assertTrue(res == op().identity())

        for op in (BosonOperator, QuadOperator):
            res = symmetric_ordering(op(''), ignore_identity=True)
            self.assertTrue(res == op().zero())
 def test_symmetric_coefficient(self):
     coeff = 0.5 + 0.6j
     op = QuadOperator('q0 p0', coeff)
     res = symmetric_ordering(op, ignore_coeff=False)
     expected = QuadOperator('q0 p0', 0.5) \
         + QuadOperator('p0 q0', 0.5)
     self.assertTrue(res == coeff * expected)
     self.assertFalse(is_hermitian(res))
    def test_symmetric_non_hermitian_order(self):
        op1 = QuadOperator('q0 p0 q0')
        op2 = QuadOperator('q0 q0 p0')
        op3 = QuadOperator('p0 q0 q0')

        w1 = symmetric_ordering(op1)
        w2 = symmetric_ordering(op2)
        w3 = symmetric_ordering(op3)

        self.assertTrue(is_hermitian(w1))
        self.assertTrue(is_hermitian(w2))
        self.assertTrue(is_hermitian(w3))

        expected = QuadOperator('q0 q0 p0', 0.5) \
            + QuadOperator('p0 q0 q0', 0.5)
        self.assertTrue(w1 == expected)
        self.assertTrue(w2 == expected)
        self.assertTrue(w3 == expected)
    def test_symmetric_non_hermitian(self):
        op = BosonOperator('0^ 0')
        res = symmetric_ordering(op)
        expected = BosonOperator('0^ 0', 0.5) \
            + BosonOperator('0 0^', 0.5)
        self.assertTrue(res == expected)
        self.assertTrue(is_hermitian(res))

        op = BosonOperator('0^ 0', 0.5)
        res = symmetric_ordering(op, ignore_coeff=False)
        expected = BosonOperator('0^ 0', 0.25) \
            + BosonOperator('0 0^', 0.25)
        self.assertTrue(res == expected)
        self.assertTrue(is_hermitian(res))

        op = QuadOperator('q0 p0')
        res = symmetric_ordering(op)
        expected = QuadOperator('q0 p0', 0.5) \
            + QuadOperator('p0 q0', 0.5)
        self.assertTrue(res == expected)
        self.assertTrue(is_hermitian(res))
 def test_symmetric_empty(self):
     for op in (BosonOperator, QuadOperator):
         res = symmetric_ordering(op())
         self.assertTrue(res == op().zero())
 def test_invalid_op(self):
     op = FermionOperator()
     with self.assertRaises(TypeError):
         _ = symmetric_ordering(op)