How to measure in an arbitrary basis in Q#?

Q# has a measurement operator defined as follows according to the docs:

operation Measure (bases : Pauli[], qubits : Qubit[]) : Result

Where you give a Pauli gate $\{I, \sigma_x, \sigma_y, \sigma_z\}$ as a measurement operator and the qbit is projected onto its eigenvectors.

How can I measure in an arbitrary basis? I know conceptually what I have to do is first rotate my state vector by some amount then measure in one of the Pauli bases, but what are the actual primitive gates and parameters I would use to do that? For example, say I wanted to measure in the following basis:

$\begin{bmatrix} \frac{\sqrt{3}}{2} \\ \frac 1 2 \end{bmatrix}, \begin{bmatrix} \frac{-1}{2} \\ \frac{\sqrt{3}}{2} \end{bmatrix}$

So basically the computational basis but rotated $\pi/6$ radians counter-clockwise around the unit circle.

Let's say you want to distinguish two states:

$$|A\rangle = \cos \alpha |0\rangle + \sin \alpha |1\rangle \\ |B\rangle = -\sin \alpha |0\rangle + \cos \alpha |1\rangle$$

For your particular example $$\cos \alpha = \frac {\sqrt{3}}{2}$$ and $$\sin \alpha = \frac{1}{2}$$, so $$\alpha = \frac{\pi}{6}$$.

These states are orthogonal and can be obtained from $$|0\rangle$$ and $$|1\rangle$$ by rotating around Y axis, i.e. by applying Ry(2.0 * alpha, _). You can verify it using the definition of the Ry operation and matrix exponentiation.

Thus, one way to measure the states $$|A\rangle$$ and $$|B\rangle$$ is to apply adjoint operation Ry(-2.0 * alpha, _) to your qubit to get those states back to $$|0\rangle$$ and $$|1\rangle$$, and then to measure the qubit in the computational basis using operation M (or Measure([PauliZ], _)).

In more general case you'd use other rotation operations (Rx and Rz) to perform the exact rotation which converts your basis states to computational basis before measuring them.

This question is effectively task 1.4 from the Measurements quantum kata, if you want to practice it in Q# (as well as more advanced measurement scenarios).

• Can the Rx/y/z operator documentation be modified to show their matrix, not just the exponential form? Is the angle given in degrees or radians? Why is it Ry instead of Rx or Rz? Sep 13 '18 at 1:34
• That's a good suggestion (I guess people who wrote that part of documentation do this kind of calculation automatically) - could you post it to quantum.uservoice.com/forums/906946-samples-and-documentation so that we don't lose track of it? All angles are in radians. Sep 13 '18 at 2:39
• And it's Ry because, well, that's what the calculation boils down to (I have to admit I'm not up to doing it right now). The matrix forms for rotations can be found at docs.microsoft.com/en-us/quantum/quantum-concepts-4-qubit Sep 13 '18 at 2:44