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

Circuit drawer reverse_bits does not reverse registerless bits #7303

Closed
enavarro51 opened this issue Nov 23, 2021 · 3 comments · Fixed by #7319
Closed

Circuit drawer reverse_bits does not reverse registerless bits #7303

enavarro51 opened this issue Nov 23, 2021 · 3 comments · Fixed by #7319
Assignees
Labels
bug Something isn't working

Comments

@enavarro51
Copy link
Contributor

Environment

  • Qiskit Terra version: Current main
  • Python version: 3.8
  • Operating system: Ubuntu

What is happening?

When using classical registers only with reverse_bits, the registers and the bits in the registers are reversed before display. However, bits without registers are reversed in terms of usage, but are not reversed when displayed.

bits = [Qubit(), Qubit(), Clbit(), Clbit()]
bits1 = [Clbit()]
cr = ClassicalRegister(2, "cr")
crx = ClassicalRegister(2, "crx")
qc = QuantumCircuit(bits, cr, [Clbit()], crx, bits1)
qc.x(0).c_if(crx[1], 0)
qc.measure(0, bits[3])
qc.draw('mpl', cregbundle=False, reverse_bits=True)

image

How can we reproduce the issue?


What should happen?

Registerless bits should show in descending order.
image

Any suggestions?

This will require the merge of #7285 before the above circuit will work.

@javabster Can you assign this to me?

@enavarro51 enavarro51 added the bug Something isn't working label Nov 23, 2021
@jakelishman
Copy link
Member

Assigned, thanks!

@kdk
Copy link
Member

kdk commented Nov 29, 2021

I'm not very familiar with the visualization code, but is there benefit to having reverse_bits implemented separately between circuit_drawer(reverse_bits=True) and QuantumCircuit.reverse_bits (I think the former predated the latter)?

@enavarro51
Copy link
Contributor Author

Oh, yeah. This looks problematic.

qc = QuantumCircuit(3,3)
qc.x(0).c_if(0, 1)
qc = qc.reverse_bits()
qc.draw('mpl', cregbundle=False)

image
or

        bits = [Qubit(), Qubit(), Clbit(), Clbit()]
        cr = ClassicalRegister(2, "cr")
        crx = ClassicalRegister(3, "cs")
        circuit = QuantumCircuit(bits, cr, [Clbit()], crx)
        circuit.x(0).c_if(crx[1], True)
        circuit.measure(0, cr[1])
        circuit = circuit.reverse_bits()
        circuit.draw('mpl', cregbundle=False)

IndexError Traceback (most recent call last)
in
5 circuit.x(0).c_if(crx[1], True)
6 circuit.measure(0, cr[1])
----> 7 circuit = circuit.reverse_bits()
8 circuit.draw('mpl', cregbundle=False)#, reverse_bits=False)

~/qiskit/qiskit-terra/qiskit/circuit/quantumcircuit.py in reverse_bits(self)
482
483 for inst, qargs, cargs in self.data:
--> 484 new_qargs = [new_qubits[num_qubits - old_qubits.index(q) - 1] for q in qargs]
485 new_cargs = [new_clbits[num_clbits - old_clbits.index(c) - 1] for c in cargs]
486 circ._append(inst, new_qargs, new_cargs)

~/qiskit/qiskit-terra/qiskit/circuit/quantumcircuit.py in (.0)
482
483 for inst, qargs, cargs in self.data:
--> 484 new_qargs = [new_qubits[num_qubits - old_qubits.index(q) - 1] for q in qargs]
485 new_cargs = [new_clbits[num_clbits - old_clbits.index(c) - 1] for c in cargs]
486 circ._append(inst, new_qargs, new_cargs)

IndexError: list index out of range

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