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

KeyError: 'cr' when loading a circuit containing c_if using qpy #11988

Closed
jiannanWang opened this issue Mar 11, 2024 · 3 comments
Closed

KeyError: 'cr' when loading a circuit containing c_if using qpy #11988

jiannanWang opened this issue Mar 11, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@jiannanWang
Copy link

Environment

  • Qiskit version: 1.0.2
  • Python version: 3.11.8
  • Operating system: Ubuntu

What is happening?

Running the below circuit resulted in a key error. I would expect it to run without error.

How can we reproduce the issue?

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.circuit.library.standard_gates import RZXGate

qr = QuantumRegister(2, name='qr')
cr = ClassicalRegister(2, name='cr')
qc = QuantumCircuit(qr, cr, name='qc')
qc.append(RZXGate(0).c_if(cr, 0).repeat(1), qargs=qr, cargs=qc)

from qiskit import qpy
with open('circuit.qpy', 'wb') as fd:
    qpy.dump(qc, fd)
with open('circuit.qpy', 'rb') as fd:
    qc = qpy.load(fd)[0]

What should happen?

Traceback (most recent call last):
  File "/mnt/tests/reproduction/reproduce_cr_1003.py", line 13, in <module>
    qc = qpy.load(fd)[0]
         ^^^^^^^^^^^^
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/qpy/interface.py", line 333, in load
    loader(
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/qpy/binary_io/circuits.py", line 1263, in read_circuit
    custom_operations = _read_custom_operations(file_obj, version, vectors)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/qpy/binary_io/circuits.py", line 516, in _read_custom_operations
    definition_circuit = common.data_from_binary(
                         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/qpy/common.py", line 262, in data_from_binary
    obj = deserializer(container, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/qpy/binary_io/circuits.py", line 1265, in read_circuit
    _read_instruction(
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/qpy/binary_io/circuits.py", line 216, in _read_instruction
    _loads_register_param(condition_register, circuit, registers),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/anaconda3/envs/qiskit1.0.2/lib/python3.11/site-packages/qiskit/qpy/binary_io/circuits.py", line 185, in _loads_register_param
    return registers["c"][data_bytes]
           ~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'cr'

Any suggestions?

No response

@jiannanWang jiannanWang added the bug Something isn't working label Mar 11, 2024
@jakelishman
Copy link
Member

Fixed by #11940.

@jakelishman
Copy link
Member

Sorry, that last comment was a bit short. The problem you're seeing should have been fixed by #11940, due to be released in Qiskit 1.1.0 in May - I didn't want to backport it, because it's a bit too much of a change to an existing method for me to feel safe backporting it to stable patch release - but you can install from the main branch if you need it sooner.

Thanks for the report, and please feel free to re-open this if there's more problems I missed.

@jiannanWang
Copy link
Author

Thank you for your quick response! Yeah I will try Qiskit 1.1.0 in May and see if it got fixed.

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

No branches or pull requests

2 participants