A calibration experiment on a chip: a qubit starts in [1, 0], goes through a Hadamard gate, then a phase gate P(\lambda) whose angle \lambda (radians) is set with a dial, then a second Hadamard gate. With i written 1j,
A matrix is a list of rows, and a gate turns [a, b] into [U[0][0]*a + U[0][1]*b, U[1][0]*a + U[1][1]*b]. At the end a measurement reads 1 with probability |b|^2, the squared size of the second amplitude.
With the dial at \lambda=0 the two Hadamards cancel and the qubit always reads 0: the two paths that lead to 1 have opposite signs and cancel out (interference). Turning the dial shifts one path against the other, and the chance of reading 1 rises.
Write phase_dial(lams, target) that returns a pair (probs, lam):
probs: for each dial angle inlams, the probability of reading 1 at the end;lam: the dial angle \lambda with 0 \le \lambda \le \pi that makes the probability of reading 1 exactlytarget. On this range the probability rises steadily from 0 to 1, so there is exactly one such angle. (Outside it the curve repeats: -\lambda and \lambda + 2\pi give the same probability, and they are not accepted.)
Press Run with plot(grid, phase_dial(grid, 0.5)[0]) for a list grid of angles from 0 to 2\pi to see the curve.
Examples
Input: lams = [0, math.pi / 2, math.pi], target = 0.5
Output: ([0.0, 0.5, 1.0], 1.5707963267948966)
Explanation: no phase: always 0; a quarter turn: a fair coin; half a turn: always 1.
Input: lams = [2 * math.pi, -math.pi / 3], target = 0.25
Output: ([0.0, 0.25], 1.0471975511965976)
Explanation: a full turn is like no turn; -pi/3 gives the same 0.25 as pi/3.
Input: lams = [], target = 1
Output: ([], 3.141592653589793)
Constraints
0 <= len(lams) <= 1000, each-20 <= lam <= 20;0 <= target <= 1- answers are compared with a tolerance of
1e-6
Goals
- See that two Hadamard gates in a row undo each other
- Turn a phase between them into a probability of reading 1
- Invert that curve to set the dial for a wanted probability