qubiter.adv_applications.StairsDerivCkt_writer module¶
-
class
qubiter.adv_applications.StairsDerivCkt_writer.
StairsDerivCkt_writer
(deriv_gate_str, has_neg_polarity, deriv_direc, dpart_name, gate_str_to_rads_list, file_prefix, emb, **kwargs)[source]¶ Bases:
qubiter.SEO_writer.SEO_writer
This class is a subclass of SEO_writer. It writes several intermediary stairs derivative circuits that will be used in class StairsDeriv_native for calculating the gradients of a quantum cost function (mean hamiltonian).
Suppose U = exp[i*(t_0 + t_1*sigx + t_2*sigy + t_3*sigz)], where sigx, sigy, sigz are the Pauli matrices and t_r for r in range(4) are 4 real parameters. To take the derivative wrt t_r of a given multi-controlled gate U in a stairs circuit, we need to evaluate several circuits (we call them dparts, which stands for derivative parts). Say, for instance, that
GATE= @---O---+---U
. To calculate d/dt_r GATE(t_0, t_1, t_2, t_3), for r=0,1, 2, 3, we need to calculate a new circuit wherein the GATE in the parent circuit is replaced by:sum_k c_k @---@---O---+---U_k
(which is said to have `has_neg_polarity`=False) and:
sum_k c_k @---@---O---+---U_k Z---@---O | |
(which is said to have `has_neg_polarity`=True)
Also, some extra stuff (a coda) must be appended to the end of the parent stairs circuit.
Note that an extra “ancilla” qbit has been added (as the new last qubit) to the parent stairs circuit being differentiated. So if the parent stairs circuit has a number parent_num_qbits of qubits, then the one written by this class has that many qubits plus one.
The index r which is in range(4) is called the derivative direction ( deriv_direc)
gate_str_to_rads_list is the same as for the parent stairs circuit.
deriv_gate_str is a well formed gate_str that specifies which U is being differentiated
The index k is given as a string called dpart_name (“dpart” stands for derivative part).
The coefficients c_k can be obtained via the method get_coef_of_dpart()
Each U_k is a U(2) matrix itself, and its 4 parameters are defined in terms of the parameters tlist=[t_0, t_1, t_2, t_3] of the U(tlist) being differentiated, via 4 functions of tlist. These functions can be obtained via the method get_fun_name_to_fun().
Variables: - deriv_direc (int) – in range(4)
- deriv_gate_str (str) –
- dpart_name (str) –
- gate_str_to_rads_list (dict[str, list[float]]) –
- has_neg_polarity (bool) –
-
__init__
(deriv_gate_str, has_neg_polarity, deriv_direc, dpart_name, gate_str_to_rads_list, file_prefix, emb, **kwargs)[source]¶ Constructor
This constructor writes English and Picture files but it doesn’t close those files after writing them. You must do that yourself using close_files().
Parameters: - deriv_gate_str (str) –
- has_neg_polarity (bool) –
- deriv_direc (int) – in range(4)
- dpart_name (str) –
- gate_str_to_rads_list (dict[int, list[float]]) –
- file_prefix (str) –
- emb (CktEmbedder) –
- kwargs (dict) – key-word arguments of SEO_writer
-
static
float_t_list
(t_list, var_num_to_rads)[source]¶ Internal method that checks whether t_list is of form list[str] or list[float]. In first case, it uses var_num_to_rads to replace t_list by a list[float].
Parameters: - t_list (list[float|str]) –
- var_num_to_rads (list[int, float]) –
Returns: Return type: list[float]
-
static
get_coef_of_dpart
(t_list, deriv_direc, dpart_name, var_num_to_rads=None)[source]¶ This method returns coefficient of dpart (derivative part), either p1, ps or -p1*ps
var_num_to_rads is used if self wrote the English file with #int string symbols for placeholder variables. var_num_to_rads is used to float those strings.
Parameters: - t_list (list[float | str]) –
- deriv_direc (int) – int in range(4)
- dpart_name (str) –
- var_num_to_rads (dict[int, float] | None) –
Returns: Return type: float
-
static
get_fun_name_to_fun
(t_list, deriv_direc, dpart_name)[source]¶ This method returns a dictionary fun_name_to_fun mapping the function name to function, for all functions defined by this class.
Parameters: - t_list (list[float | str]) –
- deriv_direc (int) –
- dpart_name (str) –
Returns: Return type: dict[str, function]
-
static
get_rads_list_of_dpart
(t_list, deriv_direc, dpart_name, var_num_to_rads=None)[source]¶ This method returns the rads list (list of 4 floats) for a given given: 1. the rads list t_list of the U that is being differentiated, 2. the dpart (derivative part), and 3. which of 4 directions deriv_direc the derivative is wrt.
var_num_to_rads is used if self wrote the English file with #int string symbols for placeholder variables. var_num_to_rads is used to float those strings.
Parameters: - t_list (list[float | str]) –
- deriv_direc (int) – int in range(4)
- dpart_name (str) –
- var_num_to_rads (dict[int, float] | None) –
Returns: Return type: list[float]
-
get_u2_pos
(gate_str)[source]¶ Given a well formed gate_str (one of the keys of gate_str_to_rads_list), this method returns the bit position of the U(2) matrix.
Class StairsCkt_writer has a method of same name but which returns a different value for the same gate_str input. This is due to the fact that the circuit generated by this class has an extra ancilla qubit compared with its parent circuit.
Parameters: gate_str (str) – Returns: Return type: int