qubiter.SEO_simulator module

class qubiter.SEO_simulator.SEO_simulator(file_prefix, num_bits, init_st_vec=None, **kwargs)[source]

Bases: qubiter.SEO_reader.SEO_reader

This class simulates the evolution of a quantum state vector.

This class has SEO_reader as a parent. Each line of an English file is read by the parent class and handed over to the use_ functions of this simulator class. The use_ functions multiply the current state vector by the unitary matrix that represents the latest line read.

An initial state vector can be entered via the constructor or else it is set to the ground state automatically by the constructor.

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.

self.cur_st_vec_dict is a dictionary of strings (called branch keys) to state vectors StateVec on num_bits qubits. We will refer to each state vec in the dict as a branch. Initially, this dict contains a single branch with branch key = “pure”. A measurement MEAS of kinds 0 or 1 does not change the number of branches in the dict, but a measurement of kind 2 doubles their number.

Note that since projectors are not unitary matrices, the branches of cur_st_vec_dict are not expected have normalized state vectors as values except when there is only a single branch.

If cur_st_vec_dict contains as values the states |br0>, |br1>, |br2>, ...], then one can construct the density matrix of that state as rho = |br0><br0| + |br1><br1| + |br2><br2| + … divided by a number so that trace(rho)=1. In other words, cur_st_vec_dict is just a particular way of storing the density matrix of a state. A state with a single branch is a pure state, but a state with more than one branch may not be.

An item of cur_st_vec_dict may be key=some string, value=None. This means the state vector of that branch is zero.

Variables:
  • cached_sts (dict[int, dict(str, StateVec)]) – A dictionary mapping an int to past values of self.cur_st_vec_dict. Used by use_PRINT() sometimes.
  • cur_st_vec_dict (dict(str, StateVec)) – dictionary with key= branch_key string and value= StateVec|None. If there is a single item in dict, the dict represents a pure state and the key is “pure”. If there is more than one item, the dict represents a mixed state and each branch key is a string that uniquely characterizes the measured controls. For example, if it has been measured previously (type 2 measurement only) that qubit 2 is True and qubit 4 is False, the branch key will be ‘4F2T’.
  • lib (str) – tensor library. Either ‘np’ for numpy or ‘tf’ for tensorflow
  • tensordot (function) –
  • transpose (function) –
  • use_tf (bool) – True iff using TensorFlow Eager. False by default
__init__(file_prefix, num_bits, init_st_vec=None, **kwargs)[source]

Constructor

Parameters:
  • file_prefix (str) –
  • num_bits (int) –
  • init_st_vec (StateVec) – get this using the functions StateVec.get_ground_st_vec() or StateVec.get_standard_basis_st_vec().
static branch_is_part_of_mcase(br_trols, case_trols)[source]

Returns True iff the controls br_trols defining a branch of self.cur_st_vec_dict agree with the controls case_trols defining a case measured.

Parameters:
Returns:

Return type:

bool

convert_tensors_to_numpy()[source]

This method is meant to be overridden to replace tensorflow tensors (or some other non-numpy tensor type) by numpy tensors.

convert_tensors_to_tf()[source]

This method is meant to be overridden to replace numpy tensors by tensorflow tensors (or some other non-numpy tensor type).

describe_st_vec_dict(**kwargs)[source]

Calls method with same name in class StateVec. It prints a description of the current state vector dictionary. Can call this at the end, after running through whole circuit.

Parameters:kwargs (dict[]) –
Returns:
Return type:None
do_array_assignment_workaround(br_key, slicex, sub_arr)[source]

internal function used in evolve_ methods iff autograd is on or use_tf is True. Should have same effect as

self.cur_st_vec_dict[br_key].arr[slicex] = sub_arr

Parameters:
  • br_key (str) –
  • slicex (tuple) –
  • sub_arr (np.ndarray) –
Returns:

Return type:

None

do_more_init_before_reading()[source]

