spheres.utils

Miscellaneous useful functions.

Functions

binomial(n, k)

Binomial coefficient \(\binom{n}{k} = \frac{n!}{k!(n-k)!}\)

bitstring_basis(bitstring[, dims])

Generates a basis vector corresponding to a given bitstring, which may be a list of integers or a string of integers.

compare_nophase(a, b)

Compares two vectors disregarding their overall complex phase.

compare_spinors(A, B[, decimals])

Compares two lists of spinors, disregarding both their phases, as well as their ordering in the list.

compare_unordered(A, B[, decimals])

Compares two sets of vectors regardless of their ordering up to some precision.

components(q)

Extracts components of qt.Qobj, whether bra or ket, as a numpy array.

density_to_purevec(density)

Converts a density matrix to a pure vector if it’s rank-1.

dirac(state[, probabilities])

Prints a pretty representation of a state in Dirac braket notation.

fix_stars(old_stars, new_stars)

Try to adjust the ordering of a list of stars to keep continuity, so that they are in the “same order.” Not always reliable.

flatten(to_flatten)

Flattens list of lists.

from_pauli_basis(coeffs[, basis])

Given a dictionary mapping Pauli strings to components, returns the corresponding density matrix/operator.

measure(state, projectors)

Given a state and a set of projectors, calculates the probability of each outcome, and returns an outcome index with that probability.

normalize(v)

Normalizes numpy vector.

normalize_phase(v)

Normalizes the phase of a complex vector (np.ndarray or qt.Qobj).

pauli_basis(n)

Generates the Pauli basis for n qubits.

phase(v)

Extracts phase of a complex vector (np.ndarray or qt.Qobj) by finding the first non-zero component and returning its phase.

phase_angle(q)

Extracts phase angle of a complex vector (np.ndarray or qt.Qobj) by finding the first non-zero component and returning its phase angle.

polygon_area(phis, thetas[, radius])

Computes area of spherical polygon.

qubits_xyz(state)

Returns XYZ expectation values for each qubit in a tensor product.

rand_c([n])

Generates (n) random extended complex coordinate(s) whose real and imaginary parts are normally distributed, and ten percent of the time, we return \(\infty\).

rand_sph([n])

Generates (n) random point(s) on the unit sphere in spherical coordinates.

rand_xyz([n])

Generates (n) random point(s) on the unit sphere in cartesian coordinates.

spinj_xyz(state)

Returns XYZ expectation values for a spin-j state.

tensor_upgrade(O, i, n)

Upgrades an operator to act on the i’th subspace of n subsystems.

to_pauli_basis(qobj[, basis])

Expands a state/operator in the Pauli basis.