Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QPY fails to serialize MCXGrayCode gate #9390

Closed
ElePT opened this issue Jan 19, 2023 · 0 comments · Fixed by #9391
Closed

QPY fails to serialize MCXGrayCode gate #9390

ElePT opened this issue Jan 19, 2023 · 0 comments · Fixed by #9391
Labels
bug Something isn't working mod: qpy Related to QPY serialization

Comments

@ElePT
Copy link
Contributor

ElePT commented Jan 19, 2023

Environment

  • Qiskit Terra version: 0.22.4
  • Python version: 3.9
  • Operating system: macOS Monterey 12.6

What is happening?

When you try to serialize a circuit with an MCXGrayCode gate using qpy, you get TypeError: __init__() missing 1 required positional argument: 'num_ctrl_qubits'. As far as I have seen, other mcx gates do not run into this issue.

I discovered this while trying to run iterative amplitude estimation using the runtime Sampler, which relies on qpy for serialization.

How can we reproduce the issue?

  from qiskit import QuantumCircuit, qpy
  circuit = QuantumCircuit(11)
  circuit.mcx(list(range(0,9)), 10)
  print('gate name:', circuit.data[0][0].name)

  with open('test.qpy', 'wb') as fd:
      qpy.dump(circuit, fd)

  with open('test.qpy', 'rb') as fd:
      new_qc = qpy.load(fd)[0]

This snippet will fail with a similar traceback to:

gate name: mcx_gray
  File "/Users/ept/Downloads/ExamplePEO.py", line 131, in <module>
    new_qc = qpy.load(fd)[0]
  File "/Users/ept/Desktop/qgt/qiskit/qpy/interface.py", line 269, in load
    loader(
  File "/Users/ept/Desktop/qgt/qiskit/qpy/binary_io/circuits.py", line 864, in read_circuit
    _read_instruction(file_obj, circ, out_registers, custom_operations, version, vectors)
  File "/Users/ept/Desktop/qgt/qiskit/qpy/binary_io/circuits.py", line 258, in _read_instruction
    gate = gate_class(*params)
TypeError: __init__() missing 1 required positional argument: 'num_ctrl_qubits'

What should happen?

It should not have failed.

Any suggestions?

I would modify the following line of qpy.binary_io.circuits.py to add "MCXGrayCode". There seem to be other gates that have been already accounted for this same issue, and adding "MCXGrayCode" to the list fixes it:

        if gate_name in {"MCPhaseGate", "MCU1Gate", "MCXGrayCode"}:
            gate = gate_class(*params, instruction.num_ctrl_qubits)

I'm happy to open a bugfix PR, but because I'm not that familiar with qpy, I wanted to document the problem in this issue first.

@ElePT ElePT added bug Something isn't working mod: qpy Related to QPY serialization labels Jan 19, 2023
@mergify mergify bot closed this as completed in #9391 Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod: qpy Related to QPY serialization
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant