Skip to content

QPY load error with backslash in parameter expression #14467

@t-imamichi

Description

@t-imamichi

Environment

  • Qiskit version: main c61d797
  • Python version: 3.12.10
  • Operating system: macOS 15.5

What is happening?

When the main branch of Qiskit attempts to load a QPY file generated by Qiskit 1.2 that contains a parameter expression with a backslash \, it raises an error.

How can we reproduce the issue?

# qpy_dump.py
from qiskit import QuantumCircuit, __version__
from qiskit.circuit import Parameter
from qiskit.qpy import dump

print(f"qiskit_version={__version__}")
qc = QuantumCircuit(2)
alpha = Parameter(r"\alpha")
beta = Parameter(r"\beta")
qc.rz(alpha + beta, 0)
print(qc.draw("text", idle_wires=False))
with open("tmp.qpy", "wb") as file:
    dump(qc, file)
# qpy_load.py
from qiskit import __version__
from qiskit.qpy import load

print(f"qiskit_version={__version__}")
with open("tmp.qpy", "rb") as file:
    qc = load(file)[0]
print(qc.draw("text", idle_wires=False))
$ activate qiskit-1.2
$ python qpy_dump.py
qiskit_version=1.2.4
     ┌────────────────────┐
q_0: ┤ Rz(\alpha + \beta) ├
     └────────────────────┘

$ # OK
$ activate qiskit-2
$ python qpy_load.py
qiskit_version=2.0.1
     ┌────────────────────┐
q_0: ┤ Rz(\alpha + \beta) ├
     └────────────────────┘

$ # NG
$ activate qiskit-main
$ python qpy_load.py
qiskit_version=2.1.0.dev0+c61d797
Traceback (most recent call last):
  File "/Users/ima/tasks/5_2025/scripts/2025/terra/qpy_load.py", line 6, in <module>
    qc = load(file)[0]
         ^^^^^^^^^^
  File "/Users/ima/tasks/5_2025/qiskit/terra/qiskit/qpy/interface.py", line 337, in load
    loader(
  File "/Users/ima/tasks/5_2025/qiskit/terra/qiskit/qpy/binary_io/circuits.py", line 1425, in read_circuit
    _read_instruction(
  File "/Users/ima/tasks/5_2025/qiskit/terra/qiskit/qpy/binary_io/circuits.py", line 297, in _read_instruction
    param = _loads_instruction_parameter(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ima/tasks/5_2025/qiskit/terra/qiskit/qpy/binary_io/circuits.py", line 201, in _loads_instruction_parameter
    param = value.loads_value(
            ^^^^^^^^^^^^^^^^^^
  File "/Users/ima/tasks/5_2025/qiskit/terra/qiskit/qpy/binary_io/value.py", line 1102, in loads_value
    return common.data_from_binary(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ima/tasks/5_2025/qiskit/terra/qiskit/qpy/common.py", line 264, in data_from_binary
    obj = deserializer(container, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ima/tasks/5_2025/qiskit/terra/qiskit/qpy/binary_io/value.py", line 557, in _read_parameter_expression_v3
    return ParameterExpression(symbol_map, str(expr_))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ima/tasks/5_2025/qiskit/terra/qiskit/circuit/parameterexpression.py", line 138, in __init__
    self._symbol_expr = SymbolExpr.Expression(expr)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: 0: at line 1:
\alpha + \beta
^
expected '(', found \

1: at line 1, in Alt:
\alpha + \beta
^

2: at line 1, in Permutation:
\alpha + \beta
^

3: at line 1, in Permutation:
\alpha + \beta
^

4: at line 1, in Permutation:
\alpha + \beta
^

What should happen?

No error as same as 2.0.1

Any suggestions?

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingmod: qpyRelated to QPY serialization

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions