## Tuesday, 6 January 2015

### some built in functions

First up in phase 2, some functions built into our ket and superposition classes. There are quite a few of these, but in this post I think I will only mention the most useful ones. (note that SP is just some superposition)

-- randomly select an element from SP
-- eventually I want a weighted pick-elt too.
pick-elt SP

-- normalize so sum of coeffs = 1
-- (this can be used to map a frequency list to a list of probabilities)
normalize SP

-- normalize so sum of coeffs = t
normalize[t] SP

-- rescale coeffs so coeff of max element = 1
rescale SP

-- rescale coeffs so coeff of max element = t
rescale[t] SP

--returns number of elements in SP in |number: x> format
count SP
how-many SP

-- returns sum of coeffs of the elements in SP in |number: x> format
count-sum SP
sum SP

-- returns the product of coeffs of the elements in SP in |number: x> format
product SP

-- drop elements from SP with coeff <= 0.
-- NB: in our model coeffs are almost always >= 0
drop SP

-- drop elements from SP with coeff below t
drop-below[t] SP

-- drop elements from SP with coeff above t
drop-above[t] SP

-- keep elements with index in range [a,b]
-- NB: index starts at 1, not 0
select-range[a,b] SP
select[a,b] SP

-- return element with index k
select-elt[k] SP

-- delete k'th element from the superposition.
delete-elt[k] SP

-- reverse the SP
reverse SP

-- shuffle the SP
shuffle SP

-- sort superposition by the coeffs of the kets
-- this one is very useful!
-- especially in combination with op-self operators
coeff-sort SP

-- sort using a natural sort of lowercase labels of the kets
-- NB: sometimes natural sort bugs out, and I have to manually
-- swap the code back to standard lowercase sort.
-- eg, the binary tree example with kets such as |00> and |0010> and so on.
ket-sort SP

-- return the first ket found with the max coeff
max-elt SP

-- return the first ket found with the min coeff
min-elt SP

-- return the kets with the max coeff
max SP

-- return the kets with the min coeff
min SP

-- return the max coeff in the SP in |number: x> format
max-coeff SP

-- return the min coeff in the SP in |number: x> format
min-coeff SP

-- mulitply all coeffs by t
mult[t] SP

-- add noise to the SP in range [0,t]
absolute-noise[t] SP

-- add noise to the SP in range [0,t*max_coeff]
relative-noise[t] SP

-- returns the difference between the largest coeff and the second largest coeff.
-- in 3| > format.
discrimination SP
discrim SP

-- returns |no> if SP is the identity element |>
-- otherwise returns |yes>
not-empty SP
do-you-know SP

I guess that is about it! Note there is a longer, more detailed version of the above here, which shows the mapping between the underlying python and the BKO (though it is incomplete).

BTW, I deliberately left out these two, as I will describe them in phase 3 of the write-up:
similar[op] |x>
find-topic[op] |x>