Example #1
0
    def compile(self,
                root: ProgramLike,
                qasm_formatter,
                optimize: bool = True) -> List[str]:
        root = Program(root)
        rsrc = ResourceAllocator()
        compile_visitor = CompileVisitor(rsrc)
        root.accept(compile_visitor)

        max_used_qubit_id = compile_visitor.get_max_used_qubit_id()
        max_used_bit_id = compile_visitor.get_max_used_bit_id()

        commands: List[ICommand] = compile_visitor.commands

        if optimize:
            commands = QuasarOpt.run(commands, max_used_qubit_id)

        qasm_formatter.set_qubits_counter(max_used_qubit_id)
        qasm_formatter.set_bits_counter(max_used_bit_id)
        qasm_formatter.set_groups([max_used_qubit_id])

        headers = qasm_formatter.get_headers()

        code = self._commands_to_code(
            commands,
            qasm_formatter,
        )

        footers = qasm_formatter.get_footers()

        return headers + code + footers
Example #2
0
 def _test_one_qubit_command_3(self, cmd_class) -> None:
   min_unused_qubit_id = 2
   cmd_1: ICommand = GateCmd(cmd_class, 0)
   cmd_2: ICommand = GateCmd(cmd_class, 1)
   commands: List[ICommand] = [cmd_1,cmd_2]
   actual: List[ICommand] = QuasarOpt().run(commands, min_unused_qubit_id)
   expected: List[ICommand] = [cmd_1, cmd_2]
   self.assertListEqual(actual, expected)
Example #3
0
 def _test_two_qubits_cu3_command_2(self) -> None:
   min_unused_qubit_id = 2
   cmd_1: ICommand = GateCmd(U3_GATE, 1, params=[2.2, 3.3, 4.4], control_qubit_ids={0})
   cmd_2: ICommand = GateCmd(U3_GATE, 1, params=[2.2, 3.3, 4.4], control_qubit_ids={0})
   commands: List[ICommand] = [cmd_1, cmd_2]
   actual: List[ICommand] = QuasarOpt().run(commands, min_unused_qubit_id)
   expected: List[ICommand] = [cmd_1, cmd_2]
   self.assertListEqual(actual, expected)
Example #4
0
 def _test_one_qubit_u3_command_2(self) -> None:
   min_unused_qubit_id = 1
   cmd_1: ICommand = GateCmd(U3_GATE, 0, params=[1.1, 2.2, 3.3])
   cmd_2: ICommand = GateCmd(U3_GATE, 0, params=[1.1, 3.3, 2.2])
   commands: List[ICommand] = [cmd_1, cmd_2]
   actual: List[ICommand] = QuasarOpt().run(commands, min_unused_qubit_id)
   expected: List[ICommand] = [cmd_1, cmd_2]
   self.assertListEqual(actual, expected)
Example #5
0
 def _test_three_qubits_command_5(self, cmd_class) -> None:
   min_unused_qubit_id = 5
   cmd_1: ICommand = GateCmd(cmd_class, 2, control_qubit_ids={0, 1})
   cmd_2: ICommand = GateCmd(cmd_class, 0, control_qubit_ids={2, 1})
   commands: List[ICommand] = [cmd_1, cmd_2]
   actual: List[ICommand] = QuasarOpt().run(commands, min_unused_qubit_id)
   expected: List[ICommand] = [cmd_1, cmd_2]
   self.assertListEqual(actual, expected)
Example #6
0
 def _test_three_qubits_reset_command_3(self, cmd_class_1, cmd_class_2) -> None:
   min_unused_qubit_id = 3
   cmd_1: ICommand = GateCmd(cmd_class_1, 2, control_qubit_ids={0, 1})
   cmd_2: ICommand = cmd_class_2(2)
   cmd_3: ICommand = GateCmd(cmd_class_1, 2, control_qubit_ids={0, 1})
   commands: List[ICommand] = [cmd_1, cmd_2, cmd_3]
   actual: List[ICommand] = QuasarOpt().run(commands, min_unused_qubit_id)
   expected: List[ICommand] = [cmd_1, cmd_2, cmd_3]
   self.assertListEqual(actual, expected)
Example #7
0
 def _test_two_qubits_command_6(self, cmd_class) -> None:
   min_unused_qubit_id = 4
   cmd_1: ICommand = GateCmd(cmd_class, 1, control_qubit_ids={0})
   cmd_2: ICommand = GateCmd(cmd_class, 3, control_qubit_ids={2})
   cmd_3: ICommand = GateCmd(cmd_class, 1, control_qubit_ids={2})
   commands: List[ICommand] = [cmd_1, cmd_2, cmd_3]
   actual: List[ICommand] = QuasarOpt().run(commands, min_unused_qubit_id)
   expected: List[ICommand] = [cmd_1, cmd_2, cmd_3]
   self.assertListEqual(actual, expected)
Example #8
0
 def _test_one_qubit_commands_2(self, cmd_class_1, cmd_class_2) -> None:
   min_unused_qubit_id = 1
   cmd_1: ICommand = GateCmd(cmd_class_1, 0)
   cmd_2: ICommand = GateCmd(cmd_class_2, 0)
   cmd_3: ICommand = GateCmd(cmd_class_2, 0)
   cmd_4: ICommand = GateCmd(cmd_class_1, 0)
   commands: List[ICommand] = [cmd_1, cmd_2, cmd_3, cmd_4]
   actual: List[ICommand] = QuasarOpt().run(commands, min_unused_qubit_id)
   expected: List[ICommand] = []
   self.assertListEqual(actual, expected)
Example #9
0
 def _test_two_qubits_commands_3(self, cmd_class_1, cmd_class_2) -> None:
   min_unused_qubit_id = 2
   cmd_1: ICommand = GateCmd(cmd_class_1, 1, control_qubit_ids={0})
   cmd_2: ICommand = GateCmd(cmd_class_2, 0, control_qubit_ids={1})
   cmd_3: ICommand = GateCmd(cmd_class_2, 1, control_qubit_ids={0})
   cmd_4: ICommand = GateCmd(cmd_class_1, 0, control_qubit_ids={1})
   commands: List[ICommand] = [cmd_1, cmd_2, cmd_3, cmd_4]
   actual: List[ICommand] = QuasarOpt().run(commands, min_unused_qubit_id)
   expected: List[ICommand] = [cmd_1, cmd_2, cmd_3, cmd_4]
   self.assertListEqual(actual, expected)