def _matplotlib_circuit_drawer(circuit, scale=0.7, filename=None, style=None, plot_barriers=True, reverse_bits=False, justify=None): """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. Returns: matplotlib.figure: a matplotlib figure object for the circuit diagram """ qregs, cregs, ops = utils._get_layered_instructions(circuit, reverse_bits=reverse_bits, justify=justify) qcd = _matplotlib.MatplotlibDrawer(qregs, cregs, ops, scale=scale, style=style, plot_barriers=plot_barriers, reverse_bits=reverse_bits) return qcd.draw(filename)
def _text_circuit_drawer(circuit, filename=None, line_length=None, reverse_bits=False, plotbarriers=True, justify=None): """ Draws a circuit using ascii art. Args: circuit (QuantumCircuit): Input circuit filename (str): optional filename to write the result line_length (int): Optional. Breaks the circuit drawing to this length. This 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 line_length=-1. reverse_bits (bool): Rearrange the bits in reverse order. plotbarriers (bool): Draws the barriers when they are there. justify (str) : `left`, `right` or `none`. Defaults to `left`. Says how the circuit should be justified. Returns: TextDrawing: An instances that, when printed, draws the circuit in ascii art. """ qregs, cregs, ops = utils._get_layered_instructions(circuit, reverse_bits=reverse_bits, justify=justify) text_drawing = _text.TextDrawing(qregs, cregs, ops) text_drawing.plotbarriers = plotbarriers text_drawing.line_length = line_length if filename: text_drawing.dump(filename) return text_drawing
def test_get_layered_instructions_reverse_bits(self): """ _get_layered_instructions with reverse_bits=True """ (qregs, cregs, layered_ops) = utils._get_layered_instructions(self.circuit, reverse_bits=True) exp = [[('cx', [(QuantumRegister(2, 'qr2'), 0), (QuantumRegister(2, 'qr2'), 1)], []), ('cx', [(QuantumRegister(2, 'qr1'), 0), (QuantumRegister(2, 'qr1'), 1)], [])], [('measure', [(QuantumRegister(2, 'qr2'), 0)], [(ClassicalRegister(2, 'cr2'), 0)])], [('measure', [(QuantumRegister(2, 'qr1'), 0)], [(ClassicalRegister(2, 'cr1'), 0)])], [('cx', [(QuantumRegister(2, 'qr2'), 1), (QuantumRegister(2, 'qr2'), 0)], []), ('cx', [(QuantumRegister(2, 'qr1'), 1), (QuantumRegister(2, 'qr1'), 0)], [])], [('measure', [(QuantumRegister(2, 'qr2'), 1)], [(ClassicalRegister(2, 'cr2'), 1)])], [('measure', [(QuantumRegister(2, 'qr1'), 1)], [(ClassicalRegister(2, 'cr1'), 1)])]] self.assertEqual([(self.qr2, 1), (self.qr2, 0), (self.qr1, 1), (self.qr1, 0)], qregs) self.assertEqual([(self.cr2, 1), (self.cr2, 0), (self.cr1, 1), (self.cr1, 0)], cregs) self.assertEqual(exp, [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
def test_get_layered_instructions_right_justification_simple(self): """ Test _get_layered_instructions right justification simple since #2802 q_0: |0>βββ βββββββ β βββββ q_1: |0>βββΌβββ€ H β β βββββ€ q_2: |0>βββΌβββ€ H β βββ΄βββββββ q_3: |0>β€ X ββββββ βββββ """ qc = QuantumCircuit(4) qc.h(1) qc.h(2) qc.cx(0, 3) (_, _, layered_ops) = utils._get_layered_instructions(qc, justify='right') r_exp = [[('cx', [Qubit(QuantumRegister(4, 'q'), 0), Qubit(QuantumRegister(4, 'q'), 3)], [])], [('h', [Qubit(QuantumRegister(4, 'q'), 1)], []), ('h', [Qubit(QuantumRegister(4, 'q'), 2)], []) ] ] self.assertEqual(r_exp, [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
def _text_circuit_drawer(circuit, filename=None, line_length=None, reverse_bits=False, plot_barriers=True, justify=None, vertical_compression='high', idle_wires=True, with_layout=True, fold=None, initial_state=True): """Draws a circuit using ascii art. Args: circuit (QuantumCircuit): Input circuit filename (str): optional filename to write the result line_length (int): Deprecated. See `fold`. 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 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: `True`. Returns: TextDrawing: An instances that, when printed, draws the circuit in ascii art. """ qregs, cregs, ops = utils._get_layered_instructions( circuit, reverse_bits=reverse_bits, justify=justify, idle_wires=idle_wires) if with_layout: layout = circuit._layout else: layout = None if line_length: warn('The parameter "line_length" is being replaced by "fold"', DeprecationWarning, 3) fold = line_length text_drawing = _text.TextDrawing(qregs, cregs, ops, layout=layout, initial_state=initial_state) text_drawing.plotbarriers = plot_barriers text_drawing.line_length = fold text_drawing.vertical_compression = vertical_compression if filename: text_drawing.dump(filename) return text_drawing
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=False): """Convert QuantumCircuit to LaTeX string. Args: circuit (QuantumCircuit): input circuit scale (float): image scaling filename (str): optional filename to write latex 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 initial_state (bool): Optional. Adds |0> in the beginning of the line. Default: `False`. cregbundle (bool): Optional. If set True bundle classical registers. Default: ``False``. Returns: str: Latex string appropriate for writing to file. """ qregs, cregs, ops = utils._get_layered_instructions( circuit, reverse_bits=reverse_bits, justify=justify, idle_wires=idle_wires) if with_layout: layout = circuit._layout else: layout = None qcimg = _latex.QCircuitImage(qregs, cregs, ops, scale, style=style, plot_barriers=plot_barriers, reverse_bits=reverse_bits, layout=layout, initial_state=initial_state, cregbundle=cregbundle) latex = qcimg.latex() if filename: with open(filename, 'w') as latex_file: latex_file.write(latex) return latex
def test_get_layered_instructions_remove_idle_wires(self): """ _get_layered_instructions with idle_wires=False """ qr1 = QuantumRegister(3, 'qr1') qr2 = QuantumRegister(3, 'qr2') cr1 = ClassicalRegister(3, 'cr1') cr2 = ClassicalRegister(3, 'cr2') circuit = QuantumCircuit(qr1, qr2, cr1, cr2) circuit.cx(qr2[0], qr2[1]) circuit.measure(qr2[0], cr2[0]) circuit.cx(qr2[1], qr2[0]) circuit.measure(qr2[1], cr2[1]) circuit.cx(qr1[0], qr1[1]) circuit.measure(qr1[0], cr1[0]) circuit.cx(qr1[1], qr1[0]) circuit.measure(qr1[1], cr1[1]) (qregs, cregs, layered_ops) = utils._get_layered_instructions(circuit, idle_wires=False) exp = [[('cx', [qr2[0], qr2[1]], []), ('cx', [qr1[0], qr1[1]], [])], [('measure', [qr2[0]], [cr2[0]])], [('measure', [qr1[0]], [cr1[0]])], [('cx', [qr2[1], qr2[0]], []), ('cx', [qr1[1], qr1[0]], [])], [('measure', [qr2[1]], [cr2[1]])], [('measure', [qr1[1]], [cr1[1]])] ] self.assertEqual([qr1[0], qr1[1], qr2[0], qr2[1]], qregs) self.assertEqual([cr1[0], cr1[1], cr2[0], cr2[1]], cregs) self.assertEqual(exp, [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
def test_get_layered_instructions_left_justification_simple(self): """Test _get_layered_instructions left justification simple since #2802 q_0: |0>ββββββββ ββ βββββ β q_1: |0>β€ H ββββΌββ βββββ€ β q_2: |0>β€ H ββββΌββ ββββββββ΄ββ q_3: |0>ββββββ€ X β βββββ """ qc = QuantumCircuit(4) qc.h(1) qc.h(2) qc.cx(0, 3) (_, _, layered_ops) = utils._get_layered_instructions(qc, justify="left") l_exp = [ [ ("h", [Qubit(QuantumRegister(4, "q"), 1)], []), ("h", [Qubit(QuantumRegister(4, "q"), 2)], []), ], [("cx", [ Qubit(QuantumRegister(4, "q"), 0), Qubit(QuantumRegister(4, "q"), 3) ], [])], ] self.assertEqual(l_exp, [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
def test_get_layered_instructions_op_with_cargs(self): """ Test _get_layered_instructions op with cargs right of measure ββββββββ q_0: |0>β€ H ββ€Mββββββββββββββ βββββββ₯ββββββββββββββ q_1: |0>βββββββ«ββ€0 β β β add_circ β c_0: 0 βββββββ©ββ‘0 β βββββββββββββ c_1: 0 βββββββββββββββββββββ """ qc = QuantumCircuit(2, 2) qc.h(0) qc.measure(0, 0) qc_2 = QuantumCircuit(1, 1, name='add_circ') qc_2.h(0).c_if(qc_2.cregs[0], 1) qc_2.measure(0, 0) qc.append(qc_2, [1], [0]) (_, _, layered_ops) = utils._get_layered_instructions(qc) expected = [[('h', [Qubit(QuantumRegister(2, 'q'), 0)], [])], [('measure', [Qubit(QuantumRegister(2, 'q'), 0)], [Clbit(ClassicalRegister(2, 'c'), 0)])], [('add_circ', [Qubit(QuantumRegister(2, 'q'), 1)], [Clbit(ClassicalRegister(2, 'c'), 0)])]] self.assertEqual(expected, [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
def test_get_layered_instructions_right_justification_less_simple(self): """ Test _get_layered_instructions right justification less simple example since #2802 βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ q_0: |0>β€ U2(0,pi/1) ββ€ X ββ€ U2(0,pi/1) ββ€Mββ€ U2(0,pi/1) ββ€ X ββ€ U2(0,pi/1) β ββββββββββββββ€βββ¬ββββββββββββββββ€ββ₯βββββββββββββββ€βββ¬ββββββββββββββββ€ q_1: |0>β€ U2(0,pi/1) ββββ βββ€ U2(0,pi/1) βββ«ββ€ U2(0,pi/1) ββββ βββ€ U2(0,pi/1) β ββββββββββββββ ββββββββββββββ β ββββββββββββββ ββββββββββββββ q_2: |0>βββββββββββββββββββββββββββββββββββ«ββββββββββββββββββββββββββββββββββ β q_3: |0>βββββββββββββββββββββββββββββββββββ«ββββββββββββββββββββββββββββββββββ β q_4: |0>βββββββββββββββββββββββββββββββββββ«ββββββββββββββββββββββββββββββββββ β c1_0: 0 βββββββββββββββββββββββββββββββββββ©ββββββββββββββββββββββββββββββββββ """ qasm = """ OPENQASM 2.0; include "qelib1.inc"; qreg q[5]; creg c1[1]; u2(0,3.14159265358979) q[0]; u2(0,3.14159265358979) q[1]; cx q[1],q[0]; u2(0,3.14159265358979) q[0]; u2(0,3.14159265358979) q[1]; u2(0,3.14159265358979) q[1]; measure q[0] -> c1[0]; u2(0,3.14159265358979) q[0]; cx q[1],q[0]; u2(0,3.14159265358979) q[0]; u2(0,3.14159265358979) q[1]; """ qc = QuantumCircuit.from_qasm_str(qasm) (_, _, layered_ops) = utils._get_layered_instructions(qc, justify='right') r_exp = [[('u2', [Qubit(QuantumRegister(5, 'q'), 0)], []), ('u2', [Qubit(QuantumRegister(5, 'q'), 1)], [])], [('cx', [ Qubit(QuantumRegister(5, 'q'), 1), Qubit(QuantumRegister(5, 'q'), 0) ], [])], [('u2', [Qubit(QuantumRegister(5, 'q'), 0)], []), ('u2', [Qubit(QuantumRegister(5, 'q'), 1)], [])], [('measure', [Qubit(QuantumRegister(5, 'q'), 0)], [Clbit(ClassicalRegister(1, 'c1'), 0)])], [('u2', [Qubit(QuantumRegister(5, 'q'), 0)], []), ('u2', [Qubit(QuantumRegister(5, 'q'), 1)], [])], [('cx', [ Qubit(QuantumRegister(5, 'q'), 1), Qubit(QuantumRegister(5, 'q'), 0) ], [])], [('u2', [Qubit(QuantumRegister(5, 'q'), 0)], []), ('u2', [Qubit(QuantumRegister(5, 'q'), 1)], [])]] self.assertEqual(r_exp, [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
def _matplotlib_circuit_drawer(circuit, scale=0.7, filename=None, style=None, plot_barriers=True, reverse_bits=False, justify=None, idle_wires=True, with_layout=True, fold=None, ax=None): """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): amount ops 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. Returns: matplotlib.figure: a matplotlib figure object for the circuit diagram if the ``ax`` kwarg is not set. """ qregs, cregs, ops = utils._get_layered_instructions(circuit, reverse_bits=reverse_bits, justify=justify, idle_wires=idle_wires) if with_layout: layout = circuit._layout else: layout = None if fold is None: fold = 25 qcd = _matplotlib.MatplotlibDrawer(qregs, cregs, ops, scale=scale, style=style, plot_barriers=plot_barriers, reverse_bits=reverse_bits, layout=layout, fold=fold, ax=ax) return qcd.draw(filename)
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=False, encoding=None): """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``. Returns: TextDrawing: An instance that, when printed, draws the circuit in ascii art. """ qubits, clbits, ops = utils._get_layered_instructions(circuit, reverse_bits=reverse_bits, justify=justify, idle_wires=idle_wires) if with_layout: layout = circuit._layout else: layout = None global_phase = circuit.global_phase if hasattr(circuit, 'global_phase') else None text_drawing = _text.TextDrawing(qubits, clbits, ops, layout=layout, initial_state=initial_state, cregbundle=cregbundle, global_phase=global_phase, encoding=encoding, qregs=circuit.qregs, cregs=circuit.cregs) 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
def _generate_latex_source(circuit, filename=None, scale=0.7, style=None, reverse_bits=False, plot_barriers=True, justify=None, idle_wires=True): """Convert QuantumCircuit to LaTeX string. Args: circuit (QuantumCircuit): input circuit scale (float): image scaling filename (str): optional filename to write latex 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. Returns: str: Latex string appropriate for writing to file. """ qregs, cregs, ops = utils._get_layered_instructions( circuit, reverse_bits=reverse_bits, justify=justify, idle_wires=idle_wires) qcimg = _latex.QCircuitImage(qregs, cregs, ops, scale, style=style, plot_barriers=plot_barriers, reverse_bits=reverse_bits) latex = qcimg.latex() if filename: with open(filename, 'w') as latex_file: latex_file.write(latex) return latex
def test_get_layered_instructions(self): """ _get_layered_instructions without reverse_bits """ (qregs, cregs, layered_ops) = utils._get_layered_instructions(self.circuit) exp = [[('cx', [self.qr2[0], self.qr2[1]], []), ('cx', [self.qr1[0], self.qr1[1]], [])], [('measure', [self.qr2[0]], [self.cr2[0]])], [('measure', [self.qr1[0]], [self.cr1[0]])], [('cx', [self.qr2[1], self.qr2[0]], []), ('cx', [self.qr1[1], self.qr1[0]], [])], [('measure', [self.qr2[1]], [self.cr2[1]])], [('measure', [self.qr1[1]], [self.cr1[1]])] ] self.assertEqual([self.qr1[0], self.qr1[1], self.qr2[0], self.qr2[1]], qregs) self.assertEqual([self.cr1[0], self.cr1[1], self.cr2[0], self.cr2[1]], cregs) self.assertEqual(exp, [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
def test_get_layered_instructions_reverse_bits(self): """_get_layered_instructions with reverse_bits=True""" (qregs, cregs, layered_ops) = utils._get_layered_instructions(self.circuit, reverse_bits=True) exp = [ [("cx", [self.qr2[0], self.qr2[1]], []), ("cx", [self.qr1[0], self.qr1[1]], [])], [("measure", [self.qr2[0]], [self.cr2[0]])], [("measure", [self.qr1[0]], [self.cr1[0]]), ("cx", [self.qr2[1], self.qr2[0]], [])], [("cx", [self.qr1[1], self.qr1[0]], [])], [("measure", [self.qr2[1]], [self.cr2[1]])], [("measure", [self.qr1[1]], [self.cr1[1]])], ] self.assertEqual([self.qr2[1], self.qr2[0], self.qr1[1], self.qr1[0]], qregs) self.assertEqual([self.cr2[1], self.cr2[0], self.cr1[1], self.cr1[0]], cregs) self.assertEqual(exp, [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
def test_get_layered_instructions_left_justification_less_simple(self): """Test _get_layered_instructions left justification less simple example since #2802 βββββββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββββ q_0: |0>β€ U2(0,pi/1) ββ€ X ββ€ U2(0,pi/1) ββββββββββββββββ€Mββ€ U2(0,pi/1) ββ€ X ββ€ U2(0,pi/1) β ββββββββββββββ€βββ¬ββββββββββββββββ€ββββββββββββββββ₯ββββββββββββββββββ¬ββββββββββββββββ€ q_1: |0>β€ U2(0,pi/1) ββββ βββ€ U2(0,pi/1) ββ€ U2(0,pi/1) βββ«ββββββββββββββββββ βββ€ U2(0,pi/1) β ββββββββββββββ ββββββββββββββββββββββββββββ β ββββββββββββββ q_2: |0>βββββββββββββββββββββββββββββββββββββββββββββββββ«ββββββββββββββββββββββββββββββββββ β q_3: |0>βββββββββββββββββββββββββββββββββββββββββββββββββ«ββββββββββββββββββββββββββββββββββ β q_4: |0>βββββββββββββββββββββββββββββββββββββββββββββββββ«ββββββββββββββββββββββββββββββββββ β c1_0: 0 βββββββββββββββββββββββββββββββββββββββββββββββββ©ββββββββββββββββββββββββββββββββββ """ qasm = """ OPENQASM 2.0; include "qelib1.inc"; qreg q[5]; creg c1[1]; u2(0,3.14159265358979) q[0]; u2(0,3.14159265358979) q[1]; cx q[1],q[0]; u2(0,3.14159265358979) q[0]; u2(0,3.14159265358979) q[1]; u2(0,3.14159265358979) q[1]; measure q[0] -> c1[0]; u2(0,3.14159265358979) q[0]; cx q[1],q[0]; u2(0,3.14159265358979) q[0]; u2(0,3.14159265358979) q[1]; """ qc = QuantumCircuit.from_qasm_str(qasm) (_, _, layered_ops) = utils._get_layered_instructions(qc, justify="left") l_exp = [ [ ("u2", (Qubit(QuantumRegister(5, "q"), 0), ), ()), ("u2", (Qubit(QuantumRegister(5, "q"), 1), ), ()), ], [("cx", (Qubit(QuantumRegister(5, "q"), 1), Qubit(QuantumRegister(5, "q"), 0)), ())], [ ("u2", (Qubit(QuantumRegister(5, "q"), 0), ), ()), ("u2", (Qubit(QuantumRegister(5, "q"), 1), ), ()), ], [("u2", (Qubit(QuantumRegister(5, "q"), 1), ), ())], [( "measure", (Qubit(QuantumRegister(5, "q"), 0), ), (Clbit(ClassicalRegister(1, "c1"), 0), ), )], [("u2", (Qubit(QuantumRegister(5, "q"), 0), ), ())], [("cx", (Qubit(QuantumRegister(5, "q"), 1), Qubit(QuantumRegister(5, "q"), 0)), ())], [ ("u2", (Qubit(QuantumRegister(5, "q"), 0), ), ()), ("u2", (Qubit(QuantumRegister(5, "q"), 1), ), ()), ], ] self.assertEqual(l_exp, [[(op.name, op.qargs, op.cargs) for op in ops] for ops in layered_ops])
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=True, wire_order=None, ): """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). 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, scale=scale, style=style, reverse_bits=reverse_bits, plot_barriers=plot_barriers, layout=None, fold=fold, ax=ax, initial_state=initial_state, cregbundle=cregbundle, global_phase=None, calibrations=None, qregs=None, cregs=None, with_layout=with_layout, circuit=circuit, ) return qcd.draw(filename)
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=False, 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. Default: ``False``. 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, layout=None, initial_state=initial_state, cregbundle=cregbundle, global_phase=None, qregs=None, cregs=None, with_layout=with_layout, circuit=circuit, ) latex = qcimg.latex() if filename: with open(filename, "w") as latex_file: latex_file.write(latex) return latex