Skip to content

Instantly share code, notes, and snippets.

View SamWolski's full-sized avatar

Sam Wolski SamWolski

View GitHub Profile
@SamWolski
SamWolski / extract_pdf_images.sh
Created February 5, 2024 18:37
Extract images from a pdf directly
## Provided by poppler, which can be installed through macports
## From https://apple.stackexchange.com/a/445704
pdfimages -all input.pdf output
@SamWolski
SamWolski / text2wkt.py
Created May 19, 2023 18:36
Convert text to WKT (shapely) polygons using arbitrary local font
'''
Convert text in any installed font to the WKT format.
Requires inkscape.
Does not currently work for characters with disjoint top-level islands (eg "=",
double quotes, capital Xi, etc.)
'''
import json
@SamWolski
SamWolski / install_qm_m2.sh
Created May 1, 2023 21:49
Install Qiskit-Metal on Apple Silicon
# Specify the env will be emulating x86
CONDA_SUBDIR=osx-64 conda create -n <env_name> python=3.10
# Activate env
conda activate <env_name>
# Set env to fetch x86 packages
conda config --env --set subdir osx-64
# Install qiskit metal
pip install qiskit-metal
# Install just the notebook component of jupyter
# Don't install the full 'jupyter' package which will lead to qt conflicts
@SamWolski
SamWolski / qutip_serialization.py
Created August 17, 2022 18:30
Qutip object serialization
import msgpack
import msgpack_numpy
import pandas as pd
import qutip
from scipy.sparse import csr_matrix
def serialize_csr(arr):
## Switch to coo format
coo = arr.tocoo()
## Separate into the components we need to reconstruct it
@SamWolski
SamWolski / qutip4_feedback.py
Created August 10, 2022 18:01
Qutip 4 feedback solver
## From https://github.com/qutip/qutip/issues/1571#issuecomment-859873615
times = [0, ...]
# Liouvillians for parts you have control over
control_liouvillians = [qutip.liouvillian(h) for h in control_hamiltonians]
# The time-dependent Hamiltonian for stuff you're not controlling
base_hamiltonian = qutip.QobjEvo([H0, [H1, time_dependence], ...])
# Turn it into a Liouvillian once, so we don't repeat the cost
base = qutip.liouvillian(base_hamiltonian, collapse_operators)
state = ...
@SamWolski
SamWolski / mpl_pdf_metadata.py
Created August 17, 2021 09:46
Add custom metadata to matplotlib pdfs
"""
Write metadata to pdf files with no warnings.
Useful for provenance, and in this way we can suppress warnings for end users of a larger application
"""
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import numpy as np
import warnings
@SamWolski
SamWolski / generic_logging.py
Created April 1, 2021 02:10
Quickly configure a python logger
from datetime import datetime
import logging
import os
import sys
def quick_config(logger,
console_log_level=logging.INFO, file_log_level=logging.DEBUG,
console_fmt='[%(asctime)s] %(message)s',
console_datefmt='%y-%m-%d %H:%M:%S',
log_file_name=None,
@SamWolski
SamWolski / check_macos_darkmode.py
Last active March 30, 2021 09:43
Check if Dark Mode is being used in macOS, using only python stdlib
import subprocess
def check_appearance():
"""Checks DARK (True)/LIGHT(False) mode of macos.
https://stackoverflow.com/a/65357166"""
cmd = 'defaults read -g AppleInterfaceStyle'
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
return bool(p.communicate()[0])
@SamWolski
SamWolski / instrument_connect_pyvisa_labber.py
Last active January 27, 2021 06:08
Instrument interaction via pyVISA and/or Labber API
import pyvisa
visa_rm = pyvisa.ResourceManager()
## Access at the address may require enabling/configuring the interface on the instrument (S)FP
pxi_vna_addr = 'TCPIP0::<HOSTNAME>::hislip_PXI0_CHASSIS1_SLOT10_INDEX0::INSTR'
inst_vna = visa_rm.open_resource(pxi_vna_addr)
inst_vna.write('SYST:PRES')
inst_vna.close()
## No need to close the RM as it is not connected to anything unless told to
import Labber