Skip to content

QPY invalid payload generation via compose() or QuantumCircuit.control() #9746

@mtreinish

Description

@mtreinish

Environment

  • Qiskit Terra version: 0.23.2
  • Python version: 3.10
  • Operating system: Linux

What is happening?

Running compose() with inplace=True from an input generated with QuantumCircuit.control() leads to a circuit that when qpy serialized is not load-able. This points to an internal state that doesn't match the actual data of the instruction object. I expect the mismatch is caused by the number of arguments or qubits the gate reported in qc which is incorrect

The specific failure in this case is:

Traceback (most recent call last):
  File "/tmp/test_qpy_roundtrip.py", line 17, in <module>
    new_qc = load(fd)[0]
  File "/tmp/foo/lib/python3.10/site-packages/qiskit/qpy/interface.py", line 269, in load
    loader(
  File "/tmp/foo/lib/python3.10/site-packages/qiskit/qpy/binary_io/circuits.py", line 905, in read_circuit
    _read_instruction(file_obj, circ, out_registers, custom_operations, version, vectors)
  File "/tmp/foo/lib/python3.10/site-packages/qiskit/qpy/binary_io/circuits.py", line 161, in _read_instruction
    struct.unpack(
struct.error: unpack requires a buffer of 33 bytes

How can we reproduce the issue?

import io

from qiskit.circuit.random import random_circuit
import numpy as np
from qiskit import QuantumCircuit
from qiskit.qpy import dump, load

qc0 = random_circuit(2, 2, seed=1).decompose(reps=1)
qc1 = random_circuit(2, 2, seed=1).decompose(reps=1)
qc = QuantumCircuit(3)
qc.compose(qc0.control(1), [0,1,2], inplace=True)
qc.compose(qc1.control(1), [0,1,2], inplace=True)

with io.BytesIO() as fd:
    dump(qc, fd)
    fd.seek(0)
    new_qc = load(fd)[0]

assert qc == new_qc

What should happen?

This should not error during the load() call

Any suggestions?

I believe something about the compose call is corrupting the internal state of the circuit which is leading to a QPY payload that has a mismatch between a size

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingmod: qpyRelated to QPY serializationpriority: high

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions