qubiter.latex_tools.AsciiPic_to_Latex module

class qubiter.latex_tools.AsciiPic_to_Latex.AsciiPic_to_Latex(reverse_bits=True, reverse_gates=True)[source]

Bases: object

This class can translate a Qubiter Picture file (which is a text file that contains an ASCII picture of a quantum circuit) into Latex code that can be compiled into a scholarly, publication quality eps or pdf picture of the circuit. The Latex code generated by this class calls commands from the package QCircuit, which in turn calls commands from the package xypic.


1. Bryan Eastin, S. Flammia, “Q-circuit Tutorial”, https://arxiv.org/abs/quant-ph/0406003v2 2. K.H. Rose, “xypic users’s guide” (There is also a much longer “xypic reference manual”

You can use the Latex code produced by this class as a starting point. You can tweak the code by hand by adding annotations in Latex or by changing various parameters as explained in the xypic and Qcircuit documentation.

Instead of going from Qubiter Picture file to Latex code directly, this class inserts an intermediate translation step that we call a Mosaic ascii picture. A Mosaic picture is a simplified, abridged version of a Qubiter “native” Picture file. In Qubiter native pictures, each gate is made up of multiple bonds, and each bond is 4 characters long. A Mosaic picture uses 1 character per bond instead of 4, usually retaining only the first character of each bond of the counterpart Qubiter native picture. Hence, a Mosaic picture is exactly 1/4 as long as its Qubiter native picture counterpart. Qubiter native pictures and Mosaic pictures can both be machine generated by Qubiter or written by hand. Because they are 1/4 as long, Mosaic pictures are easier to write by hand than Qubiter native pictures.

Here is a quantum circuit with 7 qubits and 9 gates expressed in Qubiter native picture format and in Mosaic picture format. In both pictures, time points downwards. Each line (row) represents one gate, or one unit of time. Columns 0, 4, 8, … represent a qubit in the Qubiter native pic.

Qubiter native pic:

|   X---+---+---@   |   |
|   Y---+---O---@---O   |
|   H   |   |   |   |   |
|   <---+---+--->   |   |
M   |   |   |   |   |   |
@---X   |   |   |   |   |
@---+---+---+---+---X   |
@---+---+---O---+---X   |

Mosaic pic:


As you can see, in the Mosaic pic, the white spaces and hyphens are omitted. The following symbols are kept:

kept = [
    'H',  # Hadamard matrix
    'M',  # measurement of type 2
    'O',  # Control of type False
    '@',  # Control of type True
    'X',  # sigma_X Pauli matrix
    'Y',  # sigma_Y Pauli matrix
    'Z',  # sigma_Z Pauli matrix
    '<',  # left edge of swap gate
    '>',  # right edge of swap gate
    '|',  # wire connecting two times
    '+',  # '|' connecting 2 times crossed by a '-' connecting two qubits
    '$']  # place holder to be replaced in Latex by initial ket of a qubit

In translating a Mosaic picture to Latex, this class will try to interpret each character of a Mosaic pic by using the “kept” list above. If you insert a character that it can’t interpret, like for example an ‘A’, it will translate that character to a Latex one qubit box with an ‘A’ inside it. If a gate (=time=mosaic pic row) has at least one ‘?’ in the mosaic pic, it will be drawn in the Latex with inter-qubit wiring. For example, a mosaic row |AA|?| will be drawn in the Latex with a box around each A and a box around the ? and a wire connecting all 3 boxes.

In Qubiter native pics and Mosaic pics, time points down, the way we normally read English. In the Latex pictures generated by this class, you can choose for time to point either from left to right (>) or vice versa (<). The > convention is more popular than the < convention, but the < convention is preferred by the cognoscenti because it recognizes that quantum circuit diagrams are merely a graphical representation of Dirac notation, and Dirac notation uses the < convention. Reversing a convention espoused by the universally popular Dirac notation is a totally needless complication.

  • gb_char_arr (np.ndarray) – This internal variable is a rectangular array of single characters, of shape (num_gates, num_qbits), hence its name starts with gb. Each gate is a unit of time, and, as usual in Qubiter, the words bit and qubit are used interchangeably. To build this array, we start by splitting a Mosaic picture into a character array, then we make some modifications like adding a row of ‘$’ characters and another of ‘|’ characters as placeholders.
  • gb_ch_is_measured (np.ndarray[bool]) – Internal variable, a bool array of the same shape as gb_char_arr. For each ch (aka character, node, vertex) entry in gb_char_arr, the corresponding bool entry of this array answers the question of whether or not that node is already measured (i.e., whether it occurs at the same qubit as an M node but after it).
  • init_states (list[str]) – A list of num_qbits many strings. Each string will be inserted inside a ket and displayed in the Latex picture as the starting state of a qubit. The strings in the list are ordered in top qubit to bottom qubit order.
  • reverse_bits (bool) – Set to True iff you want qubit order in Mosaic pic to be reversed in Latex pic.
  • reverse_gates (bool) – Set to True iff you want gate order in Mosaic pic to be reversed in Latex pic.
  • time_dir (str) – time_dir = ‘<’ if reverse_gates else ‘>’
__init__(reverse_bits=True, reverse_gates=True)[source]


  • reverse_bits (bool) –
  • reverse_gates (bool) –
get_ckt_latex(mosaic_word_list, init_states=None)[source]

Returns Latex code for quantum circuit described by the input mosaic_word_list.

  • mosaic_word_list (list[str]) –
  • init_states (list[str]) –

Return type:


static get_ending_latex()[source]

Returns ending code of Latex document.

Return type:str
static get_preface_latex()[source]

Returns preface code of Latex document.

Return type:str
process_mosaic_word_list(mosaic_word_list, init_states)[source]

Internal function that uses info in the input mosaic_word_list to fill certain attributes of the class.

  • mosaic_word_list (list[str]) –
  • init_states (list[str]) –

Return type:


static qubiter_pic_file_to_mosaic_word_list(file_path, num_qbits, ZL)[source]

Reads Qubiter native pic file and returns a mosaic word list, which is a list of words all of which have the same number ( =num_qbits) of characters.

  • file_path (str) – Path to Qubiter native pic file.
  • num_qbits (int) – Number of qubits. The name of every native pic file generated by Qubiter states the value of num_qbits.
  • ZL (bool) – Set to True iff file is using Zero bit Last convention. Opposite of ZL is ZF (Zero First). The name of every native pic file generated by Qubiter states either ZL or ZF.

Return type: