
 

Introduction Quantum Stuff CQuBit Quantum Functions Set Functions Sample Code Limitations Download Links
QuBit is a library to support Quantum Superpositions in C++. This allows each CQuBit variable to hold a number of different values at the same time. Normal mathematical operations can be performed on a QuBit, but they affect every value in the QuBit, at the same time. Binary Operations on two QuBits cause a result based on every possible outcome. For example,
Each value can only exist in a QuBit once. There is no 'weighting' applied if a value is added several times. Comparisons with a QuBit also occur with all the values at the same time. This means a search is simply, qb = (1,2,3,4,5);if qb eq 5 print "There's a '5' in the QuBit, somewhere!" Quantum Stuff The above examples demonstrate the basic functionality of QuBit. It's not significantly more complex than 'sets'. However, there are a few functions that take the extra step into 'gaga' land! A QuBit can be in three possible states:
Disjunctive Conjunctive Collapsed Eigenstates if v then print "Those numbers less than 20 are " eigenstates(v) Declaring and Using a QuBit We now turn to the actual C++ code require to perform these Quantum miracles! QuBit is, itself, a template and is therefore created with: CQuBit<float> q;This declares a Quantum superposition called 'q', consisting of floating point numbers. To add or remove values to this variable, you may use the following functions: q.Add(10); // Add '10'q.Add(2); // Add '2' q.AddRange(100,200); // Add every number from '100' to '200', inclusive q.AddRange(10,11,0.1f); // Add every number from '10' to '11' in steps of '0.1' q.Remove(10.5f); // Remove the number '10.5' You can also construct a QuBit, using the ranged constructor, CQuBit<float> q(100,200); // exactly the same as example three above Use of QuBit is the same as any other variable. Since all (see note below) operators are overloaded you can write natural looking code, such as 'a+b', or 'a/=2'. You can use binary operations (such as additional or multiplication) with either one CQuBit and one float, or two CQuBits, as you see fit. Note restrictions. QuantumOrientated Member Functions Having created a quantum superposition (with either Add or AddRange), it is now time to use it. Looking at the examples above, we can find if any value is less than 20 with this code. CQuBit<float> q(10,25,5);CQuBit<float> ans; ans = q.Any() < 20; if (ans.GetBoolResult()) cout << "At least one of these numbers is lower than 20"; The QuBit can also be treated as a disjunctive with the code q.Any();, allowing you to rewrite the above thus: CQuBit<float> q(10,25,5);CQuBit<float> ans; q.Any(); ans = q < 20; if (ans.GetBoolResult()) cout << "At least one of these numbers is lower than 20"; Although the results of the comparison (i.e. q < 20) is boolean, the C++ version of the library forces the result type to be a CQuBit. You then determine whether it collapsed to 'true' or 'false' with the 'GetBoolResult()' member function. This is to allow retrieval of the eigenstates (if only a bool were returned this information would get lost). Using eigenstatees to find those values less than 20, one can use:
CQuBit<float> q(10,25,5); The Eigenstates function returns a QuBit itself, so could be applied to further calculations. Note:Eigenstates could have been implemented in a similar fashion to PERL, with a cache storing the result of the last comparison operation. However, as it didn't feel very 'C' to be using global variables in this manner, so I adopted a QuBit return type, and 'GetBoolResult' to retrieve the comparisons' result. (Technically speaking this might also be considered storing a global!) All is used in exactly the same manner as above. Only the results differ (obviously!). CQuBit<float> q(10,25,5);CQuBit<float> ans; q.All(); ans = q < 20; if (ans.GetBoolResult()) cout << "All of these numbers are lower than 20: "; Streams The output stream produces space delimitted list of the states, enclosed in braces. CQuBit<float> q;q.AddRange(15,20); cout << q; produces { 15 16 17 18 19 20 } The input stream, will read this format into the QuBit, and leave the stream pointer immediately after the closing brace. SetOrientated Member Functions CQuBit<float> q2(4,6); CQuBit<float> ans; ans.Any(q1, q2); // The result is {1,2,3,4,5,6} Note: QuBit ignores the type of original quantum states. So in this case, q1 and q2 could be conjunctive or disjunctive, the answer would be identical in both cases. All collapsed QuBits are ignored Well, Any is union, it should be obvious what All (the conjunctive state) should be! One brownie point to everyone who said 'intersection'. The resultant QuBit features only those values present in both of the original superpositions. CQuBit<float> q1(1,6);CQuBit<float> q2(4,5); CQuBit<float> ans; ans.All(q1, q2); // The result is {4,5} Sample Code
Limitions QuBits are only concerned with values. Therefore, the return value from post and pre decrement operators are identical. i.e. a++ and ++a, are the same as a++. This doesn't mean that a++ and ++a are different, just that the return type from a++ is a value (as it should be), but ++a is not a reference to the a variable, as you might expect.
Some compilers (noticable Microsoft's!) refuse to compile this code as is. To help those poor unfortunates, I've removed a couple of the friend functions with an #if 0. Namely,
These prevent compilation of code such as, q2 = 5 * q1. This means you'll have to rewrite your code by swapping the arguments over, or by creating a QuBit containing one entry. The code works, however. The set of operators catered for are:
[] is overloaded to retrieve individual state values, i.e. if a={10,20,30}, then a[0]=10, a[1]=20 and a[2]=30. Download
Links
