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:
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:
Rename the __repr__ method in all IR class to either __str__ or serialize()
Renaming it to __str__ allows us to call str(qasm_program) to serialize the program, and avoids us needing to change the logic anywhere --- I'm not sure if this is a good thing or a bad thing.
Renaming it to serialize() allows us to pass arguments later if needed. However, it will need us to update the logic; the returned string will need to manually call serialize on all child objects:
That is, the returned string should only itself contain precomputed strings --- it should not result in any additional serialization occurring, either implicitly or explicitly.
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: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:
Rename the
__repr__
method in all IR class to either__str__
orserialize()
__str__
allows us to callstr(qasm_program)
to serialize the program, and avoids us needing to change the logic anywhere --- I'm not sure if this is a good thing or a bad thing.serialize()
allows us to pass arguments later if needed. However, it will need us to update the logic; the returned string will need to manually call serialize on all child objects:(Note: the above example is pseudocode just for demonstration; quantum registers do not include nested IR objects).
Create new
__repr__
methods, that return reasonable strings and does not do any recursive serialization:That is, the returned string should only itself contain precomputed strings --- it should not result in any additional serialization occurring, either implicitly or explicitly.