Utilities package¶
Qutip utilities module¶
Useful functions to get basis vectors and matrices of the right size.
- c3.utils.qt_utils.T1_sequence(length, target)[source]¶
Generate a gate sequence to measure relaxation time in a two-qubit chip.
- Parameters
length (int) – Number of Identity gates.
target (int) – Which qubit is measured.
- Returns
Relaxation sequence.
- Return type
list
- c3.utils.qt_utils.basis(lvls: int, pop_lvl: int) array [source]¶
Construct a basis state vector.
- Parameters
lvls (int) – Dimension of the state.
pop_lvl (int) – The populated entry.
- Returns
A normalized state vector with one populated entry.
- Return type
np.array
- c3.utils.qt_utils.expand_dims(op, dim)[source]¶
pad operator with zeros to be of dimension dim Attention! Not related to the TensorFlow function
- c3.utils.qt_utils.get_basis_matrices(dim)[source]¶
Basis matrices with single ones of the matrices with given dimensions.
- c3.utils.qt_utils.hilbert_space_kron(op, indx, dims)[source]¶
Extend an operator op to the full product hilbert space given by dimensions in dims.
- Parameters
op (np.array) – Operator to be extended.
indx (int) – Position of which subspace to extend.
dims (list) – New dimensions of the subspace.
- Returns
Extended operator.
- Return type
np.array
- c3.utils.qt_utils.insert_mat_kron(dims, target_ids, matrix) ndarray [source]¶
Insert matrix at given indices. All other spaces are filled with zeros.
- Parameters
dims (dimensions of each qubit subspace) –
target_ids (qubits to apply matrix to) –
matrix (matrix to be applied) –
- Return type
composed matrix
- c3.utils.qt_utils.inverseC(sequence)[source]¶
Find the clifford to end a sequence s.t. it returns identity.
- c3.utils.qt_utils.kron_ids(dims, indices, matrices)[source]¶
Kronecker product of matrices at specified indices with identities everywhere else.
- c3.utils.qt_utils.pad_matrix(matrix, dim, padding)[source]¶
Fills matrix dimensions with zeros or identity.
- c3.utils.qt_utils.pauli_basis(dims=[2])[source]¶
Qutip implementation of the Pauli basis.
- Parameters
dims (list) – List of dimensions of each subspace.
- Returns
A square matrix containing the Pauli basis of the product space
- Return type
np.array
- c3.utils.qt_utils.perfect_cliffords(lvls: List[int], proj: str = 'fulluni', num_gates: int = 1)[source]¶
Legacy function to compute the clifford gates.
- c3.utils.qt_utils.perfect_parametric_gate(paulis_str, ang, dims)[source]¶
Construct an ideal parametric gate.
- Parameters
paulis_str (str) –
- Names for the Pauli matrices that identify the rotation axis. Example:
”X” for a single-qubit rotation about the X axis
”Z:X” for an entangling rotation about Z on the first and X on the second qubit
ang (float) – Angle of the rotation
dims (list) – Dimensions of the subspaces.
- Returns
Ideal gate.
- Return type
np.array
- c3.utils.qt_utils.perfect_single_q_parametric_gate(pauli_str, target, ang, dims)[source]¶
Construct an ideal parametric gate.
- Parameters
paulis_str (str) –
- Name for the Pauli matrices that identify the rotation axis. Example:
”X” for a single-qubit rotation about the X axis
ang (float) – Angle of the rotation
dims (list) – Dimensions of the subspaces.
- Returns
Ideal gate.
- Return type
np.array
- c3.utils.qt_utils.projector(dims, indices, outdims=None)[source]¶
Computes the projector to cut down a matrix to the computational space. The subspaces indicated in indeces will be projected to the lowest two states, the rest is projected onto the lowest state. If outdims is defined projection will be performed to those states.
- c3.utils.qt_utils.ramsey_echo_sequence(length, target)[source]¶
Generate a gate sequence to measure dephasing time in a two-qubit chip including a flip in the middle. This echo reduce effects detrimental to the dephasing measurement.
- Parameters
length (int) – Number of Identity gates. Should be even.
target (str) – Which qubit is measured. Options: “left” or “right”
- Returns
Dephasing sequence.
- Return type
list
- c3.utils.qt_utils.ramsey_sequence(length, target)[source]¶
Generate a gate sequence to measure dephasing time in a two-qubit chip.
- Parameters
length (int) – Number of Identity gates.
target (str) – Which qubit is measured. Options: “left” or “right”
- Returns
Dephasing sequence.
- Return type
list
- c3.utils.qt_utils.rotation(phase: float, xyz: array) array [source]¶
General Rotation using Euler’s formula.
- Parameters
phase (np.float) – Rotation angle.
xyz (np.array) – Normal vector of the rotation axis.
- Returns
Unitary matrix
- Return type
np.array
- c3.utils.qt_utils.single_length_RB(RB_number: int, RB_length: int, target: int = 0) List[List[str]] [source]¶
Given a length and number of repetitions it compiles Randomized Benchmarking sequences.
- Parameters
RB_number (int) – The number of sequences to construct.
RB_length (int) – The number of Cliffords in each individual sequence.
target (int) – Index of the target qubit
- Returns
List of RB sequences.
- Return type
list
- c3.utils.qt_utils.two_qubit_gate_tomography(gate)[source]¶
Sequences to generate tomography for evaluating a two qubit gate.
- c3.utils.qt_utils.xy_basis(lvls: int, vect: str)[source]¶
Construct basis states on the X, Y and Z axis.
- Parameters
lvls (int) – Dimensions of the Hilbert space.
vect (str) –
Identifier of the state. Options:
’zp’, ‘zm’, ‘xp’, ‘xm’, ‘yp’, ‘ym’
- Returns
A state on one of the axis of the Bloch sphere.
- Return type
np.array
Tensorflow utilities module¶
Various utility functions to speed up tensorflow coding.
- c3.utils.tf_utils.get_tf_log_level()[source]¶
Display the current tensorflow log level of the system.
- c3.utils.tf_utils.set_tf_log_level(lvl)[source]¶
Set tensorflows system log level.
- REMARK: it seems like the ‘TF_CPP_MIN_LOG_LEVEL’ variable expects a string.
the input of this function seems to work with both string and/or integer, as casting string to string does nothing. feels hacked? but I guess it’s just python…
- c3.utils.tf_utils.tf_average_fidelity(A, B, lvls=None)[source]¶
A very useful but badly named fidelity measure.
- c3.utils.tf_utils.tf_choi_to_chi(U, dims=None)[source]¶
Convert the choi representation of a process to chi representation.
- c3.utils.tf_utils.tf_convolve(sig: Tensor, resp: Tensor)[source]¶
Compute the convolution with a time response.
- Parameters
sig (tf.Tensor) – Signal which will be convoluted, shape: [N]
resp (tf.Tensor) – Response function to be convoluted with signal, shape: [M]
- Returns
convoluted signal of shape [N]
- Return type
tf.Tensor
- c3.utils.tf_utils.tf_convolve_legacy(sig: Tensor, resp: Tensor)[source]¶
Compute the convolution with a time response. LEGACY version. Ensures compatibility with the previous response implementation.
- Parameters
sig (tf.Tensor) – Signal which will be convoluted, shape: [N]
resp (tf.Tensor) – Response function to be convoluted with signal, shape: [M]
- Returns
convoluted signal of shape [N]
- Return type
tf.Tensor
- c3.utils.tf_utils.tf_diff(l)[source]¶
Running difference of the input list l. Equivalent to np.diff, except it returns the same shape by adding a 0 in the last entry.
- c3.utils.tf_utils.tf_dmket_fid(rho, psi)[source]¶
Fidelity between a state vector and a density matrix.
- c3.utils.tf_utils.tf_kron(A, B)[source]¶
Kronecker product of 2 matrices. Can be applied with batch dimmensions.
- c3.utils.tf_utils.tf_list_avail_devices()[source]¶
List available devices.
Function for displaying all available devices for tf_setuptensorflow operations on the local machine.
- TODO: Refine output of this function. But without further knowledge
about what information is needed, best practise is to output all information available.
- c3.utils.tf_utils.tf_log_level_info()[source]¶
Display the information about different log levels in tensorflow.
- c3.utils.tf_utils.tf_matmul_left(dUs: Tensor)[source]¶
- Parameters
dUs – tf.Tensor Tensorlist of shape (N, n,m) with number N matrices of size nxm
Multiplies a list of matrices from the left.
- c3.utils.tf_utils.tf_matmul_n(tensor_list: Tensor, folding_stack: List[Callable]) Tensor [source]¶
Multipy a list of tensors using a precompiled stack of function to apply to each layer of a binary tree.
- Parameters
tensor_list (List[tf.Tensor]) – Matrices to be multiplied.
folding_stack (List[Callable]) – List of functions to multiply each layer.
- Returns
Reduced product of matrices.
- Return type
tf.Tensor
- c3.utils.tf_utils.tf_matmul_right(dUs)[source]¶
- Parameters
dUs – tf.Tensor Tensorlist of shape (N, n,m) with number N matrices of size nxm
Multiplies a list of matrices from the right.
- c3.utils.tf_utils.tf_measure_operator(M, rho)[source]¶
Expectation value of a quantum operator by tracing with a density matrix.
- Parameters
M (tf.tensor) – A quantum operator.
rho (tf.tensor) – A density matrix.
- Returns
Expectation value.
- Return type
tf.tensor
- c3.utils.tf_utils.tf_project_to_comp(A, dims, index=None, to_super=False)[source]¶
Project an operator onto the computational subspace.
- c3.utils.tf_utils.tf_superoper_average_fidelity(A, B, lvls=None)[source]¶
A very useful but badly named fidelity measure.
- c3.utils.tf_utils.tf_unitary_overlap(A: Tensor, B: Tensor, lvls: Optional[Tensor] = None) Tensor [source]¶
Unitary overlap between two matrices.
- Parameters
A (tf.Tensor) – Unitary A
B (tf.Tensor) – Unitary B
lvls (tf.Tensor, optional) – Levels, by default None
- Returns
Overlap between the two unitaries
- Return type
tf.Tensor
- Raises
TypeError – For errors during cast
ValueError – For errors during matrix multiplicaton
Log Reader utilities module¶
- c3.utils.log_reader.show_table(log: Dict[str, Any], console: Console) None [source]¶
Generate a rich table from an optimization status and display it on the console.
- Parameters
log (Dict) – Dictionary read from a json log file containing a c3-toolset optimization status.
console (Console) – Rich console for output.
Miscellaneous utilities module¶
Miscellaneous, general utilities.
- c3.utils.utils.deprecated(message: str)[source]¶
Decorator for deprecating functions
- Parameters
message (str) – Message to display along with DeprecationWarning
Examples
Add a
@deprecated("message")
decorator to the function:@deprecated("Using standard width. Better use gaussian_sigma.") def gaussian(t, params): ...
- c3.utils.utils.eng_num(val: float) Tuple[float, str] [source]¶
Convert a number to engineering notation by returning number and prefix.
- c3.utils.utils.flatten(lis: ~typing.List, ltypes=(<class 'list'>, <class 'tuple'>)) List [source]¶
Flatten lists of arbitrary lengths https://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html
- Parameters
lis (List) – The iterable to flatten
ltypes (tuple, optional) – Possibly the datatype of the iterable, by default (list, tuple)
- Returns
Flattened list
- Return type
List
- c3.utils.utils.log_setup(data_path: Optional[str] = None, run_name: str = 'run') str [source]¶
Make sure the file path to save data exists. Create an appropriately named folder with date and time. Also creates a symlink “recent” to the folder.
- Parameters
data_path (str) – File path of where to store any data.
run_name (str) – User specified name for the run.
- Returns
The file path to store new data.
- Return type
str