Skip to content

dumps() prints the original program when unroll() leaves at most one statement #493

Open

Description

@ryanhill1

dumps() (through QasmModule.__str__) prints the unrolled AST only when it has more than one statement (if len(self._unrolled_ast.statements) > 1, modules/base.py). A program that unrolls down to just its register declaration therefore comes back as the original source, custom gate definitions and all.

from pyqasm import loads, dumps
m = loads("OPENQASM 3.0;\ngate g a { }\nqubit[1] q;\ng q[0];")
m.unroll()
[type(s).__name__ for s in m.unrolled_ast.statements]  # ['QubitDeclaration']
dumps(m)  # 'OPENQASM 3.0;\ngate g a {\n}\nqubit[1] q;\ng q[0];\n'

The caller asked for the unrolled program and gets output that still has gate definitions and calls to them. With #459, ctrl @ x on a custom identity x hits the same thing. A tool that unrolls first so that every custom gate is gone, and then imports the result into Qiskit, gets the custom x back, and Qiskit's ctrl @ turns it into a real CX.

__str__ could track whether unroll() has run instead of counting statements.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions