qubiter.device_specific.Qubiter_to_AnyQasm module¶
-
class
qubiter.device_specific.Qubiter_to_AnyQasm.
Qubiter_to_AnyQasm
(file_prefix, num_qbits, aqasm_name='', strict_mode=False, c_to_tars=None, write_qubiter_files=False, vars_manager=None, aqasm_ftype='txt', prelude_str=None, ending_str=None, **kwargs)[source]¶ Bases:
qubiter.SEO_reader.SEO_reader
This abstract class is a child of SEO_reader. It reads an input English file and writes an AnyQasm file that is a translation of the input English file into the AnyQasm language. If the flag write_qubiter_files is set to True, this class will also write new English and Picture files that are in 1-1 onto line correspondence with the output AnyQasm file.
Footnote: Some AnyQasm’s distinguish between quantum registers qreg and classical registers creg. Qubiter does not use cregs because it uses the classical memory of your Linux PC instead. AnyQasm has an intricate set of commands for measurements. Qubiter has a complete set of measurement commands too (see MEAS in Rosetta stone). The AnyQasm and Qubiter measurement commands can obviously be translated into each other. We leave that part of the translation to a future version of this class.
This class can run in either a strict or a non-strict mode depending on the flag strict_mode, which equals False in default mode. In the strict mode, the set of gates allowed is constrained to a small but universal set that is specified below, and that is allowed in any target qasm. In the non-strict mode, more gates are allowed that depend on specific target qasm. In the strict mode, the program will end if you try to use gates that are not allowed. In the non-strict mode, the program will end if you try to use gates for a target language that have not been implemented yet in the Qubiter class targeting that language, often because the target language doesn’t support those gates.
Will refer to target qasm as AnyQasm or aqasm
Next we give a description of the strict_mode:
In the strict mode, the input English file that is read can only have lines of the following types or else the program will abort with an error message:
1. single qubit rotations (HAD2, SIGX, SIGY, SIGZ, ROTX, ROTY, ROTZ or ROTN with no controls)
2. simple CNOTs (SIGX with a single True control). Call them c->t=( c, t) if c is the control and t the target. (c, t) must be allowed by ‘c_to_tars’.
- NOTA or PRINT lines. PRINT lines are commented out.
If you have an English file that contains lines that are more complicated than this (because, for example, they contain rotations with one or more controls attached, or because a CNOT is not allowed according to ‘c_to_tars’), you can use the expander classes CGateExpander, DiagUnitaryExpander, MultiplexorExpander, and ForbiddenCNotExpander to expand the circuit to an equivalent albeit longer circuit that satisfies constraints 1, 2, 3.
This class can handle a chip with any number of qubits.
This class halts execution if it encounters a CNOT that is disallowed according to the input ‘c_to_tars’. ‘c_to_tars’ varies with chip. Some ‘c_to_tars’s are listed in the files ‘chip_couplings_…’ found in same folder as this file. If c_to_tars = None, the class assumes any CNOT is possible.
Variables: - all_fun_names (list[str]) – a list of all the distinct function names encountered in circuit
- all_var_nums (list[int]) – a list of all distinct numbers of the variables encountered in circuit
- aqasm_name (str) – the name of the aqasm language, for example, IBMqasm. Used as ending of file name, between ‘_’ and ‘.txt’
- aqasm_path (str) – path to aqasm file
- aqasm_out (_io.TextIOWrapper) – This output stream is used to write an aqasm file based on the input English file.
- c_to_tars (dict[int, list[int]]) – a dictionary mapping j in range(num_qbits) to a list, possibly empty, of the physically allowed targets of qubit j, when j is the control of a CNOT. If c_to_tars = None, the class assumes any CNOT is possible.
- file_prefix (str) –
- num_qbits (int) –
- qbtr_wr (SEO_writer) – A SEO_writer object created iff write_qubiter_files is True.
- strict_mode (bool) –
- vprefix (str) – all variables in aqasm file will be called vprefix + an int
- write_qubiter_files (bool) – The class always writes an AnyQasm text file based on the input English file that is read. Iff this is True, the class also writes English and Picture files in 1-1 line correspondence with the output AnyQasm file
-
__init__
(file_prefix, num_qbits, aqasm_name='', strict_mode=False, c_to_tars=None, write_qubiter_files=False, vars_manager=None, aqasm_ftype='txt', prelude_str=None, ending_str=None, **kwargs)[source]¶ Constructor
Parameters: - file_prefix (str) –
- num_qbits (int) –
- aqasm_name (str) –
- strict_mode (bool) –
- c_to_tars (dict[int, list[int]]|None) –
- write_qubiter_files (bool) –
- vars_manager (PlaceholderManager) –
- aqasm_ftype (str) – file type of output aqasm file. If this equals ‘txt’, name of aqasm file will end in ‘.txt’
- prelude_str (str | None) – string to write as prelude to aqasm file. If None, then the override method of self.write_prelude() is called
- ending_str (str | None) – string to write as ending to aqasm file. If None, then the override method of self.write_ending() is called
-
new_var_name
(var_name, coda='', strict=False)[source]¶ Starts by asserting that var_name is a legal variable name.
If var_name is not functional, this method replaces # in var_name by self.vprefix and adds coda to end of string. For example, if self.vprefix=’rads’ and var_name=’-#2*.5”, then output is ‘-rads2*.5’ + coda
If var_name is functional, this method replaces each # in var_name by self.vprefix, adds commas and parenthesis, and adds coda to end of string. For example, if self.vprefix=’rads’ and var_name=’-fun#1#2’, then output is ‘-fun(rads1, rads2)’ + coda
The above applies only if strict=False. In the strict mode, only an empty coda is allowed for functional placeholders. For non-functional placeholders, if var_name contains an *, then the str after the * and the coda are merged using eval().
Parameters: - var_name (str) –
- coda (str) –
- strict (bool) –
Returns: Return type: str
-
write
(s)[source]¶ Writes string s to aqasm and qubiter out files.
Parameters: s (str) – Returns: Return type: None