qubiter.SEO_writer module

class qubiter.SEO_writer.SEO_writer(file_prefix, emb, ZL=True, english_out=None, picture_out=None)[source]

Bases: object

The constructor of this class opens an English and a Picture file. Every other method of the class writes, each time it is called, a single line in each of those 2 files.

Note SEO stands for Sequence of Elementary Operations.

So what are English and Picture files?

We use 3 types of files to characterize a single quantum circuit (in the gate model): (1) an English file (2) a Picture file (3) a Log file.

Log files are written by class SEO_reader, whereas English and Picture files are written by this class, SEO_writer.

A log file just contains useful information like the number of lines of the English file (same as that of Picture file) and their number of operations.

The lines of an English and Picture file are in 1-1 correspondence, each line representing a single operation (e.g., a multi-controlled one qubit gate or a multi-controlled 2 qubit swap).

The English file (resp., Picture file) contains complete (resp., partial, in the form of an ASCII picture) info about the operation specified by each line.

In English and Picture files, time flows downward.

The class SEO_writer gives the bool option ZL. When this argument is set to True (resp., False), the Picture file shows the zero qubit last ( resp., first), and the remaining qubits in consecutive order. Picture files written with zero bit last (resp., first) are labelled prefix + ‘_ZLpict.text’ (resp., prefix + ‘_ZFpict.txt’). The ZL choice does not affect the English file.

See the following and earlier arXiv papers by R.R.Tucci for more info on English and Picture files.

http://arxiv.org/abs/1004.2205 “Quibbs, a Code Generator for Quantum Gibbs Sampling”

The following pdf is stored in the same folder as this python file.

qubiter_rosetta_stone.pdf

This pdf gives examples of lines in analytic/Picture/English formats. The Picture file examples follow the ZL convention.

3 kinds (called 0, 1, 2) of measurements MEAS are allowed. A type 0 measurement inserts a projector |0><0| = n = P_0 at the target bit. A type 1 measurement inserts a projector |1><1| = nbar = P_1 at the target bit. A type 2 measurement stores a copy of the state vector after |0><0| has been applied, and another copy after |1><1| has been applied.

If a vertical wire hasn’t been measured as type 2 measurement, it is drawn in pic file as “|”; otherwise, it is drawn as “:”.

Variables:
  • emb (CktEmbedder) –
  • english_out (_io.TextIOWrapper) – file object for output text file that stores English description of circuit
  • file_prefix (str) – beginning of the name of both English and Picture files
  • gate_line_counter (int) –
  • indentation (int) – Starts at 0, Grows by 4 at end of each write_LOOP and decrease by 4 at beginning of each write_NEXT
  • measured_bits (list(int)) – list of bits that have been measured with type 2 measurement and haven’t been reset to |0> or |1>
  • picture_out (_io.TextIOWrapper) – file object for output text file that stores ASCII Picture description of circuit
  • ZL (bool) –
__init__(file_prefix, emb, ZL=True, english_out=None, picture_out=None)[source]

Constructor

Parameters:
  • file_prefix (str) –
  • emb (CktEmbedder) –
  • ZL (bool) –
  • english_out (_io.TextIOWrapper) –
  • picture_out (_io.TextIOWrapper) –
close_files()[source]

Closes English and Picture files that were opened by the constructor.

Returns:
Return type:None
colonize(pic_line)[source]

This function returns new version of pic_line. Every “|” wire is replaced by “:” colon wire and vice versa iff the wire is at a bit position that has been measured (type 2 measurement) in the past and not reset. This function assumes pic_line in ZL convention so must call this function before calling write_ZF_or_ZL_pic_line()

Parameters:pic_line (str) –
Returns:
Return type:str
delete_files()[source]

This method will delete the English and Picture files. The files are closed before being deleted in case that hasn’t been done yet. Closing a file a second time does nothing.

Returns:
Return type:None
static gen_html_from_eng_or_pic_file(f)[source]

This function returns a string for an html table generated from an English or Picture file. The table has two columns. The first column gives the line numbers starting from 1.

Parameters:f (file) – f is file object returned by open()
Returns:
Return type:str
get_eng_file_path(rel=False)[source]

Returns path (relative if rel is True, absolute if rel is False) of English file.

Variables:rel (bool) –
Returns:
Return type:str
get_pic_file_path(rel=False)[source]

Returns path (relative if rel is True, absolute if rel is False) of Picture file.

Variables:rel (bool) –
Returns:
Return type:str
print_eng_file(jup=False)[source]

Prints English file.

Parameters:jup (bool) – If jup=False, it prints text. Otherwise, it draws in a jupyter notebook a table with line numbers starting at 1
Returns:
Return type:None
print_pic_file(jup=False)[source]

Prints Picture file.

Parameters:jup (bool) – If jup=False, it prints text. Otherwise, it draws in a jupyter notebook a table with line numbers starting at 1
Returns:
Return type:None
rads_to_degs_str(rads)[source]

This method returns

str(rads*180/pi) if isinstance(rads, float)

