1#   Licensed under the Apache License, Version 2.0 (the "License");
2#   you may not use this file except in compliance with the License.
3#   You may obtain a copy of the License at
4#
5#       http://www.apache.org/licenses/LICENSE-2.0
6#
7#   Unless required by applicable law or agreed to in writing, software
8#   distributed under the License is distributed on an "AS IS" BASIS,
9#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10#   See the License for the specific language governing permissions and
11#   limitations under the License.
12
13import unittest
14
15from openfermion.ops.operators import BinaryCode, FermionOperator, QubitOperator
16from openfermion.transforms.opconversions.binary_code_transform import \
17    binary_code_transform, dissolve, make_parity_list
18
19
20class CodeTransformTest(unittest.TestCase):
21
22    def test_transform(self):
23        code = BinaryCode([[1, 0, 0], [0, 1, 0]], ['W0', 'W1', '1 + W0 + W1'])
24        hamiltonian = FermionOperator('0^ 2', 0.5) + FermionOperator(
25            '2^ 0', 0.5)
26        transform = binary_code_transform(hamiltonian, code)
27        correct_op = QubitOperator('X0 Z1', 0.25) + QubitOperator('X0', 0.25)
28        self.assertTrue(transform == correct_op)
29
30        with self.assertRaises(TypeError):
31            binary_code_transform('0^ 2', code)
32        with self.assertRaises(TypeError):
33            binary_code_transform(hamiltonian, ([[1, 0], [0, 1]], ['w0', 'w1']))
34
35    def test_dissolve(self):
36        code = BinaryCode([[1, 0, 0], [0, 1, 0]], ['W0', 'W1', '1 + W0 W1'])
37        hamiltonian = FermionOperator('0^ 2', 0.5) + FermionOperator(
38            '2^ 0', 0.5)
39        transform = binary_code_transform(hamiltonian, code)
40
41        correct_op = QubitOperator('X0 Z1', 0.375) + \
42                     QubitOperator('X0', -0.125) + \
43                     QubitOperator('Y0', -0.125j) + \
44                     QubitOperator('Y0 Z1', -0.125j)
45        self.assertTrue(transform == correct_op)
46
47        with self.assertRaises(ValueError):
48            dissolve(((1, '1'),))
49
50    def test_make_parity_list_raises(self):
51        with self.assertRaises(TypeError):
52            make_parity_list('A')