Comments (4)
It turns out that the true culprit is defining equality based on the hash:
def __eq__(self, other):
return hash(self) == hash(other)
This is not a good idea! While equal expression must have the same hash, the reverse is not true (and happens much more frequently than I had thought). Note that things like dicts
are robust with respect to hash collisions, but only if equality is defined correctly.
from qnet.
Yeah, I was surprised by the way you guys talked about hash collisions as a bug. Frequent hash collisions should never result in incorrect behavior, only poor performance. The bug is in what @goerz just quoted, and any other statements that rely on the same assumption.
The contract is that a == b
is true implies hash(a) == hash(b)
is also true, not the other way around. For any questions and confusions of this kind, this page in the python documentation is a goldmine: https://docs.python.org/3/reference/datamodel.html
from qnet.
Wow, that's pretty crazy! Seems like hash collisions in Python happen very easily: https://stackoverflow.com/questions/32504977/hashing-different-tuples-in-python-give-identical-result
I'll look into whether hashlib
can provide a better alternative.
from qnet.
Thanks for the clarifications! I didn't mean to imply that the hash collision was a bug itself, but only that real bugs such as a-a == a-2a
seemed to arise from the hash collision. And as @goerz mentions, it comes from the current implementation of __eq__
(in Expression
?), which precisely assumes/enforces hash(a) == hash(b)
implies a == b
. I'm glad to hear that Python dictionaries don't rely solely on hash, since this means we could directly compare the Expression
and its operands instead for equality testing and caching.
from qnet.
Related Issues (20)
- Applying a LocalProjector to a BasisKet does not return a simplified result HOT 1
- Need Pattern.finditer
- Allow CircuitSymbols labels to contain superscripts
- CircuitSymbols labels should always be rendered as text in Latex
- Intelligent feedback
- _flat_api_tools introduces too much abstraction for linters HOT 3
- Prevent instantiation of abstract base classes HOT 1
- LocalSpace.create crashes when using local_identifiers HOT 2
- .show() method fails HOT 10
- Use robust, cross-platform compatible file paths
- Officially support Windows HOT 2
- Adapt to change of free_symbols in sympy 1.2
- Add open_file utility function
- Hilbert space supermode factors and canonical transformations HOT 10
- Commutation relations are broken: distinguish finite and truncated basis dimension HOT 3
- Incorrect result for product of sums
- Indexed LocalSpaces must not be considered disjoint
- Write tests for symbolic_*
- How to use Pauli matrices in QNET?
- Error importing QNET package
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 qnet.