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) numpy.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) numpy.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) –

Returns

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.np_kron_n(mat_list)[source]

Apply Kronecker product to a list of matrices.

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: numpy.array) numpy.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.Id_like(A)[source]

Identity of the same size as A.

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.super_to_choi(A)[source]

Convert a super operator to choi representation.

c3.utils.tf_utils.tf_abs(x)[source]

Rewritten so that is has a gradient.

c3.utils.tf_utils.tf_abs_squared(x)[source]

Rewritten so that is has a gradient.

c3.utils.tf_utils.tf_ave(x: list)[source]

Take average of a list of values in tensorflow.

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: tensorflow.python.framework.ops.Tensor, resp: tensorflow.python.framework.ops.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_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_dm_to_vec(dm)[source]

Convert a density matrix into a density vector.

c3.utils.tf_utils.tf_dmdm_fid(rho, sigma)[source]

Trace fidelity between two density matrices.

c3.utils.tf_utils.tf_dmket_fid(rho, psi)[source]

Fidelity between a state vector and a density matrix.

c3.utils.tf_utils.tf_ketket_fid(psi1, psi2)[source]

Overlap of two state vectors.

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_limit_gpu_memory(memory_limit)[source]

Set a limit for the GPU memory.

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_log10(x)[source]

Tensorflow had no logarithm with base 10. This is ours.

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: tensorflow.python.framework.ops.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)[source]

Multiply a list of tensors as binary tree.

EXPERIMENTAL

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_setup()[source]
c3.utils.tf_utils.tf_spost(A)[source]

Superoperator on the right of matrix A.

c3.utils.tf_utils.tf_spre(A)[source]

Superoperator on the left of matrix A.

c3.utils.tf_utils.tf_state_to_dm(psi_ket)[source]

Make a state vector into a density matrix.

c3.utils.tf_utils.tf_super(A)[source]

Superoperator from both sides of matrix A.

c3.utils.tf_utils.tf_super_to_fid(err, lvls)[source]

Return average fidelity of a process.

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_superoper_unitary_overlap(A, B, lvls=None)[source]
c3.utils.tf_utils.tf_unitary_overlap(A: tensorflow.python.framework.ops.Tensor, B: tensorflow.python.framework.ops.Tensor, lvls: Optional[tensorflow.python.framework.ops.Tensor] = None) tensorflow.python.framework.ops.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

c3.utils.tf_utils.tf_vec_to_dm(vec)[source]

Convert a density vector to a density matrix.

Log Reader utilities module

c3.utils.log_reader.show_table(log: Dict[str, Any], console: rich.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.ask_yn() bool[source]

Ask for y/n user decision in the command line.

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: 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

c3.utils.utils.num3str(val: float, use_prefix: bool = True) str[source]

Convert a number to a human readable string in engineering notation.

Create a symbolic link.

Module contents