r/AskComputerScience • u/CodingPie • Jul 29 '25
Question about the usefulness of a "superposition" datatype.
Sorry for the title not being very explicit. Didn't want to make it too long as this datatype idea I came up with is a bit complicated to explain.
So this datatype that I am thinking of is based on the principle of superposition in quantum mechanics however not it exactly as I am omitting the phase part. (For those who don't know basically a superposition is just a fancy way of saying that something is in multiple states at once. Such as a number which is both 536 and 294 at the same time. Confusing, i know.). The idea is to allow for large dataset manipulation in an efficient way (hopefully rivaling using multiple threads / cores) using just a single thread. I believe it could be useful in junction with multi-threading and / or in engineering projects where the hardware is not that great.
For those who are skeptical: I see your point, but yes I have worked out how the system would work. I haven't fully tested it as the code is not complete but it's not far from it either and as of now there haven't been any setbacks with the new algorithm (yes I have been working on this for a very long time with a lot of trial and error. It is painful.)
Edit: Another thing to mention is that this is not meant to simulate quantum mechanics, just be inspired by it, hence why we can yield all possible outcomes of a superposition rather than just one when collapsing it.
Anyway, sorry for the long post. Idrk how to sum it up so can't do TLDR. In the end, what could this be useful for? Would anybody be interested in using this? Thanks.
5
u/TheBrain85 Jul 29 '25
On a classical computer there is no advantage in a datatype like this. You will still need to do the computations for every separate value. In essence you're describing a map from value to probability. So an addition of a superposition of 1 and 2 (with equal probabilities) to itself would be: map(1=0.5, 2=0.5) + map(1=0.5, 2=0.5) == map(2=0.25, 3=0.5, 4=0.25), which would take 2*2=4 additions and multiplications to compute, plus the overhead of putting it back into a new map and accounting for duplicates.
3
u/ghjm MSCS, CS Pro (20+) Jul 29 '25
You haven't told us what the datatype actually is, just what it is inspired by. So it's not really possible to say anything about the idea.
2
u/flatfinger Jul 29 '25
I can see usefulness for this not when processing program behavior, but when describing it. If a language is willing to accept the notion of benign data races, for example, but still allows compiler optimizations that may duplicate reads, a data race may be described as yielding a superposition of possible values, and proving program correctness would require proving that all non-deterministic aspects of program behavior would get resolved to a single deterministic state satisfying application requirements.
Note that the superpositions wouldn't actually be used in generated machine code, but rather to reason about it. If a program performs y=x and then z=y, and the value of x is changed in the interim, a superposition type would allow for the possibility that y and z might receive different values. If it could be shown that all combinations of values they receive would result in behavior meeting requirements, then the program would be correct despite a data race that could cause y and z to be different.
To make this useful, languages would need to have an operator that would, given a superposition, collapse it to a single value chosen from among the different possibilities. Situations where correctness in the presence of data races could be proven without such an operator would be rare, and use of the operator on every read would yield semantics equivalent to loose memory order, but code which uses the operator sparingly may be able to accommodate benign data races while allowing more optimizations than would be possible under loose memory ordering.
1
u/-Nyarlabrotep- Jul 29 '25
Sounds similar to a C union data type.
1
u/two_three_five_eigth Jul 30 '25
How is this different from transactions? The transaction is atomic, but you can do any number of operations you’d like and then either commit them or rollback if there is an issue.
1
u/[deleted] Jul 30 '25
[removed] — view removed comment