Today I started using sympy and its quantum module to implement some basic calculations in Bra-Ket notation.
Executing the code:
from sympy.physics.quantum import *
from sympy.physics.quantum.qubit import *
from sympy import *
from sympy.abc import k
print Sum(Ket(k),(k,0,5))
yields the expected result, that is, Sum(|k>, (k, 0, 5)) is printed.
Now I'd like to expand the sum and therefore write:
print Sum(Ket(k),(k,0,5)).doit()
However, this doesn't give the correct result, but prints out 6*|k> which obviously is not the desired output. Apparently, the program doesn't recognize Ket(k) as depending on the index k.
How could I work around or solve this issue?
Looks like a bug. You can probably work around it by doing the sum outside of sympy, with standard python functions like sum(Ket(i) for i in range(6)).
Related
So, if the title is not clear, I am trying to take the function [sin(x)/(x3-2*x2+4x-8)] and find the integral. I know that the analytical solution is (-pi/(8e**2))+(pi/8)*cos(2) or ~ -0.2165...
The code that I can not seem create properly so far looks like
import numpy as np
from sympy import integrate
from sympy.abc import x
f = integrate(np.sin(x)/(x**3-2*x**2+4x-8), (x, -5, 2))
print(f)
I would like the code to eventually be drawn out which I plan on doing myself, however, this gives me the error
"TypeError: loop of ufunc does not support argument 0 of type Symbol which has no callable sin method"
How do I go about fixing this?
everyone! I am pretty new on this platform and need your help in one thing. I am using sympy on jupyter notebook and I am programmig a dummy formula, root square, however the output for this symbol is not the root symbol, actually it is outputting sqrt instead.
I will give you guys an example
import sympy
In: sympy.sqrt(3)
Out :sqrt(3)
This is normal behavior for SymPy. I can't test it for now but try adding init_printing(use_unicode=True) in your code before calling the sqrt function.
Source :
https://docs.sympy.org/latest/tutorial/intro.html
So i'm very new to Python and i'm not used to the syntax yet. This is probably a simple problem but then again, i'm new so I don't know what is the proper way to write this. I'm supposed to integrate f(x)= 6x*(1-x) with intervals [0,1] but I get a syntax error when I write the code. I tried changing the parenthesis but still got the same error. Again, I know this is probably very simple but i'm still trying to get used to Python so help will be very appreciated.
from sympy import Symbol
from sympy import integrate
x=Symbol('x')
print (integrate((6x)*(1-x))
f=((6x)*(1-x))
print(quad(f,0,1))
Changing what you currently have to:
from sympy import Symbol
from sympy import integrate
x = sympy.Symbol('x')
print(sympy.integrate( (6*x)* (1-x) ))
f = ((6*x)*(1-x))
print(sympy.quad(f,0,1))
Fixes your errors. For sympy, you need to write n*y, not ny, and you also had some missing parenthesis.
I am trying to apply the Q-function values for a problem. I don't know the function available for it in Python.
What is the python equivalent for the following code in octave?
>> f=0:0.01:1;
>> qfunc(f)
The Q-function can be expressed in terms of the error function. Check here for more info. "scipy" has the error function, special.erf(), that can be used to calculate the Q-function.
import numpy as np
from scipy import special
f = np.linspace(0,1,101)
0.5 - 0.5*special.erf(f/np.sqrt(2)) # Q(f) = 0.5 - 0.5 erf(f/sqrt(2))
Take a look at this https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.stats.norm.html
Looks like the norm.sf method (survival function) might be what you're looking for.
I've used this Q function for my code and it worked perfectly well,
from scipy import special as sp
def qfunc(x):
return 0.5-0.5*sp.erf(x/sqrt(2))
I'vent used this one but I think it should work,
def invQfunc(x):
return sqrt(2)*sp.erfinv(1-2x)
references:
https://mail.python.org/pipermail/scipy-dev/2016-February/021252.html
Python equivalent of MATLAB's qfuncinv()
Thanks #Anton for letting me know how to write a good answer
I am trying to solve a system of simultaneous equations as follows:
"145.0x/21025 = -0.334"
"(-48.402x-96.650y+96.650z)/21025 = -0.334"
"(-48.402x+132.070y+35.214z)/21025 = -0.334"
"sqrt(x^2+y^2+z^2) = 145.0"
I am using the following Python script:
from scipy.optimize import root
from numpy import sqrt
from sys import argv, stdout
initGuesses = eval(argv[1])
equations = argv[2:]
def f(variables):
x,y,z = variables
results = []
for eqn in equations:
results.append(eval(eqn))
return results
solution = root(f, initGuesses, method="lm")
stdout.write(str(solution["x"][0]) + "," + str(solution["x"][1]) + "," + str(solution["x"][2]))
stdout.flush()
The program is called as follows:
python3 SolvePosition3D.py "(1,1,1)" "(145.0*x+0.0*y+0.0*z)/21025.0+0.334" "(-48.402*x-96.650*y+96.650*z)/21025+0.334" "(-48.402*x+132.070*y+35.214*z)/21025+0.334" "sqrt(x**2+y**2+z**2)-145.0"
And I am receiving the following output:
48.2699997956,35.4758788666,132.042180583
This solution is wrong; the correct solution is roughly
-48,-35,-132
which is the same numbers but * -1.
The answer returned by the program satisfies the final equation, but violates all others.
Does anyone know why this is happening? Getting the correct solutions to these equations (and many others like them) is vitally important to my current project.
I was able to run the code via adding
from numpy import sqrt
from scipy.optimize import root
and switching to regular old prints for the solution.
Your starting guess seems to be wrong. Starting from (1, 1, 1), the root finder converges to 48.2699997956,35.4758788666,132.042180583, as you said. If you plug in (-1,-1,-1), you get -48.2649482763,-35.4698607274,-132.050694891 as expected.
As for why it does that, well, nonlinear systems are just hard to solve like that. Most algorithms tend to find solutions deterministically based on the starting point. If you need to try multiple starting points, try a grid-based search.