Stub. Called in SEO_simulator.__init__ immediately before calling SEO_reader.__init__

Returns:
Return type:None
evolve_by_controlled_bit_swap(bit1, bit2, controls)[source]

Evolve each branch of cur_st_vec_dict by controlled bit swap iff the bit swap line is (1) outside of any IF_M block, or (2) it is inside such a block, and it satisfies self.mcase_trols.

Parameters:
  • bit1 (int) –
  • bit2 (int) – bit1 and bit2 are the positions of the 2 bits being swapped.
  • controls (Controls) –
Returns:

Return type:

None

evolve_by_controlled_one_bit_gate(tar_bit_pos, controls, one_bit_gate)[source]

Evolve each branch of cur_st_vec_dict by controlled one bit gate ( from class OneBitGates) iff the controlled one bit gate line is (1) outside of an IF_M block, or (2) it is inside such a block, and it satisfies self.mcase_trols. Note one_bit_gate is entered as np.ndarray.

Parameters:
  • tar_bit_pos (int) – bit position of target of one bit gate.
  • controls (Controls) –
  • one_bit_gate (np.ndarray) –
Returns:

Return type:

None

finalize_next_line()[source]

Prints running documentary at the end of the reading of each line.

Returns:
Return type:None

Say new_bit_pos=2 and new_kind=True. This returns a new branch key with ‘2T’ added to the end of the string br_key

Parameters:
  • br_key (str) –
  • new_bit_pos (int) –
  • new_kind (bool) –
Returns:

Return type:

str

get_controls_from_br_key(br_key)[source]

Returns a Controls object built from br_key. br_key is assumed to be a str key for self.cur_st_vec_dict

Parameters:br_key (str) –
Returns:
Return type:Controls
get_counts(num_shots, omit_zero_counts=True, use_bin_labels=True, rand_seed=None)[source]

This method calculates a probability distribution that we call pd from the current state vector if it is pure. (If the state vec is not pure, it calculates a density matrix from the self.cur_st_vec_dict. Then it extracts the diagonal of that density matrix. That diagonal must be a probability distribution that we call pd.) Then the method samples pd, num_shots times. The method returns the result of that sampling as an OrderedDict state_name_to_counts. Depending on the value of the flag use_bin_labels, the state names are a string ‘0’, ‘1’, ‘2’, etc, or their binary representations followed by ‘ZL’, because the ZL convention is assumed.

Parameters:
  • num_shots (int) –
  • omit_zero_counts (bool) –
  • use_bin_labels (bool) –
  • rand_seed (int) –
Returns:

Return type:

OrderedDict[str, int]

reshape

Used by autodoc_mock_imports.

tensordot

Used by autodoc_mock_imports.

transpose

Used by autodoc_mock_imports.

use_DIAG(trols, rad_angles)[source]

This method should not be called. If called, it explains why it shouldn’t be called.

Parameters:
  • trols (Controls) –
  • rad_angles (list[float]) –
use_HAD2(tar_bit_pos, controls)[source]

Overrides the parent class use_ function. Calls evolve_by_controlled_one_bit_gate() for had2.

Parameters:
  • tar_bit_pos (int) –
  • controls (Controls) –
Returns:

Return type:

None

use_IF_M_beg(controls)[source]

Do nothing.

Parameters:controls (Controls) –
Returns:
Return type:None
use_IF_M_end()[source]

Do nothing.

Returns:
Return type:None
use_MEAS(tar_bit_pos, kind)[source]

Overrides the parent class use_ function.

For kind 0 (resp., 1) measurements, it applies |0><0| (resp., |1><1|) to each branch of cur_st_vec_dict.

For kind 2 measurements, it first doubles the number of branches in cur_st_vec_dict by adding a deep copy of each branch. Next, it applies P_0 to half of the branches of the dict and P_1 to the other half.

Parameters:
  • kind (int) –
  • tar_bit_pos (int) –
