Comments (15)
I'm using symengine 0.11.2, and Arch Linux builds the library with the following options:
cmake -B build -S $pkgname-$pkgver \
-DCMAKE_INSTALL_PREFIX=/usr \
-DBUILD_SHARED_LIBS=ON \
-DWITH_TCMALLOC=ON \
-DWITH_PTHREAD=ON \
-DWITH_SYMENGINE_THREAD_SAFE=ON \
-DWITH_ARB=ON \
-DWITH_ECM=ON \
-DWITH_LLVM=ON \
-DWITH_MPFR=ON \
-DWITH_MPC=ON \
-DWITH_PRIMESIEVE=ON \
-DWITH_BOOST=ON \
-DWITH_COTIRE=OFF \
-DWITH_SYSTEM_CEREAL=ON
cmake --build build
from qiskit.
If you installed symengine
from pip or conda, you'd probably have got their pre-packaged wheels, which is the same situation we're usually expecting. If you're using it from Arch's repos in the system Python installation, then this could very well be a difference: I don't think symengine's published wheels use tcmalloc
, and it could be some fault there (or perhaps some questionable behaviour that happens to work with glibc's malloc, but tcmalloc is rejecting).
The difference between pre-importing symengine
and not probably isn't indicative of everything being super fixed (since qiskit
does just import symengine
internally) - it's more likely a spooky-action-at-a-distance memory bug.
If you have the means to try, would you be able to see if the error repeats if you drop the -DWITH_TCMALLOC=ON
(or manually set it to off) while you're building the symengine
your Python installation is linking against?
from qiskit.
For some reason, if symengine
is imported, there is no crash, i.e.,
import symengine
from qiskit.circuit.parameter import Parameter
from qiskit.circuit.parameterexpression import ParameterExpression
theta = Parameter("theta")
phi = Parameter("phi")
sum_param = theta + phi
sum_param._symbol_expr.__reduce__()
from qiskit.
Thanks for the report. Do you know what version of symengine you're using?
Fwiw, it would be really pretty weird if this is caused by Qiskit and not some allocator problem within Symengine - our use of it is very much inline with their public interface, and we don't go messing around in their internals or anything.
from qiskit.
I was going to do that experiment next, yes. I will rebuild symengine
and python-symengine
without tcmalloc and see if that solves the issue.
I just installed qiskit
quickly in a venv
and when I use the symengine
from PyPI I also can't replicate the issue.
from qiskit.
I can confirm those tests don't fail when using symengine
without tcmalloc
. I will create an issue on Arch Linux.
from qiskit.
Thanks so much for looking into it in depth! I guess if the issue is somewhere in Arch / tcmalloc / symengine, we can close this issue on Qiskit? I'll close it, but feel free to re-open if there's more to discuss.
from qiskit.
After solving the crashes due to tcmalloc
, I found another error related to symengine
(this time a numerical rounding error).
In particular is this test:
It works running on a virtual env, but if fails using system shared libraries. It's a numerical error happening in this line:
qiskit/qiskit/circuit/parameterexpression.py
Line 534 in 235e581
Running on my system real_expr
has type symengine.lib.symengine_wrapper.RealDouble
and value 3140000.0
. However, running float(real_expr)
returns 3139999.9999999995
. I think this possibility was considered here but it doesn't work as expected:
Lines 71 to 74 in 235e581
The reason is that decimal
has a value 10
so this error is not removed with the rounding. Passing decimals=9
fixes the issue for me.
Note also this relevant comment from NumPy (see Notes):
np.round uses a fast but sometimes inexact algorithm to round floating-point datatypes. For positive decimals it is equivalent to np.true_divide(np.rint(a * 10decimals), 10decimals), which has error due to the inexact representation of decimal fractions in the IEEE floating point standard [1] and errors introduced when scaling by powers of ten.
(...)
If your goal is to print such values with a fixed number of decimals, it is preferable to use numpy’s float printing routines to limit the number of printed decimals:
(...)
The float printing routines use an accurate but much more computationally demanding algorithm to compute the number of digits after the decimal point.
Alternatively, Python’s builtin round function uses a more accurate but slower algorithm for 64-bit floating point values:
Perhaps it's better to use round(operand, ndigits=decimal)
and change the default value of decimal
from 10 to 9? What do you think? I can make a PR if you think it's a good idea (I will check first that there are no other side effects after changing that).
from qiskit.
Sorry, I guess round()
is out of the table because of complex numbers.
from qiskit.
Also, after thinking a bit I don't understand why there should be such a rounding error in the first place. Do you know why is that error expected from that comment in the code and why decimal
has a default value of 10 instead of 14 for example (which should be fine with double precision I guess)? I also can't replicate the error using pure symengine
code. I can try to compile symengine
without MPFR as a wild guess... but I don't understand what is going on.
from qiskit.
No difference compiling symengine
with -DWITH_MPFR=OFF
from qiskit.
I noticed that the ParameterExpression(f/1000)
is multiplied by a float here:
This causes frequency
to have the value ParameterExpression(1000000.0*f)
in this particular unit test instead of ParameterExpression(1000000*f)
which could have be obtained by multiplying by an integer instead, i.e.,
frequency = self.disassemble_value(instruction.frequency) * 10**9
This is what ultimately leads to the numerical error that could have been avoided and has nothing to do with the double precision of the float
type.
from qiskit.
And here is a small self-contained example replicating the issue with pure symengine
:
import symengine
f = symengine.Symbol("f")
expr = f / 1000
expr_wrong = expr * 1e9
expr_good = expr * 10**9
wrong_value = float(expr_wrong.subs("f", 3.14))
good_value = float(expr_good.subs("f", 3.14))
print(wrong_value)
# 3139999.9999999995
print(good_value)
# 3140000.0
from qiskit.
Related Issues (20)
- Port `ElidePermutations` to Rust
- Port `StarPrerouting` to Rust HOT 1
- Hyper efficient and more accurate diamond norm implementation HOT 2
- EfficientSU2 should support specifying two-qubit gate HOT 2
- measure_active returns register called "measure" which breaks round tripping in QASM HOT 3
- Bug in `visualization.pulse_v2.device_info` required for plotting pulse schedules. HOT 1
- Transpiling with AerSimulator with coupling map gives TypeError: 'NoneType' object is not iterable HOT 1
- plot_circuit_layout colors edges incorrectly HOT 1
- Default to idle_wires=False in drawers
- generate_preset_pass_manager compatibility issue in 'scheduling_method' options with runtime FakebakcendV2 and Aersimulator HOT 1
- transpiler introduces non-clifford gates in a purely clifford circuit
- Generation of Random Circuits with Pre-Defined Inter-Qubit Connectivity HOT 3
- Port the `Clifford` and `StabilizerState` classes to Rust
- module 'qiskit' has no attribute '_accelerate' when running with accelerated numpy HOT 1
- passmanager fails when called a second time on different circuit HOT 2
- Can't transpile circuits with new Qiskit 1.0.2 version: TranspilerError: "HighLevelSynthesis was unable to synthesize Instruction(name='reset', num_qubits=1, num_clbits=0, params=[])." HOT 1
- ImportError: DLL load failed while importing symengine_wrapper: The specified module could not be found. HOT 2
- Append `StabilizerState` natively to a `QuantumCircuit` HOT 1
- Can't simulate circuits using SamplerV2. Error: "AttributeError: '_SingletonHGate' object has no attribute 'parameters'" HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qiskit.