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

QuantumCircuit.draw fails on a condition with a registerless Clbit #7248

Closed
jakelishman opened this issue Nov 10, 2021 · 2 comments · Fixed by #7285
Closed

QuantumCircuit.draw fails on a condition with a registerless Clbit #7248

jakelishman opened this issue Nov 10, 2021 · 2 comments · Fixed by #7285
Assignees
Labels
bug Something isn't working

Comments

@jakelishman
Copy link
Member

Information

  • Qiskit Terra version: main
  • Python version: any
  • Operating system: any

What is the current behavior?

The circuit drawers (certainly the text one, prob fail if there is a condition on a Clbit that is not in any registers.

Steps to reproduce the problem

In [1]: from qiskit.circuit import QuantumCircuit, Qubit, Clbit, ClassicalRegister
   ...:
   ...: bits = [Qubit(), Clbit()]
   ...: qc = QuantumCircuit(bits, ClassicalRegister(1))
   ...: qc.x(0).c_if(bits[1], 0)
   ...: qc.draw()
Out[1]: ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/.miniconda3/envs/qiskit/lib/python3.9/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/.miniconda3/envs/qiskit/lib/python3.9/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    392                         if cls is not object \
    393                                 and callable(cls.__dict__.get('__repr__')):
--> 394                             return _repr_pprint(obj, self, cycle)
    395
    396             return _default_pprint(obj, self, cycle)

~/.miniconda3/envs/qiskit/lib/python3.9/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    698     """A pprint that just redirects to the normal repr function."""
    699     # Find newlines and replace them with p.break_()
--> 700     output = repr(obj)
    701     lines = output.splitlines()
    702     with p.group():

~/code/qiskit/terra/qiskit/visualization/text.py in __repr__(self)
    650
    651     def __repr__(self):
--> 652         return self.single_string()
    653
    654     def single_string(self):

~/code/qiskit/terra/qiskit/visualization/text.py in single_string(self)
    658         """
    659         try:
--> 660             return "\n".join(self.lines()).encode(self.encoding).decode(self.encoding)
    661         except (UnicodeEncodeError, UnicodeDecodeError):
    662             warn(

~/code/qiskit/terra/qiskit/visualization/text.py in lines(self, line_length)
    702
    703         try:
--> 704             layers = self.build_layers()
    705         except TextDrawerCregBundle:
    706             self.cregbundle = False

~/code/qiskit/terra/qiskit/visualization/text.py in build_layers(self)
   1132
   1133             for node in node_layer:
-> 1134                 layer, current_connections, connection_label = self._node_to_gate(node, layer)
   1135
   1136                 layer.connections.append((connection_label, current_connections))

~/code/qiskit/terra/qiskit/visualization/text.py in _node_to_gate(self, node, layer)
   1003             # conditional
   1004             op_cond = op.condition
-> 1005             layer.set_cl_multibox(op_cond[0], op_cond[1], top_connect="╨")
   1006             conditional = True
   1007

~/code/qiskit/terra/qiskit/visualization/text.py in set_cl_multibox(self, creg, val, top_connect)
   1343                 bit_index = self._clbit_locations[creg]["index"]
   1344                 label_bool = "= T" if val is True else "= F"
-> 1345                 label = f"{bit_reg.name}_{bit_index} {label_bool}"
   1346                 self.set_clbit(creg, BoxOnClWire(label=label, top_connect=top_connect))
   1347             else:

AttributeError: 'NoneType' object has no attribute 'name'

This happens with output="mpl" and output="latex" as well.

What is the expected behavior?

No error.

Suggested solutions

This is related to a previous issue (#6475), which included a few PRs to the drawers (#6261, #6248, #6259) to add single-bit classical conditioning, but perhaps this was only considering the case of single-qubit conditioning, whereas this is one step further: registerless single-qubit conditioning.

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

@jakelishman I can take a look at this for all 3 drawers. Just doing a quick change on 'mpl' I got this. Not sure what a label should look like for this, but I guess _0 is reasonable.
image

@jakelishman
Copy link
Member Author

Honestly, I have no opinion on the drawers, and I'm generally prepared to go with whatever label you suggest seems sensible. I guess as long as it in some way matches the wire's label on the very left edge I'm happy. Thanks for taking it!

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.

2 participants