-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
circuit_visualization.py
727 lines (658 loc) · 28.4 KB
/
circuit_visualization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
Module for the primary interface to the circuit drawers.
This module contains the end user facing API for drawing quantum circuits.
There are 3 available drawer backends:
0. ASCII art
1. LaTeX
2. Matplotlib
This provides a single function entry point to drawing a circuit object with
any of the backends.
"""
from __future__ import annotations
import logging
import os
import shutil
import subprocess
import tempfile
import typing
from warnings import warn
from qiskit import user_config
from qiskit.circuit import ControlFlowOp, Measure
from qiskit.utils import optionals as _optionals
from ..exceptions import VisualizationError
from ..utils import _trim as trim_image
from . import _utils
from . import latex as _latex
from . import matplotlib as _matplotlib
from . import text as _text
if typing.TYPE_CHECKING:
from typing import Any
from qiskit.circuit import QuantumCircuit # pylint: disable=cyclic-import
logger = logging.getLogger(__name__)
def circuit_drawer(
circuit: QuantumCircuit,
scale: float | None = None,
filename: str | None = None,
style: dict | str | None = None,
output: str | None = None,
interactive: bool = False,
plot_barriers: bool = True,
reverse_bits: bool | None = None,
justify: str | None = None,
vertical_compression: str | None = "medium",
idle_wires: bool | None = None,
with_layout: bool = True,
fold: int | None = None,
# The type of ax is matplotlib.axes.Axes, but this is not a fixed dependency, so cannot be
# safely forward-referenced.
ax: Any | None = None,
initial_state: bool = False,
cregbundle: bool | None = None,
wire_order: list[int] | None = None,
expr_len: int = 30,
):
r"""Draw the quantum circuit. Use the output parameter to choose the drawing format:
**text**: ASCII art TextDrawing that can be printed in the console.
**mpl**: images with color rendered purely in Python using matplotlib.
**latex**: high-quality images compiled via latex.
**latex_source**: raw uncompiled latex output.
.. warning::
Support for :class:`~.expr.Expr` nodes in conditions and :attr:`.SwitchCaseOp.target`
fields is preliminary and incomplete. The ``text`` and ``mpl`` drawers will make a
best-effort attempt to show data dependencies, but the LaTeX-based drawers will skip
these completely.
Args:
circuit: The circuit to visualize.
scale: Scale of image to draw (shrink if ``< 1.0``). Only used by
the ``mpl``, ``latex`` and ``latex_source`` outputs. Defaults to ``1.0``.
filename: File path to save image to. Defaults to ``None`` (result not saved in a file).
style: Style name, file name of style JSON file, or a dictionary specifying the style.
* The supported style names are ``"iqp"`` (default), ``"iqp-dark"``, ``"clifford"``,
``"textbook"`` and ``"bw"``.
* If given a JSON file, e.g. ``my_style.json`` or ``my_style`` (the ``.json``
extension may be omitted), this function attempts to load the style dictionary
from that location. Note, that the JSON file must completely specify the
visualization specifications. The file is searched for in
``qiskit/visualization/circuit/styles``, the current working directory, and
the location specified in ``~/.qiskit/settings.conf``.
* If a dictionary, every entry overrides the default configuration. If the
``"name"`` key is given, the default configuration is given by that style.
For example, ``{"name": "textbook", "subfontsize": 5}`` loads the ``"texbook"``
style and sets the subfontsize (e.g. the gate angles) to ``5``.
* If ``None`` the default style ``"iqp"`` is used or, if given, the default style
specified in ``~/.qiskit/settings.conf``.
output: Select the output method to use for drawing the circuit.
Valid choices are ``text``, ``mpl``, ``latex``, ``latex_source``.
By default, the ``text`` drawer is used unless the user config file
(usually ``~/.qiskit/settings.conf``) has an alternative backend set
as the default. For example, ``circuit_drawer = latex``. If the output
kwarg is set, that backend will always be used over the default in
the user config file.
interactive: When set to ``True``, show the circuit in a new window
(for ``mpl`` this depends on the matplotlib backend being used
supporting this). Note when used with either the `text` or the
``latex_source`` output type this has no effect and will be silently
ignored. Defaults to ``False``.
reverse_bits: When set to ``True``, reverse the bit order inside
registers for the output visualization. Defaults to ``False`` unless the
user config file (usually ``~/.qiskit/settings.conf``) has an
alternative value set. For example, ``circuit_reverse_bits = True``.
plot_barriers: Enable/disable drawing barriers in the output
circuit. Defaults to ``True``.
justify: Options are ``"left"``, ``"right"`` or ``"none"`` (str).
If anything else is supplied, left justified will be used instead.
It refers to where gates should be placed in the output circuit if
there is an option. ``none`` results in each gate being placed in
its own column. Defaults to ``left``.
vertical_compression: ``high``, ``medium`` or ``low``. It
merges the lines generated by the `text` output so the drawing
will take less vertical room. Default is ``medium``. Only used by
the ``text`` output, will be silently ignored otherwise.
idle_wires: Include idle wires (wires with no circuit elements)
in output visualization. Default is ``True`` unless the
user config file (usually ``~/.qiskit/settings.conf``) has an
alternative value set. For example, ``circuit_idle_wires = False``.
with_layout: Include layout information, with labels on the
physical layout. Default is ``True``.
fold: Sets pagination. It can be disabled using -1. In ``text``,
sets the length of the lines. This is useful when the drawing does
not fit in the console. If None (default), it will try to guess the
console width using ``shutil.get_terminal_size()``. However, if
running in jupyter, the default line length is set to 80 characters.
In ``mpl``, it is the number of (visual) layers before folding.
Default is 25.
ax: Only used by the `mpl` backend. An optional ``matplotlib.axes.Axes``
object to be used for the visualization output. If none is
specified, a new matplotlib Figure will be created and used.
Additionally, if specified there will be no returned Figure since
it is redundant.
initial_state: Adds :math:`|0\rangle` in the beginning of the qubit wires and
:math:`0` to classical wires. Default is ``False``.
cregbundle: If set to ``True``, bundle classical registers.
Default is ``True``, except for when ``output`` is set to ``"text"``.
wire_order: A list of integers used to reorder the display
of the bits. The list must have an entry for every bit with the bits
in the range 0 to (``num_qubits`` + ``num_clbits``).
expr_len: The number of characters to display if an :class:`~.expr.Expr`
is used for the condition in a :class:`.ControlFlowOp`. If this number is exceeded,
the string will be truncated at that number and '...' added to the end.
Returns:
:class:`.TextDrawing` or :class:`matplotlib.figure` or :class:`PIL.Image` or
:class:`str`:
* ``TextDrawing`` (if ``output='text'``)
A drawing that can be printed as ascii art.
* ``matplotlib.figure.Figure`` (if ``output='mpl'``)
A matplotlib figure object for the circuit diagram.
* ``PIL.Image`` (if ``output='latex``')
An in-memory representation of the image of the circuit diagram.
* ``str`` (if ``output='latex_source'``)
The LaTeX source code for visualizing the circuit diagram.
Raises:
VisualizationError: when an invalid output method is selected
ImportError: when the output methods requires non-installed libraries.
Example:
.. plot::
:include-source:
from qiskit import QuantumCircuit
from qiskit.visualization import circuit_drawer
qc = QuantumCircuit(1, 1)
qc.h(0)
qc.measure(0, 0)
circuit_drawer(qc, output="mpl", style={"backgroundcolor": "#EEEEEE"})
"""
image = None
expr_len = max(expr_len, 0)
config = user_config.get_config()
# Get default from config file else use text
default_output = "text"
default_reverse_bits = False
default_idle_wires = config.get("circuit_idle_wires", True)
if config:
default_output = config.get("circuit_drawer", "text")
if default_output == "auto":
if _optionals.HAS_MATPLOTLIB:
default_output = "mpl"
else:
default_output = "text"
if wire_order is None:
default_reverse_bits = config.get("circuit_reverse_bits", False)
if output is None:
output = default_output
if reverse_bits is None:
reverse_bits = default_reverse_bits
if idle_wires is None:
idle_wires = default_idle_wires
if wire_order is not None and reverse_bits:
raise VisualizationError(
"The wire_order option cannot be set when the reverse_bits option is True."
)
complete_wire_order = wire_order
if wire_order is not None:
wire_order_len = len(wire_order)
total_wire_len = circuit.num_qubits + circuit.num_clbits
if wire_order_len not in [circuit.num_qubits, total_wire_len]:
raise VisualizationError(
f"The wire_order list (length {wire_order_len}) should as long as "
f"the number of qubits ({circuit.num_qubits}) or the "
f"total numbers of qubits and classical bits {total_wire_len}."
)
if len(set(wire_order)) != len(wire_order):
raise VisualizationError("The wire_order list should not have repeated elements.")
if wire_order_len == circuit.num_qubits:
complete_wire_order = wire_order + list(range(circuit.num_qubits, total_wire_len))
if (
circuit.clbits
and (reverse_bits or wire_order is not None)
and not set(wire_order or []).issubset(set(range(circuit.num_qubits)))
):
if cregbundle:
warn(
"cregbundle set to False since either reverse_bits or wire_order "
"(over classical bit) has been set.",
RuntimeWarning,
2,
)
cregbundle = False
def check_clbit_in_inst(circuit, cregbundle):
if cregbundle is False:
return False
for inst in circuit.data:
if isinstance(inst.operation, ControlFlowOp):
for block in inst.operation.blocks:
if check_clbit_in_inst(block, cregbundle) is False:
return False
elif inst.clbits and not isinstance(inst.operation, Measure):
if cregbundle is not False:
warn(
"Cregbundle set to False since an instruction needs to refer"
" to individual classical wire",
RuntimeWarning,
3,
)
return False
return True
cregbundle = check_clbit_in_inst(circuit, cregbundle)
if output == "text":
return _text_circuit_drawer(
circuit,
filename=filename,
reverse_bits=reverse_bits,
plot_barriers=plot_barriers,
justify=justify,
vertical_compression=vertical_compression,
idle_wires=idle_wires,
with_layout=with_layout,
fold=fold,
initial_state=initial_state,
cregbundle=cregbundle,
wire_order=complete_wire_order,
expr_len=expr_len,
)
elif output == "latex":
image = _latex_circuit_drawer(
circuit,
filename=filename,
scale=scale,
style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits,
justify=justify,
idle_wires=idle_wires,
with_layout=with_layout,
initial_state=initial_state,
cregbundle=cregbundle,
wire_order=complete_wire_order,
)
elif output == "latex_source":
return _generate_latex_source(
circuit,
filename=filename,
scale=scale,
style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits,
justify=justify,
idle_wires=idle_wires,
with_layout=with_layout,
initial_state=initial_state,
cregbundle=cregbundle,
wire_order=complete_wire_order,
)
elif output == "mpl":
image = _matplotlib_circuit_drawer(
circuit,
scale=scale,
filename=filename,
style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits,
justify=justify,
idle_wires=idle_wires,
with_layout=with_layout,
fold=fold,
ax=ax,
initial_state=initial_state,
cregbundle=cregbundle,
wire_order=complete_wire_order,
expr_len=expr_len,
)
else:
raise VisualizationError(
f"Invalid output type {output} selected. The only valid choices "
"are text, latex, latex_source, and mpl"
)
if image and interactive:
image.show()
return image
# -----------------------------------------------------------------------------
# _text_circuit_drawer
# -----------------------------------------------------------------------------
def _text_circuit_drawer(
circuit,
filename=None,
reverse_bits=False,
plot_barriers=True,
justify=None,
vertical_compression="high",
idle_wires=True,
with_layout=True,
fold=None,
initial_state=True,
cregbundle=None,
encoding=None,
wire_order=None,
expr_len=30,
):
"""Draws a circuit using ascii art.
Args:
circuit (QuantumCircuit): Input circuit
filename (str): Optional filename to write the result
reverse_bits (bool): Rearrange the bits in reverse order.
plot_barriers (bool): Draws the barriers when they are there.
justify (str) : `left`, `right` or `none`. Defaults to `left`. Says how
the circuit should be justified.
vertical_compression (string): `high`, `medium`, or `low`. It merges the
lines so the drawing will take less vertical room. Default is `high`.
idle_wires (bool): Include idle wires. Default is True.
with_layout (bool): Include layout information with labels on the physical
layout. Default: True
fold (int): Optional. Breaks the circuit drawing to this length. This
is useful when the drawing does not fit in the console. If
None (default), it will try to guess the console width using
`shutil.get_terminal_size()`. If you don't want pagination
at all, set `fold=-1`.
initial_state (bool): Optional. Adds |0> in the beginning of the line.
Default: `False`.
cregbundle (bool): Optional. If set True, bundle classical registers.
Default: ``True``.
encoding (str): Optional. Sets the encoding preference of the output.
Default: ``sys.stdout.encoding``.
wire_order (list): Optional. A list of integers used to reorder the display
of the bits. The list must have an entry for every bit with the bits
in the range 0 to (num_qubits + num_clbits).
expr_len (int): Optional. The number of characters to display if an :class:`~.expr.Expr`
is used for the condition in a :class:`.ControlFlowOp`. If this number is exceeded,
the string will be truncated at that number and '...' added to the end.
Returns:
TextDrawing: An instance that, when printed, draws the circuit in ascii art.
Raises:
VisualizationError: When the filename extension is not .txt.
"""
qubits, clbits, nodes = _utils._get_layered_instructions(
circuit,
reverse_bits=reverse_bits,
justify=justify,
idle_wires=idle_wires,
wire_order=wire_order,
)
text_drawing = _text.TextDrawing(
qubits,
clbits,
nodes,
circuit,
reverse_bits=reverse_bits,
initial_state=initial_state,
cregbundle=cregbundle,
encoding=encoding,
with_layout=with_layout,
expr_len=expr_len,
)
text_drawing.plotbarriers = plot_barriers
text_drawing.line_length = fold
text_drawing.vertical_compression = vertical_compression
if filename:
text_drawing.dump(filename, encoding=encoding)
return text_drawing
# -----------------------------------------------------------------------------
# latex_circuit_drawer
# -----------------------------------------------------------------------------
@_optionals.HAS_PDFLATEX.require_in_call("LaTeX circuit drawing")
@_optionals.HAS_PDFTOCAIRO.require_in_call("LaTeX circuit drawing")
@_optionals.HAS_PIL.require_in_call("LaTeX circuit drawing")
def _latex_circuit_drawer(
circuit,
scale=0.7,
style=None,
filename=None,
plot_barriers=True,
reverse_bits=False,
justify=None,
idle_wires=True,
with_layout=True,
initial_state=False,
cregbundle=None,
wire_order=None,
):
"""Draw a quantum circuit based on latex (Qcircuit package)
Requires version >=2.6.0 of the qcircuit LaTeX package.
Args:
circuit (QuantumCircuit): a quantum circuit
scale (float): scaling factor
style (dict or str): dictionary of style or file name of style file
filename (str): file path to save image to
reverse_bits (bool): When set to True reverse the bit order inside
registers for the output visualization.
plot_barriers (bool): Enable/disable drawing barriers in the output
circuit. Defaults to True.
justify (str) : `left`, `right` or `none`. Defaults to `left`. Says how
the circuit should be justified.
idle_wires (bool): Include idle wires. Default is True.
with_layout (bool): Include layout information, with labels on the physical
layout. Default: True
initial_state (bool): Optional. Adds |0> in the beginning of the line.
Default: `False`.
cregbundle (bool): Optional. If set True, bundle classical registers. On by default, if
this is possible for the given circuit, otherwise off.
wire_order (list): Optional. A list of integers used to reorder the display
of the bits. The list must have an entry for every bit with the bits
in the range 0 to (num_qubits + num_clbits).
Returns:
PIL.Image: an in-memory representation of the circuit diagram
Raises:
MissingOptionalLibraryError: if pillow, pdflatex, or poppler are not installed
VisualizationError: if one of the conversion utilities failed for some internal or
file-access reason.
"""
from PIL import Image
tmpfilename = "circuit"
with tempfile.TemporaryDirectory() as tmpdirname:
tmppath = os.path.join(tmpdirname, tmpfilename + ".tex")
_generate_latex_source(
circuit,
filename=tmppath,
scale=scale,
style=style,
plot_barriers=plot_barriers,
reverse_bits=reverse_bits,
justify=justify,
idle_wires=idle_wires,
with_layout=with_layout,
initial_state=initial_state,
cregbundle=cregbundle,
wire_order=wire_order,
)
try:
subprocess.run(
[
"pdflatex",
"-halt-on-error",
f"-output-directory={tmpdirname}",
f"{tmpfilename + '.tex'}",
],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
check=True,
)
except OSError as exc:
# OSError should generally not occur, because it's usually only triggered if `pdflatex`
# doesn't exist as a command, but we've already checked that.
raise VisualizationError("`pdflatex` command could not be run.") from exc
except subprocess.CalledProcessError as exc:
with open("latex_error.log", "wb") as error_file:
error_file.write(exc.stdout)
logger.warning(
"Unable to compile LaTeX. Perhaps you are missing the `qcircuit` package."
" The output from the `pdflatex` command is in `latex_error.log`."
)
raise VisualizationError(
"`pdflatex` call did not succeed: see `latex_error.log`."
) from exc
base = os.path.join(tmpdirname, tmpfilename)
try:
subprocess.run(
["pdftocairo", "-singlefile", "-png", "-q", base + ".pdf", base],
check=True,
)
except (OSError, subprocess.CalledProcessError) as exc:
message = "`pdftocairo` failed to produce an image."
logger.warning(message)
raise VisualizationError(message) from exc
image = Image.open(base + ".png")
image = trim_image(image)
if filename:
if filename.endswith(".pdf"):
shutil.move(base + ".pdf", filename)
else:
try:
image.save(filename)
except (ValueError, OSError) as exc:
raise VisualizationError(
f"Pillow could not write the image file '{filename}'."
) from exc
return image
def _generate_latex_source(
circuit,
filename=None,
scale=0.7,
style=None,
reverse_bits=False,
plot_barriers=True,
justify=None,
idle_wires=True,
with_layout=True,
initial_state=False,
cregbundle=None,
wire_order=None,
):
"""Convert QuantumCircuit to LaTeX string.
Args:
circuit (QuantumCircuit): a quantum circuit
scale (float): scaling factor
style (dict or str): dictionary of style or file name of style file
filename (str): optional filename to write latex
reverse_bits (bool): When set to True reverse the bit order inside
registers for the output visualization.
plot_barriers (bool): Enable/disable drawing barriers in the output
circuit. Defaults to True.
justify (str) : `left`, `right` or `none`. Defaults to `left`. Says how
the circuit should be justified.
idle_wires (bool): Include idle wires. Default is True.
with_layout (bool): Include layout information, with labels on the physical
layout. Default: True
initial_state (bool): Optional. Adds |0> in the beginning of the line.
Default: `False`.
cregbundle (bool): Optional. If set True, bundle classical registers.
wire_order (list): Optional. A list of integers used to reorder the display
of the bits. The list must have an entry for every bit with the bits
in the range 0 to (num_qubits + num_clbits).
Returns:
str: Latex string appropriate for writing to file.
"""
qubits, clbits, nodes = _utils._get_layered_instructions(
circuit,
reverse_bits=reverse_bits,
justify=justify,
idle_wires=idle_wires,
wire_order=wire_order,
)
qcimg = _latex.QCircuitImage(
qubits,
clbits,
nodes,
scale,
style=style,
reverse_bits=reverse_bits,
plot_barriers=plot_barriers,
initial_state=initial_state,
cregbundle=cregbundle,
with_layout=with_layout,
circuit=circuit,
)
latex = qcimg.latex()
if filename:
with open(filename, "w") as latex_file:
latex_file.write(latex)
return latex
# -----------------------------------------------------------------------------
# matplotlib_circuit_drawer
# -----------------------------------------------------------------------------
def _matplotlib_circuit_drawer(
circuit,
scale=None,
filename=None,
style=None,
plot_barriers=True,
reverse_bits=False,
justify=None,
idle_wires=True,
with_layout=True,
fold=None,
ax=None,
initial_state=False,
cregbundle=None,
wire_order=None,
expr_len=30,
):
"""Draw a quantum circuit based on matplotlib.
If `%matplotlib inline` is invoked in a Jupyter notebook, it visualizes a circuit inline.
We recommend `%config InlineBackend.figure_format = 'svg'` for the inline visualization.
Args:
circuit (QuantumCircuit): a quantum circuit
scale (float): scaling factor
filename (str): file path to save image to
style (dict or str): dictionary of style or file name of style file
reverse_bits (bool): When set to True, reverse the bit order inside
registers for the output visualization.
plot_barriers (bool): Enable/disable drawing barriers in the output
circuit. Defaults to True.
justify (str): `left`, `right` or `none`. Defaults to `left`. Says how
the circuit should be justified.
idle_wires (bool): Include idle wires. Default is True.
with_layout (bool): Include layout information, with labels on the physical
layout. Default: True.
fold (int): Number of vertical layers allowed before folding. Default is 25.
ax (matplotlib.axes.Axes): An optional Axes object to be used for
the visualization output. If none is specified, a new matplotlib
Figure will be created and used. Additionally, if specified there
will be no returned Figure since it is redundant.
initial_state (bool): Optional. Adds |0> in the beginning of the line.
Default: `False`.
cregbundle (bool): Optional. If set True bundle classical registers.
Default: ``True``.
wire_order (list): Optional. A list of integers used to reorder the display
of the bits. The list must have an entry for every bit with the bits
in the range 0 to (num_qubits + num_clbits).
expr_len (int): Optional. The number of characters to display if an :class:`~.expr.Expr`
is used for the condition in a :class:`.ControlFlowOp`. If this number is exceeded,
the string will be truncated at that number and '...' added to the end.
Returns:
matplotlib.figure: a matplotlib figure object for the circuit diagram
if the ``ax`` kwarg is not set.
"""
qubits, clbits, nodes = _utils._get_layered_instructions(
circuit,
reverse_bits=reverse_bits,
justify=justify,
idle_wires=idle_wires,
wire_order=wire_order,
)
if fold is None:
fold = 25
qcd = _matplotlib.MatplotlibDrawer(
qubits,
clbits,
nodes,
circuit,
scale=scale,
style=style,
reverse_bits=reverse_bits,
plot_barriers=plot_barriers,
fold=fold,
ax=ax,
initial_state=initial_state,
cregbundle=cregbundle,
with_layout=with_layout,
expr_len=expr_len,
)
return qcd.draw(filename)