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 roundtrip for FourierChecking fails with TypeError: DiagonalGate.__init__() takes 2 positional arguments but 5 were given #10364

Closed
rht opened this issue Jun 30, 2023 · 3 comments · Fixed by #10376
Labels
bug Something isn't working good first issue Good for newcomers
Milestone

Comments

@rht
Copy link
Contributor

rht commented Jun 30, 2023

Environment

  • Qiskit Terra version: 0.24.1
  • Python version: 3.10
  • Operating system: NixOS 23.11

What is happening?

This is likely because the named gate (DiagonalGate) is not explicitly supported in the gates in the following code branch: https://github.com/Qiskit/qiskit-terra/blob/e9f8b7c50968501e019d0cb426676ac606eb5a10/qiskit/qpy/binary_io/circuits.py#L282-L289

How can we reproduce the issue?

import io
from qiskit import qpy
import qiskit.circuit.library as qcl

def encode(qc):
    with io.BytesIO() as f:
        qpy.dump(qc, f)
        # Rewind back to the beginning of the "file".
        f.seek(0)
        serialized = f.read()
    return serialized

def decode(data):
    with io.BytesIO(data) as f:
        circuits = qpy.load(f)
    return circuits[0]

# Works
decode(encode(qcl.QFT(3)))
# Doesn't work
decode(encode(qcl.FourierChecking([1, -1, -1, -1], [1, 1, -1, -1])))

What should happen?

The error message should have said that the DiagonalGate is not supported when it is not covered by the if-else checks in the _read_instruction.

Any suggestions?

No response

@rht rht added the bug Something isn't working label Jun 30, 2023
@rht
Copy link
Contributor Author

rht commented Jun 30, 2023

The traceback:

Traceback (most recent call last):
  File "/qiskit_bug_report.py", line 21, in <module>
    decode(encode(qcl.FourierChecking([1, -1, -1, -1], [1, 1, -1, -1])))
  File "/qiskit_bug_report.py", line 15, in decode
    circuits = qpy.load(f)
  File "/home/rht/code/venv/lib/python3.10/site-packages/qiskit/qpy/interface.py", line 269, in load
    loader(
  File "/home/rht/code/venv/lib/python3.10/site-packages/qiskit/qpy/binary_io/circuits.py", line 932, in read_circuit
    custom_operations = _read_custom_operations(file_obj, version, vectors)
  File "/home/rht/code/venv/lib/python3.10/site-packages/qiskit/qpy/binary_io/circuits.py", line 423, in _read_custom_operations
    definition_circuit = common.data_from_binary(
  File "/home/rht/code/venv/lib/python3.10/site-packages/qiskit/qpy/common.py", line 261, in data_from_binary
    obj = deserializer(container, **kwargs)
  File "/home/rht/code/venv/lib/python3.10/site-packages/qiskit/qpy/binary_io/circuits.py", line 934, in read_circuit
    _read_instruction(file_obj, circ, out_registers, custom_operations, version, vectors)
  File "/home/rht/code/venv/lib/python3.10/site-packages/qiskit/qpy/binary_io/circuits.py", line 289, in _read_instruction
    gate = gate_class(*params)
TypeError: DiagonalGate.__init__() takes 2 positional arguments but 5 were given

@jakelishman
Copy link
Member

Thanks for the report. The fix should be a case of adding the string "diagonal" to the check of line 282 in your code link - DiagonalGate is an old gate that has its constructor and params fields behave in slightly different ways to most Qiskit gates.

@jakelishman jakelishman added the good first issue Good for newcomers label Jul 3, 2023
@Raghav-Bell
Copy link
Contributor

Let me resolve it by adding diagonal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants