utils module

Collection of functions that might be useful, or might not.

class presto.utils.ProgressBar(size, update_time=1.0)

Bases: object

Prints a progress bar to stderr, keeping track of remaining time.

Parameters
  • size (int) – how many “units of work” will be done

  • update_time (float, optional) – limit the update of the progress bar to update_time seconds

Examples

>>> pb = ProgressBar(100)
>>> pb.start()
>>> for _ in range(100):
>>>     time.sleep(0.1)  # <-- do work here!
>>>     pb.increment()
>>> pb.done()
done()

Terminate the progress bar and print total time.

Return type

None

increment(inc=1)

Increment the progress bar.

Will not change the text on stderr if too little time has passed since last update.

Parameters

inc (int, optional) – how many “units of work” have been done since last call

Return type

None

start()

Start counting time.

Return type

None

presto.utils.as_flat_list(x)

Return x as a flat unidimensional list.

Parameters

x (object) – An arbitrarily nested list, tuple, np.ndarray, or a mixture thereof.

Return type

list

Returns

A flat, unidimensional list with all the objects of x.

presto.utils.format_precision(n, s)

Format a value and uncertainty to the correct number of significant digits.

Parameters
  • n (float) – nominal value.

  • s (float) – uncertainty.

Return type

str

Returns

a formatted string with numbers rounded to significant digits.

Examples

>>> format_precision(36.91226461435421, 0.4060358649863922)
'36.9 ± 0.4'
presto.utils.format_sec(s)

Format a time interval in seconds into a more human-readable string.

Args: s: time interval in seconds.

Return type

str

Returns

time interval in the form “Xh Ym Z.zs”.

Examples

>>> format_sec(np.pi * 1e+8)
'9y 348d 20h 27m 45.4s'
>>> format_sec(np.exp(-10))
'45.4us'
presto.utils.get_sourcecode(script_filename)

Open a file and return its content.

Parameters

script_filename (str) – path to the file.

Return type

List[str]

Returns

the lines of the file at script_filename.

presto.utils.rotate_opt(data, return_x=False)

Rotates data so that all the signal is in the I quadrature (real part).

Parameters
  • data (ndarray) – dtype should be complex128.

  • return_x (bool) – if True, return also the angle by which data was rotated.

Return type

ndarray

Returns

data * np.exp(1j * x), with x such that np.std(ret.imag) is minimum. dtype=complex128.

If return_x=True, also return the angle x.

presto.utils.sin2(nr_samples, drag=0.0)

Create a \(\sin^2\) envelope/template.

Parameters
  • nr_samples (int) –

  • drag (float) – if nonzero, use DRAG with \(\lambda / \alpha\) equal to drag

Return type

ndarray

Returns

NumPy array for desired pulse with dtype=float64 if drag=0.0, or dtype=complex128 otherwise.

presto.utils.sinP(P, nr_samples)

Create a \(\sin^P\) envelope/template.

Parameters
Return type

ndarray

Returns

NumPy array for desired pulse with dtype=float64.

presto.utils.ssh_download(remote_filename, local_filename=None, address='192.168.42.50')

Copy a file from the hardware to the local computer.

Parameters
  • remote_filename (str) –

  • local_filename (Optional[str]) –

  • address (str) – IP address or hostname of the hardware.

Raises

ImportError – Python module fabric not found.

presto.utils.ssh_execute(command, address='192.168.42.50')

Execute a command on the hardware.

Parameters
  • command (str) –

  • address (str) – IP address or hostname of the hardware.

Raises

ImportError – Python module fabric not found.

presto.utils.ssh_reboot(address='192.168.42.50')

Reboot the Linux system on the hardware.

It is equivalent to ssh_execute(“/sbin/shutdown -r now”).

Parameters

address (str) – IP address or hostname of the hardware

Raises

ImportError – Python module fabric not found.

presto.utils.ssh_upload(local_filename, remote_filename=None, address='192.168.42.50')

Copy a file from the local computer to the hardware.

Parameters
  • local_filename (str) –

  • remote_filename (Optional[str]) –

  • address (str) – IP address or hostname of the hardware.

Raises

ImportError – Python module fabric not found.

presto.utils.to_pm_pi(phase)

Converts a phase in radians into the [-π, +π) interval.

Parameters

phase (float) –

Return type

float

presto.utils.triangle(nr_samples)

Create a triangular envelope/template.

Parameters

nr_samples (int) –

Return type

ndarray

Returns

NumPy array for desired pulse with dtype=float64.

presto.utils.untwist_downconversion(I_port, Q_port)

Convert a measured IQ pair into a low/high sideband pair.

Parameters
  • I_port (ndarray) – dtype=complex128

  • Q_port (ndarray) – dtype=complex128

Return type

Tuple[ndarray, ndarray]

Returns

a tuple (L_sideband, H_sideband) of NumPy arrays with dtype=complex128.