rads if is_legal_var_name(rads) (this implies rads is str)

aborts otherwise.

The method is only used inside this class so I am making it non-static even though it doesn’t use self.

Parameters:rads (float | str) –
Returns:
Return type:str
write_H(tar_bit_pos)[source]

Writes HAD2 with no controls.

Parameters:tar_bit_pos (int) –
Returns:
Return type:None
write_IF_M_beg(trols)[source]

Writes a ‘IF_M( <controls> ){‘ line in eng & pic files.

Parameters:trols (Controls) –
Returns:
Return type:None
write_IF_M_end()[source]

Writes an ‘}IF_M’ line in eng and pic files.

Returns:
Return type:None
write_LOOP(loop_num, nreps)[source]

Writes a ‘LOOP’ line in eng & pic files. The gates between a LOOP line and its partner NEXT line are to be repeated a number of times called nreps.

Parameters:
  • loop_num (int) –
  • nreps (int) –
Returns:

Return type:

None

write_MEAS(tar_bit_pos, kind)[source]

Writes a ‘MEAS’ line in eng & pic files. This denotes a measurement step. We allow 3 kinds of measurements (0, 1, 2) at a target bit.

Parameters:
  • tar_bit_pos (int) –
  • kind (int) – either 0, 1 or 2
Returns:

Return type:

None

write_NEXT(loop_num)[source]

Writes a ‘NEXT’ line in eng & pic files.

Parameters:loop_num (int) –
Returns:
Return type:None
write_NOTA(bla_str, permission=True)[source]

Writes a ‘NOTA’ line in eng & pic files. As the name implies, a NOTA is just a note or comment such as “I love you Mary”. It is not a gate.

Parameters:
  • bla_str (str) –
  • permission (bool) – General permission, useful for turning off whole batches of NOTAs
Returns:

Return type:

None

write_PRINT(style)[source]

Writes a ‘PRINT’ line in eng & pic files.

Parameters:style (str) –
Returns:
Return type:None
write_Rn(tar_bit_pos, rads_list)[source]

Writes

ROTN = exp(1j*(rads_x*sig_x + rads_y*sig_y + rads_z*sig_z))

with no controls.

Parameters:
  • tar_bit_pos (int) –
  • rads_list (list[float]) – [rads_x, rads_y, rads_z]
Returns:

Return type:

None

write_Rx(tar_bit_pos, rads)[source]

writes ROTX = exp(1j*rads*sig_x) with no controls.

Parameters:
  • tar_bit_pos (int) –
  • rads (float) –
Returns:

Return type:

None

write_Ry(tar_bit_pos, rads)[source]

Writes ROTY = exp(1j*rads*sig_y) with no controls.

Parameters:
  • tar_bit_pos (int) –
  • rads (float) –
Returns:

Return type:

None

write_Rz(tar_bit_pos, rads)[source]

Writes ROTZ = exp(1j*rads*sig_z) with no controls.

Parameters:
  • tar_bit_pos (int) –
  • rads (float) –
Returns:

Return type:

None

write_S(tar_bit_pos, herm=False)[source]

Writes P1PH = exp(1j*P_1*pi/2) or its Hermitian with no controls.

Parameters:
  • tar_bit_pos (int) –
  • herm (bool) –
Returns:

Return type:

None

write_T(tar_bit_pos, herm=False)[source]

Writes P1PH = exp(1j*P_1*pi/4) or its Hermitian with no controls.

Parameters:
  • tar_bit_pos (int) –
  • herm (bool) –
Returns:

Return type:

None

write_U2(tar_bit_pos, rads_list)[source]

Writes

UN_2= exp(1j*(rads0 + rads1*sig_x + rads2*sig_y + rads3*sig_z))

with no controls.

Parameters:
  • tar_bit_pos (int) –
  • rads_list (list[float]) – [rads0, rads1, rads2, rads3]
Returns:

Return type:

None

write_X(tar_bit_pos)[source]

Writes SIGX with no controls.

Parameters:tar_bit_pos (int) –
Returns:
Return type:None
write_Y(tar_bit_pos)[source]

Writes SIGY with no controls.

Parameters:tar_bit_pos (int) –
Returns:
Return type:None
write_Z(tar_bit_pos)[source]

Writes SIGZ with no controls.

Parameters:tar_bit_pos (int) –
Returns:
Return type:None
write_ZF_or_ZL_pic_line(pic_line)[source]

Writes a line in the Picture file using either the ZF or ZL conventions. pic_line is originally written in ZL format, so this method does nothing to pic_line if ZL option is chosen but reverses order of gates if ZF option chosen.

Parameters:pic_line (str) –
Returns:
Return type:None
write_c_P1PH(control_bit, target_bit, rads=<sphinx.ext.autodoc.importer._MockObject object>, kind=True)[source]

Writes a simple singly controlled P1PH. If kind=True (resp. False), c_P1PH fires when control is |1> (resp. |0>). When kind= True and rads=p1, c_P1PH equals (-1)^{n(t)n(c)} = sigz(t)^{n(c)} where c is the control and t is the target. This is often called a controlled Z, and denoted by Cz.

