Permalink
Browse files

Coding style: examples

  • Loading branch information...
1 parent 477fc2a commit b94ae1a286d5af7ed81a77a5e23effdc06511e49 @jrioux jrioux committed Oct 20, 2012
@@ -36,7 +36,7 @@
from sympy.utilities.lambdify import implemented_function
from sympy.utilities.autowrap import autowrap, ufuncify
from sympy import Idx, IndexedBase, Lambda, pprint, Symbol, oo, Integral,\
- Function
+ Function
from sympy.physics.sho import R_nl
from sympy.physics.hydrogen import R_nl as hydro_nl
@@ -78,19 +78,16 @@ def main():
expr = expr.evalf(15)
print "The h.o. wave function with l = %i and n = %i is" % (
- orbital_momentum_l, n)
+ orbital_momentum_l, n)
pprint(expr)
# implement, compile and wrap it as a ufunc
basis_ho[n] = ufuncify(x, expr)
-
# now let's see if we can express a hydrogen radial wave in terms of
# the ho basis. Here's the solution we will approximate:
-
H_ufunc = ufuncify(x, hydro_nl(hydrogen_n, orbital_momentum_l, 1, x))
-
# The transformation to a different basis can be written like this,
#
# psi(r) = sum_i c(i) phi_i(r)
@@ -110,7 +107,6 @@ def main():
# the low-level code. (In fact, summations are very easy to create, and as
# we will see it is often necessary to take extra steps in order to avoid
# them.)
-
# we need one integration ufunc for each wave function in the h.o. basis
binary_integrator = {}
for n in range(basis_dimension):
@@ -131,7 +127,6 @@ def main():
psi_ho = implemented_function('psi_ho',
Lambda(x, R_nl(n, orbital_momentum_l, omega2, x)))
-
# We represent the hydrogen function by an array which will be an input
# argument to the binary routine. This will let the integrators find
# h.o. basis coefficients for any wave function we throw at them.
@@ -150,7 +145,7 @@ def main():
expr = A[i]**2*psi_ho(A[i])*psi[i]*step
- if n==0:
+ if n == 0:
print "Setting up binary integrators for the integral:"
pprint(Integral(x**2*psi_ho(x)*Function('psi')(x), (x, 0, oo)))
@@ -165,37 +160,34 @@ def main():
binary_integrator[n] = autowrap(expr, args=[A.label, psi.label, step, m])
# Lets see how it converges with the grid dimension
- print "Checking convergence of integrator for n = %i" %n
+ print "Checking convergence of integrator for n = %i" % n
for g in range(3, 8):
grid, step = np.linspace(0, rmax, 2**g, retstep=True)
print "grid dimension %5i, integral = %e" % (2**g,
binary_integrator[n](grid, H_ufunc(grid), step))
-
print "A binary integrator has been set up for each basis state"
print "We will now use them to reconstruct a hydrogen solution."
# Note: We didn't need to specify grid or use gridsize before now
grid, stepsize = np.linspace(0, rmax, gridsize, retstep=True)
- print "Calculating coefficients with gridsize = %i and stepsize %f" %(
- len(grid), stepsize)
+ print "Calculating coefficients with gridsize = %i and stepsize %f" % (
+ len(grid), stepsize)
coeffs = {}
for n in range(basis_dimension):
coeffs[n] = binary_integrator[n](grid, H_ufunc(grid), stepsize)
print "c(%i) = %e" % (n, coeffs[n])
-
print "Constructing the approximate hydrogen wave"
hydro_approx = 0
all_steps = {}
for n in range(basis_dimension):
hydro_approx += basis_ho[n](grid)*coeffs[n]
all_steps[n] = hydro_approx.copy()
if pylab:
- line = pylab.plot(grid, all_steps[n], ':', label='max n = %i'%n)
-
+ line = pylab.plot(grid, all_steps[n], ':', label='max n = %i' % n)
# check error numerically
diff = np.max(np.abs(hydro_approx - H_ufunc(grid)))
@@ -205,7 +197,6 @@ def main():
else:
print "Ah, that's a pretty good approximation!"
-
# Check visually
if pylab:
print "Here's a plot showing the contribution for each n"
@@ -10,7 +10,7 @@
"""
from sympy import var, sin, cos, pprint, Matrix, eye, trigsimp, Eq, \
- Function, simplify, sinh, cosh, expand
+ Function, simplify, sinh, cosh, expand
def laplace(f, g_inv, g_det, X):
"""
@@ -85,15 +85,15 @@ def main():
Matrix([rho*sin(theta)*cos(phi), rho*sin(theta)*sin(phi),
rho*cos(theta)]),
[rho, theta, phi],
- recursive=True
- )
+ recursive=True
+ )
transform("rotating disk",
- Matrix([t, x*cos(w*t)-y*sin(w*t), x*sin(w*t)+y*cos(w*t), z]),
+ Matrix([t, x*cos(w*t) - y*sin(w*t), x*sin(w*t) + y*cos(w*t), z]),
[t, x, y, z])
transform("parabolic",
- Matrix([sigma*tau, (tau**2-sigma**2)/2]),
+ Matrix([sigma*tau, (tau**2 - sigma**2)/2]),
[sigma, tau])
# too complex:
@@ -106,7 +106,7 @@ def main():
transform("elliptic",
Matrix([a*cosh(mu)*cos(nu), a*sinh(mu)*sin(nu)]),
[mu, nu]
- )
+ )
if __name__ == "__main__":
main()
@@ -10,7 +10,8 @@
from sympy.physics.quantum.grover import superposition_basis
def main():
- psi = superposition_basis(2); psi
+ psi = superposition_basis(2)
+ psi
# Dense coding demo:
@@ -25,26 +26,30 @@ def main():
# To Send Bob the message |0>|0>
print "To Send Bob the message |00>."
- circuit = H(1)*CNOT(1,0)
- result = qapply(circuit*psi); result
+ circuit = H(1)*CNOT(1, 0)
+ result = qapply(circuit*psi)
+ result
pprint(result)
# To send Bob the message |0>|1>
print "To Send Bob the message |01>."
- circuit = H(1)*CNOT(1,0)*X(1)
- result = qapply(circuit*psi); result
+ circuit = H(1)*CNOT(1, 0)*X(1)
+ result = qapply(circuit*psi)
+ result
pprint(result)
# To send Bob the message |1>|0>
print "To Send Bob the message |10>."
- circuit = H(1)*CNOT(1,0)*Z(1)
- result = qapply(circuit*psi); result
+ circuit = H(1)*CNOT(1, 0)*Z(1)
+ result = qapply(circuit*psi)
+ result
pprint(result)
# To send Bob the message |1>|1>
print "To Send Bob the message |11>."
- circuit = H(1)*CNOT(1,0)*Z(1)*X(1)
- result = qapply(circuit*psi); result
+ circuit = H(1)*CNOT(1, 0)*Z(1)*X(1)
+ result = qapply(circuit*psi)
+ result
pprint(result)
if __name__ == "__main__":
Oops, something went wrong.

0 comments on commit b94ae1a

Please sign in to comment.