Skip to content

Instantly share code, notes, and snippets.

using MacroTools
using MacroTools: striplines
###
### Macros
###
"""
@qc code
@jlapeyre
jlapeyre / zchop.py
Created November 1, 2021 14:43
Replace numbers that are close to zero by zero in arrays and lists.
import numpy as np
ZCHOP_EPS = 1e-12
def zchop_real_np(m, eps=ZCHOP_EPS):
m[abs(m) < eps] = 0.0
return m
def zchop(m, eps=ZCHOP_EPS):
if isinstance(m, float):
from qiskit.quantum_info import SparsePauliOp, Operator
from qiskit.opflow import PauliSumOp, AbelianGrouper
import numpy as np
def try_sparse_pauli_op(n=1):
mat = np.random.rand(2**n, 2**n)
omat = Operator(mat)
sp = SparsePauliOp.from_operator(omat)
return sp
def get_pauli_sum_op(n=1):
@jlapeyre
jlapeyre / symmetry_tests.py
Last active May 19, 2021 05:58
test arrays of two-electron integrals for symmetries
import numpy
## These are the permutation symmetries satisfied by
## a rank-4 tensor of real two-body integrals in chemists'
## index order.
CHEM_INDEX_PERMUTATIONS = [
'pqrs->qprs', # (1.4.38) in HJO book (1)
'pqrs->pqsr', # (1.4.38) (2)
'pqrs->qpsr', # (1.4.38) (3)
'pqrs->rspq', # (1.4.17) (4)
# price schemes
struct NormalHours end
struct HappyHour end
actual_price(price, ::NormalHours) = price
actual_price(price, ::HappyHour) = price / 2
struct CustomerBill
drinks::Vector{Float64}
end
@jlapeyre
jlapeyre / blas_get_num_threads.jl
Created September 24, 2018 06:56
Allow setting number of threads in BLAS
const BLASVENDOR = Compat.LinearAlgebra.BLAS.vendor()
@eval Compat.LinearAlgebra.BLAS function get_num_threads end
# Following fails in v"0.7.0-DEV.4810". But, this appears to
# be a bug.
"""
BLAS.get_num_threads()
Get the number of threads that the BLAS library currently uses.
## This file benchmarks taking slices of lazily-wrapped transpose and adjoint of sparse matrices.
## The times appear in triplets. First, the array with no wrapper, then the transpose, then the
## adjoint.
## The first time should always be the same or a bit faster than the second or third.
## (But, @btime was not tuned).
## Below is a file with post-PR results and a file with pre-PR results.
using BenchmarkTools
using SparseArrays
using LinearAlgebra
@jlapeyre
jlapeyre / walkstep.jl
Last active September 24, 2016 19:11
generate code for a random walk step in `n` dimensions
# See the first comment for an explanation
using Lazy
function walkifs(dim)
conditions = Any[]
actions = Any[]
for i in 1:(2*dim-1)
push!(conditions, :( dir == $i ))
end
for i in 1:dim
@jlapeyre
jlapeyre / buildif.jl
Last active September 24, 2016 19:28
`buildif` builds an if-elseif-else construct programmatically.
doc"""
buildif(exprs...)
`buildif` builds an if-elseif-else construct programmatically.
`buildif` returns an expression equivalent to an if-elseif-else
construct, where, if the length of exprs is even, then the odd-indexed
elements of exprs are condition expressions and the even-indexed
elements are code blocks for the corresponding branches. If the length
of exprs is odd, then the last element is the default code block. In