Skip to content

Invalid Characters in Symbols for Saving and Loading Circuits #2067

@KripaPanchagnula-Quantinuum

Description

When trying to save and load symbolic circuits with

with open("circ.json", "w") as f:
    json.dump(circuit, f)

with open("circ.json", "r") as f:
   c = Circuit.from_dict(f)

or the pickle equivalent, if the circuit symbols contain certain non-alphanumeric characters this errors with

RuntimeError: syntax error

Some protected characters include + and -, which make sense as they may be interpreted as mathematical operations. However, {}() are also not allowed, but the curly braces are necessary for TeX rendering of the symbols, if superscripts and subscripts contain more than 1 character.

For example, from InQuanto ansatze, FermionSpaceAnsatztUPS makes extensive use of curly braces and parentheses in the symbols as they are chemically meaningful, and when the circuit is used within an algorithm and protocol it's perfectly happy to do its thing, but for saving/loading it is not happy. Contrast with FermionSpaceAnsatzUCCSD whose symbols are names s{n} and d{m} by enumerating through the single and double excitations but this circuit can be saved and loaded.

Do we know a priori what non-alphanumeric characters are protected so that they can be avoided? Although I would be sad to not allow () if that happens so be it, but I think if we support TeX rendering of symbols we should try to allow {}.

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