co9olguy / qdata

1 stars 3 forks source link

Avoid using `__repr__` for IR serialization implementation #20

Closed josh146 closed 3 years ago

josh146 commented 3 years ago

Currently, the intermediate representation uses class composition; there is a container class QasmProgram that stores the parsed program; individual statements are then represented via a nested list of constituent classes (e.g., ArithmeticOperation, ClassicalRegister, Gate, etc.)

This is a nice approach; it preserves the tree-like structure of the program, and provides a convenient method of serialization; simply traverse the IR, and serialize each constituent class/node.

At the moment, each class in the IR computes its serialization via the __repr__ method:

class QuantumRegister(Declaration):
    def __init__(self, name, size):
        super().__init__(decl_type=Declarations.QREG, id_=name, size=size)

    def __repr__(self):
        return f"qreg {self.kwargs['id_']}[{self.kwargs['size']}];"

This was convenient when prototyping, as calling __repr__ is naturally recursive; any IR object appearing in the string returned by __repr__ will itself be serialized.

However, we should probably update this, as this is not ideal; just doing >>> qasm_program on an interactive prompt should not start the serialization process.

Instead, we should do the following: