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

Handle instructions without label in mpl drawer #4389

Closed
wants to merge 12 commits into from
30 changes: 24 additions & 6 deletions qiskit/visualization/matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,14 +862,20 @@ def _draw_ops(self, verbose=False):
self._barrier(_barriers)
elif op.name == 'initialize':
vec = '[%s]' % param
text_str = getattr(op.op, 'label', None)
if not text_str:
text_str = "|psi>"
self._custom_multiqubit_gate(q_xy, wide=_iswide,
text=op.op.label or "|psi>",
text=text_str,
subtext=vec)
elif op.name == 'unitary':
# TODO(mtreinish): Look into adding the unitary to the
# subtext
text_str = getattr(op.op, 'label', None)
if not text_str:
text_str = "Unitary"
self._custom_multiqubit_gate(q_xy, wide=_iswide,
text=op.op.label or "Unitary")
text=text_str)
elif isinstance(op.op, ControlledGate) and op.name not in [
'ccx', 'cx', 'c3x', 'c4x', 'cy', 'cz', 'ch', 'cu1', 'cu3', 'crz',
'cswap']:
Expand Down Expand Up @@ -1005,14 +1011,20 @@ def _draw_ops(self, verbose=False):

# dcx and iswap gate
elif op.name in ['dcx', 'iswap']:
text_str = getattr(op.op, 'label', None)
if not text_str:
text_str = op.op.name
self._custom_multiqubit_gate(q_xy, c_xy, wide=_iswide,
fc=self._style.dispcol[op.name],
text=op.op.label or op.name)
text=text_str)

# Custom gate
else:
text_str = getattr(op.op, 'label', None)
if not text_str:
text_str = op.op.name
self._custom_multiqubit_gate(q_xy, c_xy, wide=_iswide,
text=op.op.label or op.name)
text=text_str)
#
# draw multi-qubit gates (n=3)
#
Expand Down Expand Up @@ -1043,13 +1055,19 @@ def _draw_ops(self, verbose=False):
self._line(qreg_b, qreg_t, lc=self._style.dispcol['multi'])
# custom gate
else:
text_str = getattr(op.op, 'label', None)
if not text_str:
text_str = op.op.name
self._custom_multiqubit_gate(q_xy, c_xy, wide=_iswide,
text=getattr(op.op, 'label', None) or op.name)
text=text_str)

# draw custom multi-qubit gate
elif len(q_xy) > 5:
text_str = getattr(op.op, 'label', None)
if not text_str:
text_str = op.op.name
self._custom_multiqubit_gate(q_xy, c_xy, wide=_iswide,
text=op.op.label or op.name)
text=text_str)
else:
logger.critical('Invalid gate %s', op)
raise exceptions.VisualizationError('invalid gate {}'.format(op))
Expand Down