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

qc.draw('text', idle_wires=False) results in ValueError : min() arg is an empty sequence #13128

Closed
sumit-kale opened this issue Sep 11, 2024 · 3 comments · Fixed by #13149
Closed
Labels
bug Something isn't working mod: visualization qiskit.visualization
Milestone

Comments

@sumit-kale
Copy link
Contributor

Environment

  • Qiskit version: 1.2.0
  • Python version: 3.10.14
  • Operating system: MacOS (M1)

What is happening?

For a parametrised quantum circuit qc.draw('text', idle_wires=False) results in ValueError: min() arg is an empty sequence
This issue only comes in Qiskit version: 1.2.0 and the same code works fine in Qiskit version: 1.1.0.

Additionally qc.draw('mpl', idle_wires=False) prints the quantum circuit but the number of qubits gets reduced. We haven't trinspiled the circuit yet and didn't mention anything about qubit reuse.

This is the full traceback!

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File /opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/IPython/core/formatters.py:347, in BaseFormatter.__call__(self, obj)
    [345](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/IPython/core/formatters.py:345)     method = get_real_method(obj, self.print_method)
    [346](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/IPython/core/formatters.py:346)     if method is not None:
--> [347](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/IPython/core/formatters.py:347)         return method()
    [348](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/IPython/core/formatters.py:348)     return None
    [349](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/IPython/core/formatters.py:349) else:

File /opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:762, in TextDrawing._repr_html_(self)
    [755](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:755) def _repr_html_(self):
    [756](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:756)     return (
    [757](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:757)         '<pre style="word-wrap: normal;'
    [758](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:758)         "white-space: pre;"
    [759](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:759)         "background: #fff0;"
    [760](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:760)         "line-height: 1.1;"
    [761](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:761)         'font-family: &quot;Courier New&quot;,Courier,monospace">'
--> [762](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:762)         f"{self.single_string()}</pre>"
    [763](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:763)     )

File /opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:779, in TextDrawing.single_string(self)
    [776](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:776)     return self._single_string
    [777](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:777) try:
    [778](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:778)     self._single_string = (
--> [779](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:779)         "\n".join(self.lines()).encode(self.encoding).decode(self.encoding)
...
-> [1079](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:1079)     if min(qubit_indices) > wire_map[ctrl_qubit[0]]:
   [1080](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:1080)         top_box.append(ctrl_qubit)
   [1081](https://file+.vscode-resource.vscode-cdn.net/opt/miniconda3/envs/qiskitenv/lib/python3.10/site-packages/qiskit/visualization/circuit/text.py:1081)     elif max(qubit_indices) < wire_map[ctrl_qubit[0]]:

ValueError: min() arg is an empty sequence

How can we reproduce the issue?

# Minimal code

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit.circuit import ParameterVector
import numpy as np

nx=2

thetas = ParameterVector('θ',length=1)
phis = ParameterVector('φ',length=nx+1)  

qr = QuantumRegister(nx+1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(qr,cr)

for i in range(nx):
    qc.ry(thetas[0], i)
qc.barrier()

for i in range(nx):
    j=nx-i # start from the highest power
    qc.ry(phis[j], j)
    qc.rz(np.pi/2,j-1)
    qc.cx(j-1,j)
            
qc.barrier()
qc.ry(phis[0], 0)  
qc.measure(0, 0)

print(qc.draw('text',idle_wires=False))
print(qc.draw('mpl',idle_wires=False))

What should happen?

The user should be able to print the quantum circuit without getting the ValueError and the circuit with 'mpl' should not reduce the number of qubits.

Any suggestions?

No response

@sumit-kale sumit-kale added the bug Something isn't working label Sep 11, 2024
@t-imamichi
Copy link
Member

t-imamichi commented Sep 13, 2024

It has been fixed by the main branch already. But the fix is not ported to the latest release 1.2.1, unfortunately.

# main branch

      ┌──────────┐ ░ ┌─────────┐                        ░ ┌──────────┐┌─┐
q0_0: ┤ Ry(θ[0]) ├─░─┤ Rz(π/2) ├────────────────────■───░─┤ Ry(φ[0]) ├┤M├
      ├──────────┤ ░ ├─────────┤      ┌──────────┐┌─┴─┐ ░ └──────────┘└╥┘
q0_1: ┤ Ry(θ[0]) ├─░─┤ Rz(π/2) ├───■──┤ Ry(φ[1]) ├┤ X ├─░──────────────╫─
      └──────────┘ ░ ├─────────┴┐┌─┴─┐└──────────┘└───┘ ░              ║
q0_2: ─────────────░─┤ Ry(φ[2]) ├┤ X ├──────────────────░──────────────╫─
                   ░ └──────────┘└───┘                  ░              ║
c0: 1/═════════════════════════════════════════════════════════════════╩═
                                                                       0
# qiskit 1.2.1

Traceback (most recent call last):
  File "/Users/ima/tasks/4_2024/qiskit/terra/tmp/13128.py", line 30, in <module>
    print(qc.draw('text',idle_wires=False))
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/visualization/circuit/text.py", line 753, in __str__
    return self.single_string()
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/visualization/circuit/text.py", line 779, in single_string
    "\n".join(self.lines()).encode(self.encoding).decode(self.encoding)
              ^^^^^^^^^^^^
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/visualization/circuit/text.py", line 827, in lines
    layers = self.build_layers()
             ^^^^^^^^^^^^^^^^^^^
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/visualization/circuit/text.py", line 1287, in build_layers
    layer, current_cons, current_cons_cond, connection_label = self._node_to_gate(
                                                               ^^^^^^^^^^^^^^^^^^^
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/visualization/circuit/text.py", line 1181, in _node_to_gate
    controls_array = TextDrawing.controlled_wires(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ima/envs/qiskit312/lib/python3.12/site-packages/qiskit/visualization/circuit/text.py", line 1079, in controlled_wires
    if min(qubit_indices) > wire_map[ctrl_qubit[0]]:
       ^^^^^^^^^^^^^^^^^^
ValueError: min() iterable argument is empty

@t-imamichi t-imamichi added the mod: visualization qiskit.visualization label Sep 13, 2024
@Cryoris
Copy link
Contributor

Cryoris commented Sep 13, 2024

With @mrossinek we had quite the bug hunt for this:

After some surgical insertions of print statements it turns out that the DAGCircuit.layers method is the culprit, from what we understood because it adds nodes to a layer via reference instead of a copy, as before.

@mrvee-qC-bee
Copy link
Contributor

mrvee-qC-bee commented Sep 16, 2024

@kifumi -san for your awareness!

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

Successfully merging a pull request may close this issue.

6 participants