def __init__(self, qregs, cregs, ops, scale=1.0, style=None, plot_barriers=True, reverse_bits=False): if not HAS_MATPLOTLIB: raise ImportError('The class MatplotlibDrawer needs matplotlib. ' 'Run "pip install matplotlib" before.') self._ast = None self._scale = DEFAULT_SCALE * scale self._creg = [] self._qreg = [] self._registers(cregs, qregs) self._ops = ops self._qreg_dict = collections.OrderedDict() self._creg_dict = collections.OrderedDict() self._cond = { 'n_lines': 0, 'xmax': 0, 'ymax': 0, } self._style = qcstyle.QCStyle() self.plot_barriers = plot_barriers self.reverse_bits = reverse_bits if style: if isinstance(style, dict): self._style.set_style(style) elif isinstance(style, str): with open(style, 'r') as infile: dic = json.load(infile) self._style.set_style(dic) self.figure = plt.figure() self.figure.patch.set_facecolor(color=self._style.bg) self.ax = self.figure.add_subplot(111) self.ax.axis('off') self.ax.set_aspect('equal') self.ax.tick_params(labelbottom=False, labeltop=False, labelleft=False, labelright=False)
def __init__(self, qregs, cregs, ops, scale, style=None, plot_barriers=True, reverse_bits=False): """ Args: qregs (list): A list of tuples for the quantum registers cregs (list): A list of tuples for the classical registers ops (list): A list of dicts where each entry is a operation from the circuit. scale (float): image scaling 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. """ # style sheet self._style = _qcstyle.QCStyle() if style: if isinstance(style, dict): self._style.set_style(style) elif isinstance(style, str): with open(style, 'r') as infile: dic = json.load(infile) self._style.set_style(dic) # list of lists corresponding to layers of the circuit self.ops = ops # image scaling self.scale = scale # Map of qregs to sizes self.qregs = {} # Map of cregs to sizes self.cregs = {} # List of qregs and cregs in order of appearance in code and image self.ordered_regs = [] # Map from registers to the list they appear in the image self.img_regs = {} # Array to hold the \\LaTeX commands to generate a circuit image. self._latex = [] # Variable to hold image depth (width) self.img_depth = 0 # Variable to hold image width (height) self.img_width = 0 # Variable to hold total circuit depth self.sum_column_widths = 0 # Variable to hold total circuit width self.sum_row_heights = 0 # em points of separation between circuit columns self.column_separation = 0.5 # em points of separation between circuit row self.row_separation = 0.0 # presence of "box" or "target" determines row spacing self.has_box = False self.has_target = False self.reverse_bits = reverse_bits self.plot_barriers = plot_barriers ################################# self.qregs = collections.OrderedDict(_get_register_specs(qregs)) self.qubit_list = qregs self.ordered_regs = qregs + cregs self.cregs = collections.OrderedDict(_get_register_specs(cregs)) self.clbit_list = cregs self.img_regs = {bit: ind for ind, bit in enumerate(self.ordered_regs)} self.img_width = len(self.img_regs) self.wire_type = {} for key, value in self.ordered_regs: self.wire_type[(key, value)] = key in self.cregs.keys()