Returns:

Return type:

None

use_MP_Y(tar_bit_pos, trols, rad_angles)[source]

This method should not be called. If called, it explains why it shouldn’t be called.

Parameters:
  • tar_bit_pos (int) –
  • trols (Controls) –
  • rad_angles (list[float]) –
use_NOTA(bla_str)[source]

Do nothing.

Parameters:bla_str (str) –
Returns:
Return type:None
use_PHAS(angle_rads, tar_bit_pos, controls)[source]

Overrides the parent class use_ function. Calls evolve_by_controlled_one_bit_gate() for PHAS.

Parameters:
  • angle_rads (float) –
  • tar_bit_pos (int) –
  • controls (Controls) –
Returns:

Return type:

None

use_PRINT(style, line_num)[source]

Prints to screen a description of self.cur_st_vec_dict.

Parameters:
  • style (str) – style in which to print
  • line_num (int) – line number in eng & pic files in which PRINT command appears
Returns:

Return type:

None

use_P_PH(projection_bit, angle_rads, tar_bit_pos, controls)[source]

Overrides the parent class use_ function. Calls evolve_by_controlled_one_bit_gate() for P_0 and P_1 phase factors.

Parameters:
  • projection_bit (int) – 0 (resp. 1) for P_0 (resp. P_1) projection
  • angle_rads (float) –
  • tar_bit_pos (int) –
  • controls (Controls) –
Returns:

Return type:

None

use_ROTA(axis, angle_rads, tar_bit_pos, controls)[source]

Overrides the parent class use_ function. Calls evolve_by_controlled_one_bit_gate() for rot along axes x, y, or z.

Parameters:
  • axis (int) – 1, 2, 3 for x, y, z
  • angle_rads (float) –
  • tar_bit_pos (int) –
  • controls (Controls) –
Returns:

Return type:

None

use_ROTN(angle_x_rads, angle_y_rads, angle_z_rads, tar_bit_pos, controls)[source]

Overrides the parent class use_ function. Calls evolve_by_controlled_one_bit_gate() for rot along arbitrary axis.

Parameters:
  • angle_x_rads (float) –
  • angle_y_rads (float) –
  • angle_z_rads (float) –
  • tar_bit_pos (int) –
  • controls (Controls) –
Returns:

Return type:

None

use_SIG(axis, tar_bit_pos, controls)[source]

Overrides the parent class use_ function. Calls evolve_by_controlled_one_bit_gate() for sigx, sigy, sigz.

Parameters:
  • axis (int) – 1, 2, 3 for x, y, z
  • tar_bit_pos (int) –
  • controls (Controls) –
Returns:

Return type:

None

use_SWAP(bit1, bit2, controls)[source]

Overrides the parent class use_ function. Calls evolve_by_controlled_bit_swap().

Parameters:
  • bit1 (int) –
  • bit2 (int) –
  • controls (Controls) –
Returns:

Return type:

None

use_SWAY(bit1, bit2, controls, rads_list)[source]

Overrides the parent class use_ function. Calls evolve_by_controlled_one_bit_gate() 3 times.

This relies on the fact that

SWAY(0, 1) = SWAY(1, 0) = X—@ @—U2 X—@

U2 = exp(j*(rads0 + rads1*sig_x))

rads_list = [rads0, rads1]

Parameters:
  • bit1 (int) –
  • bit2 (int) –
  • controls (Controls) –
  • rads_list (list[float]) –
Returns:

Return type:

None

use_U_2_(rads0, rads1, rads2, rads3, tar_bit_pos, controls)[source]

Overrides the parent class use_ function. Calls evolve_by_controlled_one_bit_gate() for arbitrary unitary 2-dim matrix.

Parameters:
  • rads0 (float) –
  • rads1 (float) –
  • rads2 (float) –
  • rads3 (float) –
  • tar_bit_pos (int) –
  • controls (Controls) –
Returns:

Return type:

None