Skip to content

Instantly share code, notes, and snippets.

@jcmgray
jcmgray / get_outdated_pkgs_not_managed_by_conda.py
Last active July 17, 2018 14:57
Find the set of python packages, not managed by conda, which are out of date according to pip
#!/usr/bin/env python
import subprocess
# find the names of packages conda says are managed by pip
conda_raw = subprocess.run(['conda', 'list'], stdout=subprocess.PIPE).stdout.decode('utf-8')
pip_only_pkgs = {ln.split(' ')[0] for ln in conda_raw.split('\n') if '<pip>' in ln}
# find the names of packages pip says are outdated (assumes column pip output)
pip_raw = subprocess.run(['pip', 'list', '--outdated'], stdout=subprocess.PIPE).stdout.decode('utf-8')
@jcmgray
jcmgray / batch_contract.py
Last active May 12, 2024 07:58
Batched tensor contraction - essentially batched tensordot, but using einsum notation.
from numpy import tensordot, stack
def einsum_to_axes(lhix, rhix):
"""Take two einsum inputs (strs) and produce
the tuples of ints required for tensordot.
"""
ix_rm = set(lhix) & set(rhix)
lh_ax, rh_ax = [], []
for ix in ix_rm:
@jcmgray
jcmgray / rand_reg_graph_maxcut_step_qasm_create.py
Last active July 25, 2018 12:48
Create quantum circuit file for a single trotterization step of the maxcut QAOA algorithm.
import networkx as nx
reg = 3
n = 100
seed = 0
gamma0 = -0.743043
beta0 = 0.754082
# create the random graph
@jcmgray
jcmgray / cached_contract.py
Created August 8, 2018 22:20
Uses xxhash and LRU to cache the results of tensordot based on the input arrays themselves.
import functools
import numpy as np
transpose = np.transpose
einsum = np.einsum
@functools.lru_cache(1)
def get_hasher():
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jcmgray
jcmgray / .gitignore
Last active October 11, 2018 14:01
Sample data for opt_einsum large contractions
\.ipynb_checkpoints/
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jcmgray
jcmgray / opt-einsum-benchmark-paths.ipynb
Last active September 1, 2019 21:18
Benchmarking path finding for opt-einsum v3.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jcmgray
jcmgray / compare_pip_and_conda_packages.py
Last active March 12, 2019 01:10
List: (a) pip installed packages that can be found on conda, (b) pip-only installed packages that are outdated.
#!/usr/bin/env python
import subprocess
print("Finding packages conda says are installed by pip...")
conda_raw = subprocess.run(
['conda', 'list'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).stdout.decode('utf-8')
@jcmgray
jcmgray / oinsum.py
Created May 30, 2020 07:15
oinsum - an einsum implementation for numpy object arrays
def oinsum(eq, *arrays):
"""A ``einsum`` implementation for ``numpy`` object arrays.
"""
import numpy as np
import functools
import operator
lhs, output = eq.split('->')
inputs = lhs.split(',')