Finding Theta

This has been bothering me ever since I read a paper about a quantum game that rotated a qubit to determine the probability that you reached the finish. If the rotation was pi/3 you were 25% complete, pi/2 50%, and 2pi/3 75%. Those examples were all written into the paper, and you can find cheatsheets all over the Internet with the most common rotations.

But, what if you’re only 20% finished? Or, how about 30%? How do I figure out how much to rotate a qubit to update your progress?

With all things Python, there’s probably a library that does this easily. But, I wanted to figure it out the hard way and really understand it.

import math
You could always hardcode pi with 3.14…, but this is going to be the easiest way to calculate an inverse cosine, which is what I used to calculate theta.

ground = .4
This is the probability of measuring 0. So, with .4 I’m looking for a 40% probability of measuring 0 and, consequently, a 60% probability of measuring 1.

adjacent = abs((ground - .5) * 2)
I know the hypotenuse is 1, so if I can calculate the adjacent side I can use an inverse cosine function to calculate theta. I subtract .5 to get the length from the center, and then multiply by 2 to get the percentage of the radius. The abs() removes any negative sign.

Image credit: someone on Wikimedia. I kept tapping and tapping and couldn’t find the original.

theta = math.acos(adjacent)
The cosine of theta is the adjacent side divided by the hypotenuse, but the hypotenuse is 1. Therefore, you can find theta with the inverse cosine of the adjacent side.

if (ground < .5):
theta = math.pi - theta
Once you rotate past pi/2, the right triangle flips. Theta is then facing |1>, not |0>. Therefore, once you calculate theta from |1>, you need to subtract theta from pi to determine how much to rotate away from |0>.

your_circuit_name.ry(theta, the_qubit_to_rotate)
I ran a series of test rotations from 0 to pi, and the results consistently showed the probabilities that I was trying to achieve.

I’ll be honest, this was a little tricky. I was calculating 0 to pi/2 accurately, but I didn’t immediate consider the implications of the triangle flip. Calculating pi/2 to pi was inaccurate, because I was really calculating backward from pi to pi/2.

The mystery of theta, and phi (z axis) for that matter, is solved.