Haar's Half Measure

What I talk about when I talk about physics.

22 Nov 2023

My coding cheatsheet

I still Google how to create matplotlib figures with two subplots. This ends today!

Python in general

Check directory

To check for the current working directory

import os
os.getcwd()

Import .py files

Switch to a certain directory

import os
os.chdir('/path/to/that/directory')

Array and .pkl

Say I have an $n$-dimensional array and I want to save it as a storable file, in this case .pkl

import pickle
with open('pkl_name.pkl', 'wb') as f:
    pickle.dump(array_name, f)

To convert a .pkl to an $n$-dimensional array

import numpy
array_name = np.load('pkl_name.pkl', allow_pickle=True)

Qiskit

Pulse specifications

For example, to look at the parameters of a default $\pi$ pulse on qubit = 0.

qubit = 0
x_default = backend.defaults().instruction_schedule_map.get('x', qubits = [qubit])
x_default_pulse = x_default.instructions[qubit][1].pulse
print(f"Amplitude = {x_default_pulse.amp}. DRAG coefficient = {x_default_pulse.beta}. Duration = {x_default_pulse.duration}. Sigma = {x_default_pulse.sigma}")
Next time, we'll talk about "How a spherically symmetric cow is different from a symmetrically spherical cow."