Parameters:
  • control_bit (int) –
  • target_bit (int) –
  • rads (float) –
  • kind (bool) –
Returns:

Return type:

None

write_cnot(control_bit, target_bit, kind=True)[source]

Writes a simple singly controlled not. If kind=True (resp. False), cnot fires when control is |1> (resp. |0>).

Parameters:
  • control_bit (int) –
  • target_bit (int) –
  • kind (bool) –
Returns:

Return type:

None

write_controlled_diag_unitary_gate(trols, rad_angles)[source]

Writes a line in eng & pic files for a diagonal unitary gate ‘DIAG’ with >= 0 controls of either int (integer, intrinsic) or True/False kind.

Parameters:
  • trols (Controls) –
  • rad_angles (list[float]) –
Returns:

Return type:

None

write_controlled_multiplexor_gate(tar_bit_pos, trols, rad_angles)[source]

Writes a line in eng & pic files for a multiplexor ‘MP_Y’ with >= 0 controls of either int (integer, intrinsic) or True/False kind.

Parameters:
  • tar_bit_pos (int) –
  • trols (Controls) –
  • rad_angles (list[float]) –
Returns:

Return type:

None

write_controlled_one_qbit_gate(tar_bit_pos, trols, one_qbit_gate_fun, fun_arg_list=None)[source]

Writes a line in eng & pic files for a one bit gate (from class OneQubitGate) with >= 0 controls.

Parameters:
  • tar_bit_pos (int) –
  • trols (Controls) –
  • one_qbit_gate_fun (function) – maps Any->np.ndarray
  • fun_arg_list (list) –
Returns:

Return type:

None

write_controlled_preamble(trols)[source]

This is an internal function, used as preamble to all methods that are named write_controlled_…()

Parameters:trols (Controls) –
Returns:
Return type:Controls
write_controlled_qbit_swap(bit1, bit2, trols, rads_list=None)[source]

If rads_list=None, this method writes a line in eng & pic files for a ‘SWAP’ with >= 0 controls

NOTE: SWAP is qbit symmetric: SWAP(0,1) = SWAP(1,0)

If rads_list is not None and equals a list of 2 angles, rads_list=[ rads0, rads1], this method writes a generalization of SWAP that I call SWAY (just to have a verb that is close to swap)

SWAY = [1 0 0] [0 U2 0] [0 0 1]

where U2 is the most general 2-dim unitary matrix satisfying

sigx U2 sigx = U2.

If U2 is parametrized as

U2 = exp(j*(rads0 + rads1*sigx + rads2*sigy + rads3*sigz))

then SWAY is qbit symmetric (SWAY(0,1)=SWAY(1,0)) iff sigx U2 sigx = U2 iff rads2=rads3=0.

SWAY includes SWAP, sqrt(SWAP), iSWAP, sqrt(iSWAP), PWAP, sqrt(PSWAP) etc.

Parameters:
  • bit1 (int) –
  • bit2 (int) – bit1 and bit2 are the positions of the swapped bits.
  • trols (Controls) –
  • rads_list (list[float | str ] | None) –
Returns:

Return type:

None

write_cz(control_bit, target_bit, kind=True)[source]

Writes a simple singly controlled Z. If kind=True (resp. False), cz fires when control is |1> (resp. |0>).

Parameters:
  • control_bit (int) –
  • target_bit (int) –
  • kind (bool) –
Returns:

Return type:

None

write_diag_unitary_gate(controls, rad_angles)[source]

Write a line in eng & pic files for a diagonal unitary ‘DIAG’ with no T/F controls.

Parameters:
  • controls (Controls) –
  • rad_angles (list[float]) –
Returns:

Return type:

None

write_global_phase_fac(ang_rads)[source]

Write a line in eng & pic files for a global phase factor ‘PHAS’ with no controls.

Parameters:ang_rads (float) –
Returns:
Return type:None
write_multiplexor_gate(tar_bit_pos, controls, rad_angles)[source]

Write a line in eng & pic files for a multiplexor ‘MP_Y’ with no T/F controls.

Parameters:
  • tar_bit_pos (int) –
  • controls (Controls) –
  • rad_angles (list[float]) –
Returns:

Return type:

None

write_one_qbit_gate(tar_bit_pos, one_qbit_gate_fun, fun_arg_list=None)[source]

Write a line in eng & pic files for a one qubit gate (from class OneQubitGate) with no controls.

Parameters:
  • tar_bit_pos (int) –
  • one_qbit_gate_fun (function) –
  • fun_arg_list (list) –
Returns:

Return type:

None

write_qbit_swap(bit1, bit2, rads_list=None)[source]

Write a line in eng & pic files for a ‘SWAP’ if rads_list=None or ‘SWAY’ if rads_list!=None, with no controls.

Parameters:
  • bit1 (int) –
  • bit2 (int) –
  • rads_list (list[float | str] | None) –
Returns:

Return type:

None