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: Clbit(ClassicalRegister(2, 'cr'), 1) during transpile of a conditional circuit #11935

Closed
jiannanWang opened this issue Mar 1, 2024 · 2 comments · Fixed by #11940
Closed
Labels
bug Something isn't working

Comments

@jiannanWang
Copy link

Environment

  • Qiskit version: 1.0.1
  • Python version: 3.10.13
  • Operating system: Ubuntu 22.04.3 LTS

What is happening?

Running below code results in the exception copied in the "What should happen" section. I would expect the code to run without error.

How can we reproduce the issue?

import qiskit
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, transpile
from qiskit.circuit.library.standard_gates import CXGate
from qiskit.providers.basic_provider import BasicSimulator


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

backend = BasicSimulator()
qc = transpile(qc, backend)

What should happen?

Traceback (most recent call last):
  File "/mnt/tests/reproduction/reproduce_KeyError:Clbit(ClassicalRegister(2,'cr'),1).py", line 13, in <module>
    qc = transpile(qc, backend)
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/compiler/transpiler.py", line 425, in transpile
    out_circuits = pm.run(circuits, callback=callback)
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/transpiler/passmanager.py", line 397, in run
    return super().run(circuits, output_name, callback, num_processes=num_processes)
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/transpiler/passmanager.py", line 420, in wrapper
    return meth(*meth_args, **meth_kwargs)
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/transpiler/passmanager.py", line 182, in run
    return super().run(
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/passmanager/passmanager.py", line 229, in run
    out_program = _run_workflow(
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/passmanager/passmanager.py", line 292, in _run_workflow
    passmanager_ir, final_state = flow_controller.execute(
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/passmanager/base_tasks.py", line 218, in execute
    passmanager_ir, state = next_task.execute(
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/transpiler/basepasses.py", line 195, in execute
    new_dag, state = super().execute(
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/passmanager/base_tasks.py", line 98, in execute
    ret = self.run(passmanager_ir)
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/transpiler/passes/synthesis/high_level_synthesis.py", line 379, in run
    decomposition, modified = self._recursively_handle_op(node.op, qubits)
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/transpiler/passes/synthesis/high_level_synthesis.py", line 467, in _recursively_handle_op
    dag = circuit_to_dag(definition, copy_operations=False)
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/converters/circuit_to_dag.py", line 92, in circuit_to_dag
    dagcircuit.apply_operation_back(op, instruction.qubits, instruction.clbits, check=False)
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/dagcircuit/dagcircuit.py", line 682, in apply_operation_back
    [self.output_map[bit]._node_id for bits in (qargs, all_cbits) for bit in bits],
  File "/root/anaconda3/envs/qiskit1.0.1/lib/python3.10/site-packages/qiskit/dagcircuit/dagcircuit.py", line 682, in <listcomp>
    [self.output_map[bit]._node_id for bits in (qargs, all_cbits) for bit in bits],
KeyError: Clbit(ClassicalRegister(2, 'cr'), 0)

Any suggestions?

No response

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

1ucian0 commented Mar 2, 2024

I confirmed this is indeed a bug. The problem is the conditional in combination with the repeat, indeed.

@jakelishman
Copy link
Member

I've put up #11940 which is one way of fixing this, but it's not 100% clear to me that the route I went with #11940 is fully acceptable.

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

Successfully merging a pull request may close this issue.

3 participants