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

Simulator can run indefinately when running circuits with conditionals #2230

Closed
IlanIwumbwe opened this issue Sep 13, 2024 · 4 comments · Fixed by #2243
Closed

Simulator can run indefinately when running circuits with conditionals #2230

IlanIwumbwe opened this issue Sep 13, 2024 · 4 comments · Fixed by #2243
Assignees
Labels
bug Something isn't working

Comments

@IlanIwumbwe
Copy link

Benny and I found this bug.

Informations

  • Qiskit Aer version: 0.15.0
  • Python version: 3.11.6
  • Operating system: Ubuntu 23.01 LTS

What is the current behavior?

Runs indefinately

Steps to reproduce the problem

Run this code:

from qiskit import QuantumCircuit, ClassicalRegister
from helpers.qiskit_helpers import compare_statevectors, run_on_simulator, run_routing_simulation

subcirc1 = QuantumCircuit(0)
# subcirc1.x(0)

main_circ = QuantumCircuit(1)
# Adding creg resources 
creg_0 = ClassicalRegister(1)
main_circ.add_register(creg_0)


main_circ.measure(0, creg_0[0])
main_circ.x(0)
with main_circ.if_test((creg_0[0],0)) as else_1:
    main_circ.append(subcirc1)
with else_1:
    main_circ.append(subcirc1)

main_circ.measure_active()
print(main_circ)
run_on_simulator(main_circ, 1)

Making the subroutine non-empty makes it work

from qiskit import QuantumCircuit, ClassicalRegister
from helpers.qiskit_helpers import compare_statevectors, run_on_simulator, run_routing_simulation

subcirc1 = QuantumCircuit(1)
subcirc1.x(0)

main_circ = QuantumCircuit(1)
# Adding creg resources 
creg_0 = ClassicalRegister(1)
main_circ.add_register(creg_0)


main_circ.measure(0, creg_0[0])
main_circ.x(0)
with main_circ.if_test((creg_0[0],0)) as else_1:
    main_circ.append(subcirc1, [0])
with else_1:
    main_circ.append(subcirc1, [0])

main_circ.measure_active()
print(main_circ)
run_on_simulator(main_circ, 1)

Moving the x gate before the measure makes it work

from qiskit import QuantumCircuit, ClassicalRegister
from helpers.qiskit_helpers import compare_statevectors, run_on_simulator, run_routing_simulation

subcirc1 = QuantumCircuit(0)

main_circ = QuantumCircuit(1)
# Adding creg resources 
creg_0 = ClassicalRegister(1)
main_circ.add_register(creg_0)

main_circ.x(0)
main_circ.measure(0, creg_0[0])

with main_circ.if_test((creg_0[0],0)) as else_1:
    main_circ.append(subcirc1)
with else_1:
    main_circ.append(subcirc1)

main_circ.measure_active()
print(main_circ)
run_on_simulator(main_circ, 1)

What is the expected behavior?

Should return results, in the case of empty subroutine, should always treat it as identity

Suggested solutions

@IlanIwumbwe IlanIwumbwe added the bug Something isn't working label Sep 13, 2024
@IlanIwumbwe IlanIwumbwe changed the title Simulator can run indefinately when running circuits with conditions Simulator can run indefinately when running circuits with conditionals Sep 13, 2024
@hhorii hhorii self-assigned this Sep 17, 2024
@gadial gadial self-assigned this Sep 30, 2024
@gadial
Copy link
Collaborator

gadial commented Oct 6, 2024

I was unable to run your code (I'm missing your qiskit helpers and getting qiskit_aer.aererror.AerError: 'unknown instruction: circuit-166' when trying to replicate on my own) but I did manage to reproduce the infinite loop with

from qiskit import ClassicalRegister
backend = self.backend(method="statevector", device="CPU")
backend.set_options(max_parallel_experiments=0)
main_circ = QuantumCircuit(1)
creg_0 = ClassicalRegister(1)
main_circ.add_register(creg_0)
main_circ.measure(0, creg_0[0])
main_circ.x(0)
with main_circ.if_test((creg_0[0],0)) as else_1:
    pass
with else_1:
    pass

main_circ.measure_active()
result = backend.run(main_circ, shots=1).result()
print(result)

It seems to me that it's not an Aer problem but a transpiler problem. Aer attempts to run the transpiler, and it messes up the circuit, moving jump instructions to the beginning. This results from the conversion to DAG and back, not anything Aer-specific. I've forwarded the issue to the Qiskit crew.

@jakelishman
Copy link
Member

Gadi: the "jump" instructions are inserted by Aer itself as part of how it makes the tree-like structure of control-flow into a sequence of instructions. If the jumps and marks are getting set wrong, that'll be happening within Aer.

@gadial
Copy link
Collaborator

gadial commented Oct 6, 2024

Thanks Jake. It's now clearer to me what's going on: Aer generates the jump command without assigning it to any qubit (which does not happen in the case of non-empty branches). This leads to the reordering of the instructions when converting back from DAG to circuit. So it's indeed a problem with the way Aer uses the transpiler.

@IlanIwumbwe
Copy link
Author

I was unable to run your code

Sorry about that, that was my mistake. I forgot to add the helper functions we were using into the issue.

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.

4 participants